diff --git a/lib/index.js b/lib/index.js index 55f56c8..6c91fd4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -252,18 +252,23 @@ exports.DOMString = (value, options = {}) => { exports.ByteString = (value, options = {}) => { const x = exports.DOMString(value, options); - let c; - for (let i = 0; (c = x.codePointAt(i)) !== undefined; ++i) { - if (c > 255) { - throw makeException(TypeError, "is not a valid ByteString", options); - } + + // Unicode regex is identical here but is ~5x slower on 16-bit strings + // eslint-disable-next-line require-unicode-regexp + if (!/[^\x00-\xff]/.test(x)) { + return x; } - return x; + throw makeException(TypeError, "is not a valid ByteString", options); }; exports.USVString = (value, options = {}) => { const S = exports.DOMString(value, options); + + if (S.toWellFormed) { + return S.toWellFormed(); + } + const n = S.length; const U = []; for (let i = 0; i < n; ++i) {