Skip to content

Commit

Permalink
feat($location): test $location enhancements from angular#6421 and fi…
Browse files Browse the repository at this point in the history
…x them

This is temporary
  • Loading branch information
caitp committed Mar 28, 2014
1 parent 25467d5 commit b3d3fe6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,27 +638,27 @@ function $LocationProvider(){
// TODO check browser is in standards mode
var href = elm[0].getAttribute('href');

if (href.indexOf('://' == -1)) { // Ignore absolute URLs
if (href.indexOf('://') < 0) { // Ignore absolute URLs
var prefix = '#' + hashPrefix;
if (href[0] == '/') {
// absolute path - replace old path
absHref = serverBase(absHref) + href;
absHref = appBase + prefix + href;
} else if (href[0] == '#') {
// local anchor
absHref = serverBase(absHref) + $location.path() + href;
absHref = appBase + prefix + ($location.path() || '/') + href;
} else {
// relative path - join with current path
var stack = $location.path().split("/"),
parts = href.split("/");
stack.pop(); // remove top file
for (var i=0; i<parts.length; i++) {
if (parts[i] == ".")
continue;
else if (parts[i] == "..")
stack.pop();
else
else if (parts[i].length)
stack.push(parts[i]);
}
absHref = serverBase(absHref) + stack.join("/");
absHref = appBase + prefix + stack.join('/');
}
}
}
Expand Down
62 changes: 57 additions & 5 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,15 +779,22 @@ describe('$location', function() {

var root, link, originalBrowser, lastEventPreventDefault;

function configureService(linkHref, html5Mode, supportHist, attrs, content) {
function configureService(linkHref, html5Mode, supportHist, relLink, attrs, content) {
if (typeof relLink !== "boolean") {
content = attrs;
attrs = relLink;
relLink = false;
}
module(function($provide, $locationProvider) {
attrs = attrs ? ' ' + attrs + ' ' : '';

// fake the base behavior
if (linkHref[0] == '/') {
linkHref = 'http://host.com' + linkHref;
} else if(!linkHref.match(/:\/\//)) {
linkHref = 'http://host.com/base/' + linkHref;
if (!relLink) {
if (linkHref[0] == '/') {
linkHref = 'http://host.com' + linkHref;
} else if(!linkHref.match(/:\/\//)) {
linkHref = 'http://host.com/base/' + linkHref;
}
}

link = jqLite('<a href="' + linkHref + '"' + attrs + '>' + content + '</a>')[0];
Expand Down Expand Up @@ -1067,6 +1074,51 @@ describe('$location', function() {
});


it('should rewrite relative links relative to current path when history disabled', function() {
configureService('link', true, false, true);
inject(
initBrowser(),
initLocation(),
function($browser, $location) {
$location.path('/some');
browserTrigger(link, 'click');
expectRewriteTo($browser, 'http://host.com/base/index.html#!/some/link');
}
);
});


it('should replace current path when link begins with "/" and history disabled', function() {
configureService('/link', true, false, true);
inject(
initBrowser(),
initLocation(),
function($browser, $location) {
$location.path('/some');
browserTrigger(link, 'click');
expectRewriteTo($browser, 'http://host.com/base/index.html#!/link');
}
);
});


it('should replace current hash fragment when link begins with "#" history disabled', function() {
configureService('#link', true, false, true);
inject(
initBrowser(),
initLocation(),
function($browser, $location) {
// Initialize browser URL
$location.path('/some');
$location.hash('foo');
browserTrigger(link, 'click');
expect($location.hash()).toBe('link');
expectRewriteTo($browser, 'http://host.com/base/index.html#!/some#link');
}
);
});


// don't run next tests on IE<9, as browserTrigger does not simulate pressed keys
if (!(msie < 9)) {

Expand Down

0 comments on commit b3d3fe6

Please sign in to comment.