Skip to content

Fix top-level nesting selectors in loaded plain CSS #2560

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 1.87.0

* **Potentially breaking bug fix:** When a plain CSS file with a top-level
nesting selector `&` is loaded into a nested Sass context via
`meta.load-css()` or `@import`, Sass now emits plain CSS nesting rather than
incorrectly combining it with the parent selector using a descendant
combinator.

## 1.86.3

* Fix a bug introduced in 1.86.1 where Sass fails to resolve paths starting with
Expand Down
17 changes: 16 additions & 1 deletion lib/src/ast/selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import '../visitor/serialize.dart';
import 'node.dart';
import 'selector/complex.dart';
import 'selector/list.dart';
import 'selector/parent.dart';
import 'selector/placeholder.dart';
import 'selector/pseudo.dart';

Expand Down Expand Up @@ -49,6 +50,13 @@ abstract base class Selector implements AstNode {
@internal
bool get isInvisible => accept(const _IsInvisibleVisitor(includeBogus: true));

/// Whether this selector contains a [ParentSelector].
///
/// @nodoc
@internal
bool get containsParentSelector =>
accept(const _ContainsParentSelectorVisitor());

// Whether this selector would be invisible even if it didn't have bogus
// combinators.
///
Expand Down Expand Up @@ -169,7 +177,7 @@ class _IsBogusVisitor with AnySelectorVisitor {
}
}

/// The visitor used to implement [Selector.isUseless]
/// The visitor used to implement [Selector.isUseless].
class _IsUselessVisitor with AnySelectorVisitor {
const _IsUselessVisitor();

Expand All @@ -182,3 +190,10 @@ class _IsUselessVisitor with AnySelectorVisitor {

bool visitPseudoSelector(PseudoSelector pseudo) => pseudo.isBogus;
}

/// The visitor used to implement [Selector.containsParentSelector].
class _ContainsParentSelectorVisitor with AnySelectorVisitor {
const _ContainsParentSelectorVisitor();

bool visitParentSelector(ParentSelector _) => true;
}
12 changes: 10 additions & 2 deletions lib/src/visitor/async_evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2373,7 +2373,11 @@ final class _EvaluateVisitor
plainCss: _stylesheet.plainCss,
);

var nest = !(_styleRule?.fromPlainCss ?? false);
var nest = switch (_styleRule) {
null => true,
CssStyleRule(fromPlainCss: true) => false,
_ => !(_stylesheet.plainCss && parsedSelector.containsParentSelector)
};
if (nest) {
if (_stylesheet.plainCss) {
for (var complex in parsedSelector.components) {
Expand Down Expand Up @@ -4071,7 +4075,11 @@ final class _EvaluateVisitor
}

var styleRule = _styleRule;
var nest = !(_styleRule?.fromPlainCss ?? false);
var nest = switch (_styleRule) {
null => true,
CssStyleRule(fromPlainCss: true) => false,
_ => !(node.fromPlainCss && node.selector.containsParentSelector)
};
var originalSelector = nest
? node.selector.nestWithin(
styleRule?.originalSelector,
Expand Down
14 changes: 11 additions & 3 deletions lib/src/visitor/evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_evaluate.dart.
// See tool/grind/synchronize.dart for details.
//
// Checksum: 25aa2d050126950ea37dc1c53539f0b041356e8e
// Checksum: 607745b48d0737b3be112d0a8753dd87492fcc31
//
// ignore_for_file: unused_import

Expand Down Expand Up @@ -2380,7 +2380,11 @@ final class _EvaluateVisitor
plainCss: _stylesheet.plainCss,
);

var nest = !(_styleRule?.fromPlainCss ?? false);
var nest = switch (_styleRule) {
null => true,
CssStyleRule(fromPlainCss: true) => false,
_ => !(_stylesheet.plainCss && parsedSelector.containsParentSelector)
};
if (nest) {
if (_stylesheet.plainCss) {
for (var complex in parsedSelector.components) {
Expand Down Expand Up @@ -4072,7 +4076,11 @@ final class _EvaluateVisitor
}

var styleRule = _styleRule;
var nest = !(_styleRule?.fromPlainCss ?? false);
var nest = switch (_styleRule) {
null => true,
CssStyleRule(fromPlainCss: true) => false,
_ => !(node.fromPlainCss && node.selector.containsParentSelector)
};
var originalSelector = nest
? node.selector.nestWithin(
styleRule?.originalSelector,
Expand Down
2 changes: 1 addition & 1 deletion pkg/sass_api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 15.3.3
## 15.4.0

* No user-visible changes.

Expand Down
4 changes: 2 additions & 2 deletions pkg/sass_api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: sass_api
# Note: Every time we add a new Sass AST node, we need to bump the *major*
# version because it's a breaking change for anyone who's implementing the
# visitor interface(s).
version: 15.3.3
version: 15.4.0
description: Additional APIs for Dart Sass.
homepage: https://github.com/sass/dart-sass

environment:
sdk: ">=3.6.0 <4.0.0"

dependencies:
sass: 1.86.3
sass: 1.87.0

dev_dependencies:
dartdoc: ^8.0.14
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass
version: 1.86.3
version: 1.87.0
description: A Sass implementation in Dart.
homepage: https://github.com/sass/dart-sass

Expand Down