From 4184946b4b391942f504b941d01f2b7ee0394420 Mon Sep 17 00:00:00 2001 From: Adithya kumar Reddy Gopavaram <91198686+ADITHYAKUMARREDDY@users.noreply.github.com> Date: Wed, 25 Nov 2026 18:51:50 +0000 Subject: [PATCH] fix: render :has() hover preview as nested HTML Show the relative selector argument of :has() as descendant/child markup instead of a bogus attribute like `
`. Fixes microsoft/vscode#191897 --- src/services/selectorPrinting.ts | 49 +++++++++++++++++++++++++++ src/test/css/selectorPrinting.test.ts | 27 +++++++++++++-- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/src/services/selectorPrinting.ts b/src/services/selectorPrinting.ts index 8380a6ca..b5d3729e 100644 --- a/src/services/selectorPrinting.ts +++ b/src/services/selectorPrinting.ts @@ -234,6 +234,50 @@ class Specificity { public tag = 0; } +/** + * Renders `:has(relative-selector)` as nested HTML under the subject element. + * Descendant (implicit) and child (`>`) combinators are supported. Sibling + * combinators (`+`, `~`) keep the attribute form because they need a parent + * context the simple-selector builder does not have yet. + * Returns true when the pseudo was handled as `:has()`. + */ +function appendHasArgument(subject: Element, pseudo: nodes.Node): boolean { + const identifier = pseudo.getChild(0); + if (!identifier || !/^has$/i.test(unescape(identifier.getText()))) { + return false; + } + + const selectorList = pseudo.getChildren().find(c => c.type === nodes.NodeType.SelectorList); + const firstSelector = selectorList?.getChild(0); + if (!(firstSelector instanceof nodes.Selector)) { + return false; + } + + const first = firstSelector.getChild(0); + if (first && (first.matches('+') || first.matches('~'))) { + return false; + } + + const startsWithCombinator = !!first && ( + first.type === nodes.NodeType.SelectorCombinatorParent || + first.type === nodes.NodeType.SelectorCombinatorShadowPiercingDescendant || + first.type === nodes.NodeType.SelectorCombinator || + first.type === nodes.NodeType.SelectorCombinatorSibling || + first.type === nodes.NodeType.SelectorCombinatorAllSiblings + ); + + // Relative selectors without a leading combinator imply a descendant. + let attachPoint: Element = subject; + if (!startsWithCombinator) { + const ellipsis = new LabelElement('\u2026'); + subject.addChild(ellipsis); + attachPoint = ellipsis; + } + + new SelectorElementBuilder(attachPoint).processSelector(firstSelector); + return true; +} + export function toElement(node: nodes.SimpleSelector, parentElement?: Element | null): Element { let result = new Element(); for (const child of node.getChildren()) { @@ -280,6 +324,11 @@ export function toElement(node: nodes.SimpleSelector, parentElement?: Element | result.addAttr('class', (child).getName()); break; case nodes.NodeType.PseudoSelector: + // :has() matches relative to the subject — render the argument as nested HTML + // instead of a misleading attribute like `
`. + if (appendHasArgument(result, child)) { + break; + } // Only unescape the name; scanning the arguments as an unquoted string truncates them. const pseudoIdentifier = child.getChild(0); const pseudoText = child.getText(); diff --git a/src/test/css/selectorPrinting.test.ts b/src/test/css/selectorPrinting.test.ts index a3f8f347..3077a250 100644 --- a/src/test/css/selectorPrinting.test.ts +++ b/src/test/css/selectorPrinting.test.ts @@ -139,6 +139,27 @@ suite('CSS - MarkedStringPrinter selectors', () => { '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 0, 2)' ]); }); + test('has pseudo-class renders relative selector as nested HTML', function () { + let p = new Parser(); + // microsoft/vscode#191897 — :has() must not render as a bogus HTML attribute + assertSelectorMarkdown(p, 'article:has(.alert) { }', 'article', [ + { language: 'html', value: '
\n …\n ' }, + '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)' + ]); + assertSelectorMarkdown(p, 'article:has(> .alert) { }', 'article', [ + { language: 'html', value: '
\n ' }, + '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)' + ]); + assertSelectorMarkdown(p, 'article:has(.a, .b) { }', 'article', [ + { language: 'html', value: '
\n …\n ' }, + '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)' + ]); + // Sibling relative selectors keep the attribute form (need parent context) + assertSelectorMarkdown(p, 'article:has(+ .sib) { }', 'article', [ + { language: 'html', value: '
' }, + '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)' + ]); + }); test('sibling selector referenced by a nested parent selector', function () { const assertHtml = (p: Parser, input: string, expected: string) => { const selector = doParse(p, input, '&'); @@ -290,15 +311,15 @@ suite('CSS - MarkedStringPrinter selectors specificities', () => { ]); assertSelectorMarkdown(p, '#s12:has(foo)', '#s12', [ - { language: 'html', value: '' }, + { language: 'html', value: '\n …\n ' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 1)' ]); assertSelectorMarkdown(p, '#s12:has(foo > foo)', '#s12', [ - { language: 'html', value: ' foo)>' }, + { language: 'html', value: '\n …\n \n ' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 0, 2)' ]); assertSelectorMarkdown(p, '#s12:has(foo > foo, .bar > baz)', '#s12', [ - { language: 'html', value: ' foo, .bar > baz)>' }, + { language: 'html', value: '\n …\n \n ' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (1, 1, 1)' ]);