Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect transpilation of direct child selectors in scoped Light DOM styles with native CSS nesting #5209

Open
Mr-VincentW opened this issue Feb 11, 2025 · 2 comments
Labels
BUG P3 light-dom This label represents any issue that relates to the feature of Light DOM

Comments

@Mr-VincentW
Copy link

Mr-VincentW commented Feb 11, 2025

Description

Native CSS nesting syntax fails to transpile correctly in certain edge cases when used with scoped styles in the light DOM, particularly for direct child selectors. This occurs when child selectors are used without the & symbol.

Steps to Reproduce

  1. Transpiles Correctly: CSS with class selectors and the & symbol for child selectors:
.parent-class {
  & > .child-class {
   color: red;
  }
}
/* Results in something like:
 * .parent-class.lwc-6le4c39rot8 {
 *   &.lwc-6le4c39rot8 > .child-class.lwc-6le4c39rot8 {
 *     color: red;
 *   }
 * }
 */
  1. Transpiles Incorrectly: CSS with class selectors but without the & symbol for child selectors:
.parent-class {
  > .child-class {
   color: red;
  }
}
/* Results in something like:
 * .parent-class.lwc-6le4c39rot8 {
 *   > .lwc-6le4c39rot8.child-class.lwc-6le4c39rot8 {  // <-- ".lwc-6le4c39rot8.child-class", works "by coincidence", but not correct
 *     color: red;
 *   }
 * }
 */
  1. Fails for Type Selectors: CSS with type selectors (e.g., h2) and without the & symbol for child selectors:
.parent-class {
  > h2 {
   color: red;
  }
}
/* Results in something like:
 * .parent-class.lwc-6le4c39rot8 {
 *   > .lwc-6le4c39rot8h2.lwc-6le4c39rot8 {  // <-- ".lwc-6le4c39rot8h2", cannot work as desired
 *     color: red;
 *   }
 * }
 */

In this case, the output includes an incorrect concatenation of class and type selectors, which fails to target the intended elements properly.

Expected Results

CSS nesting should transpile correctly for direct child selectors, even when using type selectors without the & symbol. For example:

.parent-class {
  > h2 {
   color: red;
  }
}
/* Results in something like:
 * .parent-class.lwc-6le4c39rot8 {
 *   > h2.lwc-6le4c39rot8 {
 *     color: red;
 *   }
 * }
 */

Browsers Affected

All browsers supporting CSS nesting. Please refer to caniuse for browser compatibility infomation.

Version

  • "lwc": "^8.12.4"
  • "node": ">=18"

Possible Solution

Consider removing the scoping hash from nested selectors, as the top-most ancestor selector is already scoped.

@wjhsf
Copy link
Collaborator

wjhsf commented Feb 11, 2025

Thanks for such a detailed bug report! Native CSS nesting is a pretty new feature in browsers, so I'm not surprised there's cases that don't work quite right.

@wjhsf wjhsf added BUG P3 light-dom This label represents any issue that relates to the feature of Light DOM labels Feb 11, 2025
Copy link

git2gus bot commented Feb 11, 2025

This issue has been linked to a new work item: W-17800792

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BUG P3 light-dom This label represents any issue that relates to the feature of Light DOM
Projects
None yet
Development

No branches or pull requests

2 participants