Skip to content

Commit cb1abde

Browse files
committed
Refactor to remove unneeded utilities
* Reorder pseudo handlers * Add docs to a bunch of functions * Refactor to always index queries (will be improved in the future)
1 parent f763600 commit cb1abde

File tree

10 files changed

+359
-307
lines changed

10 files changed

+359
-307
lines changed

lib/attribute.js

+16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export function attribute(query, element, schema) {
4848
}
4949

5050
/**
51+
* Check whether an attribute exists.
52+
*
5153
* `[attr]`
5254
*
5355
* @param {RuleAttr} _
@@ -60,6 +62,8 @@ function exists(_, element, info) {
6062
}
6163

6264
/**
65+
* Check whether an attribute has an exact value.
66+
*
6367
* `[attr=value]`
6468
*
6569
* @param {RuleAttr} query
@@ -76,6 +80,9 @@ function exact(query, element, info) {
7680
}
7781

7882
/**
83+
* Check whether an attribute, interpreted as a space-separated list, contains
84+
* a value.
85+
*
7986
* `[attr~=value]`
8087
*
8188
* @param {RuleAttr} query
@@ -102,6 +109,9 @@ function spaceSeparatedList(query, element, info) {
102109
}
103110

104111
/**
112+
* Check whether an attribute has a substring as either the exact value or a
113+
* prefix.
114+
*
105115
* `[attr|=value]`
106116
*
107117
* @param {RuleAttr} query
@@ -125,6 +135,8 @@ function exactOrPrefix(query, element, info) {
125135
}
126136

127137
/**
138+
* Check whether an attribute has a substring as its start.
139+
*
128140
* `[attr^=value]`
129141
*
130142
* @param {RuleAttr} query
@@ -145,6 +157,8 @@ function begins(query, element, info) {
145157
}
146158

147159
/**
160+
* Check whether an attribute has a substring as its end.
161+
*
148162
* `[attr$=value]`
149163
*
150164
* @param {RuleAttr} query
@@ -164,6 +178,8 @@ function ends(query, element, info) {
164178
}
165179

166180
/**
181+
* Check whether an attribute contains a substring.
182+
*
167183
* `[attr*=value]`
168184
*
169185
* @param {RuleAttr} query

lib/class-name.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55

66
/**
7+
* Check whether an element has all class names.
8+
*
79
* @param {Rule} query
810
* @param {Element} element
911
* @returns {boolean}

lib/enter-state.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
*/
88

99
import {direction} from 'direction'
10-
import {isElement} from 'hast-util-is-element'
1110
import {toString} from 'hast-util-to-string'
1211
import {svg} from 'property-information'
1312
import {visit, EXIT, SKIP} from 'unist-util-visit'
14-
import {element} from './util.js'
1513

1614
/**
1715
* Enter a node.
@@ -36,7 +34,7 @@ export function enterState(state, node) {
3634
/** @type {Direction | undefined} */
3735
let dirInferred
3836

39-
if (element(node) && node.properties) {
37+
if (node.type === 'element' && node.properties) {
4038
const lang = node.properties.xmlLang || node.properties.lang
4139
const type = node.properties.type || 'text'
4240
const dir = dirProperty(node)
@@ -50,7 +48,7 @@ export function enterState(state, node) {
5048
state.editableOrEditingHost = true
5149
}
5250

53-
if (isElement(node, 'svg')) {
51+
if (node.tagName === 'svg') {
5452
state.schema = svg
5553
}
5654

@@ -62,25 +60,24 @@ export function enterState(state, node) {
6260
// Explicit `[dir=ltr]`.
6361
dir === 'ltr' ||
6462
// HTML with an invalid or no `[dir]`.
65-
(dir !== 'auto' && isElement(node, 'html')) ||
63+
(dir !== 'auto' && node.tagName === 'html') ||
6664
// `input[type=tel]` with an invalid or no `[dir]`.
67-
(dir !== 'auto' && isElement(node, 'input') && type === 'tel')
65+
(dir !== 'auto' && node.tagName === 'input' && type === 'tel')
6866
) {
6967
dirInferred = 'ltr'
7068
// `[dir=auto]` or `bdi` with an invalid or no `[dir]`.
71-
} else if (dir === 'auto' || isElement(node, 'bdi')) {
72-
if (isElement(node, 'textarea')) {
69+
} else if (dir === 'auto' || node.tagName === 'bdi') {
70+
if (node.tagName === 'textarea') {
7371
// Check contents of `<textarea>`.
7472
dirInferred = dirBidi(toString(node))
7573
} else if (
76-
isElement(node, 'input') &&
74+
node.tagName === 'input' &&
7775
(type === 'email' ||
7876
type === 'search' ||
7977
type === 'tel' ||
8078
type === 'text')
8179
) {
8280
// Check value of `<input>`.
83-
// @ts-expect-error something is `never` in types but this is needed.
8481
dirInferred = node.properties.value
8582
? // @ts-expect-error Assume string
8683
dirBidi(node.properties.value)
@@ -119,7 +116,11 @@ export function enterState(state, node) {
119116

120117
if (
121118
child !== node &&
122-
(isElement(child, ['bdi', 'script', 'style', 'textare']) ||
119+
child.type === 'element' &&
120+
(child.tagName === 'bdi' ||
121+
child.tagName === 'script' ||
122+
child.tagName === 'style' ||
123+
child.tagName === 'textare' ||
123124
dirProperty(child))
124125
) {
125126
return SKIP
@@ -142,7 +143,9 @@ function dirBidi(value) {
142143
*/
143144
function dirProperty(node) {
144145
const value =
145-
element(node) && node.properties && typeof node.properties.dir === 'string'
146+
node.type === 'element' &&
147+
node.properties &&
148+
typeof node.properties.dir === 'string'
146149
? node.properties.dir.toLowerCase()
147150
: undefined
148151

lib/id.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55

66
/**
7+
* Check whether an element has an ID.
8+
*
79
* @param {Rule} query
810
* @param {Element} element
911
* @returns {boolean}

lib/name.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55

66
/**
7+
* Check whether an element has a tag name.
8+
*
79
* @param {Rule} query
810
* @param {Element} element
911
* @returns {boolean}

0 commit comments

Comments
 (0)