From 5b3751b8c585bc59979c58672f2b5ef70f0a7a51 Mon Sep 17 00:00:00 2001 From: xnervwang Date: Mon, 9 Nov 2026 03:24:58 +0000 Subject: [PATCH 1/2] Fix name attributes in CSS selector hovers --- src/services/selectorPrinting.ts | 13 ++++++++----- src/test/css/selectorPrinting.test.ts | 21 ++++++++++++++++++--- src/test/scss/selectorPrinting.test.ts | 3 +++ src/test/testUtil/selectorPrinting.ts | 4 ++-- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/services/selectorPrinting.ts b/src/services/selectorPrinting.ts index 5ddf0d0c..20233c12 100644 --- a/src/services/selectorPrinting.ts +++ b/src/services/selectorPrinting.ts @@ -11,6 +11,9 @@ import * as l10n from '@vscode/l10n'; import { CSSDataManager } from '../languageFacts/dataManager.js'; import { Parser } from '../parser/cssParser.js'; +// attribute key for the element name; 'name' would clash with [name=x] +export const ELEMENT_NAME = '<>'; + export class Element { public parent: Element | null = null; public children: Element[] | null = null; @@ -115,7 +118,7 @@ export class RootElement extends Element { } export class LabelElement extends Element { constructor(label: string) { super(); - this.addAttr('name', label); + this.addAttr(ELEMENT_NAME, label); } } @@ -159,7 +162,7 @@ class MarkedStringPrinter { } private doPrintElement(element: Element, indent: number) { - const name = element.findAttribute('name'); + const name = element.findAttribute(ELEMENT_NAME); // special case: a simple label if (element instanceof LabelElement || name === '\u2026') { @@ -180,7 +183,7 @@ class MarkedStringPrinter { // attributes if (element.attributes) { for (const attr of element.attributes) { - if (attr.name !== 'name') { + if (attr.name !== ELEMENT_NAME) { content.push(' '); content.push(attr.name); const value = attr.value; @@ -229,7 +232,7 @@ export function toElement(node: nodes.SimpleSelector, parentElement?: Element | const segments = child.getText().split('&'); if (segments.length === 1) { // should not happen - result.addAttr('name', segments[0]); + result.addAttr(ELEMENT_NAME, segments[0]); break; } result = parentElement.cloneWithParent(); @@ -254,7 +257,7 @@ export function toElement(node: nodes.SimpleSelector, parentElement?: Element | // fall through case nodes.NodeType.ElementNameSelector: const text = child.getText(); - result.addAttr('name', text === '*' ? 'element' : unescape(text)); + result.addAttr(ELEMENT_NAME, text === '*' ? 'element' : unescape(text)); break; case nodes.NodeType.ClassSelector: result.addAttr('class', unescape(child.getText().substring(1))); diff --git a/src/test/css/selectorPrinting.test.ts b/src/test/css/selectorPrinting.test.ts index 9f7505ac..b5eae4a4 100644 --- a/src/test/css/selectorPrinting.test.ts +++ b/src/test/css/selectorPrinting.test.ts @@ -40,12 +40,12 @@ function assertSelectorMarkdown( suite('CSS - Selector Printing', () => { test('class/hash/elementname/attr', function () { let p = new Parser(); - assertElement(p, 'element', [{ name: 'name', value: 'element' }]); + assertElement(p, 'element', [{ name: '<>', value: 'element' }]); assertElement(p, '.div', [{ name: 'class', value: 'div' }]); assertElement(p, '#first', [{ name: 'id', value: 'first' }]); - assertElement(p, 'element.on', [{ name: 'name', value: 'element' }, { name: 'class', value: 'on' }]); + assertElement(p, 'element.on', [{ name: '<>', value: 'element' }, { name: 'class', value: 'on' }]); assertElement(p, 'element.on#first', [ - { name: 'name', value: 'element' }, + { name: '<>', value: 'element' }, { name: 'class', value: 'on' }, { name: 'id', value: 'first' } ]); @@ -53,6 +53,7 @@ suite('CSS - Selector Printing', () => { assertElement(p, "[lang='de']", [{ name: 'lang', value: 'de' }]); assertElement(p, '[enabled]', [{ name: 'enabled', value: void 0 }]); + assertElement(p, "element[name='x']", [{ name: '<>', value: 'element' }, { name: 'name', value: 'x' }]); }); test('simple selector', function () { @@ -63,6 +64,8 @@ suite('CSS - Selector Printing', () => { assertSelector(p, 'element:hover { }', 'element', '{element[:hover=]}'); assertSelector(p, "element[lang='de'] { }", 'element', '{element[lang=de]}'); assertSelector(p, 'element[enabled] { }', 'element', '{element[enabled=undefined]}'); + assertSelector(p, "element[name='x'] { }", 'element', '{element[name=x]}'); + assertSelector(p, "[name='x'] { }", '[', '{[name=x]}'); assertSelector(p, 'element[foo~="warning"] { }', 'element', '{element[foo= … warning … ]}'); assertSelector(p, 'element[lang|="en"] { }', 'element', '{element[lang=en-…]}'); assertSelector(p, '* { }', '*', '{element}'); @@ -134,6 +137,18 @@ suite('CSS - MarkedStringPrinter selectors specificities', () => { { language: 'html', value: '

\n' }, '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)' ]); + assertSelectorMarkdown(p, '[name="jmeno"]', '[', [ + { language: 'html', value: '' }, + '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 0)' + ]); + assertSelectorMarkdown(p, 'input[name="jmeno"]', 'input', [ + { language: 'html', value: '' }, + '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)' + ]); + assertSelectorMarkdown(p, 'input[name]', 'input', [ + { language: 'html', value: '' }, + '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 1)' + ]); }); test('class selector', function () { diff --git a/src/test/scss/selectorPrinting.test.ts b/src/test/scss/selectorPrinting.test.ts index a7c1ad33..35a96967 100644 --- a/src/test/scss/selectorPrinting.test.ts +++ b/src/test/scss/selectorPrinting.test.ts @@ -20,6 +20,7 @@ suite('SCSS - Selector Printing', () => { assertSelector(p, 'o1#div { }', 'o1', '{o1[id=div]}'); assertSelector(p, '#div.o1 { }', 'o1', '{[id=div|class=o1]}'); assertSelector(p, '.o1#div { }', 'o1', '{[class=o1|id=div]}'); + assertSelector(p, '[name="jmeno"] { }', '[', '{[name=jmeno]}'); }); test('nested selector', function () { @@ -40,6 +41,8 @@ suite('SCSS - Selector Printing', () => { assertSelector(p, 'o1 { &__bar {}}', '&', '{o1__bar}'); assertSelector(p, '.c1 { &__bar {}}', '&', '{[class=c1__bar]}'); assertSelector(p, 'o.c1 { &__bar {}}', '&', '{o[class=c1__bar]}'); + assertSelector(p, 'o1 { &[name=x] { }}', '&', '{o1[name=x]}'); + assertSelector(p, 'o1[name=x] { .c & { }}', '&', '{[class=c]{…{o1[name=x]}}}'); }); test('placeholders', function () { diff --git a/src/test/testUtil/selectorPrinting.ts b/src/test/testUtil/selectorPrinting.ts index 9387fab6..e7312614 100644 --- a/src/test/testUtil/selectorPrinting.ts +++ b/src/test/testUtil/selectorPrinting.ts @@ -10,8 +10,8 @@ import { Parser } from '../../parser/cssParser.js'; import * as selectorPrinting from '../../services/selectorPrinting.js'; function elementToString(element: selectorPrinting.Element): string { - let label = element.findAttribute('name') || ''; - const attributes = element.attributes?.filter(attribute => attribute.name !== 'name'); + let label = element.findAttribute(selectorPrinting.ELEMENT_NAME) || ''; + const attributes = element.attributes?.filter(attribute => attribute.name !== selectorPrinting.ELEMENT_NAME); if (attributes?.length) { label += '['; label += attributes.map(attribute => attribute.name + '=' + attribute.value).join('|'); From c502ce26b86557309108be0d34ec993373ccc80e Mon Sep 17 00:00:00 2001 From: usmanqt Date: Fri, 13 Nov 2026 20:11:21 -0500 Subject: [PATCH 2/2] Keep sibling combinators when a nested selector references the parent with `&` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a parent selector contains `+` or `~` and a nested rule references it with `&` (e.g. `.foo + .bar { .baz & {} }`), the hover preview left `.foo` at the top level and only moved `.bar` under `.baz`. - processSelector now moves the whole parent context out of the root - cloneWithParent also clones preceding siblings on the path - clone keeps LabelElement so `⋮` still prints as a label Fixes microsoft/vscode#169900 Co-Authored-By: Claude Opus 5.5 --- src/services/selectorPrinting.ts | 36 ++++++++++++++++++++++---- src/test/css/selectorPrinting.test.ts | 14 ++++++++++ src/test/scss/selectorPrinting.test.ts | 9 +++++++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/services/selectorPrinting.ts b/src/services/selectorPrinting.ts index 5ddf0d0c..6d39dc37 100644 --- a/src/services/selectorPrinting.ts +++ b/src/services/selectorPrinting.ts @@ -84,6 +84,9 @@ export class Element { } public clone(cloneChildren: boolean = true): Element { + if (this instanceof LabelElement) { + return new LabelElement(this.findAttribute('name')!); + } const elem = new Element(); if (this.attributes) { elem.attributes = []; @@ -102,8 +105,16 @@ export class Element { public cloneWithParent(): Element { const clone = this.clone(false); - if (this.parent && !(this.parent instanceof RootElement)) { - const parentClone = this.parent.cloneWithParent(); + const parent = this.parent; + if (parent) { + const parentClone = parent instanceof RootElement ? new RootElement() : parent.cloneWithParent(); + // keep the preceding siblings (`a + b`, `a ~ b`) so that a reference to `b` still includes `a` + for (const sibling of parent.children ?? []) { + if (sibling === this) { + break; + } + parentClone.addChild(sibling.clone()); + } parentClone.addChild(clone); } return clone; @@ -553,8 +564,16 @@ class SelectorElementBuilder { if (curr.parent instanceof RootElement) { parentElement = this.element; - this.element = curr.parent; - this.element.removeChild(curr); + // the parent selector is referenced with `&`: move the whole parent context, + // including preceding siblings (`a + b`, `a ~ b`), out of the root + const root = curr.parent; + const context = new RootElement(); + for (const child of root.children ?? []) { + context.addChild(child); + } + root.children = null; + + this.element = root; this.prev = null; } } @@ -577,7 +596,14 @@ class SelectorElementBuilder { const thisElement = toElement(selectorChild, parentElement); const root = thisElement.findRoot(); - this.element.addChild(root); + if (root.parent instanceof RootElement && root.parent.children) { + // a referenced parent selector can come with preceding siblings + for (const sibling of [...root.parent.children]) { + this.element.addChild(sibling); + } + } else { + this.element.addChild(root); + } this.element = thisElement; } if ( diff --git a/src/test/css/selectorPrinting.test.ts b/src/test/css/selectorPrinting.test.ts index 9f7505ac..94da7ebf 100644 --- a/src/test/css/selectorPrinting.test.ts +++ b/src/test/css/selectorPrinting.test.ts @@ -7,6 +7,8 @@ import { suite, test } from 'node:test'; import * as assert from 'node:assert'; import { Parser } from '../../parser/cssParser.js'; +import { SCSSParser } from '../../parser/scssParser.js'; +import { LESSParser } from '../../parser/lessParser.js'; import * as nodes from '../../parser/cssNodes.js'; import * as selectorPrinting from '../../services/selectorPrinting.js'; import { TextDocument, MarkedString } from '../../cssLanguageTypes.js'; @@ -125,6 +127,18 @@ suite('CSS - MarkedStringPrinter selectors', () => { '[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 0, 2)' ]); }); + test('sibling selector referenced by a nested parent selector', function () { + const assertHtml = (p: Parser, input: string, expected: string) => { + const selector = doParse(p, input, '&'); + assert.ok(selector); + const printed = new selectorPrinting.SelectorPrinting(cssDataManager).selectorToMarkedString(selector!); + assert.deepEqual(printed[0], { language: 'html', value: expected }); + }; + for (const p of [new SCSSParser(), new LESSParser()]) { + assertHtml(p, '.foo + .bar { .baz & { color: red; } }', '\n …\n \n '); + assertHtml(p, '.foo ~ .bar { .baz & { color: red; } }', '\n …\n \n ⋮\n '); + } + }); }); suite('CSS - MarkedStringPrinter selectors specificities', () => { diff --git a/src/test/scss/selectorPrinting.test.ts b/src/test/scss/selectorPrinting.test.ts index a7c1ad33..4355c341 100644 --- a/src/test/scss/selectorPrinting.test.ts +++ b/src/test/scss/selectorPrinting.test.ts @@ -42,6 +42,15 @@ suite('SCSS - Selector Printing', () => { assertSelector(p, 'o.c1 { &__bar {}}', '&', '{o[class=c1__bar]}'); }); + test('referencing selector with sibling combinators', function () { + let p = new SCSSParser(); + assertSelector(p, '.foo + .bar { .baz & { } }', '&', '{[class=baz]{…{[class=foo]|[class=bar]}}}'); + assertSelector(p, '.foo ~ .bar { .baz & { } }', '&', '{[class=baz]{…{[class=foo]|⋮|[class=bar]}}}'); + assertSelector(p, 'o1 o2 + o3 { .baz & { } }', '&', '{[class=baz]{…{o1{…{o2|o3}}}}}'); + assertSelector(p, 'o1 + o2 { &:hover { } }', '&', '{o1|o2[:hover=]}'); + assertSelector(p, 'o1 + o2 { e1 { } }', 'e1', '{o1|o2{…{e1}}}'); + }); + test('placeholders', function () { let p = new SCSSParser(); assertSelector(p, '%o1 { e1 { } }', 'e1', '{%o1{…{e1}}}');