Skip to content

Commit

Permalink
fix data url test
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Jan 2, 2024
1 parent db635f7 commit 6f1ce16
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions lib/fetch/dataURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,30 +614,25 @@ function removeHTTPWhitespace (str, leading = true, trailing = true) {
return i === 0 && j === str.length ? str : str.substring(i, j)
}

/**
* @see https://infra.spec.whatwg.org/#ascii-whitespace
* @param {number} char
*/
function isASCIIWhitespace (char) {
// "\r\n\t\f "
return char === 0x00d || char === 0x00a || char === 0x009 || char === 0x00c || char === 0x020
}

/**
* @see https://infra.spec.whatwg.org/#strip-leading-and-trailing-ascii-whitespace
* @param {string} str
* @param {boolean} [leading=true]
* @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 && isHTTPWhiteSpace(str.charCodeAt(lead))) lead++
}

if (trailing) {
while (j > i && isASCIIWhitespace(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)
}

module.exports = {
Expand Down

0 comments on commit 6f1ce16

Please sign in to comment.