Skip to content

Commit 60a8e07

Browse files
committed
add :first-child
1 parent f51177d commit 60a8e07

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ That's it!
6565
- [ ] `:nth-last-child(2n+1)`
6666
- [ ] `:nth-of-type(2n+1)`
6767
- [ ] `:nth-last-of-type(2n+1)`
68-
- [ ] `:first-child`
68+
- [x] `:first-child`
6969
- [ ] `:last-child`
7070
- [ ] `:first-of-type`
7171
- [ ] `:last-of-type`

lib/match-node.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ function matchPseudos (rule, node, nodeIndex, parent) {
6767
case 'nth-child':
6868
return pseudo.value(nodeIndex);
6969

70+
case 'first-child':
71+
return nodeIndex == 0;
72+
7073
case 'not':
7174
return !matchNode(pseudo.value.rule, node, nodeIndex, parent);
7275

test/select.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ test('structural pseudo-classes', function (t) {
154154
t.end();
155155
});
156156

157+
t.test(':first-child', function (t) {
158+
t.deepEqual(select(ast, ':root:first-child'), [ast]);
159+
t.deepEqual(select(ast, 'heading:first-child'), [path(ast, [0])]);
160+
t.deepEqual(select(ast, 'list listItem:first-child [value]:first-child'), [
161+
path(ast, [4, 0, 0, 0]),
162+
path(ast, [4, 1, 1, 0, 0, 0]),
163+
path(ast, [4, 1, 1, 0, 1, 0, 0, 0]),
164+
path(ast, [6, 0, 0, 0])
165+
]);
166+
t.end();
167+
});
168+
157169
t.end();
158170
});
159171

0 commit comments

Comments
 (0)