unist-util-select
Advanced tools
Comparing version 3.0.1 to 3.0.2
106
lib/any.js
@@ -6,15 +6,16 @@ 'use strict' | ||
var zwitch = require('zwitch') | ||
var needsIndex = require('./pseudo').needsIndex | ||
var pseudo = require('./pseudo') | ||
var test = require('./test') | ||
var nest = require('./nest') | ||
var type = zwitch('type') | ||
var handlers = type.handlers | ||
var type = zwitch('type', { | ||
unknown: unknownType, | ||
invalid: invalidType, | ||
handlers: { | ||
selectors: selectors, | ||
ruleSet: ruleSet, | ||
rule: rule | ||
} | ||
}) | ||
type.unknown = unknownType | ||
type.invalid = invalidType | ||
handlers.selectors = selectors | ||
handlers.ruleSet = ruleSet | ||
handlers.rule = rule | ||
function match(query, node, state) { | ||
@@ -26,8 +27,6 @@ return query && node ? type(query, node, state) : [] | ||
var collect = collector(state.one) | ||
var ruleSets = query.selectors | ||
var length = ruleSets.length | ||
var index = -1 | ||
while (++index < length) { | ||
collect(ruleSet(ruleSets[index], node, state)) | ||
while (++index < query.selectors.length) { | ||
collect(ruleSet(query.selectors[index], node, state)) | ||
} | ||
@@ -44,8 +43,2 @@ | ||
var collect = collector(state.one) | ||
var opts = { | ||
scopeNodes: tree.type === 'root' ? tree.children : [tree], | ||
iterator: match, | ||
one: state.one, | ||
shallow: state.shallow | ||
} | ||
@@ -56,7 +49,18 @@ if (state.shallow && query.rule) { | ||
nest(query, tree, 0, null, configure(query, opts)) | ||
nest( | ||
query, | ||
tree, | ||
0, | ||
null, | ||
configure(query, { | ||
scopeNodes: tree.type === 'root' ? tree.children : [tree], | ||
iterator: iterator, | ||
one: state.one, | ||
shallow: state.shallow | ||
}) | ||
) | ||
return collect.result | ||
function match(query, node, index, parent, state) { | ||
function iterator(query, node, index, parent, state) { | ||
if (test(query, node, index, parent, state)) { | ||
@@ -71,17 +75,16 @@ if (query.rule) { | ||
} | ||
} | ||
function configure(query, state) { | ||
var pseudos = query.pseudos | ||
var length = pseudos && pseudos.length | ||
var index = -1 | ||
function configure(query, state) { | ||
var pseudos = query.pseudos || [] | ||
var index = -1 | ||
while (++index < length) { | ||
if (needsIndex.indexOf(pseudos[index].name) !== -1) { | ||
state.index = true | ||
break | ||
} | ||
while (++index < pseudos.length) { | ||
if (pseudo.needsIndex.indexOf(pseudos[index].name) > -1) { | ||
state.index = true | ||
break | ||
} | ||
} | ||
return state | ||
} | ||
return state | ||
} | ||
@@ -108,33 +111,24 @@ | ||
/* Append nodes to array, filtering out duplicates. */ | ||
function collect(source) { | ||
if ('length' in source) { | ||
collectAll() | ||
function collect(node) { | ||
var index = -1 | ||
if ('length' in node) { | ||
while (++index < node.length) { | ||
collectOne(node[index]) | ||
} | ||
} else { | ||
collectOne(source) | ||
collectOne(node) | ||
} | ||
} | ||
function collectAll() { | ||
var length = source.length | ||
var index = -1 | ||
function collectOne(node) { | ||
if (one) { | ||
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */ | ||
if (found) throw new Error('Cannot collect multiple nodes') | ||
while (++index < length) { | ||
collectOne(source[index]) | ||
} | ||
found = true | ||
} | ||
function collectOne(node) { | ||
if (one) { | ||
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */ | ||
if (found) { | ||
throw new Error('Cannot collect multiple nodes') | ||
} | ||
found = true | ||
} | ||
if (result.indexOf(node) === -1) { | ||
result.push(node) | ||
} | ||
} | ||
if (result.indexOf(node) < 0) result.push(node) | ||
} | ||
} |
@@ -7,25 +7,20 @@ 'use strict' | ||
var handle = zwitch('operator') | ||
var handlers = handle.handlers | ||
var handle = zwitch('operator', { | ||
unknown: unknownOperator, | ||
invalid: exists, | ||
handlers: { | ||
'=': exact, | ||
'^=': begins, | ||
'$=': ends, | ||
'*=': containsString, | ||
'~=': containsArray | ||
} | ||
}) | ||
handle.unknown = unknownOperator | ||
handle.invalid = exists | ||
handlers['='] = exact | ||
handlers['^='] = begins | ||
handlers['$='] = ends | ||
handlers['*='] = containsString | ||
handlers['~='] = containsArray | ||
function match(query, node) { | ||
var attrs = query.attrs | ||
var length = attrs.length | ||
var index = -1 | ||
var attr | ||
while (++index < length) { | ||
attr = attrs[index] | ||
if (!handle(attr, node)) { | ||
return false | ||
} | ||
while (++index < attrs.length) { | ||
if (!handle(attrs[index], node)) return false | ||
} | ||
@@ -38,3 +33,3 @@ | ||
function exists(query, node) { | ||
return has(node, query.name) | ||
return node[query.name] != null | ||
} | ||
@@ -44,3 +39,3 @@ | ||
function exact(query, node) { | ||
return has(node, query.name) && String(node[query.name]) === query.value | ||
return node[query.name] != null && String(node[query.name]) === query.value | ||
} | ||
@@ -50,21 +45,17 @@ | ||
function containsArray(query, node) { | ||
var value | ||
var value = node[query.name] | ||
if (has(node, query.name)) { | ||
value = node[query.name] | ||
if (value == null) return false | ||
// If this is an array, and the query is contained in it, return true. | ||
if ( | ||
typeof value === 'object' && | ||
'length' in value && | ||
value.indexOf(query.value) !== -1 | ||
) { | ||
return true | ||
} | ||
// For all other values, return whether this is an exact match. | ||
return String(value) === query.value | ||
// If this is an array, and the query is contained in it, return true. | ||
if ( | ||
typeof value === 'object' && | ||
'length' in value && | ||
value.indexOf(query.value) > -1 | ||
) { | ||
return true | ||
} | ||
return false | ||
// For all other values, return whether this is an exact match. | ||
return String(value) === query.value | ||
} | ||
@@ -95,3 +86,3 @@ | ||
var value = node[query.name] | ||
return typeof value === 'string' && value.indexOf(query.value) !== -1 | ||
return typeof value === 'string' && value.indexOf(query.value) > -1 | ||
} | ||
@@ -103,5 +94,1 @@ | ||
} | ||
function has(node, name) { | ||
return node[name] !== null && node[name] !== undefined | ||
} |
100
lib/nest.js
@@ -8,14 +8,14 @@ 'use strict' | ||
var own = {}.hasOwnProperty | ||
var slice = [].slice | ||
var handle = zwitch('nestingOperator') | ||
var handlers = handle.handlers | ||
var handle = zwitch('nestingOperator', { | ||
unknown: unknownNesting, | ||
invalid: topScan, // `undefined` is the top query selector. | ||
handlers: { | ||
null: descendant, // `null` is the descendant combinator. | ||
'>': child, | ||
'+': adjacentSibling, | ||
'~': generalSibling | ||
} | ||
}) | ||
handle.unknown = unknownNesting | ||
handle.invalid = topScan // `undefined` is the top query selector. | ||
handlers.null = descendant // `null` is the descendant combinator. | ||
handlers['>'] = child | ||
handlers['+'] = adjacentSibling | ||
handlers['~'] = generalSibling | ||
function match(query, node, index, parent, state) { | ||
@@ -38,9 +38,7 @@ return handle(query, node, index, parent, state) | ||
if (!state.shallow) { | ||
descendant.apply(this, arguments) | ||
} | ||
if (!state.shallow) descendant.apply(this, arguments) | ||
} | ||
function descendant(query, node, index, parent, state) { | ||
var prev = state.iterator | ||
var previous = state.iterator | ||
@@ -51,12 +49,10 @@ state.iterator = iterator | ||
function iterator() { | ||
state.iterator = prev | ||
prev.apply(this, arguments) | ||
function iterator(_, node, index, parent, state) { | ||
state.iterator = previous | ||
previous.apply(this, arguments) | ||
state.iterator = iterator | ||
if (state.one && state.found) { | ||
return | ||
} | ||
if (state.one && state.found) return | ||
child.apply(this, [query].concat(slice.call(arguments, 1))) | ||
child.call(this, query, node, index, parent, state) | ||
} | ||
@@ -66,9 +62,5 @@ } | ||
function child(query, node, index, parent, state) { | ||
if (!node.children || node.children.length === 0) { | ||
return | ||
} | ||
if (!node.children || !node.children.length) return | ||
walkIterator(query, node, state) | ||
.each() | ||
.done() | ||
walkIterator(query, node, state).each().done() | ||
} | ||
@@ -78,5 +70,3 @@ | ||
/* istanbul ignore if - Shouldn’t happen. */ | ||
if (!parent) { | ||
return | ||
} | ||
if (!parent) return | ||
@@ -92,5 +82,3 @@ walkIterator(query, parent, state) | ||
/* istanbul ignore if - Shouldn’t happen. */ | ||
if (!parent) { | ||
return | ||
} | ||
if (!parent) return | ||
@@ -105,4 +93,4 @@ walkIterator(query, parent, state) | ||
function walkIterator(query, parent, state) { | ||
var nodes = parent.children | ||
var typeIndex = state.index ? createTypeIndex() : null | ||
var typeIndex = state.index && createTypeIndex() | ||
var siblings = parent.children | ||
var delayed = [] | ||
@@ -117,11 +105,7 @@ | ||
function done() { | ||
var length = delayed.length | ||
var index = -1 | ||
while (++index < length) { | ||
while (++index < delayed.length) { | ||
delayed[index]() | ||
if (state.one && state.found) { | ||
break | ||
} | ||
if (state.one && state.found) break | ||
} | ||
@@ -135,3 +119,3 @@ | ||
while (start < end) { | ||
typeIndex(nodes[start]) | ||
typeIndex(siblings[start]) | ||
start++ | ||
@@ -145,9 +129,7 @@ } | ||
function each(start, end) { | ||
var child = nodes[start] | ||
var child = siblings[start] | ||
var index | ||
var nodeIndex | ||
if (start >= end) { | ||
return this | ||
} | ||
if (start >= end) return this | ||
@@ -159,9 +141,7 @@ if (typeIndex) { | ||
} else { | ||
pushNode() | ||
state.iterator(query, child, start, parent, state) | ||
} | ||
// Stop if we’re looking for one node and it’s already found. | ||
if (state.one && state.found) { | ||
return this | ||
} | ||
if (state.one && state.found) return this | ||
@@ -175,6 +155,2 @@ return each.call(this, start + 1, end) | ||
state.nodeCount = typeIndex.nodes | ||
pushNode() | ||
} | ||
function pushNode() { | ||
state.iterator(query, child, start, parent, state) | ||
@@ -188,10 +164,4 @@ } | ||
function rangeDefault(start, end) { | ||
if (start === null || start === undefined || start < 0) { | ||
start = 0 | ||
} | ||
if (end === null || end === undefined || end > nodes.length) { | ||
end = nodes.length | ||
} | ||
if (start == null || start < 0) start = 0 | ||
if (end == null || end > siblings.length) end = siblings.length | ||
return iterator.call(this, start, end) | ||
@@ -215,7 +185,5 @@ } | ||
if (!own.call(counts, type)) { | ||
counts[type] = 0 | ||
} | ||
if (!own.call(counts, type)) counts[type] = 0 | ||
// Note: ++ is intended to be postfixed! | ||
// Note: `++` is intended to be postfixed! | ||
return counts[type]++ | ||
@@ -222,0 +190,0 @@ } |
@@ -12,5 +12,11 @@ 'use strict' | ||
var parser = new Parser() | ||
var compile = zwitch('type') | ||
var handlers = compile.handlers | ||
var compile = zwitch('type', { | ||
handlers: { | ||
selectors: selectors, | ||
ruleSet: ruleSet, | ||
rule: rule | ||
} | ||
}) | ||
parser.registerAttrEqualityMods('~', '^', '$', '*') | ||
@@ -20,6 +26,2 @@ parser.registerSelectorPseudos('any', 'matches', 'not', 'has') | ||
handlers.selectors = selectors | ||
handlers.ruleSet = ruleSet | ||
handlers.rule = rule | ||
function parse(selector) { | ||
@@ -35,6 +37,5 @@ if (typeof selector !== 'string') { | ||
var selectors = query.selectors | ||
var length = selectors.length | ||
var index = -1 | ||
while (++index < length) { | ||
while (++index < selectors.length) { | ||
compile(selectors[index]) | ||
@@ -51,11 +52,10 @@ } | ||
function rule(query) { | ||
var pseudos = query.pseudos | ||
var length = pseudos && pseudos.length | ||
var pseudos = query.pseudos || [] | ||
var index = -1 | ||
var pseudo | ||
while (++index < length) { | ||
while (++index < pseudos.length) { | ||
pseudo = pseudos[index] | ||
if (nth.indexOf(pseudo.name) !== -1) { | ||
if (nth.indexOf(pseudo.name) > -1) { | ||
pseudo.value = nthCheck(pseudo.value) | ||
@@ -62,0 +62,0 @@ pseudo.valueType = 'function' |
@@ -8,2 +8,3 @@ 'use strict' | ||
var convert = require('unist-util-is/convert') | ||
var anything = require('./any') | ||
@@ -25,37 +26,33 @@ var is = convert() | ||
var anything = require('./any') | ||
var handle = zwitch('name', { | ||
unknown: unknownPseudo, | ||
invalid: invalidPseudo, | ||
handlers: { | ||
any: matches, | ||
blank: empty, | ||
empty: empty, | ||
'first-child': firstChild, | ||
'first-of-type': firstOfType, | ||
has: hasSelector, | ||
'last-child': lastChild, | ||
'last-of-type': lastOfType, | ||
matches: matches, | ||
not: not(matches), | ||
'nth-child': nthChild, | ||
'nth-last-child': nthLastChild, | ||
'nth-of-type': nthOfType, | ||
'nth-last-of-type': nthLastOfType, | ||
'only-child': onlyChild, | ||
'only-of-type': onlyOfType, | ||
root: root, | ||
scope: scope | ||
} | ||
}) | ||
var handle = zwitch('name') | ||
var handlers = handle.handlers | ||
handle.unknown = unknownPseudo | ||
handle.invalid = invalidPseudo | ||
handlers.any = matches | ||
handlers.blank = empty | ||
handlers.empty = empty | ||
handlers['first-child'] = firstChild | ||
handlers['first-of-type'] = firstOfType | ||
handlers.has = hasSelector | ||
handlers['last-child'] = lastChild | ||
handlers['last-of-type'] = lastOfType | ||
handlers.matches = matches | ||
handlers.not = not(matches) | ||
handlers['nth-child'] = nthChild | ||
handlers['nth-last-child'] = nthLastChild | ||
handlers['nth-of-type'] = nthOfType | ||
handlers['nth-last-of-type'] = nthLastOfType | ||
handlers['only-child'] = onlyChild | ||
handlers['only-of-type'] = onlyOfType | ||
handlers.root = root | ||
handlers.scope = scope | ||
function match(query, node, index, parent, state) { | ||
var pseudos = query.pseudos | ||
var length = pseudos.length | ||
var offset = -1 | ||
while (++offset < length) { | ||
if (!handle(pseudos[offset], node, index, parent, state)) { | ||
return false | ||
} | ||
while (++offset < pseudos.length) { | ||
if (!handle(pseudos[offset], node, index, parent, state)) return false | ||
} | ||
@@ -71,4 +68,4 @@ | ||
state.one = true | ||
state.shallow = true | ||
state.one = true | ||
@@ -88,7 +85,7 @@ result = anything(query.value, node, state)[0] === node | ||
function scope(query, node, index, parent, state) { | ||
return is(node) && state.scopeNodes.indexOf(node) !== -1 | ||
return is(node) && state.scopeNodes.indexOf(node) > -1 | ||
} | ||
function empty(query, node) { | ||
return node.children ? node.children.length === 0 : !('value' in node) | ||
return node.children ? !node.children.length : !('value' in node) | ||
} | ||
@@ -98,3 +95,3 @@ | ||
assertDeep(state, query) | ||
return state.nodeIndex === 0 | ||
return state.nodeIndex === 0 // Specifically `0`, not falsey. | ||
} | ||
@@ -186,18 +183,10 @@ | ||
function appendScope(selector) { | ||
var selectors | ||
var length | ||
var index | ||
function appendScope(value) { | ||
var selector = | ||
value.type === 'ruleSet' ? {type: 'selectors', selectors: [value]} : value | ||
var index = -1 | ||
var rule | ||
if (selector.type === 'ruleSet') { | ||
selector = {type: 'selectors', selectors: [selector]} | ||
} | ||
selectors = selector.selectors | ||
length = selectors.length | ||
index = -1 | ||
while (++index < length) { | ||
rule = selectors[index].rule | ||
while (++index < selector.selectors.length) { | ||
rule = selector.selectors[index].rule | ||
rule.nestingOperator = null | ||
@@ -211,6 +200,8 @@ | ||
) { | ||
rule = {type: 'rule', rule: rule, pseudos: [{name: 'scope'}]} | ||
selector.selectors[index] = { | ||
type: 'rule', | ||
rule: rule, | ||
pseudos: [{name: 'scope'}] | ||
} | ||
} | ||
selectors[index] = rule | ||
} | ||
@@ -217,0 +208,0 @@ |
{ | ||
"name": "unist-util-select", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "unist utility to select nodes with CSS-like selectors", | ||
@@ -55,13 +55,13 @@ "license": "MIT", | ||
"devDependencies": { | ||
"dtslint": "^2.0.0", | ||
"dtslint": "^4.0.0", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.0.0", | ||
"remark-cli": "^7.0.0", | ||
"remark-preset-wooorm": "^6.0.0", | ||
"tape": "^4.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"tape": "^5.0.0", | ||
"unist-builder": "^2.0.0", | ||
"xo": "^0.25.0" | ||
"xo": "^0.34.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test", | ||
@@ -89,6 +89,18 @@ "test-coverage": "nyc --reporter lcov tape test/index.js", | ||
"esnext": false, | ||
"ignore": [ | ||
"types" | ||
], | ||
"rules": { | ||
"eqeqeq": [ | ||
"error", | ||
"always", | ||
{ | ||
"null": "ignore" | ||
} | ||
], | ||
"no-eq-null": "off", | ||
"max-params": "off", | ||
"unicorn/prefer-reflect-apply": "off", | ||
"unicorn/prefer-includes": "off" | ||
"unicorn/explicit-length-check": "off", | ||
"unicorn/prefer-includes": "off", | ||
"unicorn/prefer-reflect-apply": "off" | ||
} | ||
@@ -95,0 +107,0 @@ }, |
@@ -11,4 +11,4 @@ # unist-util-select | ||
[**unist**][unist] utility with equivalents `querySelector`, `querySelectorAll`, | ||
and `matches`. | ||
[**unist**][unist] utility with equivalents for `querySelector`, | ||
`querySelectorAll`, and `matches`. | ||
@@ -76,4 +76,3 @@ Note that the DOM has references to their parent nodes, meaning that | ||
```js | ||
{ type: 'paragraph', | ||
children: [ { type: 'text', value: 'Delta' } ] } | ||
{type: 'paragraph', children: [{type: 'text', value: 'Delta'}]} | ||
``` | ||
@@ -110,6 +109,6 @@ | ||
```js | ||
[ { type: 'paragraph', | ||
children: [ { type: 'text', value: 'Delta' } ] }, | ||
{ type: 'paragraph', | ||
children: [ { type: 'text', value: 'Foxtrot' } ] } ] | ||
[ | ||
{type: 'paragraph', children: [{type: 'text', value: 'Delta'}]}, | ||
{type: 'paragraph', children: [{type: 'text', value: 'Foxtrot'}]} | ||
] | ||
``` | ||
@@ -127,3 +126,3 @@ | ||
* [x] `[attr]` (attribute existence, checks that the value on the tree is not | ||
nully) | ||
nullish) | ||
* [x] `[attr=value]` (attribute equality, this stringifies values on the tree) | ||
@@ -163,3 +162,3 @@ * [x] `[attr^=value]` (attribute begins with, only works on strings) | ||
* [`unist-util-filter`](https://github.com/eush77/unist-util-filter) | ||
* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter) | ||
— Create a new tree with all nodes that pass a test | ||
@@ -169,4 +168,3 @@ * [`unist-util-map`](https://github.com/syntax-tree/unist-util-map) | ||
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap) | ||
— Create a new tree by mapping (to an array) with the provided function and | ||
then flattening | ||
— Create a new tree by mapping (to an array) with the given function | ||
* [`unist-util-is`](https://github.com/syntax-tree/unist-util-is) | ||
@@ -191,4 +189,4 @@ — Check if a node passes a test | ||
This project has a [Code of Conduct][coc]. | ||
By interacting with this repository, organisation, or community you agree to | ||
This project has a [code of conduct][coc]. | ||
By interacting with this repository, organization, or community you agree to | ||
abide by its terms. | ||
@@ -224,5 +222,5 @@ | ||
[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[chat]: https://spectrum.chat/unified/syntax-tree | ||
[chat]: https://github.com/syntax-tree/unist/discussions | ||
@@ -233,7 +231,7 @@ [npm]: https://docs.npmjs.com/cli/install | ||
[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[help]: https://github.com/syntax-tree/.github/blob/master/support.md | ||
[help]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
@@ -240,0 +238,0 @@ [unist]: https://github.com/syntax-tree/unist |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27559
584
234