Skip to content

Commit

Permalink
add :any(simple-selector, ...) pseudoselector
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdickinson committed Jul 15, 2013
1 parent 31f9955 commit 5ebc96d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Returns true or false depending on whether the provided node matches the selecto
`:empty`
`:root`
`:contains(text)`
`:any(selector, selector, selector)`

## Supported attribute lookups

Expand Down
22 changes: 17 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function parse(selector, options) {
function group(token) {
if(token.type === 'comma') {
selectors.unshift(bits = [])

return
}

Expand Down Expand Up @@ -89,6 +90,7 @@ function parse(selector, options) {
return true
}
}

return false
}

Expand Down Expand Up @@ -199,13 +201,23 @@ function valid_pseudo(options, match) {
case 'root': return valid_root(options)
}

if(match.indexOf('contains') !== 0) {
return function() {
return false
}
if(match.indexOf('contains') === 0) {
return valid_contains(options, match.slice(9, -1))
}

if(match.indexOf('any') === 0) {
return valid_any_match(options, match.slice(4, -1))
}

return valid_contains(options, match.slice(9, -1))
return function() {
return false
}
}

function valid_any_match(options, selector) {
var fn = parse(selector, options)

return fn
}

function valid_attr(fn, lhs, cmp, rhs) {
Expand Down
5 changes: 5 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ function test_select_multiple() {
assert.ok(!language(':contains(world)')(data))
assert.ok(language(':contains(hello)')(data2))
assert.ok(language(':contains(world)')(data2))

assert.ok(
language(':root > :any(thing-tag, parent-tag, #asdf) > #one-id')(data)
)

}

// utils
Expand Down
29 changes: 18 additions & 11 deletions tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function tokenize() {
, stream
, length
, quote
, depth
, lhs
, cmp
, c
Expand Down Expand Up @@ -136,6 +137,7 @@ function tokenize() {
lhs = gathered.join('')
state = PSEUDOSTART
gathered.length = 0
depth = 1
++idx

return
Expand Down Expand Up @@ -170,20 +172,25 @@ function tokenize() {
return
}

state_gather(true)
gathered.push(c)

if(state !== READY) {
return
if(c === '(') {
++depth
} else if(c === ')') {
--depth
}

stream.queue({
type: rhs
, data: lhs+'('+gathered.join('')+')'
})

if(!depth) {
gathered.pop()
stream.queue({
type: rhs
, data: lhs+'('+gathered.join('')+')'
})

state = READY
lhs = rhs = cmp = null
gathered.length = 0
state = READY
lhs = rhs = cmp = null
gathered.length = 0
}

return
}
Expand Down

0 comments on commit 5ebc96d

Please sign in to comment.