Skip to content

Commit 8107052

Browse files
committed
add :last-child
1 parent 60a8e07 commit 8107052

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

README.md

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

lib/match-node.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ function matchPseudos (rule, node, nodeIndex, parent) {
6868
return pseudo.value(nodeIndex);
6969

7070
case 'first-child':
71-
return nodeIndex == 0;
71+
return !parent || nodeIndex == 0;
72+
73+
case 'last-child':
74+
return !parent || nodeIndex == parent.children.length - 1;
7275

7376
case 'not':
7477
return !matchNode(pseudo.value.rule, node, nodeIndex, parent);

test/select.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ test('structural pseudo-classes', function (t) {
166166
t.end();
167167
});
168168

169+
t.test(':last-child', function (t) {
170+
t.deepEqual(select(ast, ':root:last-child'), [ast]);
171+
t.deepEqual(select(ast, 'tableCell:last-child *')
172+
.map(function (node) { return node.value }),
173+
['mi', 'dolor', '15000']);
174+
t.end();
175+
});
176+
169177
t.end();
170178
});
171179

0 commit comments

Comments
 (0)