Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
36 changes: 31 additions & 5 deletions src/services/selectorPrinting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -577,7 +596,14 @@ class SelectorElementBuilder {
const thisElement = toElement(<nodes.SimpleSelector>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 (
Expand Down
14 changes: 14 additions & 0 deletions src/test/css/selectorPrinting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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; } }', '<element class="baz">\n …\n <element class="foo">\n <element class="bar">');
assertHtml(p, '.foo ~ .bar { .baz & { color: red; } }', '<element class="baz">\n …\n <element class="foo">\n ⋮\n <element class="bar">');
}
});
});

suite('CSS - MarkedStringPrinter selectors specificities', () => {
Expand Down
9 changes: 9 additions & 0 deletions src/test/scss/selectorPrinting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}');
Expand Down