Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 28, 2021
1 parent 4683510 commit 465234f
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 345 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
hast-util-select.js
hast-util-select.min.js
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
coverage/
hast-util-select.js
hast-util-select.min.js
*.json
*.md
22 changes: 8 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
'use strict'
import {any} from './lib/any.js'
import {parse} from './lib/parse.js'

exports.matches = matches
exports.selectAll = selectAll
exports.select = select

var any = require('./lib/any')
var parse = require('./lib/parse')

function matches(selector, node, space) {
export function matches(selector, node, space) {
return Boolean(
any(parse(selector), node, {space: space, one: true, shallow: true})[0]
any(parse(selector), node, {space, one: true, shallow: true})[0]
)
}

function select(selector, node, space) {
return any(parse(selector), node, {space: space, one: true})[0] || null
export function select(selector, node, space) {
return any(parse(selector), node, {space, one: true})[0] || null
}

function selectAll(selector, node, space) {
return any(parse(selector), node, {space: space})
export function selectAll(selector, node, space) {
return any(parse(selector), node, {space})
}
42 changes: 18 additions & 24 deletions lib/any.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
'use strict'

module.exports = match

var html = require('property-information/html')
var svg = require('property-information/svg')
var zwitch = require('zwitch')
var enter = require('./enter-state')
var nest = require('./nest')
var pseudo = require('./pseudo')
var test = require('./test')
import {html, svg} from 'property-information'
import {zwitch} from 'zwitch'
import {enterState} from './enter-state.js'
import {nest} from './nest.js'
import {pseudo} from './pseudo.js'
import {test} from './test.js'

var type = zwitch('type', {
unknown: unknownType,
invalid: invalidType,
handlers: {
selectors: selectors,
ruleSet: ruleSet,
rule: rule
}
handlers: {selectors, ruleSet, rule}
})

function match(query, node, state) {
export function any(query, node, state) {
return query && node ? type(query, node, state) : []
}

Expand Down Expand Up @@ -57,7 +48,7 @@ function rule(query, tree, state) {
direction: 'ltr',
editableOrEditingHost: false,
scopeElements: tree.type === 'root' ? tree.children : [tree],
iterator: iterator,
iterator,
one: state.one,
shallow: state.shallow
})
Expand All @@ -66,7 +57,7 @@ function rule(query, tree, state) {
return collect.result

function iterator(query, node, index, parent, state) {
var exit = enter(state, node)
var exit = enterState(state, node)

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

while (++index < pseudos.length) {
if (pseudo.needsIndex.indexOf(pseudos[index].name) > -1) {
if (pseudo.needsIndex.includes(pseudos[index].name)) {
state.index = true
break
}
Expand All @@ -95,12 +86,14 @@ function rule(query, tree, state) {
}
}

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

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

function collectOne(element) {
if (one) {
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */
// Shouldn’t happen, safeguards performance problems.
/* c8 ignore next */
if (found) throw new Error('Cannot collect multiple nodes')
found = true
}

if (result.indexOf(element) < 0) result.push(element)
if (!result.includes(element)) result.push(element)
}
}
}
41 changes: 19 additions & 22 deletions lib/attribute.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
'use strict'

module.exports = match

var commas = require('comma-separated-tokens')
var has = require('hast-util-has-property')
var find = require('property-information/find')
var spaces = require('space-separated-tokens')
var zwitch = require('zwitch')
import {stringify as commas} from 'comma-separated-tokens'
import {hasProperty} from 'hast-util-has-property'
import {find} from 'property-information'
import {stringify as spaces} from 'space-separated-tokens'
import {zwitch} from 'zwitch'

var handle = zwitch('operator', {
unknown: unknownOperator,
Expand All @@ -21,7 +17,7 @@ var handle = zwitch('operator', {
}
})

function match(query, node, schema) {
export function attribute(query, node, schema) {
var attrs = query.attrs
var index = -1

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

// `[attr]`
function exists(query, node, info) {
return has(node, info.property)
return hasProperty(node, info.property)
}

// `[attr=value]`
function exact(query, node, info) {
return (
has(node, info.property) &&
hasProperty(node, info.property) &&
normalizeValue(node.properties[info.property], info) === query.value
)
}
Expand All @@ -55,10 +51,11 @@ function spaceSeparatedList(query, node, info) {
(!info.commaSeparated &&
value &&
typeof value === 'object' &&
value.indexOf(query.value) > -1) ||
value.includes(query.value)) ||
// For all other values (including comma-separated lists), return whether this
// is an exact match.
(has(node, info.property) && normalizeValue(value, info) === query.value)
(hasProperty(node, info.property) &&
normalizeValue(value, info) === query.value)
)
}

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

return (
has(node, info.property) &&
hasProperty(node, info.property) &&
(value === query.value ||
(value.slice(0, query.value.length) === query.value &&
value.charAt(query.value.length) === '-'))
Expand All @@ -77,7 +74,7 @@ function exactOrPrefix(query, node, info) {
// `[attr^=value]`
function begins(query, node, info) {
return (
has(node, info.property) &&
hasProperty(node, info.property) &&
normalizeValue(node.properties[info.property], info).slice(
0,
query.value.length
Expand All @@ -88,7 +85,7 @@ function begins(query, node, info) {
// `[attr$=value]`
function ends(query, node, info) {
return (
has(node, info.property) &&
hasProperty(node, info.property) &&
normalizeValue(node.properties[info.property], info).slice(
-query.value.length
) === query.value
Expand All @@ -98,13 +95,13 @@ function ends(query, node, info) {
// `[attr*=value]`
function contains(query, node, info) {
return (
has(node, info.property) &&
normalizeValue(node.properties[info.property], info).indexOf(query.value) >
-1
hasProperty(node, info.property) &&
normalizeValue(node.properties[info.property], info).includes(query.value)
)
}

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

if (typeof value === 'object' && 'length' in value) {
return (info.commaSeparated ? commas.stringify : spaces.stringify)(value)
return (info.commaSeparated ? commas : spaces)(value)
}

return value
Expand Down
8 changes: 2 additions & 6 deletions lib/class-name.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
'use strict'

module.exports = match

function match(query, node) {
export function className(query, node) {
var value = node.properties.className || []
var index = -1

while (++index < query.classNames.length) {
if (value.indexOf(query.classNames[index]) < 0) return
if (!value.includes(query.classNames[index])) return
}

return true
Expand Down
37 changes: 17 additions & 20 deletions lib/enter-state.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
'use strict'

module.exports = enter

var direction = require('direction')
var is = require('hast-util-is-element')
var toString = require('hast-util-to-string')
var svg = require('property-information/svg')
var visit = require('unist-util-visit')
import {direction} from 'direction'
import {isElement} from 'hast-util-is-element'
import toString from 'hast-util-to-string'
import {svg} from 'property-information'
import {visit, EXIT, SKIP} from 'unist-util-visit'

// eslint-disable-next-line complexity
function enter(state, node) {
export function enterState(state, node) {
var schema = state.schema
var language = state.language
var currentDirection = state.direction
Expand All @@ -25,7 +21,7 @@ function enter(state, node) {
type = node.properties.type || 'text'
dir = dirProperty(node)

if (lang != null) {
if (lang !== undefined && lang !== null) {
state.language = lang
found = true
}
Expand All @@ -36,7 +32,7 @@ function enter(state, node) {
found = true
}

if (is(node, 'svg')) {
if (isElement(node, 'svg')) {
state.schema = svg
found = true
}
Expand All @@ -49,18 +45,18 @@ function enter(state, node) {
// Explicit `[dir=ltr]`.
dir === 'ltr' ||
// HTML with an invalid or no `[dir]`.
(dir !== 'auto' && is(node, 'html')) ||
(dir !== 'auto' && isElement(node, 'html')) ||
// `input[type=tel]` with an invalid or no `[dir]`.
(dir !== 'auto' && is(node, 'input') && type === 'tel')
(dir !== 'auto' && isElement(node, 'input') && type === 'tel')
) {
dirInferred = 'ltr'
// `[dir=auto]` or `bdi` with an invalid or no `[dir]`.
} else if (dir === 'auto' || is(node, 'bdi')) {
if (is(node, 'textarea')) {
} else if (dir === 'auto' || isElement(node, 'bdi')) {
if (isElement(node, 'textarea')) {
// Check contents of `<textarea>`.
dirInferred = dirBidi(toString(node))
} else if (
is(node, 'input') &&
isElement(node, 'input') &&
(type === 'email' ||
type === 'search' ||
type === 'tel' ||
Expand Down Expand Up @@ -100,14 +96,15 @@ function enter(state, node) {
function inferDirectionality(child) {
if (child.type === 'text') {
dirInferred = dirBidi(child.value)
return dirInferred ? visit.EXIT : null
return dirInferred ? EXIT : null
}

if (
child !== node &&
(is(child, ['bdi', 'script', 'style', 'textare']) || dirProperty(child))
(isElement(child, ['bdi', 'script', 'style', 'textare']) ||
dirProperty(child))
) {
return visit.SKIP
return SKIP
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions lib/id.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
'use strict'

module.exports = match

function match(query, node) {
export function id(query, node) {
return node.properties.id === query.id
}
6 changes: 1 addition & 5 deletions lib/name.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
'use strict'

module.exports = match

function match(query, node) {
export function name(query, node) {
return query.tagName === '*' || query.tagName === node.tagName
}
Loading

0 comments on commit 465234f

Please sign in to comment.