Skip to content

Commit 13da7f1

Browse files
imyllerFishrock123
authored andcommitted
url: url.format() encodes all # in search
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: nodejs#8064 PR-URL: nodejs#8072 Reviewed-By: James M Snell <[email protected]> Conflicts: test/parallel/test-url.js
1 parent c21bfae commit 13da7f1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/url.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ Url.prototype.format = function() {
619619
host = '';
620620
}
621621

622-
search = search.replace('#', '%23');
622+
search = search.replace(/#/g, '%23');
623623

624624
if (hash && hash.charCodeAt(0) !== 35/*#*/) hash = '#' + hash;
625625
if (search && search.charCodeAt(0) !== 63/*?*/) search = '?' + search;

test/parallel/test-url.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,19 @@ var formatTests = {
11651165
hash: '#frag',
11661166
search: '?abc=the#1?&foo=bar',
11671167
pathname: '/fooA100%mBr',
1168+
},
1169+
1170+
// multiple `#` in search
1171+
'http://example.com/?foo=bar%231%232%233&abc=%234%23%235#frag': {
1172+
href: 'http://example.com/?foo=bar%231%232%233&abc=%234%23%235#frag',
1173+
protocol: 'http:',
1174+
slashes: true,
1175+
host: 'example.com',
1176+
hostname: 'example.com',
1177+
hash: '#frag',
1178+
search: '?foo=bar#1#2#3&abc=#4##5',
1179+
query: {},
1180+
pathname: '/'
11681181
}
11691182
};
11701183
for (const u in formatTests) {

0 commit comments

Comments
 (0)