(input: string)
| 33 | } |
| 34 | |
| 35 | export function parse(input: string): AttributeSelector | null { |
| 36 | // Must start with `[` and end with `]` |
| 37 | if (input[0] !== '[' || input[input.length - 1] !== ']') { |
| 38 | return null |
| 39 | } |
| 40 | |
| 41 | let i = 1 |
| 42 | let start = i |
| 43 | let end = input.length - 1 |
| 44 | |
| 45 | // Skip whitespace, e.g.: [ data-foo] |
| 46 | // ^^^ |
| 47 | while (isAsciiWhitespace(input.charCodeAt(i))) i++ |
| 48 | |
| 49 | // Attribute name, e.g.: [data-foo] |
| 50 | // ^^^^^^^^ |
| 51 | { |
| 52 | start = i |
| 53 | for (; i < end; i++) { |
| 54 | let currentChar = input.charCodeAt(i) |
| 55 | // Skip escaped character |
| 56 | if (currentChar === BACKSLASH) { |
| 57 | i++ |
| 58 | continue |
| 59 | } |
| 60 | if (currentChar >= UPPER_A && currentChar <= UPPER_Z) continue |
| 61 | if (currentChar >= LOWER_A && currentChar <= LOWER_Z) continue |
| 62 | if (currentChar >= ZERO && currentChar <= NINE) continue |
| 63 | if (currentChar === DASH || currentChar === UNDERSCORE) continue |
| 64 | break |
| 65 | } |
| 66 | |
| 67 | // Must have at least one character in the attribute name |
| 68 | if (start === i) { |
| 69 | return null |
| 70 | } |
| 71 | } |
| 72 | let attribute = input.slice(start, i) |
| 73 | |
| 74 | // Skip whitespace, e.g.: [data-foo =value] |
| 75 | // ^^^ |
| 76 | while (isAsciiWhitespace(input.charCodeAt(i))) i++ |
| 77 | |
| 78 | // At the end, e.g.: `[data-foo]` |
| 79 | if (i === end) { |
| 80 | return { |
| 81 | attribute, |
| 82 | operator: null, |
| 83 | quote: null, |
| 84 | value: null, |
| 85 | sensitivity: null, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Operator, e.g.: [data-foo*=value] |
| 90 | // ^^ |
| 91 | let operator = null |
| 92 | let currentChar = input.charCodeAt(i) |
no test coverage detected