|
| 1 | +import type { AstRule } from 'css-selector-parser'; |
| 2 | +import { replaceTextNode } from './Utility.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Valid positioning pseudo classes. |
| 6 | + */ |
| 7 | +const positioningPseudoClasses = [ |
| 8 | + 'first-child', |
| 9 | + 'nth-child', |
| 10 | + 'nth-last-child', |
| 11 | + 'last-child', |
| 12 | + 'first-of-type', |
| 13 | + 'nth-of-type', |
| 14 | + 'nth-last-of-type', |
| 15 | + 'last-of-type' |
| 16 | +]; |
| 17 | + |
| 18 | +/** |
| 19 | + * Parse the position formula of `nth` pseudo classes. |
| 20 | + * |
| 21 | + * Position formulae are expressed as `an + b`, where `a` and `b` are either `0` or positive integers. |
| 22 | + * @param a |
| 23 | + * @param b |
| 24 | + * @returns A positive integer representing the desired position of the selector, or `false` if the position is invalid. |
| 25 | + */ |
| 26 | +function parsePositionFormula (a: number, b: number): number | false { |
| 27 | + // Invalid. |
| 28 | + if (a < 0 || b < 0) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + // Invalid. |
| 32 | + if (!Number.isInteger(a) || !Number.isInteger(b)) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + // Valid, return `b`. |
| 36 | + if (a === 0) { |
| 37 | + return b; |
| 38 | + } |
| 39 | + return false; |
| 40 | + // TODO: Add a case for when both `a` and `b` are positive. |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Describes an element based on pieces of a selector. |
| 45 | + */ |
| 46 | +export class Descriptor { |
| 47 | + public rule; |
| 48 | + public element; |
| 49 | + public combinator = ''; |
| 50 | + public position = { |
| 51 | + explicit: false, |
| 52 | + from: 'end' as 'start' | 'end', |
| 53 | + index: 1, |
| 54 | + type: 'child' as 'child' | 'type' |
| 55 | + }; |
| 56 | + public invalid = false; |
| 57 | + private rawContent = ''; |
| 58 | + |
| 59 | + constructor (rule: AstRule, content?: string) { |
| 60 | + this.rule = rule; |
| 61 | + |
| 62 | + // Create the element. |
| 63 | + let tag = 'div'; |
| 64 | + if (rule.tag?.type === 'TagName') { |
| 65 | + tag = rule.tag.name; |
| 66 | + } else if (rule.tag?.type === 'WildcardTag') { |
| 67 | + this.invalid = true; |
| 68 | + } |
| 69 | + this.element = document.createElement(tag); |
| 70 | + |
| 71 | + // Check for pseudo elements. |
| 72 | + if (rule.pseudoElement) { |
| 73 | + this.invalid = true |
| 74 | + } |
| 75 | + |
| 76 | + if (this.invalid) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Set the ids. |
| 81 | + if (rule.ids) { |
| 82 | + this.element.id = rule.ids.join(' '); |
| 83 | + } |
| 84 | + |
| 85 | + // Set the classes. |
| 86 | + if (rule.classNames) { |
| 87 | + this.element.className = rule.classNames.join(' '); |
| 88 | + } |
| 89 | + |
| 90 | + // Set the attributes. |
| 91 | + if (rule.attributes) { |
| 92 | + for (const attribute of rule.attributes) { |
| 93 | + let value = ''; |
| 94 | + if (attribute.value?.type === 'String') { |
| 95 | + value = attribute.value.value; |
| 96 | + } |
| 97 | + this.element.setAttribute(attribute.name, value); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Set the content. |
| 102 | + if (content) { |
| 103 | + this.content = content; |
| 104 | + } |
| 105 | + |
| 106 | + // Set the combinator. |
| 107 | + this.combinator = rule.combinator ?? ' '; |
| 108 | + |
| 109 | + // Set the position. |
| 110 | + if (rule.pseudoClasses && rule.pseudoClasses.length > 0) { |
| 111 | + const pseudoClass = rule.pseudoClasses[0]; |
| 112 | + this.invalid = this.invalid || rule.pseudoClasses.length > 1 || !positioningPseudoClasses.includes(pseudoClass.name); |
| 113 | + if (this.invalid) return; |
| 114 | + this.position.explicit = true; |
| 115 | + this.position.from = pseudoClass.name.includes('last') ? 'end' : 'start'; |
| 116 | + this.position.type = pseudoClass.name.includes('type') ? 'type' : 'child'; |
| 117 | + if (pseudoClass.name.includes('nth')) { |
| 118 | + const position = pseudoClass.argument?.type === 'Formula' && parsePositionFormula(pseudoClass.argument.a, pseudoClass.argument.b); |
| 119 | + if (position) { |
| 120 | + this.position.index = position; |
| 121 | + } else { |
| 122 | + this.invalid = true; |
| 123 | + return; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * The content of the element. |
| 131 | + */ |
| 132 | + public get content (): string { |
| 133 | + return this.rawContent; |
| 134 | + } |
| 135 | + |
| 136 | + public set content (value: string) { |
| 137 | + this.rawContent = value; |
| 138 | + // Strip any quote marks from around the content string. |
| 139 | + if (/(?:'|")/.test(value.charAt(0)) && /(?:'|")/.test(value.charAt(value.length - 1))) { |
| 140 | + value = value.substring(1, value.length - 1); |
| 141 | + } |
| 142 | + // Place the content in the `href` property of anchor elements. |
| 143 | + if (this.element instanceof HTMLAnchorElement) { |
| 144 | + this.element.href = value; |
| 145 | + } |
| 146 | + // Place the content in the `src` property of audio, iframe, image, and video elements. |
| 147 | + else if ( |
| 148 | + this.element instanceof HTMLAudioElement |
| 149 | + || this.element instanceof HTMLIFrameElement |
| 150 | + || this.element instanceof HTMLImageElement |
| 151 | + || this.element instanceof HTMLVideoElement |
| 152 | + ) { |
| 153 | + this.element.src = value; |
| 154 | + } |
| 155 | + // Place the content in the `placeholder` property of input and textarea elements. |
| 156 | + else if ( |
| 157 | + this.element instanceof HTMLInputElement |
| 158 | + || this.element instanceof HTMLTextAreaElement |
| 159 | + ) { |
| 160 | + this.element.placeholder = value; |
| 161 | + } |
| 162 | + // Use the content as inner-text and place it in the `value` property of option elements. |
| 163 | + else if (this.element instanceof HTMLOptionElement) { |
| 164 | + replaceTextNode(this.element, value); |
| 165 | + this.element.value = value; |
| 166 | + } |
| 167 | + // Place the content in the `value` property of select elements. |
| 168 | + else if (this.element instanceof HTMLSelectElement) { |
| 169 | + this.element.value = value; |
| 170 | + } |
| 171 | + // Use the content as inner-text for all other elements. |
| 172 | + else { |
| 173 | + replaceTextNode(this.element, value); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * A selector string suitable for selecting similar sibling elements. |
| 179 | + */ |
| 180 | + public get siblingSelector (): string { |
| 181 | + let selector = ':scope > '; |
| 182 | + selector += this.position.type === 'type' ? this.element.tagName : '*'; |
| 183 | + selector += ':nth'; |
| 184 | + selector += this.position.from === 'end' ? '-last' : ''; |
| 185 | + selector += this.position.type === 'type' ? '-of-type' : '-child'; |
| 186 | + selector += `(${this.position.index})`; |
| 187 | + return selector; |
| 188 | + } |
| 189 | +} |
0 commit comments