Skip to content

Commit

Permalink
fix(ngResource): don't append number to '$' in url param value when e…
Browse files Browse the repository at this point in the history
…ncoding URI

Previously, if a URL parameter value included a $, it would replace the dollar sign with a literal
'$1' for mysterious reasons. Using a function rather than a replacement string circumvents this
behaviour and produces a more expected result.

Closes angular#6003
  • Loading branch information
caitp committed Jan 27, 2014
1 parent 766b3d5 commit f7cefcb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,9 @@ angular.module('ngResource', ['ng']).
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
if (angular.isDefined(val) && val !== null) {
encodedVal = encodeUriSegment(val);
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), encodedVal + "$1");
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), function(match, p1) {
return encodedVal + p1;
});
} else {
url = url.replace(new RegExp("(\/?):" + urlParam + "(\\W|$)", "g"), function(match,
leadingSlashes, tail) {
Expand All @@ -399,7 +401,7 @@ angular.module('ngResource', ['ng']).
});
}
});

// strip trailing slashes and set the url
url = url.replace(/\/+$/, '') || '/';
// then replace collapse `/.` if found in the last URL path segment before the query
Expand All @@ -408,7 +410,6 @@ angular.module('ngResource', ['ng']).
// replace escaped `/\.` with `/.`
config.url = url.replace(/\/\\\./, '/.');


// set params - delegate param encoding to $http
forEach(params, function(value, key){
if (!self.urlParams[key]) {
Expand Down
2 changes: 2 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,11 @@ describe("resource", function() {

$httpBackend.expect('GET', '/Path/foo%231').respond('{}');
$httpBackend.expect('GET', '/Path/doh!@foo?bar=baz%231').respond('{}');
$httpBackend.expect('GET', '/Path/herp$').respond('{}');

R.get({a: 'foo#1'});
R.get({a: 'doh!@foo', bar: 'baz#1'});
R.get({a: 'herp$'});
});


Expand Down

0 comments on commit f7cefcb

Please sign in to comment.