|
| 1 | +// Forked from https://github.com/jsdom/jsdom/blob/main/lib/jsdom/living/helpers/strings.js |
| 2 | + |
| 3 | +"use strict"; |
| 4 | + |
| 5 | +// https://infra.spec.whatwg.org/#ascii-whitespace |
| 6 | +const asciiWhitespaceRe = /^[\t\n\f\r ]$/; |
| 7 | +exports.asciiWhitespaceRe = asciiWhitespaceRe; |
| 8 | + |
| 9 | +// https://infra.spec.whatwg.org/#ascii-lowercase |
| 10 | +exports.asciiLowercase = (s) => { |
| 11 | + const len = s.length; |
| 12 | + const out = new Array(len); |
| 13 | + for (let i = 0; i < len; i++) { |
| 14 | + const code = s.charCodeAt(i); |
| 15 | + // If the character is between 'A' (65) and 'Z' (90), convert using bitwise OR with 32 |
| 16 | + out[i] = code >= 65 && code <= 90 ? String.fromCharCode(code | 32) : s[i]; |
| 17 | + } |
| 18 | + return out.join(""); |
| 19 | +}; |
| 20 | + |
| 21 | +// https://infra.spec.whatwg.org/#ascii-uppercase |
| 22 | +exports.asciiUppercase = (s) => { |
| 23 | + const len = s.length; |
| 24 | + const out = new Array(len); |
| 25 | + for (let i = 0; i < len; i++) { |
| 26 | + const code = s.charCodeAt(i); |
| 27 | + // If the character is between 'a' (97) and 'z' (122), convert using bitwise AND with ~32 |
| 28 | + out[i] = code >= 97 && code <= 122 ? String.fromCharCode(code & ~32) : s[i]; |
| 29 | + } |
| 30 | + return out.join(""); |
| 31 | +}; |
| 32 | + |
| 33 | +// https://infra.spec.whatwg.org/#strip-newlines |
| 34 | +exports.stripNewlines = (s) => { |
| 35 | + return s.replace(/[\n\r]+/g, ""); |
| 36 | +}; |
| 37 | + |
| 38 | +// https://infra.spec.whatwg.org/#strip-leading-and-trailing-ascii-whitespace |
| 39 | +exports.stripLeadingAndTrailingASCIIWhitespace = (s) => { |
| 40 | + return s.replace(/^[ \t\n\f\r]+/, "").replace(/[ \t\n\f\r]+$/, ""); |
| 41 | +}; |
| 42 | + |
| 43 | +// https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace |
| 44 | +exports.stripAndCollapseASCIIWhitespace = (s) => { |
| 45 | + return s |
| 46 | + .replace(/[ \t\n\f\r]+/g, " ") |
| 47 | + .replace(/^[ \t\n\f\r]+/, "") |
| 48 | + .replace(/[ \t\n\f\r]+$/, ""); |
| 49 | +}; |
| 50 | + |
| 51 | +// https://html.spec.whatwg.org/multipage/infrastructure.html#valid-simple-colour |
| 52 | +exports.isValidSimpleColor = (s) => { |
| 53 | + return /^#[a-fA-F\d]{6}$/.test(s); |
| 54 | +}; |
| 55 | + |
| 56 | +// https://infra.spec.whatwg.org/#ascii-case-insensitive |
| 57 | +exports.asciiCaseInsensitiveMatch = (a, b) => { |
| 58 | + if (a.length !== b.length) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + for (let i = 0; i < a.length; ++i) { |
| 63 | + if ((a.charCodeAt(i) | 32) !== (b.charCodeAt(i) | 32)) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return true; |
| 69 | +}; |
| 70 | + |
| 71 | +// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-integers |
| 72 | +// Error is represented as null. |
| 73 | +const parseInteger = (exports.parseInteger = (input) => { |
| 74 | + // The implementation here is slightly different from the spec's. We want to use parseInt(), but parseInt() trims |
| 75 | + // Unicode whitespace in addition to just ASCII ones, so we make sure that the trimmed prefix contains only ASCII |
| 76 | + // whitespace ourselves. |
| 77 | + const numWhitespace = input.length - input.trimStart().length; |
| 78 | + if (/[^\t\n\f\r ]/.test(input.slice(0, numWhitespace))) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + // We don't allow hexadecimal numbers here. |
| 82 | + // eslint-disable-next-line radix |
| 83 | + const value = parseInt(input, 10); |
| 84 | + if (Number.isNaN(value)) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + // parseInt() returns -0 for "-0". Normalize that here. |
| 88 | + return value === 0 ? 0 : value; |
| 89 | +}); |
| 90 | + |
| 91 | +// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-non-negative-integers |
| 92 | +// Error is represented as null. |
| 93 | +exports.parseNonNegativeInteger = (input) => { |
| 94 | + const value = parseInteger(input); |
| 95 | + if (value === null) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + if (value < 0) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + return value; |
| 102 | +}; |
| 103 | + |
| 104 | +// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-floating-point-number |
| 105 | +const floatingPointNumRe = /^-?(?:\d+|\d*\.\d+)(?:[eE][-+]?\d+)?$/; |
| 106 | +exports.isValidFloatingPointNumber = (str) => floatingPointNumRe.test(str); |
| 107 | + |
| 108 | +// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-floating-point-number-values |
| 109 | +// Error is represented as null. |
| 110 | +exports.parseFloatingPointNumber = (str) => { |
| 111 | + // The implementation here is slightly different from the spec's. We need to use parseFloat() in order to retain |
| 112 | + // accuracy, but parseFloat() trims Unicode whitespace in addition to just ASCII ones, so we make sure that the |
| 113 | + // trimmed prefix contains only ASCII whitespace ourselves. |
| 114 | + const numWhitespace = str.length - str.trimStart().length; |
| 115 | + if (/[^\t\n\f\r ]/.test(str.slice(0, numWhitespace))) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + const parsed = parseFloat(str); |
| 119 | + return isFinite(parsed) ? parsed : null; |
| 120 | +}; |
| 121 | + |
| 122 | +// https://infra.spec.whatwg.org/#split-on-ascii-whitespace |
| 123 | +exports.splitOnASCIIWhitespace = (str) => { |
| 124 | + let position = 0; |
| 125 | + const tokens = []; |
| 126 | + while (position < str.length && asciiWhitespaceRe.test(str[position])) { |
| 127 | + position++; |
| 128 | + } |
| 129 | + if (position === str.length) { |
| 130 | + return tokens; |
| 131 | + } |
| 132 | + while (position < str.length) { |
| 133 | + const start = position; |
| 134 | + while (position < str.length && !asciiWhitespaceRe.test(str[position])) { |
| 135 | + position++; |
| 136 | + } |
| 137 | + tokens.push(str.slice(start, position)); |
| 138 | + while (position < str.length && asciiWhitespaceRe.test(str[position])) { |
| 139 | + position++; |
| 140 | + } |
| 141 | + } |
| 142 | + return tokens; |
| 143 | +}; |
| 144 | + |
| 145 | +// https://infra.spec.whatwg.org/#split-on-commas |
| 146 | +exports.splitOnCommas = (str) => { |
| 147 | + let position = 0; |
| 148 | + const tokens = []; |
| 149 | + while (position < str.length) { |
| 150 | + let start = position; |
| 151 | + while (position < str.length && str[position] !== ",") { |
| 152 | + position++; |
| 153 | + } |
| 154 | + let end = position; |
| 155 | + while (start < str.length && asciiWhitespaceRe.test(str[start])) { |
| 156 | + start++; |
| 157 | + } |
| 158 | + while (end > start && asciiWhitespaceRe.test(str[end - 1])) { |
| 159 | + end--; |
| 160 | + } |
| 161 | + tokens.push(str.slice(start, end)); |
| 162 | + if (position < str.length) { |
| 163 | + position++; |
| 164 | + } |
| 165 | + } |
| 166 | + return tokens; |
| 167 | +}; |
0 commit comments