Skip to content

Commit bf90103

Browse files
bartvenemanBart Veneman
andauthored
Do not crash when nth-(last-)-child has no children (#29)
Co-authored-by: Bart Veneman <[email protected]>
1 parent 879d25c commit bf90103

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/core/calculate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const calculateSpecificityOfSelectorObject = (selectorObj) => {
5858
case 'nth-last-child':
5959
specificity.b += 1;
6060

61-
if (child.children.first.selector) {
61+
if (child.children && child.children.first.selector) {
6262
// Calculate Specificity from SelectorList
6363
const max2 = max(...calculate(child.children.first.selector));
6464

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ describe('CALCULATE', () => {
160160
it(':focus', () => {
161161
deepEqual(Specificity.calculate(':focus')[0].toObject(), { a: 0, b: 1, c: 0 });
162162
});
163+
it('p:nth-child(1) = (0,1,1)', () => {
164+
deepEqual(Specificity.calculate('p:nth-child(1)')[0].toObject(), { a: 0, b: 1, c: 1 });
165+
});
166+
it('p:nth-child(2n+1) = (0,1,1)', () => {
167+
deepEqual(Specificity.calculate('p:nth-child(2n+1)')[0].toObject(), { a: 0, b: 1, c: 1 });
168+
});
169+
it('p:nth-child = (0,1,1) & do not crash', () => {
170+
deepEqual(Specificity.calculate('p:nth-child')[0].toObject(), { a: 0, b: 1, c: 1 });
171+
});
163172
});
164173

165174
describe('CSS :is(), :matches(), :-moz-any = Specificity of the most specific complex selector in its selector list argument', () => {

0 commit comments

Comments
 (0)