Skip to content

Commit 0f5c86c

Browse files
committed
Merge pull request angular-ui#279 from dkang81/master
Fixed incorrect encoding/decoding in urlMatcherFactory for parameters.
2 parents e0064a2 + 016b3bd commit 0f5c86c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/urlMatcherFactory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ UrlMatcher.prototype.exec = function (path, searchParams) {
163163

164164
if (nPath !== m.length - 1) throw new Error("Unbalanced capture group in route '" + this.source + "'");
165165

166-
for (i=0; i<nPath; i++) values[params[i]] = decodeURIComponent(m[i+1]);
166+
for (i=0; i<nPath; i++) values[params[i]] = m[i+1];
167167
for (/**/; i<nTotal; i++) values[params[i]] = searchParams[params[i]];
168168

169169
return values;
@@ -202,7 +202,7 @@ UrlMatcher.prototype.format = function (values) {
202202
for (i=0; i<nPath; i++) {
203203
value = values[params[i]];
204204
// TODO: Maybe we should throw on null here? It's not really good style to use '' and null interchangeabley
205-
if (value != null) result += value;
205+
if (value != null) result += encodeURIComponent(value);
206206
result += segments[i+1];
207207
}
208208
for (/**/; i<nTotal; i++) {

test/urlMatcherFactorySpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ describe("UrlMatcher", function () {
4040
.toBeNull();
4141
});
4242

43+
it(".exec() treats the URL as already decoded and does not decode it further", function () {
44+
expect(new UrlMatcher('/users/:id').exec('/users/100%25', {})).toEqual({ id: '100%25'});
45+
});
46+
4347
it('.exec() throws on unbalanced capture list', function () {
4448
var shouldThrow = {
4549
"/url/{matchedParam:([a-z]+)}/child/{childParam}": '/url/someword/child/childParam',
@@ -69,6 +73,10 @@ describe("UrlMatcher", function () {
6973
.toEqual('/users/123/details/default/444?from=1970');
7074
});
7175

76+
it(".format() encodes URL parameters", function () {
77+
expect(new UrlMatcher('/users/:id').format({ id:'100%'})).toEqual('/users/100%25');
78+
});
79+
7280
it(".concat() concatenates matchers", function () {
7381
var matcher = new UrlMatcher('/users/:id/details/{type}?from').concat('/{repeat:[0-9]+}?to');
7482
var params = matcher.parameters();

0 commit comments

Comments
 (0)