Skip to content

Commit 0d224f6

Browse files
committed
Add support for :root selector
1 parent 61bd5d1 commit 0d224f6

File tree

5 files changed

+253
-63
lines changed

5 files changed

+253
-63
lines changed

lib/pseudo.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var anything = require('./any')
2727
var handle = zwitch('name')
2828
var handlers = handle.handlers
2929

30+
var roots = {html: 'html', svg: 'svg'}
3031
var disableable = [
3132
'button',
3233
'input',
@@ -63,6 +64,7 @@ handlers['only-child'] = onlyChild
6364
handlers['only-of-type'] = onlyOfType
6465
handlers.optional = not(required)
6566
handlers.required = required
67+
handlers.root = root
6668

6769
function match(query, node, index, parent, state) {
6870
var pseudos = query.pseudos
@@ -111,6 +113,15 @@ function required(query, node) {
111113
return is(node, requirable) && has(node, 'required')
112114
}
113115

116+
function root(query, node, index, parent, state) {
117+
var space = state.schema.space
118+
return (
119+
space in roots &&
120+
node.tagName === roots[space] &&
121+
(!parent || parent.type === 'root')
122+
)
123+
}
124+
114125
function empty(query, node) {
115126
return !someChildren(node, check)
116127

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Yields:
176176
* [x] `:enabled` (pseudo-class)
177177
* [x] `:optional` (pseudo-class)
178178
* [x] `:required` (pseudo-class)
179+
* [x] `:root` (pseudo-class)
179180
* [x] `article p` (combinator: descendant selector)
180181
* [x] `article > p` (combinator: child selector)
181182
* [x] `h1 + p` (combinator: adjacent sibling selector)
@@ -221,7 +222,6 @@ Yields:
221222
* [ ]`:playing` (pseudo-class)
222223
* [ ] § `:read-only` (pseudo-class)
223224
* [ ] § `:read-write` (pseudo-class)
224-
* [ ] § `:root` (pseudo-class)
225225
* [ ] § `:scope` (pseudo-class)
226226
* [ ]`:user-error` (pseudo-class)
227227
* [ ]`:user-invalid` (pseudo-class)

test/matches.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var test = require('tape')
44
var u = require('unist-builder')
55
var h = require('hastscript')
6+
var s = require('hastscript/svg')
67
var matches = require('..').matches
78

89
test('select.matches()', function(t) {
@@ -85,10 +86,10 @@ test('select.matches()', function(t) {
8586
t.test('parent-sensitive pseudo-selectors', function(st) {
8687
;[
8788
'first-child',
88-
'last-child',
89-
'only-child',
9089
'first-of-type',
90+
'last-child',
9191
'last-of-type',
92+
'only-child',
9293
'only-of-type'
9394
].forEach(function(pseudo) {
9495
st.throws(
@@ -899,6 +900,24 @@ test('select.matches()', function(t) {
899900
sst.end()
900901
})
901902

903+
st.test(':root', function(sst) {
904+
sst.ok(matches(':root', h('html')), 'true if `<html>` in HTML space')
905+
906+
sst.notOk(
907+
matches(':root', h('div')),
908+
'false if not `<html>` in HTML space'
909+
)
910+
911+
sst.ok(matches(':root', s('svg'), 'svg'), 'true if `<svg>` in SVG space')
912+
913+
sst.notOk(
914+
matches(':root', s('circle'), 'svg'),
915+
'false if not `<svg>` in SVG space'
916+
)
917+
918+
sst.end()
919+
})
920+
902921
st.end()
903922
})
904923

0 commit comments

Comments
 (0)