Skip to content

Commit f32796f

Browse files
dayninaddaleax
authored andcommitted
url: refactor "escapeParam" function to make it common
PR-URL: #19076 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 5fdee52 commit f32796f

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

lib/internal/url.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ const noEscape = [
814814
const paramHexTable = hexTable.slice();
815815
paramHexTable[0x20] = '+';
816816

817-
function escapeParam(str) {
817+
function encodeStr(str, noEscapeTable, hexTable) {
818818
const len = str.length;
819819
if (len === 0)
820820
return '';
@@ -827,12 +827,12 @@ function escapeParam(str) {
827827

828828
// ASCII
829829
if (c < 0x80) {
830-
if (noEscape[c] === 1)
830+
if (noEscapeTable[c] === 1)
831831
continue;
832832
if (lastPos < i)
833833
out += str.slice(lastPos, i);
834834
lastPos = i + 1;
835-
out += paramHexTable[c];
835+
out += hexTable[c];
836836
continue;
837837
}
838838

@@ -842,15 +842,15 @@ function escapeParam(str) {
842842
// Multi-byte characters ...
843843
if (c < 0x800) {
844844
lastPos = i + 1;
845-
out += paramHexTable[0xC0 | (c >> 6)] +
846-
paramHexTable[0x80 | (c & 0x3F)];
845+
out += hexTable[0xC0 | (c >> 6)] +
846+
hexTable[0x80 | (c & 0x3F)];
847847
continue;
848848
}
849849
if (c < 0xD800 || c >= 0xE000) {
850850
lastPos = i + 1;
851-
out += paramHexTable[0xE0 | (c >> 12)] +
852-
paramHexTable[0x80 | ((c >> 6) & 0x3F)] +
853-
paramHexTable[0x80 | (c & 0x3F)];
851+
out += hexTable[0xE0 | (c >> 12)] +
852+
hexTable[0x80 | ((c >> 6) & 0x3F)] +
853+
hexTable[0x80 | (c & 0x3F)];
854854
continue;
855855
}
856856
// Surrogate pair
@@ -866,10 +866,10 @@ function escapeParam(str) {
866866
}
867867
lastPos = i + 1;
868868
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
869-
out += paramHexTable[0xF0 | (c >> 18)] +
870-
paramHexTable[0x80 | ((c >> 12) & 0x3F)] +
871-
paramHexTable[0x80 | ((c >> 6) & 0x3F)] +
872-
paramHexTable[0x80 | (c & 0x3F)];
869+
out += hexTable[0xF0 | (c >> 18)] +
870+
hexTable[0x80 | ((c >> 12) & 0x3F)] +
871+
hexTable[0x80 | ((c >> 6) & 0x3F)] +
872+
hexTable[0x80 | (c & 0x3F)];
873873
}
874874
if (lastPos === 0)
875875
return str;
@@ -885,9 +885,17 @@ function serializeParams(array) {
885885
if (len === 0)
886886
return '';
887887

888-
var output = `${escapeParam(array[0])}=${escapeParam(array[1])}`;
889-
for (var i = 2; i < len; i += 2)
890-
output += `&${escapeParam(array[i])}=${escapeParam(array[i + 1])}`;
888+
const firstEncodedParam = encodeStr(array[0], noEscape, paramHexTable);
889+
const firstEncodedValue = encodeStr(array[1], noEscape, paramHexTable);
890+
let output =
891+
`${firstEncodedParam}=${firstEncodedValue}`;
892+
893+
for (var i = 2; i < len; i += 2) {
894+
const encodedParam = encodeStr(array[i], noEscape, paramHexTable);
895+
const encodedValue = encodeStr(array[i + 1], noEscape, paramHexTable);
896+
output += `&${encodedParam}=${encodedValue}`;
897+
}
898+
891899
return output;
892900
}
893901

@@ -1431,5 +1439,6 @@ module.exports = {
14311439
domainToUnicode,
14321440
urlToOptions,
14331441
formatSymbol: kFormat,
1434-
searchParamsSymbol: searchParams
1442+
searchParamsSymbol: searchParams,
1443+
encodeStr
14351444
};

0 commit comments

Comments
 (0)