Skip to content

Commit f57758c

Browse files
committed
WIP: experiment with more intelligent path encoding
WIP: improve encodeUriSegment function
1 parent 99a2f0a commit f57758c

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

src/Angular.js

+34-5
Original file line numberDiff line numberDiff line change
@@ -1150,11 +1150,40 @@ function toKeyValue(obj) {
11501150
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
11511151
* / "*" / "+" / "," / ";" / "="
11521152
*/
1153-
function encodeUriSegment(val) {
1154-
return encodeUriQuery(val, true).
1155-
replace(/%26/gi, '&').
1156-
replace(/%3D/gi, '=').
1157-
replace(/%2B/gi, '+');
1153+
var subDelimsExp = /[\!\$'\(\)\*\+,;\=@:]/g;
1154+
var encodedExp = /%[A-Z0-9]{2}/ig;
1155+
function encodeUriSegment(segment) {
1156+
var match, segments, newSegment, matchIndex, encoded, endIndex;
1157+
if ((match = segment.match(encodedExp)) || (match = segment.match(subDelimsExp))) {
1158+
newSegment = '';
1159+
while (encoded = match.shift()) {
1160+
matchIndex = segment.indexOf(encoded);
1161+
if (matchIndex === 0) {
1162+
newSegment += encoded;
1163+
}
1164+
else {
1165+
var sub = segment.substring(0, matchIndex);
1166+
newSegment += encodeUriSegment(sub);
1167+
newSegment += encoded;
1168+
}
1169+
1170+
endIndex = match[0]?segment.indexOf(match[0]):null;
1171+
if (endIndex) {
1172+
newSegment += segment.substring(matchIndex + encoded.length, endIndex);
1173+
}
1174+
else {
1175+
newSegment += segment.substring(matchIndex + encoded.length);
1176+
}
1177+
1178+
}
1179+
1180+
segment = newSegment;
1181+
}
1182+
else {
1183+
segment = encodeURIComponent(segment);
1184+
}
1185+
1186+
return segment;
11581187
}
11591188

11601189

test/AngularSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ describe('angular', function() {
663663

664664
//encode '/', ';' and ' ''
665665
expect(encodeUriSegment('/; /;')).
666-
toEqual('%2F%3B%20%2F%3B');
666+
toEqual('%2F;%20%2F;');
667667
});
668668
});
669669

test/ng/locationSpec.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global LocationHashbangUrl: false, LocationHtml5Url: false */
22
'use strict';
33

4-
describe('$location', function() {
4+
ddescribe('$location', function() {
55
var url;
66

77
beforeEach(module(provideLog));
@@ -264,6 +264,7 @@ describe('$location', function() {
264264
);
265265
});
266266

267+
267268
it('should prepend path with basePath', function() {
268269
url = new LocationHtml5Url('http://server/base/');
269270
url.$$parse('http://server/base/abc?a');
@@ -307,12 +308,21 @@ describe('$location', function() {
307308
});
308309

309310

310-
it('should not encode !$:@', function() {
311-
url.path('/!$:@');
311+
it('should honor path delimiters and preserve encoded delimiters', function() {
312+
url.path('/foo;ba%2Br/baz%3Bfoo/bar');
313+
url.hash('');
314+
url.search('');
315+
expect(url.path()).toBe('/foo;ba%2Br/baz%3Bfoo/bar');
316+
expect(url.absUrl()).toBe('http://www.domain.com:9877/foo;ba%2Br/baz%3Bfoo/bar');
317+
})
318+
319+
320+
it('should not encode !$:@;', function() {
321+
url.path('/!$:@;');
312322
url.search('');
313323
url.hash('!$:@');
314324

315-
expect(url.absUrl()).toBe('http://www.domain.com:9877/!$:@#!$:@');
325+
expect(url.absUrl()).toBe('http://www.domain.com:9877/!$:@;#!$:@');
316326
});
317327

318328

0 commit comments

Comments
 (0)