diff --git a/src/services/selectorPrinting.ts b/src/services/selectorPrinting.ts index 8b38b5ae..8380a6ca 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; @@ -84,6 +87,9 @@ export class Element { } public clone(cloneChildren: boolean = true): Element { + if (this instanceof LabelElement) { + return new LabelElement(this.findAttribute(ELEMENT_NAME)!); + } const elem = new Element(); if (this.attributes) { elem.attributes = []; @@ -102,8 +108,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; @@ -115,7 +129,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 +173,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 +194,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 +243,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 +268,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))); @@ -557,8 +571,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; } } @@ -581,7 +603,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 1dfa3cd9..a3f8f347 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'; @@ -40,12 +42,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 +55,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('functional pseudo selectors preserve their arguments', function () { @@ -72,6 +75,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 +139,20 @@ 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 Parser(), 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 '); + assertHtml(p, '.foo[name=first] ~ .bar[name=second] { .baz & { color: red; } }', '\n …\n \n ⋮\n '); + assertHtml(p, 'a b + c { d & { color: red; } }', '\n …\n \n …\n \n '); + } + }); }); suite('CSS - MarkedStringPrinter selectors specificities', () => { @@ -143,6 +162,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..45470fff 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,17 @@ 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('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 () { 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('|');