diff --git a/lib/fetch/dataURL.js b/lib/fetch/dataURL.js index 12fa1abbb93..d7a638866a3 100644 --- a/lib/fetch/dataURL.js +++ b/lib/fetch/dataURL.js @@ -604,14 +604,18 @@ function isHTTPWhiteSpace (char) { * @param {boolean} [trailing=true] */ function removeHTTPWhitespace (str, leading = true, trailing = true) { - let i = 0; let j = str.length + let lead = 0 + let trail = str.length - 1 + if (leading) { - while (j > i && isHTTPWhiteSpace(str.charCodeAt(i))) --i + while (lead < str.length && isHTTPWhiteSpace(str.charCodeAt(lead))) lead++ } + if (trailing) { - while (j > i && isHTTPWhiteSpace(str.charCodeAt(j - 1))) --j + while (trail > 0 && isHTTPWhiteSpace(str.charCodeAt(trail))) trail-- } - return i === 0 && j === str.length ? str : str.substring(i, j) + + return lead === 0 && trail === str.length - 1 ? str : str.slice(lead, trail + 1) } /** @@ -630,14 +634,18 @@ function isASCIIWhitespace (char) { * @param {boolean} [trailing=true] */ function removeASCIIWhitespace (str, leading = true, trailing = true) { - let i = 0; let j = str.length + let lead = 0 + let trail = str.length - 1 + if (leading) { - while (j > i && isASCIIWhitespace(str.charCodeAt(i))) --i + while (lead < str.length && isASCIIWhitespace(str.charCodeAt(lead))) lead++ } + if (trailing) { - while (j > i && isASCIIWhitespace(str.charCodeAt(j - 1))) --j + while (trail > 0 && isASCIIWhitespace(str.charCodeAt(trail))) trail-- } - return i === 0 && j === str.length ? str : str.substring(i, j) + + return lead === 0 && trail === str.length - 1 ? str : str.slice(lead, trail + 1) } module.exports = {