Skip to content

feat(urlMatcherFactory): add type "urlEncode" param #2834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ UrlMatcher.prototype.format = function (values) {

var i, search = false, nPath = segments.length - 1, nTotal = params.length, result = segments[0];

function encodeDashes(str) { // Replace dashes with encoded "\-"
return encodeURIComponent(str).replace(/-/g, function(c) { return '%5C%' + c.charCodeAt(0).toString(16).toUpperCase(); });
function encodeDashes(urlEncode, str) { // Replace dashes with encoded "\-"
return (urlEncode ? encodeURIComponent(str) : str).replace(/-/g, function(c) { return '%5C%' + c.charCodeAt(0).toString(16).toUpperCase(); });
}

for (i = 0; i < nTotal; i++) {
Expand All @@ -352,6 +352,7 @@ UrlMatcher.prototype.format = function (values) {
var isDefaultValue = param.isOptional && param.type.equals(param.value(), value);
var squash = isDefaultValue ? param.squash : false;
var encoded = param.type.encode(value);
var urlEncode = param.type.urlEncode === undefined || param.type.urlEncode === null ? true : urlEncode;

if (isPathParam) {
var nextSegment = segments[i + 1];
Expand All @@ -360,9 +361,9 @@ UrlMatcher.prototype.format = function (values) {
if (squash === false) {
if (encoded != null) {
if (isArray(encoded)) {
result += map(encoded, encodeDashes).join("-");
result += map(encoded, encodeDashes.bind(null, urlEncode)).join("-");
} else {
result += encodeURIComponent(encoded);
result += urlEncode ? encodeURIComponent(encoded) : encoded;
}
}
result += nextSegment;
Expand Down
13 changes: 9 additions & 4 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe("UrlMatcher", function () {
err = "Invalid parameter name 'periods.' in pattern '/users/?from&to&periods.'";
expect(function() { new UrlMatcher('/users/?from&to&periods.'); }).toThrow(err);
});
});
});

describe(".exec()", function() {
it("should capture parameter values", function () {
Expand Down Expand Up @@ -196,6 +196,11 @@ describe("UrlMatcher", function () {
expect(new UrlMatcher('/users/:id').format({ id:'100%'})).toEqual('/users/100%25');
});

it("should not encode URL parameters", function () {
provider.type("unencodedUrlType", { urlEncode: false });
expect(new UrlMatcher('/users/{id:unencodedUrlType}').format({ id:'100%'})).toEqual('/users/100%');
});

it("encodes URL parameters with hashes", function () {
var m = new UrlMatcher('/users/:id#:section'),
params = { id: 'bob', section: 'contact-details' };
Expand Down Expand Up @@ -234,7 +239,7 @@ describe("UrlMatcher", function () {
provider.strictMode(false);
m = m.concat("foo");
expect(m.exec("/foo")).toEqual({});
expect(m.exec("/foo/")).toEqual({})
expect(m.exec("/foo/")).toEqual({});
});

it("should respect $urlMatcherFactoryProvider.caseInsensitive", function() {
Expand Down Expand Up @@ -387,7 +392,7 @@ describe("UrlMatcher", function () {
"param5": []
};

expect(parsed).toEqualData(expected)
expect(parsed).toEqualData(expected);
expect(m.params.$$values(parsed)).toEqualData(expected);
}));

Expand Down Expand Up @@ -673,7 +678,7 @@ describe("urlMatcherFactory", function () {
expect(m.exec($location.path(), $location.search())).toEqual( { fooid: 5, bar: [ 1, 2, 3 ] } );
expect(m.format({ fooid: 5, bar: [ 1, 2, 3 ] })).toEqual("/foo/5?bar=1&bar=2&bar=3");

m.format()
m.format();
}));

it("should allow custom types to handle multiple search param values manually", inject(function($location) {
Expand Down