Skip to content

Commit 96394f3

Browse files
committed
add :nth-last-child
1 parent f1b3855 commit 96394f3

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ That's it!
6262
- [ ] Structural pseudo-classes: `paragraph:first-of-type`
6363
- [x] `:root`
6464
- [x] `:nth-child(2n+1)`
65-
- [ ] `:nth-last-child(2n+1)`
65+
- [x] `:nth-last-child(2n+1)`
6666
- [ ] `:nth-of-type(2n+1)`
6767
- [ ] `:nth-last-of-type(2n+1)`
6868
- [x] `:first-child`

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 parent && pseudo.value(nodeIndex);
6969

70+
case 'nth-last-child':
71+
return parent && pseudo.value(parent.children.length - 1 - nodeIndex);
72+
7073
case 'first-child':
7174
return parent && nodeIndex == 0;
7275

lib/selector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function compileNthChecks (ast) {
3030
case 'rule':
3131
if (ast.pseudos) {
3232
ast.pseudos.forEach(function (pseudo) {
33-
if (pseudo.name == 'nth-child') {
33+
if (pseudo.name == 'nth-child' || pseudo.name == 'nth-last-child') {
3434
pseudo.value = nthCheck(pseudo.value);
3535
pseudo.valueType = 'function';
3636
}

test/select.js

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

158+
t.test(':nth-last-child', function (t) {
159+
t.deepEqual(select(ast, ':root:nth-last-child(n)'), []);
160+
t.deepEqual(select(ast, 'tableCell:nth-last-child(-n+2)'), [
161+
path(ast, [10, 0, 1]),
162+
path(ast, [10, 0, 2]),
163+
path(ast, [10, 1, 1]),
164+
path(ast, [10, 1, 2]),
165+
path(ast, [10, 2, 1]),
166+
path(ast, [10, 2, 2])
167+
]);
168+
t.deepEqual(select(ast, 'definition:nth-last-child(odd)')
169+
.map(function (node) { return node.identifier }),
170+
['viverra', 'interdum']);
171+
t.end();
172+
});
173+
158174
t.test(':first-child', function (t) {
159175
t.deepEqual(select(ast, ':root:first-child'), []);
160176
t.deepEqual(select(ast, 'heading:first-child'), [path(ast, [0])]);

0 commit comments

Comments
 (0)