diff --git a/index.js b/index.js index 5f1352b..9ec0af1 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ var any = require('./lib/any') var parse = require('./lib/parse') function matches(selector, node) { - return Boolean(any(parse(selector), node, {one: true, shallow: true})[0]) + return Boolean(any(parse(selector), node, {one: true, shallow: true, any: any})[0]) } function select(selector, node) { diff --git a/lib/any.js b/lib/any.js index fef9448..b1ecedc 100644 --- a/lib/any.js +++ b/lib/any.js @@ -3,7 +3,6 @@ module.exports = match var zwitch = require('zwitch') -var pseudo = require('./pseudo') var test = require('./test') var nest = require('./nest') @@ -52,7 +51,8 @@ function rule(query, tree, state) { scopeNodes: tree.type === 'root' ? tree.children : [tree], iterator: iterator, one: state.one, - shallow: state.shallow + shallow: state.shallow, + any: state.any }) ) @@ -71,11 +71,23 @@ function rule(query, tree, state) { } function configure(query, state) { + var needsIndex = [ + 'first-child', + 'first-of-type', + 'last-child', + 'last-of-type', + 'nth-child', + 'nth-last-child', + 'nth-of-type', + 'nth-last-of-type', + 'only-child', + 'only-of-type' + ] var pseudos = query.pseudos || [] var index = -1 while (++index < pseudos.length) { - if (pseudo.needsIndex.indexOf(pseudos[index].name) > -1) { + if (needsIndex.indexOf(pseudos[index].name) > -1) { state.index = true break } diff --git a/lib/pseudo.js b/lib/pseudo.js index 855fa74..8c6cb0e 100644 --- a/lib/pseudo.js +++ b/lib/pseudo.js @@ -5,23 +5,9 @@ module.exports = match var zwitch = require('zwitch') var not = require('not') var convert = require('unist-util-is/convert') -var anything = require('./any') var is = convert() -match.needsIndex = [ - 'first-child', - 'first-of-type', - 'last-child', - 'last-of-type', - 'nth-child', - 'nth-last-child', - 'nth-of-type', - 'nth-last-of-type', - 'only-child', - 'only-of-type' -] - var handle = zwitch('name', { unknown: unknownPseudo, invalid: invalidPseudo, @@ -61,6 +47,7 @@ function match(query, node, index, parent, state) { function matches(query, node, index, parent, state) { var shallow = state.shallow var one = state.one + var anything = state.any var result state.one = true @@ -160,6 +147,7 @@ function hasSelector(query, node, index, parent, state) { var one = state.one var scopeNodes = state.scopeNodes var value = appendScope(query.value) + var anything = state.any var result state.shallow = false