url: refactor "escapeParam" function to make it common
PR-URL: https://github.com/nodejs/node/pull/19076 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
5fdee52c5e
commit
f32796fad2
@ -814,7 +814,7 @@ const noEscape = [
|
||||
const paramHexTable = hexTable.slice();
|
||||
paramHexTable[0x20] = '+';
|
||||
|
||||
function escapeParam(str) {
|
||||
function encodeStr(str, noEscapeTable, hexTable) {
|
||||
const len = str.length;
|
||||
if (len === 0)
|
||||
return '';
|
||||
@ -827,12 +827,12 @@ function escapeParam(str) {
|
||||
|
||||
// ASCII
|
||||
if (c < 0x80) {
|
||||
if (noEscape[c] === 1)
|
||||
if (noEscapeTable[c] === 1)
|
||||
continue;
|
||||
if (lastPos < i)
|
||||
out += str.slice(lastPos, i);
|
||||
lastPos = i + 1;
|
||||
out += paramHexTable[c];
|
||||
out += hexTable[c];
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -842,15 +842,15 @@ function escapeParam(str) {
|
||||
// Multi-byte characters ...
|
||||
if (c < 0x800) {
|
||||
lastPos = i + 1;
|
||||
out += paramHexTable[0xC0 | (c >> 6)] +
|
||||
paramHexTable[0x80 | (c & 0x3F)];
|
||||
out += hexTable[0xC0 | (c >> 6)] +
|
||||
hexTable[0x80 | (c & 0x3F)];
|
||||
continue;
|
||||
}
|
||||
if (c < 0xD800 || c >= 0xE000) {
|
||||
lastPos = i + 1;
|
||||
out += paramHexTable[0xE0 | (c >> 12)] +
|
||||
paramHexTable[0x80 | ((c >> 6) & 0x3F)] +
|
||||
paramHexTable[0x80 | (c & 0x3F)];
|
||||
out += hexTable[0xE0 | (c >> 12)] +
|
||||
hexTable[0x80 | ((c >> 6) & 0x3F)] +
|
||||
hexTable[0x80 | (c & 0x3F)];
|
||||
continue;
|
||||
}
|
||||
// Surrogate pair
|
||||
@ -866,10 +866,10 @@ function escapeParam(str) {
|
||||
}
|
||||
lastPos = i + 1;
|
||||
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
|
||||
out += paramHexTable[0xF0 | (c >> 18)] +
|
||||
paramHexTable[0x80 | ((c >> 12) & 0x3F)] +
|
||||
paramHexTable[0x80 | ((c >> 6) & 0x3F)] +
|
||||
paramHexTable[0x80 | (c & 0x3F)];
|
||||
out += hexTable[0xF0 | (c >> 18)] +
|
||||
hexTable[0x80 | ((c >> 12) & 0x3F)] +
|
||||
hexTable[0x80 | ((c >> 6) & 0x3F)] +
|
||||
hexTable[0x80 | (c & 0x3F)];
|
||||
}
|
||||
if (lastPos === 0)
|
||||
return str;
|
||||
@ -885,9 +885,17 @@ function serializeParams(array) {
|
||||
if (len === 0)
|
||||
return '';
|
||||
|
||||
var output = `${escapeParam(array[0])}=${escapeParam(array[1])}`;
|
||||
for (var i = 2; i < len; i += 2)
|
||||
output += `&${escapeParam(array[i])}=${escapeParam(array[i + 1])}`;
|
||||
const firstEncodedParam = encodeStr(array[0], noEscape, paramHexTable);
|
||||
const firstEncodedValue = encodeStr(array[1], noEscape, paramHexTable);
|
||||
let output =
|
||||
`${firstEncodedParam}=${firstEncodedValue}`;
|
||||
|
||||
for (var i = 2; i < len; i += 2) {
|
||||
const encodedParam = encodeStr(array[i], noEscape, paramHexTable);
|
||||
const encodedValue = encodeStr(array[i + 1], noEscape, paramHexTable);
|
||||
output += `&${encodedParam}=${encodedValue}`;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
@ -1431,5 +1439,6 @@ module.exports = {
|
||||
domainToUnicode,
|
||||
urlToOptions,
|
||||
formatSymbol: kFormat,
|
||||
searchParamsSymbol: searchParams
|
||||
searchParamsSymbol: searchParams,
|
||||
encodeStr
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user