Skip to content

Commit f1b3855

Browse files
committed
add :only-child
1 parent 61b76c3 commit f1b3855

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ That's it!
6969
- [x] `:last-child`
7070
- [ ] `:first-of-type`
7171
- [ ] `:last-of-type`
72-
- [ ] `:only-child`
72+
- [x] `:only-child`
7373
- [ ] `:only-of-type`
7474
- [ ] `:empty`
7575
- [x] Negation pseudo-class: `*:not(paragraph)`

lib/match-node.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ function matchPseudos (rule, node, nodeIndex, parent) {
7373
case 'last-child':
7474
return parent && nodeIndex == parent.children.length - 1;
7575

76+
case 'only-child':
77+
return parent && parent.children.length == 1;
78+
7679
case 'not':
7780
return !matchNode(pseudo.value.rule, node, nodeIndex, parent);
7881

test/select.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ test('structural pseudo-classes', function (t) {
175175
t.end();
176176
});
177177

178+
t.test(':only-child', function (t) {
179+
t.deepEqual(select(ast, ':root:only-child'), []);
180+
t.deepEqual(select(ast, 'table:only-child'), []);
181+
t.deepEqual(select(ast, ':root > *:not(paragraph) > text:only-child')
182+
.map(function (node) { return node.value }),
183+
['Risus pretium quam!', 'Vitae', 'References', 'License']);
184+
t.end();
185+
});
186+
178187
t.end();
179188
});
180189

0 commit comments

Comments
 (0)