From 7f9326107117bae616fb97e1640b0a2d76c0fa0e Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Tue, 27 Oct 2026 22:07:46 +0100 Subject: [PATCH] fix: calculate specificity of `::highlight` correctly --- src/selectors/specificity.test.ts | 32 +++++++++++++++++++++++++++++++ src/selectors/specificity.ts | 13 +++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/selectors/specificity.test.ts b/src/selectors/specificity.test.ts index e2f0ec4..0a0a067 100644 --- a/src/selectors/specificity.test.ts +++ b/src/selectors/specificity.test.ts @@ -171,6 +171,38 @@ test('handles pseudo element selectors', () => { expect(actual).toEqual(expected) }) +test('handles the ::part pseudo element', () => { + const fixture = ` + ::part(my-ident), + ::part(first-ident second-ident) + {} + ` + const actual = analyze(fixture).selectors.specificity.items + const expected = [ + [0, 0, 1], + [0, 0, 1], + ] + expect(actual).toEqual(expected) +}) + +test('handles the ::highlight pseudo element', () => { + const fixture = ` + ::highlight, + ::highlight(), + ::highlight(my-ident), + ::highlight(*) + {} + ` + const actual = analyze(fixture).selectors.specificity.items + const expected = [ + [0, 0, 0], + [0, 0, 0], + [0, 0, 1], + [0, 0, 0], + ] + expect(actual).toEqual(expected) +}) + test('calculates the lowest value', () => { const fixture = ` #test, diff --git a/src/selectors/specificity.ts b/src/selectors/specificity.ts index fe86c32..81b2233 100644 --- a/src/selectors/specificity.ts +++ b/src/selectors/specificity.ts @@ -206,6 +206,19 @@ export const calculateForAST = (selectorAST: Selector): Specificity => { break } + case 'highlight': { + if (current.first_child) { + // The specificity of a ::highlight() selector with a * argument is zero. + if (current.first_child.text === '*') { + break + } + // The specificity of a ::highlight() selector with a argument + // is equivalent to a type selector. + c += 1 + } + break + } + default: { c += 1 break