Skip to content

Keep sibling combinators when a nested selector references the parent with & - #546

Open
Usman Tahir Qureshi (UsmanQT) wants to merge 1 commit into
microsoft:mainfrom
UsmanQT:fix/169900-sibling-parent-selector
Open

Usman Tahir Qureshi (UsmanQT) wants to merge 1 commit into
microsoft:mainfrom
UsmanQT:fix/169900-sibling-parent-selector

Conversation

@UsmanQT

@UsmanQT Usman Tahir Qureshi (UsmanQT) commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes microsoft/vscode#169900

When a parent selector contains a sibling combinator (+ or ~) and a nested rule references it with &, the hover preview splits the sibling chain apart:

.foo + .bar {
    .baz & { color: red; }
}

Before:

<element class="foo">
<element class="baz">
  …
    <element class="bar">

After:

<element class="baz">
  …
    <element class="foo">
    <element class="bar">

The same happens with ~ (the ⋮ marker is now kept too) and in Less.

Cause

  • SelectorElementBuilder.processSelector removed only the root of the last element (.bar) from the tree when it saw &, so .foo was left behind at the top level.
  • Element.cloneWithParent() cloned only the ancestor chain of the referenced element, so preceding siblings in the same parent were dropped (for example o1 o2 + o3 { .baz & {} } lost o2).

Change

  • processSelector moves the whole parent context out of the root when & is used, instead of only the last element.
  • cloneWithParent() also clones the preceding siblings of each element on the path. When the element is at the top level, those clones go under a new RootElement.
  • The code that adds a referenced selector to the tree adds those preceding siblings along with it.
  • clone() keeps LabelElement (⋮), so it still prints as a label after cloning.

Tests

  • src/test/scss/selectorPrinting.test.ts: element-tree assertions for +, ~, a descendant-plus-sibling parent, &:hover on a sibling chain, and a plain nested rule under a sibling chain.
  • src/test/css/selectorPrinting.test.ts: checks the HTML preview from the issue for both SCSS and Less. It deliberately checks only the HTML part, not specificity, so it doesn't overlap with Include parent rules in the specificity of nested selectors #525.
  • Without the fix, the new tests fail. With it, npm test passes (361/361) and lint passes.

🤖 Generated with Claude Code

… with `&`

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 <noreply@anthropic.com>
@UsmanQT

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinate label cloning with #545 before merging both

This change passes independently, but combining it with #545 produces a runtime preview regression. The new LabelElement.clone() reads this.findAttribute("name"), while #545 moves the element/label name to ELEMENT_NAME (<>). After both changes the lookup returns null.

Reproduction:

.foo ~ .bar {
  .baz & { color: red; }
}

The combined HTML preview prints literal null where the sibling placeholder should be ⋮. Descendant placeholders can similarly become null instead of ….

Please update the clone lookup to use the same name storage as #545 when integrating these PRs, and run the combined CSS/SCSS sibling-reference tests. This was reproduced after clean three-way application; it is an integration defect, not a standalone failure of this head. In the full combined PR suite, both the CSS and SCSS sibling-reference tests fail on this mismatch.

Reviewed commit: c502ce2. Location: src/services/selectorPrinting.ts, LabelElement.clone() around line 88.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[scss] Display a wrong selector preview when using sibling combinators in SCSS, Less

2 participants