Skip to content

Commit 45596a0

Browse files
committed
adds nth-child category
1 parent f97e348 commit 45596a0

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

src/core/selector-type.js

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const SelectorType = {
2626
AdjacentSiblingCombinator : 16,
2727
GeneralSiblingCombinator : 17,
2828
Root : 18, // E:root, :root
29+
NthChild : 19
2930
};
3031

3132
/**
@@ -36,6 +37,7 @@ export const SelectorType = {
3637
const SelectorCategoryRegex = {
3738
Type : /^[A-Za-z]+/,
3839
Attribute : /^(\#|\.)?[A-Za-z]+\[.+?\]$/,
40+
NthChild : /^(\#|\.)?([A-Za-z]+)?\:nth-child\(([0-9]+|n[0-9]+\+[0-9]|odd|even)\)/
3941
};
4042

4143
/**
@@ -60,6 +62,8 @@ const SelectorTypeRegex = {
6062
AttributeSuffix : /^(\#|\.)?[A-Za-z]+\[[A-Za-z]+\$\=\"[A-Za-z]+\"\]$/, // E[attr$=value]
6163
AttributeContains : /^(\#|\.)?[A-Za-z]+\[[A-Za-z]+\*\=\"[A-Za-z]+\"\]$/, // E[attr*=value]
6264
Root : /^(\#|\.)?[a-zA-Z]*:root/, // E:root, :root
65+
// todo add nth-child selectors like in attribute, odd, even, #n+# etc)
66+
6367
};
6468

6569
/**
@@ -82,6 +86,10 @@ export default function getType(selector) {
8286
return SelectorType.Root;
8387
}
8488

89+
if (SelectorCategoryRegex.NthChild.test(selector)) {
90+
return SelectorType.NthChild;
91+
}
92+
8593
if (SelectorCategoryRegex.Attribute.test(selector)) {
8694
return SelectorTypeRegex.AttributePrefix.test(selector) ? SelectorType.AttributePrefix :
8795
SelectorTypeRegex.AttributeSuffix.test(selector) ? SelectorType.AttributeSuffix :

test.css

Whitespace-only changes.

test/get-type.js

+4
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,9 @@ describe('getType(selector)', function() {
131131
it('Root selector (.class:root)', () => {
132132
expect(getType('.myClass:root')).to.equal(SelectorType.Root);
133133
});
134+
135+
it('Nth-child Selector', () => {
136+
expect(getType('div:nth-child(2)')).to.equal(SelectorType.NthChild);
137+
});
134138
});
135139
});

0 commit comments

Comments
 (0)