Skip to content

Commit

Permalink
url: url.format() encodes all # in search
Browse files Browse the repository at this point in the history
This commit fixes an error where only the first occurrence of `#` in
`search` parameter is URL encoded, and subsequent occurrences are not.

Also added a test for the case.

Fixes: #8064
PR-URL: #8072
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
imyller authored and Myles Borins committed Sep 30, 2016
1 parent c947929 commit 2069ba9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ Url.prototype.format = function() {
pathname = pathname.replace(/[?#]/g, function(match) {
return encodeURIComponent(match);
});
search = search.replace('#', '%23');
search = search.replace(/#/g, '%23');

return protocol + host + pathname + search + hash;
};
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,19 @@ var formatTests = {
hash: '#frag',
search: '?abc=the#1?&foo=bar',
pathname: '/fooA100%mBr',
},

// multiple `#` in search
'http://example.com/?foo=bar%231%232%233&abc=%234%23%235#frag': {
href: 'http://example.com/?foo=bar%231%232%233&abc=%234%23%235#frag',
protocol: 'http:',
slashes: true,
host: 'example.com',
hostname: 'example.com',
hash: '#frag',
search: '?foo=bar#1#2#3&abc=#4##5',
query: {},
pathname: '/'
}
};
for (const u in formatTests) {
Expand Down

0 comments on commit 2069ba9

Please sign in to comment.