-
Notifications
You must be signed in to change notification settings - Fork 376
Deprecate bogus combinators #1740
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,20 @@ | |
|
|
||
| import 'package:meta/meta.dart'; | ||
|
|
||
| import '../evaluation_context.dart'; | ||
| import '../visitor/any_selector.dart'; | ||
| import '../visitor/interface/selector.dart'; | ||
| import '../visitor/serialize.dart'; | ||
| import 'selector/complex.dart'; | ||
| import 'selector/list.dart'; | ||
| import 'selector/placeholder.dart'; | ||
| import 'selector/pseudo.dart'; | ||
|
|
||
| export 'selector/attribute.dart'; | ||
| export 'selector/class.dart'; | ||
| export 'selector/combinator.dart'; | ||
| export 'selector/complex.dart'; | ||
| export 'selector/complex_component.dart'; | ||
| export 'selector/compound.dart'; | ||
| export 'selector/id.dart'; | ||
| export 'selector/list.dart'; | ||
|
|
@@ -32,11 +40,131 @@ export 'selector/universal.dart'; | |
| abstract class Selector { | ||
| /// Whether this selector, and complex selectors containing it, should not be | ||
| /// emitted. | ||
| /// | ||
| /// @nodoc | ||
| @internal | ||
| bool get isInvisible => false; | ||
| bool get isInvisible => accept(const _IsInvisibleVisitor(includeBogus: true)); | ||
|
|
||
| // Whether this selector would be invisible even if it didn't have bogus | ||
| // combinators. | ||
| /// | ||
| /// @nodoc | ||
| @internal | ||
| bool get isInvisibleOtherThanBogusCombinators => | ||
| accept(const _IsInvisibleVisitor(includeBogus: false)); | ||
|
|
||
| /// Whether this selector is not valid CSS. | ||
| /// | ||
| /// This includes both selectors that are useful exclusively for build-time | ||
| /// nesting (`> .foo)` and selectors with invalid combiantors that are still | ||
| /// supported for backwards-compatibility reasons (`.foo + ~ .bar`). | ||
| bool get isBogus => | ||
| accept(const _IsBogusVisitor(includeLeadingCombinator: true)); | ||
|
|
||
| /// Whether this selector is bogus other than having a leading combinator. | ||
| /// | ||
| /// @nodoc | ||
| @internal | ||
| bool get isBogusOtherThanLeadingCombinator => | ||
| accept(const _IsBogusVisitor(includeLeadingCombinator: false)); | ||
|
|
||
| /// Whether this is a useless selector (that is, it's bogus _and_ it can't be | ||
| /// transformed into valid CSS by `@extend` or nesting). | ||
| /// | ||
| /// @nodoc | ||
| @internal | ||
| bool get isUseless => accept(const _IsUselessVisitor()); | ||
|
|
||
| /// Prints a warning if [this] is a bogus selector. | ||
| /// | ||
| /// This may only be called from within a custom Sass function. This will | ||
| /// throw a [SassScriptException] in Dart Sass 2.0.0. | ||
| void assertNotBogus({String? name}) { | ||
| if (!isBogus) return; | ||
| warn( | ||
| (name == null ? '' : '\$$name: ') + | ||
| '$this is not valid CSS.\n' | ||
| 'This will be an error in Dart Sass 2.0.0.\n' | ||
| '\n' | ||
| 'More info: https://sass-lang.com/d/bogus-combinators', | ||
| deprecation: true); | ||
| } | ||
|
|
||
| /// Calls the appropriate visit method on [visitor]. | ||
| T accept<T>(SelectorVisitor<T> visitor); | ||
|
|
||
| String toString() => serializeSelector(this, inspect: true); | ||
| } | ||
|
|
||
| /// The visitor used to implement [Selector.isInvisible]. | ||
| class _IsInvisibleVisitor extends AnySelectorVisitor { | ||
| /// Whether to consider selectors with bogus combinators invisible. | ||
| final bool includeBogus; | ||
|
|
||
| const _IsInvisibleVisitor({required this.includeBogus}); | ||
|
|
||
| bool visitSelectorList(SelectorList list) => | ||
| list.components.every(visitComplexSelector); | ||
|
|
||
| bool visitComplexSelector(ComplexSelector complex) => | ||
| super.visitComplexSelector(complex) || | ||
| (includeBogus && complex.isBogusOtherThanLeadingCombinator); | ||
|
|
||
| bool visitPlaceholderSelector(PlaceholderSelector placeholder) => true; | ||
|
|
||
| bool visitPseudoSelector(PseudoSelector pseudo) { | ||
| var selector = pseudo.selector; | ||
| if (selector == null) return false; | ||
|
|
||
| // We don't consider `:not(%foo)` to be invisible because, semantically, it | ||
| // means "doesn't match this selector that matches nothing", so it's | ||
| // equivalent to *. If the entire compound selector is composed of `:not`s | ||
| // with invisible lists, the serializer emits it as `*`. | ||
| return pseudo.name == 'not' | ||
| ? (includeBogus && selector.isBogus) | ||
| : selector.accept(this); | ||
| } | ||
| } | ||
|
|
||
| /// The visitor used to implement [Selector.isBogus]. | ||
| class _IsBogusVisitor extends AnySelectorVisitor { | ||
| /// Whether to consider selectors with leading combinators as bogus. | ||
| final bool includeLeadingCombinator; | ||
|
|
||
| const _IsBogusVisitor({required this.includeLeadingCombinator}); | ||
|
|
||
| bool visitComplexSelector(ComplexSelector complex) { | ||
| if (complex.components.isEmpty) { | ||
| return complex.leadingCombinators.isNotEmpty; | ||
| } else { | ||
| return complex.leadingCombinators.length > | ||
| (includeLeadingCombinator ? 0 : 1) || | ||
| complex.components.last.combinators.isNotEmpty || | ||
| complex.components.any((component) => | ||
| component.combinators.length > 1 || | ||
| component.selector.accept(this)); | ||
| } | ||
| } | ||
|
|
||
| bool visitPseudoSelector(PseudoSelector pseudo) { | ||
| var selector = pseudo.selector; | ||
| if (selector == null) return false; | ||
|
|
||
| // The CSS spec specifically allows leading combinators in `:has()`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The CSS spec allows 1 leading combinator. |
||
| return pseudo.name == 'has' | ||
| ? selector.isBogusOtherThanLeadingCombinator | ||
| : selector.isBogus; | ||
| } | ||
| } | ||
|
|
||
| /// The visitor used to implement [Selector.isUseless] | ||
| class _IsUselessVisitor extends AnySelectorVisitor { | ||
| const _IsUselessVisitor(); | ||
|
|
||
| bool visitComplexSelector(ComplexSelector complex) => | ||
| complex.leadingCombinators.length > 1 || | ||
| complex.components.any((component) => | ||
| component.combinators.length > 1 || component.selector.accept(this)); | ||
|
|
||
| bool visitPseudoSelector(PseudoSelector pseudo) => pseudo.isBogus; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright 2022 Google Inc. Use of this source code is governed by an | ||
| // MIT-style license that can be found in the LICENSE file or at | ||
| // https://opensource.org/licenses/MIT. | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
|
|
||
| /// A combinator that defines the relationship between selectors in a | ||
| /// [ComplexSelector]. | ||
| /// | ||
| /// {@category Selector} | ||
| @sealed | ||
| class Combinator { | ||
nex3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Matches the right-hand selector if it's immediately adjacent to the | ||
| /// left-hand selector in the DOM tree. | ||
| static const nextSibling = Combinator._("+"); | ||
|
|
||
| /// Matches the right-hand selector if it's a direct child of the left-hand | ||
| /// selector in the DOM tree. | ||
| static const child = Combinator._(">"); | ||
|
|
||
| /// Matches the right-hand selector if it comes after the left-hand selector | ||
| /// in the DOM tree. | ||
| static const followingSibling = Combinator._("~"); | ||
|
|
||
| /// The combinator's token text. | ||
| final String _text; | ||
|
|
||
| const Combinator._(this._text); | ||
|
|
||
| String toString() => _text; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.