Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/core/calculate.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const calculateForAST = (selectorAST) => {

case '-webkit-any':
case 'any':
if (child.children) {
if (child.children && child.children.first) {
b += 1;
}
break;
Expand All @@ -45,7 +45,7 @@ const calculateForAST = (selectorAST) => {
case 'matches':
case 'not':
case 'has':
if (child.children) {
if (child.children && child.children.first) {
// Calculate Specificity from nested SelectorList
const max1 = max(...calculate(child.children.first));

Expand All @@ -62,7 +62,7 @@ const calculateForAST = (selectorAST) => {
case 'nth-last-child':
b += 1;

if (child.children && child.children.first.selector) {
if (child.children && child.children.first && child.children.first.selector) {
Comment thread
bramus marked this conversation as resolved.
Outdated
// Calculate Specificity from SelectorList
const max2 = max(...calculate(child.children.first.selector));

Expand All @@ -79,7 +79,7 @@ const calculateForAST = (selectorAST) => {
case 'host':
b += 1;

if (child.children) {
if (child.children && child.children.first && child.children.first.children) {
Comment thread
bramus marked this conversation as resolved.
Outdated
// Workaround to a css-tree bug in which it allows complex selectors instead of only compound selectors
// We work around it by filtering out any Combinator and successive Selectors
const childAST = { type: 'Selector', children: [] };
Expand Down Expand Up @@ -124,7 +124,7 @@ const calculateForAST = (selectorAST) => {
case 'slotted':
c += 1;

if (child.children) {
if (child.children && child.children.first && child.children.first.children) {
Comment thread
bramus marked this conversation as resolved.
Outdated
// Workaround to a css-tree bug in which it allows complex selectors instead of only compound selectors
// We work around it by filtering out any Combinator and successive Selectors
const childAST = { type: 'Selector', children: [] };
Expand Down Expand Up @@ -153,7 +153,7 @@ const calculateForAST = (selectorAST) => {
case 'view-transition-old':
case 'view-transition-new':
// The specificity of a view-transition selector with a * argument is zero.
if (child.children && child.children.first.value === '*') {
if (child.children && child.children.first && child.children.first.value === '*') {
break;
}
// The specificity of a view-transition selector with an argument is the same
Expand Down
28 changes: 25 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('CALCULATE', () => {
it('::slotted', () => {
deepEqual(Specificity.calculate('::slotted')[0].toObject(), { a: 0, b: 0, c: 1 });
});
it.skip('::slotted()', () => {
it('::slotted() & do not crash', () => {
deepEqual(Specificity.calculate('::slotted()')[0].toObject(), { a: 0, b: 0, c: 1 });
});
it('::slotted(div#foo)', () => {
Expand All @@ -105,6 +105,9 @@ describe('CALCULATE', () => {
it('::view-transition', () => {
deepEqual(Specificity.calculate('::view-transition')[0].toObject(), { a: 0, b: 0, c: 1 });
});
it('::view-transition-old() & do not crash', () => {
deepEqual(Specificity.calculate('::view-transition-old()')[0].toObject(), { a: 0, b: 0, c: 1 });
});
it('::view-transition-group(test)', () => {
deepEqual(Specificity.calculate('::view-transition-group(test)')[0].toObject(), { a: 0, b: 0, c: 1 });
});
Expand Down Expand Up @@ -171,6 +174,9 @@ describe('CALCULATE', () => {
it('p:nth-child = (0,1,1) & do not crash', () => {
deepEqual(Specificity.calculate('p:nth-child')[0].toObject(), { a: 0, b: 1, c: 1 });
});
it('p:nth-child() = (0,1,1) & do not crash', () => {
deepEqual(Specificity.calculate('p:nth-child()')[0].toObject(), { a: 0, b: 1, c: 1 });
});
});

describe('CSS :is(), :matches(), :-moz-any = Specificity of the most specific complex selector in its selector list argument', () => {
Expand All @@ -183,6 +189,9 @@ describe('CALCULATE', () => {
it(':-moz-any(#foo, .bar, baz) = (1,0,0)', () => {
deepEqual(Specificity.calculate(':-moz-any(#foo, .bar, baz)')[0].toObject(), { a: 1, b: 0, c: 0 });
});
it(':has() & do not crash', () => {
deepEqual(Specificity.calculate(':has()')[0].toObject(), { a: 0, b: 0, c: 0 });
});
});

describe('CSS :any() = (0,1,0)', () => {
Expand All @@ -207,6 +216,19 @@ describe('CALCULATE', () => {
it(':where = (0,0,0)', () => {
deepEqual(Specificity.calculate(':where')[0].toObject(), { a: 0, b: 0, c: 0 });
});

it(':is() = (0,0,0)', () => {
deepEqual(Specificity.calculate(':is()')[0].toObject(), { a: 0, b: 0, c: 0 });
});
it(':matches() = (0,0,0)', () => {
deepEqual(Specificity.calculate(':matches()')[0].toObject(), { a: 0, b: 0, c: 0 });
});
it(':any() = (0,0,0)', () => {
deepEqual(Specificity.calculate(':any()')[0].toObject(), { a: 0, b: 0, c: 0 });
});
it(':where() = (0,0,0)', () => {
deepEqual(Specificity.calculate(':where()')[0].toObject(), { a: 0, b: 0, c: 0 });
});
});

describe('CSS :has() = Specificity of the most specific complex selector in its selector list argument', () => {
Expand All @@ -231,7 +253,7 @@ describe('CALCULATE', () => {
it(':host = (0,1,0)', () => {
deepEqual(Specificity.calculate(':host')[0].toObject(), { a: 0, b: 1, c: 0 });
});
it.skip(':host() = (0,1,0)', () => {
it(':host() = (0,1,0) & do not crash', () => {
deepEqual(Specificity.calculate(':host()')[0].toObject(), { a: 0, b: 1, c: 0 });
});
it(':host(#foo.bar) = (1,2,0)', () => {
Expand All @@ -240,7 +262,7 @@ describe('CALCULATE', () => {
it(':host(#foo.bar invalid) = (1,2,0)', () => {
deepEqual(Specificity.calculate(':host(#foo.bar invalid)')[0].toObject(), { a: 1, b: 2, c: 0 });
});
it.skip(':host-context() = (0,1,0)', () => {
it(':host-context() = (0,1,0) & do not crash', () => {
deepEqual(Specificity.calculate(':host-context()')[0].toObject(), { a: 0, b: 1, c: 0 });
});
it(':host-context(#foo.bar) = (1,2,0)', () => {
Expand Down