Skip to content

Commit 465234f

Browse files
committed
Use ESM
1 parent 4683510 commit 465234f

21 files changed

+271
-345
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
hast-util-select.js
7-
hast-util-select.min.js
85
yarn.lock

.prettierignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
hast-util-select.js
3-
hast-util-select.min.js
4-
*.json
52
*.md

index.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
'use strict'
1+
import {any} from './lib/any.js'
2+
import {parse} from './lib/parse.js'
23

3-
exports.matches = matches
4-
exports.selectAll = selectAll
5-
exports.select = select
6-
7-
var any = require('./lib/any')
8-
var parse = require('./lib/parse')
9-
10-
function matches(selector, node, space) {
4+
export function matches(selector, node, space) {
115
return Boolean(
12-
any(parse(selector), node, {space: space, one: true, shallow: true})[0]
6+
any(parse(selector), node, {space, one: true, shallow: true})[0]
137
)
148
}
159

16-
function select(selector, node, space) {
17-
return any(parse(selector), node, {space: space, one: true})[0] || null
10+
export function select(selector, node, space) {
11+
return any(parse(selector), node, {space, one: true})[0] || null
1812
}
1913

20-
function selectAll(selector, node, space) {
21-
return any(parse(selector), node, {space: space})
14+
export function selectAll(selector, node, space) {
15+
return any(parse(selector), node, {space})
2216
}

lib/any.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
'use strict'
2-
3-
module.exports = match
4-
5-
var html = require('property-information/html')
6-
var svg = require('property-information/svg')
7-
var zwitch = require('zwitch')
8-
var enter = require('./enter-state')
9-
var nest = require('./nest')
10-
var pseudo = require('./pseudo')
11-
var test = require('./test')
1+
import {html, svg} from 'property-information'
2+
import {zwitch} from 'zwitch'
3+
import {enterState} from './enter-state.js'
4+
import {nest} from './nest.js'
5+
import {pseudo} from './pseudo.js'
6+
import {test} from './test.js'
127

138
var type = zwitch('type', {
149
unknown: unknownType,
1510
invalid: invalidType,
16-
handlers: {
17-
selectors: selectors,
18-
ruleSet: ruleSet,
19-
rule: rule
20-
}
11+
handlers: {selectors, ruleSet, rule}
2112
})
2213

23-
function match(query, node, state) {
14+
export function any(query, node, state) {
2415
return query && node ? type(query, node, state) : []
2516
}
2617

@@ -57,7 +48,7 @@ function rule(query, tree, state) {
5748
direction: 'ltr',
5849
editableOrEditingHost: false,
5950
scopeElements: tree.type === 'root' ? tree.children : [tree],
60-
iterator: iterator,
51+
iterator,
6152
one: state.one,
6253
shallow: state.shallow
6354
})
@@ -66,7 +57,7 @@ function rule(query, tree, state) {
6657
return collect.result
6758

6859
function iterator(query, node, index, parent, state) {
69-
var exit = enter(state, node)
60+
var exit = enterState(state, node)
7061

7162
if (test(query, node, index, parent, state)) {
7263
if (query.rule) {
@@ -85,7 +76,7 @@ function rule(query, tree, state) {
8576
var index = -1
8677

8778
while (++index < pseudos.length) {
88-
if (pseudo.needsIndex.indexOf(pseudos[index].name) > -1) {
79+
if (pseudo.needsIndex.includes(pseudos[index].name)) {
8980
state.index = true
9081
break
9182
}
@@ -95,12 +86,14 @@ function rule(query, tree, state) {
9586
}
9687
}
9788

98-
/* istanbul ignore next - Shouldn’t be invoked, all data is handled. */
89+
// Shouldn’t be called, all data is handled.
90+
/* c8 ignore next 3 */
9991
function unknownType(query) {
10092
throw new Error('Unknown type `' + query.type + '`')
10193
}
10294

103-
/* istanbul ignore next - Shouldn’t be invoked, parser gives correct data. */
95+
// Shouldn’t be called, parser gives correct data.
96+
/* c8 ignore next 3 */
10497
function invalidType() {
10598
throw new Error('Invalid type')
10699
}
@@ -127,12 +120,13 @@ function collector(one) {
127120

128121
function collectOne(element) {
129122
if (one) {
130-
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */
123+
// Shouldn’t happen, safeguards performance problems.
124+
/* c8 ignore next */
131125
if (found) throw new Error('Cannot collect multiple nodes')
132126
found = true
133127
}
134128

135-
if (result.indexOf(element) < 0) result.push(element)
129+
if (!result.includes(element)) result.push(element)
136130
}
137131
}
138132
}

lib/attribute.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
'use strict'
2-
3-
module.exports = match
4-
5-
var commas = require('comma-separated-tokens')
6-
var has = require('hast-util-has-property')
7-
var find = require('property-information/find')
8-
var spaces = require('space-separated-tokens')
9-
var zwitch = require('zwitch')
1+
import {stringify as commas} from 'comma-separated-tokens'
2+
import {hasProperty} from 'hast-util-has-property'
3+
import {find} from 'property-information'
4+
import {stringify as spaces} from 'space-separated-tokens'
5+
import {zwitch} from 'zwitch'
106

117
var handle = zwitch('operator', {
128
unknown: unknownOperator,
@@ -21,7 +17,7 @@ var handle = zwitch('operator', {
2117
}
2218
})
2319

24-
function match(query, node, schema) {
20+
export function attribute(query, node, schema) {
2521
var attrs = query.attrs
2622
var index = -1
2723

@@ -34,13 +30,13 @@ function match(query, node, schema) {
3430

3531
// `[attr]`
3632
function exists(query, node, info) {
37-
return has(node, info.property)
33+
return hasProperty(node, info.property)
3834
}
3935

4036
// `[attr=value]`
4137
function exact(query, node, info) {
4238
return (
43-
has(node, info.property) &&
39+
hasProperty(node, info.property) &&
4440
normalizeValue(node.properties[info.property], info) === query.value
4541
)
4642
}
@@ -55,10 +51,11 @@ function spaceSeparatedList(query, node, info) {
5551
(!info.commaSeparated &&
5652
value &&
5753
typeof value === 'object' &&
58-
value.indexOf(query.value) > -1) ||
54+
value.includes(query.value)) ||
5955
// For all other values (including comma-separated lists), return whether this
6056
// is an exact match.
61-
(has(node, info.property) && normalizeValue(value, info) === query.value)
57+
(hasProperty(node, info.property) &&
58+
normalizeValue(value, info) === query.value)
6259
)
6360
}
6461

@@ -67,7 +64,7 @@ function exactOrPrefix(query, node, info) {
6764
var value = normalizeValue(node.properties[info.property], info)
6865

6966
return (
70-
has(node, info.property) &&
67+
hasProperty(node, info.property) &&
7168
(value === query.value ||
7269
(value.slice(0, query.value.length) === query.value &&
7370
value.charAt(query.value.length) === '-'))
@@ -77,7 +74,7 @@ function exactOrPrefix(query, node, info) {
7774
// `[attr^=value]`
7875
function begins(query, node, info) {
7976
return (
80-
has(node, info.property) &&
77+
hasProperty(node, info.property) &&
8178
normalizeValue(node.properties[info.property], info).slice(
8279
0,
8380
query.value.length
@@ -88,7 +85,7 @@ function begins(query, node, info) {
8885
// `[attr$=value]`
8986
function ends(query, node, info) {
9087
return (
91-
has(node, info.property) &&
88+
hasProperty(node, info.property) &&
9289
normalizeValue(node.properties[info.property], info).slice(
9390
-query.value.length
9491
) === query.value
@@ -98,13 +95,13 @@ function ends(query, node, info) {
9895
// `[attr*=value]`
9996
function contains(query, node, info) {
10097
return (
101-
has(node, info.property) &&
102-
normalizeValue(node.properties[info.property], info).indexOf(query.value) >
103-
-1
98+
hasProperty(node, info.property) &&
99+
normalizeValue(node.properties[info.property], info).includes(query.value)
104100
)
105101
}
106102

107-
/* istanbul ignore next - Shouldn’t be invoked, Parser throws an error instead. */
103+
// Shouldn’t be called, Parser throws an error instead.
104+
/* c8 ignore next 3 */
108105
function unknownOperator(query) {
109106
throw new Error('Unknown operator `' + query.operator + '`')
110107
}
@@ -120,7 +117,7 @@ function normalizeValue(value, info) {
120117
}
121118

122119
if (typeof value === 'object' && 'length' in value) {
123-
return (info.commaSeparated ? commas.stringify : spaces.stringify)(value)
120+
return (info.commaSeparated ? commas : spaces)(value)
124121
}
125122

126123
return value

lib/class-name.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
'use strict'
2-
3-
module.exports = match
4-
5-
function match(query, node) {
1+
export function className(query, node) {
62
var value = node.properties.className || []
73
var index = -1
84

95
while (++index < query.classNames.length) {
10-
if (value.indexOf(query.classNames[index]) < 0) return
6+
if (!value.includes(query.classNames[index])) return
117
}
128

139
return true

lib/enter-state.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
'use strict'
2-
3-
module.exports = enter
4-
5-
var direction = require('direction')
6-
var is = require('hast-util-is-element')
7-
var toString = require('hast-util-to-string')
8-
var svg = require('property-information/svg')
9-
var visit = require('unist-util-visit')
1+
import {direction} from 'direction'
2+
import {isElement} from 'hast-util-is-element'
3+
import toString from 'hast-util-to-string'
4+
import {svg} from 'property-information'
5+
import {visit, EXIT, SKIP} from 'unist-util-visit'
106

117
// eslint-disable-next-line complexity
12-
function enter(state, node) {
8+
export function enterState(state, node) {
139
var schema = state.schema
1410
var language = state.language
1511
var currentDirection = state.direction
@@ -25,7 +21,7 @@ function enter(state, node) {
2521
type = node.properties.type || 'text'
2622
dir = dirProperty(node)
2723

28-
if (lang != null) {
24+
if (lang !== undefined && lang !== null) {
2925
state.language = lang
3026
found = true
3127
}
@@ -36,7 +32,7 @@ function enter(state, node) {
3632
found = true
3733
}
3834

39-
if (is(node, 'svg')) {
35+
if (isElement(node, 'svg')) {
4036
state.schema = svg
4137
found = true
4238
}
@@ -49,18 +45,18 @@ function enter(state, node) {
4945
// Explicit `[dir=ltr]`.
5046
dir === 'ltr' ||
5147
// HTML with an invalid or no `[dir]`.
52-
(dir !== 'auto' && is(node, 'html')) ||
48+
(dir !== 'auto' && isElement(node, 'html')) ||
5349
// `input[type=tel]` with an invalid or no `[dir]`.
54-
(dir !== 'auto' && is(node, 'input') && type === 'tel')
50+
(dir !== 'auto' && isElement(node, 'input') && type === 'tel')
5551
) {
5652
dirInferred = 'ltr'
5753
// `[dir=auto]` or `bdi` with an invalid or no `[dir]`.
58-
} else if (dir === 'auto' || is(node, 'bdi')) {
59-
if (is(node, 'textarea')) {
54+
} else if (dir === 'auto' || isElement(node, 'bdi')) {
55+
if (isElement(node, 'textarea')) {
6056
// Check contents of `<textarea>`.
6157
dirInferred = dirBidi(toString(node))
6258
} else if (
63-
is(node, 'input') &&
59+
isElement(node, 'input') &&
6460
(type === 'email' ||
6561
type === 'search' ||
6662
type === 'tel' ||
@@ -100,14 +96,15 @@ function enter(state, node) {
10096
function inferDirectionality(child) {
10197
if (child.type === 'text') {
10298
dirInferred = dirBidi(child.value)
103-
return dirInferred ? visit.EXIT : null
99+
return dirInferred ? EXIT : null
104100
}
105101

106102
if (
107103
child !== node &&
108-
(is(child, ['bdi', 'script', 'style', 'textare']) || dirProperty(child))
104+
(isElement(child, ['bdi', 'script', 'style', 'textare']) ||
105+
dirProperty(child))
109106
) {
110-
return visit.SKIP
107+
return SKIP
111108
}
112109
}
113110
}

lib/id.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
'use strict'
2-
3-
module.exports = match
4-
5-
function match(query, node) {
1+
export function id(query, node) {
62
return node.properties.id === query.id
73
}

lib/name.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
'use strict'
2-
3-
module.exports = match
4-
5-
function match(query, node) {
1+
export function name(query, node) {
62
return query.tagName === '*' || query.tagName === node.tagName
73
}

0 commit comments

Comments
 (0)