Skip to content

Commit c55235d

Browse files
authored
Don't separate out media queries after one has bubbled (#1933)
Closes #777
1 parent e68818a commit c55235d

File tree

12 files changed

+49
-7
lines changed

12 files changed

+49
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
native CSS `filter` functions. This is in addition to number values which were
99
already allowed.
1010

11+
* Fix a cosmetic bug where an outer rule could be duplicated after nesting was
12+
resolved, instead of re-using a shared rule.
13+
1114
## 1.61.0
1215

1316
* **Potentially breaking change:** Drop support for End-of-Life Node.js 12.

lib/src/ast/css/modifiable/at_rule.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class ModifiableCssAtRule extends ModifiableCssParentNode implements CssAtRule {
2222

2323
T accept<T>(ModifiableCssVisitor<T> visitor) => visitor.visitCssAtRule(this);
2424

25+
bool equalsIgnoringChildren(ModifiableCssNode other) =>
26+
other is ModifiableCssAtRule &&
27+
name == other.name &&
28+
value == other.value &&
29+
isChildless == other.isChildless;
30+
2531
ModifiableCssAtRule copyWithoutChildren() =>
2632
ModifiableCssAtRule(name, span, childless: isChildless, value: value);
2733

lib/src/ast/css/modifiable/keyframe_block.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:source_span/source_span.dart';
66

7+
import '../../../utils.dart';
78
import '../../../visitor/interface/modifiable_css.dart';
89
import '../keyframe_block.dart';
910
import '../value.dart';
@@ -20,6 +21,10 @@ class ModifiableCssKeyframeBlock extends ModifiableCssParentNode
2021
T accept<T>(ModifiableCssVisitor<T> visitor) =>
2122
visitor.visitCssKeyframeBlock(this);
2223

24+
bool equalsIgnoringChildren(ModifiableCssNode other) =>
25+
other is ModifiableCssKeyframeBlock &&
26+
listEquals(selector.value, other.selector.value);
27+
2328
ModifiableCssKeyframeBlock copyWithoutChildren() =>
2429
ModifiableCssKeyframeBlock(selector, span);
2530
}

lib/src/ast/css/modifiable/media_rule.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:source_span/source_span.dart';
66

7+
import '../../../utils.dart';
78
import '../../../visitor/interface/modifiable_css.dart';
89
import '../media_query.dart';
910
import '../media_rule.dart';
@@ -25,6 +26,9 @@ class ModifiableCssMediaRule extends ModifiableCssParentNode
2526
T accept<T>(ModifiableCssVisitor<T> visitor) =>
2627
visitor.visitCssMediaRule(this);
2728

29+
bool equalsIgnoringChildren(ModifiableCssNode other) =>
30+
other is ModifiableCssMediaRule && listEquals(queries, other.queries);
31+
2832
ModifiableCssMediaRule copyWithoutChildren() =>
2933
ModifiableCssMediaRule(queries, span);
3034
}

lib/src/ast/css/modifiable/node.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ abstract class ModifiableCssParentNode extends ModifiableCssNode
6666
: _children = children,
6767
children = UnmodifiableListView(children);
6868

69+
/// Returns whether [this] is equal to [other], ignoring their child nodes.
70+
bool equalsIgnoringChildren(ModifiableCssNode other);
71+
6972
/// Returns a copy of [this] with an empty [children] list.
7073
///
7174
/// This is *not* a deep copy. If other parts of this node are modifiable,

lib/src/ast/css/modifiable/style_rule.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class ModifiableCssStyleRule extends ModifiableCssParentNode
3232
T accept<T>(ModifiableCssVisitor<T> visitor) =>
3333
visitor.visitCssStyleRule(this);
3434

35+
bool equalsIgnoringChildren(ModifiableCssNode other) =>
36+
other is ModifiableCssStyleRule && other.selector == selector;
37+
3538
ModifiableCssStyleRule copyWithoutChildren() =>
3639
ModifiableCssStyleRule(_selector, span,
3740
originalSelector: originalSelector);

lib/src/ast/css/modifiable/stylesheet.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class ModifiableCssStylesheet extends ModifiableCssParentNode
1818
T accept<T>(ModifiableCssVisitor<T> visitor) =>
1919
visitor.visitCssStylesheet(this);
2020

21+
bool equalsIgnoringChildren(ModifiableCssNode other) =>
22+
other is ModifiableCssStylesheet;
23+
2124
ModifiableCssStylesheet copyWithoutChildren() =>
2225
ModifiableCssStylesheet(span);
2326
}

lib/src/ast/css/modifiable/supports_rule.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class ModifiableCssSupportsRule extends ModifiableCssParentNode
2020
T accept<T>(ModifiableCssVisitor<T> visitor) =>
2121
visitor.visitCssSupportsRule(this);
2222

23+
bool equalsIgnoringChildren(ModifiableCssNode other) =>
24+
other is ModifiableCssSupportsRule && condition == other.condition;
25+
2326
ModifiableCssSupportsRule copyWithoutChildren() =>
2427
ModifiableCssSupportsRule(condition, span);
2528
}

lib/src/visitor/async_evaluate.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3343,8 +3343,14 @@ class _EvaluateVisitor
33433343
if (parent.hasFollowingSibling) {
33443344
// A node with siblings must have a parent
33453345
var grandparent = parent.parent!;
3346-
parent = parent.copyWithoutChildren();
3347-
grandparent.addChild(parent);
3346+
if (parent.equalsIgnoringChildren(grandparent.children.last)) {
3347+
// If we've already made a copy of [parent] and nothing else has been
3348+
// added after it, re-use it.
3349+
parent = grandparent.children.last as ModifiableCssParentNode;
3350+
} else {
3351+
parent = parent.copyWithoutChildren();
3352+
grandparent.addChild(parent);
3353+
}
33483354
}
33493355
}
33503356

lib/src/visitor/evaluate.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// DO NOT EDIT. This file was generated from async_evaluate.dart.
66
// See tool/grind/synchronize.dart for details.
77
//
8-
// Checksum: 06d1dd221c149650242b3e09b3f507125606bf0f
8+
// Checksum: 17862153344c8577d780b3e039a1ce5ebb774c17
99
//
1010
// ignore_for_file: unused_import
1111

@@ -3314,8 +3314,14 @@ class _EvaluateVisitor
33143314
if (parent.hasFollowingSibling) {
33153315
// A node with siblings must have a parent
33163316
var grandparent = parent.parent!;
3317-
parent = parent.copyWithoutChildren();
3318-
grandparent.addChild(parent);
3317+
if (parent.equalsIgnoringChildren(grandparent.children.last)) {
3318+
// If we've already made a copy of [parent] and nothing else has been
3319+
// added after it, re-use it.
3320+
parent = grandparent.children.last as ModifiableCssParentNode;
3321+
} else {
3322+
parent = parent.copyWithoutChildren();
3323+
grandparent.addChild(parent);
3324+
}
33193325
}
33203326
}
33213327

0 commit comments

Comments
 (0)