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:
Sergey Golovin 2018-03-01 21:56:39 +03:00 committed by Anna Henningsen
parent 5fdee52c5e
commit f32796fad2
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9

View File

@ -814,7 +814,7 @@ const noEscape = [
const paramHexTable = hexTable.slice(); const paramHexTable = hexTable.slice();
paramHexTable[0x20] = '+'; paramHexTable[0x20] = '+';
function escapeParam(str) { function encodeStr(str, noEscapeTable, hexTable) {
const len = str.length; const len = str.length;
if (len === 0) if (len === 0)
return ''; return '';
@ -827,12 +827,12 @@ function escapeParam(str) {
// ASCII // ASCII
if (c < 0x80) { if (c < 0x80) {
if (noEscape[c] === 1) if (noEscapeTable[c] === 1)
continue; continue;
if (lastPos < i) if (lastPos < i)
out += str.slice(lastPos, i); out += str.slice(lastPos, i);
lastPos = i + 1; lastPos = i + 1;
out += paramHexTable[c]; out += hexTable[c];
continue; continue;
} }
@ -842,15 +842,15 @@ function escapeParam(str) {
// Multi-byte characters ... // Multi-byte characters ...
if (c < 0x800) { if (c < 0x800) {
lastPos = i + 1; lastPos = i + 1;
out += paramHexTable[0xC0 | (c >> 6)] + out += hexTable[0xC0 | (c >> 6)] +
paramHexTable[0x80 | (c & 0x3F)]; hexTable[0x80 | (c & 0x3F)];
continue; continue;
} }
if (c < 0xD800 || c >= 0xE000) { if (c < 0xD800 || c >= 0xE000) {
lastPos = i + 1; lastPos = i + 1;
out += paramHexTable[0xE0 | (c >> 12)] + out += hexTable[0xE0 | (c >> 12)] +
paramHexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] +
paramHexTable[0x80 | (c & 0x3F)]; hexTable[0x80 | (c & 0x3F)];
continue; continue;
} }
// Surrogate pair // Surrogate pair
@ -866,10 +866,10 @@ function escapeParam(str) {
} }
lastPos = i + 1; lastPos = i + 1;
c = 0x10000 + (((c & 0x3FF) << 10) | c2); c = 0x10000 + (((c & 0x3FF) << 10) | c2);
out += paramHexTable[0xF0 | (c >> 18)] + out += hexTable[0xF0 | (c >> 18)] +
paramHexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 12) & 0x3F)] +
paramHexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] +
paramHexTable[0x80 | (c & 0x3F)]; hexTable[0x80 | (c & 0x3F)];
} }
if (lastPos === 0) if (lastPos === 0)
return str; return str;
@ -885,9 +885,17 @@ function serializeParams(array) {
if (len === 0) if (len === 0)
return ''; return '';
var output = `${escapeParam(array[0])}=${escapeParam(array[1])}`; const firstEncodedParam = encodeStr(array[0], noEscape, paramHexTable);
for (var i = 2; i < len; i += 2) const firstEncodedValue = encodeStr(array[1], noEscape, paramHexTable);
output += `&${escapeParam(array[i])}=${escapeParam(array[i + 1])}`; 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; return output;
} }
@ -1431,5 +1439,6 @@ module.exports = {
domainToUnicode, domainToUnicode,
urlToOptions, urlToOptions,
formatSymbol: kFormat, formatSymbol: kFormat,
searchParamsSymbol: searchParams searchParamsSymbol: searchParams,
encodeStr
}; };