Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

refactor(ngResource): Use core encode methods in ngResource #14309

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
4 changes: 3 additions & 1 deletion src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ function publishExternalAPI(angular) {
'getTestability': getTestability,
'$$minErr': minErr,
'$$csp': csp,
'reloadWithDebugInfo': reloadWithDebugInfo
'reloadWithDebugInfo': reloadWithDebugInfo,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a just a refactoring, this exposes these two functions on the angular global, which is something we don't do lightly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct. This is my first patch contributed, so I wasn't sure if this was a big deal or not. Although I do see various utility methods exposed publicly. The nature of the encoding methods makes me feel that they could be useful as public methods.

Interested to hear your thoughts, or general conditions on public methods on the core module. Thanks for the feedback :)

'$$encodeUriSegment': encodeUriSegment,
'$$encodeUriQuery': encodeUriQuery
});

angularModule = setupModuleLoader(window);
Expand Down
43 changes: 2 additions & 41 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,45 +424,6 @@ angular.module('ngResource', ['ng']).
copy = angular.copy,
isFunction = angular.isFunction;

/**
* We need our custom method because encodeURIComponent is too aggressive and doesn't follow
* http://www.ietf.org/rfc/rfc3986.txt with regards to the character set
* (pchar) allowed in path segments:
* segment = *pchar
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
* pct-encoded = "%" HEXDIG HEXDIG
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*/
function encodeUriSegment(val) {
return encodeUriQuery(val, true).
replace(/%26/gi, '&').
replace(/%3D/gi, '=').
replace(/%2B/gi, '+');
}


/**
* This method is intended for encoding *key* or *value* parts of query component. We need a
* custom method because encodeURIComponent is too aggressive and encodes stuff that doesn't
* have to be encoded per http://tools.ietf.org/html/rfc3986:
* query = *( pchar / "/" / "?" )
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* pct-encoded = "%" HEXDIG HEXDIG
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*/
function encodeUriQuery(val, pctEncodeSpaces) {
return encodeURIComponent(val).
replace(/%40/gi, '@').
replace(/%3A/gi, ':').
replace(/%24/g, '$').
replace(/%2C/gi, ',').
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
}

function Route(template, defaults) {
this.template = template;
this.defaults = extend({}, provider.defaults, defaults);
Expand Down Expand Up @@ -500,9 +461,9 @@ angular.module('ngResource', ['ng']).
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
if (angular.isDefined(val) && val !== null) {
if (paramInfo.isQueryParamValue) {
encodedVal = encodeUriQuery(val, true);
encodedVal = angular.$$encodeUriQuery(val, true);
} else {
encodedVal = encodeUriSegment(val);
encodedVal = angular.$$encodeUriSegment(val);
}
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), function(match, p1) {
return encodedVal + p1;
Expand Down