Skip to content

Commit ec061f4

Browse files
committed
feat($location): test $location enhancements from angular#6421 and fix them
This is temporary
1 parent 25467d5 commit ec061f4

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

src/ng/location.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -638,27 +638,27 @@ function $LocationProvider(){
638638
// TODO check browser is in standards mode
639639
var href = elm[0].getAttribute('href');
640640

641-
if (href.indexOf('://' == -1)) { // Ignore absolute URLs
641+
if (href.indexOf('://') < 0) { // Ignore absolute URLs
642+
var prefix = '#' + hashPrefix;
642643
if (href[0] == '/') {
643644
// absolute path - replace old path
644-
absHref = serverBase(absHref) + href;
645+
absHref = appBase + prefix + href;
645646
} else if (href[0] == '#') {
646647
// local anchor
647-
absHref = serverBase(absHref) + $location.path() + href;
648+
absHref = appBase + prefix + ($location.path() || '/') + href;
648649
} else {
649650
// relative path - join with current path
650651
var stack = $location.path().split("/"),
651652
parts = href.split("/");
652-
stack.pop(); // remove top file
653653
for (var i=0; i<parts.length; i++) {
654654
if (parts[i] == ".")
655655
continue;
656656
else if (parts[i] == "..")
657657
stack.pop();
658-
else
658+
else if (parts[i].length)
659659
stack.push(parts[i]);
660660
}
661-
absHref = serverBase(absHref) + stack.join("/");
661+
absHref = appBase + prefix + stack.join('/');
662662
}
663663
}
664664
}

test/ng/locationSpec.js

+57-5
Original file line numberDiff line numberDiff line change
@@ -779,15 +779,22 @@ describe('$location', function() {
779779

780780
var root, link, originalBrowser, lastEventPreventDefault;
781781

782-
function configureService(linkHref, html5Mode, supportHist, attrs, content) {
782+
function configureService(linkHref, html5Mode, supportHist, relLink, attrs, content) {
783+
if (typeof relLink !== "boolean") {
784+
content = attrs;
785+
attrs = relLink;
786+
relLink = false;
787+
}
783788
module(function($provide, $locationProvider) {
784789
attrs = attrs ? ' ' + attrs + ' ' : '';
785790

786791
// fake the base behavior
787-
if (linkHref[0] == '/') {
788-
linkHref = 'http://host.com' + linkHref;
789-
} else if(!linkHref.match(/:\/\//)) {
790-
linkHref = 'http://host.com/base/' + linkHref;
792+
if (!relLink) {
793+
if (linkHref[0] == '/') {
794+
linkHref = 'http://host.com' + linkHref;
795+
} else if(!linkHref.match(/:\/\//)) {
796+
linkHref = 'http://host.com/base/' + linkHref;
797+
}
791798
}
792799

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

10691076

1077+
it('should rewrite relative links relative to current path when history disabled', function() {
1078+
configureService('link', true, false, true);
1079+
inject(
1080+
initBrowser(),
1081+
initLocation(),
1082+
function($browser, $location) {
1083+
$location.path('/some');
1084+
browserTrigger(link, 'click');
1085+
expectRewriteTo($browser, 'http://host.com/base/index.html#!/some/link');
1086+
}
1087+
);
1088+
});
1089+
1090+
1091+
it('should replace current path when link begins with "/" and history disabled', function() {
1092+
configureService('/link', true, false, true);
1093+
inject(
1094+
initBrowser(),
1095+
initLocation(),
1096+
function($browser, $location) {
1097+
$location.path('/some');
1098+
browserTrigger(link, 'click');
1099+
expectRewriteTo($browser, 'http://host.com/base/index.html#!/link');
1100+
}
1101+
);
1102+
});
1103+
1104+
1105+
it('should replace current hash fragment when link begins with "#" history disabled', function() {
1106+
configureService('#link', true, false, true);
1107+
inject(
1108+
initBrowser(),
1109+
initLocation(),
1110+
function($browser, $location) {
1111+
// Initialize browser URL
1112+
$location.path('/some');
1113+
$location.hash('foo');
1114+
browserTrigger(link, 'click');
1115+
expect($location.hash()).toBe('link');
1116+
expectRewriteTo($browser, 'http://host.com/base/index.html#!/some#link');
1117+
}
1118+
);
1119+
});
1120+
1121+
10701122
// don't run next tests on IE<9, as browserTrigger does not simulate pressed keys
10711123
if (!(msie < 9)) {
10721124

0 commit comments

Comments
 (0)