Skip to content

Commit 99e3c77

Browse files
daynintargos
authored andcommitted
url: refactor "escapeParam" function to make it common
PR-URL: #19076 Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent ee4390a commit 99e3c77

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

lib/internal/url.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ const noEscape = [
805805
const paramHexTable = hexTable.slice();
806806
paramHexTable[0x20] = '+';
807807

808-
function escapeParam(str) {
808+
function encodeStr(str, noEscapeTable, hexTable) {
809809
const len = str.length;
810810
if (len === 0)
811811
return '';
@@ -818,12 +818,12 @@ function escapeParam(str) {
818818

819819
// ASCII
820820
if (c < 0x80) {
821-
if (noEscape[c] === 1)
821+
if (noEscapeTable[c] === 1)
822822
continue;
823823
if (lastPos < i)
824824
out += str.slice(lastPos, i);
825825
lastPos = i + 1;
826-
out += paramHexTable[c];
826+
out += hexTable[c];
827827
continue;
828828
}
829829

@@ -833,15 +833,15 @@ function escapeParam(str) {
833833
// Multi-byte characters ...
834834
if (c < 0x800) {
835835
lastPos = i + 1;
836-
out += paramHexTable[0xC0 | (c >> 6)] +
837-
paramHexTable[0x80 | (c & 0x3F)];
836+
out += hexTable[0xC0 | (c >> 6)] +
837+
hexTable[0x80 | (c & 0x3F)];
838838
continue;
839839
}
840840
if (c < 0xD800 || c >= 0xE000) {
841841
lastPos = i + 1;
842-
out += paramHexTable[0xE0 | (c >> 12)] +
843-
paramHexTable[0x80 | ((c >> 6) & 0x3F)] +
844-
paramHexTable[0x80 | (c & 0x3F)];
842+
out += hexTable[0xE0 | (c >> 12)] +
843+
hexTable[0x80 | ((c >> 6) & 0x3F)] +
844+
hexTable[0x80 | (c & 0x3F)];
845845
continue;
846846
}
847847
// Surrogate pair
@@ -857,10 +857,10 @@ function escapeParam(str) {
857857
}
858858
lastPos = i + 1;
859859
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
860-
out += paramHexTable[0xF0 | (c >> 18)] +
861-
paramHexTable[0x80 | ((c >> 12) & 0x3F)] +
862-
paramHexTable[0x80 | ((c >> 6) & 0x3F)] +
863-
paramHexTable[0x80 | (c & 0x3F)];
860+
out += hexTable[0xF0 | (c >> 18)] +
861+
hexTable[0x80 | ((c >> 12) & 0x3F)] +
862+
hexTable[0x80 | ((c >> 6) & 0x3F)] +
863+
hexTable[0x80 | (c & 0x3F)];
864864
}
865865
if (lastPos === 0)
866866
return str;
@@ -876,9 +876,17 @@ function serializeParams(array) {
876876
if (len === 0)
877877
return '';
878878

879-
var output = `${escapeParam(array[0])}=${escapeParam(array[1])}`;
880-
for (var i = 2; i < len; i += 2)
881-
output += `&${escapeParam(array[i])}=${escapeParam(array[i + 1])}`;
879+
const firstEncodedParam = encodeStr(array[0], noEscape, paramHexTable);
880+
const firstEncodedValue = encodeStr(array[1], noEscape, paramHexTable);
881+
let output =
882+
`${firstEncodedParam}=${firstEncodedValue}`;
883+
884+
for (var i = 2; i < len; i += 2) {
885+
const encodedParam = encodeStr(array[i], noEscape, paramHexTable);
886+
const encodedValue = encodeStr(array[i + 1], noEscape, paramHexTable);
887+
output += `&${encodedParam}=${encodedValue}`;
888+
}
889+
882890
return output;
883891
}
884892

@@ -1422,5 +1430,6 @@ module.exports = {
14221430
domainToUnicode,
14231431
urlToOptions,
14241432
formatSymbol: kFormat,
1425-
searchParamsSymbol: searchParams
1433+
searchParamsSymbol: searchParams,
1434+
encodeStr
14261435
};

0 commit comments

Comments
 (0)