Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/fetch/dataURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand All @@ -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
Copy link
Member Author

@KhafraDev KhafraDev Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so unless str.length was 0 j > i would always be true... and we're decrementing from i, which means if isASCIIWhitespace(...) was true, i would be negative? Not sure why I didn't realize this before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also using i and j instead of lead and trail shouldn't have landed

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 = {
Expand Down