Skip to content

Commit

Permalink
url: refactor "escapeParam" function to make it common
Browse files Browse the repository at this point in the history
PR-URL: #19076
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
daynin authored and addaleax committed Mar 23, 2018
1 parent 5fdee52 commit f32796f
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand All @@ -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;
}

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -1431,5 +1439,6 @@ module.exports = {
domainToUnicode,
urlToOptions,
formatSymbol: kFormat,
searchParamsSymbol: searchParams
searchParamsSymbol: searchParams,
encodeStr
};

0 comments on commit f32796f

Please sign in to comment.