Skip to content
Closed
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
18 changes: 11 additions & 7 deletions lib/web/webidl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,9 @@ webidl.converters.DOMString = function (V, prefix, argument, flags) {
return String(V)
}

// Matches any UTF-16 code unit above 255.
const nonByteStringCodeUnit = /[\u0100-\uffff]/

// https://webidl.spec.whatwg.org/#es-ByteString
webidl.converters.ByteString = function (V, prefix, argument) {
// 1. Let x be ? ToString(V).
Expand All @@ -621,13 +624,14 @@ webidl.converters.ByteString = function (V, prefix, argument) {

// 2. If the value of any element of x is greater than
// 255, then throw a TypeError.
for (let index = 0; index < x.length; index++) {
if (x.charCodeAt(index) > 255) {
throw new TypeError(
'Cannot convert argument to a ByteString because the character at ' +
`index ${index} has a value of ${x.charCodeAt(index)} which is greater than 255.`
)
}
// Note: A native regular expression scan instead of a loop over x. The
// index is only needed for the error message.
if (nonByteStringCodeUnit.test(x)) {
const index = x.search(nonByteStringCodeUnit)
throw new TypeError(
'Cannot convert argument to a ByteString because the character at ' +
`index ${index} has a value of ${x.charCodeAt(index)} which is greater than 255.`
)
}

// 3. Return an IDL ByteString value whose length is the
Expand Down