Skip to content

Commit

Permalink
Remove circular dependency
Browse files Browse the repository at this point in the history
Closes GH-9.
Closes GH-10.

Co-authored-by: Peter Babič <babicpet@gmail.com>
Co-authored-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
3 people committed Dec 15, 2020
1 parent 2269b1c commit 5da284c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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) {
return any(parse(selector), node, {one: true})[0] || null
return any(parse(selector), node, {one: true, any: any})[0] || null
}

function selectAll(selector, node) {
return any(parse(selector), node, {})
return any(parse(selector), node, {any: any})
}
3 changes: 2 additions & 1 deletion lib/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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
})
)

Expand Down
3 changes: 2 additions & 1 deletion lib/pseudo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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()

Expand Down Expand Up @@ -61,6 +60,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
Expand Down Expand Up @@ -160,6 +160,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
Expand Down
10 changes: 10 additions & 0 deletions test/select-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,16 @@ test('select.selectAll()', function (t) {
sst.end()
})

st.test(':has', function (sst) {
sst.deepEqual(
selectAll('c:has(:first-child)', u('a', [u('b'), u('c', [u('d')])])),
[u('c', [u('d')])],
'should select a node'
)

sst.end()
})

st.end()
})

Expand Down
10 changes: 10 additions & 0 deletions test/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,16 @@ test('select.select()', function (t) {
sst.end()
})

st.test(':has', function (sst) {
sst.deepEqual(
select('c:has(:first-child)', u('a', [u('b'), u('c', [u('d')])])),
u('c', [u('d')]),
'should select a node'
)

sst.end()
})

st.end()
})

Expand Down

0 comments on commit 5da284c

Please sign in to comment.