Skip to content

Commit

Permalink
add :nth-last-child
Browse files Browse the repository at this point in the history
  • Loading branch information
eush77 committed Dec 20, 2015
1 parent f1b3855 commit 96394f3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ That's it!
- [ ] Structural pseudo-classes: `paragraph:first-of-type`
- [x] `:root`
- [x] `:nth-child(2n+1)`
- [ ] `:nth-last-child(2n+1)`
- [x] `:nth-last-child(2n+1)`
- [ ] `:nth-of-type(2n+1)`
- [ ] `:nth-last-of-type(2n+1)`
- [x] `:first-child`
Expand Down
3 changes: 3 additions & 0 deletions lib/match-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ function matchPseudos (rule, node, nodeIndex, parent) {
case 'nth-child':
return parent && pseudo.value(nodeIndex);

case 'nth-last-child':
return parent && pseudo.value(parent.children.length - 1 - nodeIndex);

case 'first-child':
return parent && nodeIndex == 0;

Expand Down
2 changes: 1 addition & 1 deletion lib/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function compileNthChecks (ast) {
case 'rule':
if (ast.pseudos) {
ast.pseudos.forEach(function (pseudo) {
if (pseudo.name == 'nth-child') {
if (pseudo.name == 'nth-child' || pseudo.name == 'nth-last-child') {
pseudo.value = nthCheck(pseudo.value);
pseudo.valueType = 'function';
}
Expand Down
16 changes: 16 additions & 0 deletions test/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ test('structural pseudo-classes', function (t) {
t.end();
});

t.test(':nth-last-child', function (t) {
t.deepEqual(select(ast, ':root:nth-last-child(n)'), []);
t.deepEqual(select(ast, 'tableCell:nth-last-child(-n+2)'), [
path(ast, [10, 0, 1]),
path(ast, [10, 0, 2]),
path(ast, [10, 1, 1]),
path(ast, [10, 1, 2]),
path(ast, [10, 2, 1]),
path(ast, [10, 2, 2])
]);
t.deepEqual(select(ast, 'definition:nth-last-child(odd)')
.map(function (node) { return node.identifier }),
['viverra', 'interdum']);
t.end();
});

t.test(':first-child', function (t) {
t.deepEqual(select(ast, ':root:first-child'), []);
t.deepEqual(select(ast, 'heading:first-child'), [path(ast, [0])]);
Expand Down

0 comments on commit 96394f3

Please sign in to comment.