Skip to content

Commit 60622a1

Browse files
committed
fix order of properties with all shorthand
fixes parcel-bundler#746
1 parent 81d21b9 commit 60622a1

File tree

5 files changed

+35
-31
lines changed

5 files changed

+35
-31
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const-str = "0.3.1"
6262
pathdiff = "0.2.1"
6363
ahash = "0.8.7"
6464
paste = "1.0.12"
65-
indexmap = "2.2.6"
6665
# CLI deps
6766
atty = { version = "0.2", optional = true }
6867
clap = { version = "3.0.6", features = ["derive"], optional = true }

node/test/visitor.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ test('custom units', () => {
116116
}
117117
});
118118

119-
assert.equal(res.code.toString(), '.foo{font-size:calc(3*var(--step));--step:.25rem}');
119+
assert.equal(res.code.toString(), '.foo{--step:.25rem;font-size:calc(3*var(--step))}');
120120
});
121121

122122
test('design tokens', () => {
@@ -822,7 +822,7 @@ test('dashed idents', () => {
822822
}
823823
});
824824

825-
assert.equal(res.code.toString(), '.foo{color:var(--prefix-foo);--prefix-foo:#ff0}');
825+
assert.equal(res.code.toString(), '.foo{--prefix-foo:#ff0;color:var(--prefix-foo)}');
826826
});
827827

828828
test('custom idents', () => {

src/declaration.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! CSS declarations.
22
33
use std::borrow::Cow;
4+
use std::collections::HashMap;
45
use std::ops::Range;
56

67
use crate::context::{DeclarationContext, PropertyHandlerContext};
@@ -33,14 +34,13 @@ use crate::properties::{
3334
transition::TransitionHandler,
3435
ui::ColorSchemeHandler,
3536
};
36-
use crate::properties::{CSSWideKeyword, Property, PropertyId};
37+
use crate::properties::{Property, PropertyId};
3738
use crate::traits::{PropertyHandler, ToCss};
3839
use crate::values::ident::DashedIdent;
3940
use crate::values::string::CowArcStr;
4041
#[cfg(feature = "visitor")]
4142
use crate::visitor::Visit;
4243
use cssparser::*;
43-
use indexmap::IndexMap;
4444

4545
/// A CSS declaration block.
4646
///
@@ -516,10 +516,9 @@ pub(crate) struct DeclarationHandler<'i> {
516516
color_scheme: ColorSchemeHandler,
517517
fallback: FallbackHandler,
518518
prefix: PrefixHandler,
519-
all: Option<CSSWideKeyword>,
520519
direction: Option<Direction>,
521520
unicode_bidi: Option<UnicodeBidi>,
522-
custom_properties: IndexMap<DashedIdent<'i>, CustomProperty<'i>>,
521+
custom_properties: HashMap<DashedIdent<'i>, usize>,
523522
decls: DeclarationList<'i>,
524523
}
525524

@@ -571,13 +570,18 @@ impl<'i> DeclarationHandler<'i> {
571570
}
572571

573572
if let CustomPropertyName::Custom(name) = &custom.name {
574-
if let Some(prev) = self.custom_properties.get_mut(name) {
575-
if prev.value == custom.value {
573+
if let Some(index) = self.custom_properties.get(name) {
574+
if self.decls[*index] == *property {
576575
return true;
577576
}
578-
*prev = custom.clone();
577+
let mut custom = custom.clone();
578+
self.add_conditional_fallbacks(&mut custom, context);
579+
self.decls[*index] = Property::Custom(custom);
579580
} else {
580-
self.custom_properties.insert(name.clone(), custom.clone());
581+
self.custom_properties.insert(name.clone(), self.decls.len());
582+
let mut custom = custom.clone();
583+
self.add_conditional_fallbacks(&mut custom, context);
584+
self.decls.push(Property::Custom(custom));
581585
}
582586

583587
return true;
@@ -600,13 +604,17 @@ impl<'i> DeclarationHandler<'i> {
600604
true
601605
}
602606
Property::All(keyword) => {
603-
*self = DeclarationHandler {
604-
custom_properties: std::mem::take(&mut self.custom_properties),
607+
let mut handler = DeclarationHandler {
605608
unicode_bidi: self.unicode_bidi.clone(),
606609
direction: self.direction.clone(),
607-
all: Some(keyword.clone()),
608610
..Default::default()
609611
};
612+
for (key, index) in self.custom_properties.drain() {
613+
handler.custom_properties.insert(key, handler.decls.len());
614+
handler.decls.push(self.decls[index].clone());
615+
}
616+
handler.decls.push(Property::All(keyword.clone()));
617+
*self = handler;
610618
true
611619
}
612620
_ => false,
@@ -633,20 +641,12 @@ impl<'i> DeclarationHandler<'i> {
633641
}
634642

635643
pub fn finalize(&mut self, context: &mut PropertyHandlerContext<'i, '_>) {
636-
// Always place the `all` property first. Previous properties will have been omitted.
637-
if let Some(all) = std::mem::take(&mut self.all) {
638-
self.decls.push(Property::All(all));
639-
}
640644
if let Some(direction) = std::mem::take(&mut self.direction) {
641645
self.decls.push(Property::Direction(direction));
642646
}
643647
if let Some(unicode_bidi) = std::mem::take(&mut self.unicode_bidi) {
644648
self.decls.push(Property::UnicodeBidi(unicode_bidi));
645649
}
646-
for (_, mut value) in std::mem::take(&mut self.custom_properties) {
647-
self.add_conditional_fallbacks(&mut value, context);
648-
self.decls.push(Property::Custom(value));
649-
}
650650

651651
self.background.finalize(&mut self.decls, context);
652652
self.border.finalize(&mut self.decls, context);
@@ -675,5 +675,6 @@ impl<'i> DeclarationHandler<'i> {
675675
self.color_scheme.finalize(&mut self.decls, context);
676676
self.fallback.finalize(&mut self.decls, context);
677677
self.prefix.finalize(&mut self.decls, context);
678+
self.custom_properties.clear();
678679
}
679680
}

src/lib.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21583,26 +21583,26 @@ mod tests {
2158321583
indoc! {r#"
2158421584
@keyframes foo {
2158521585
from {
21586-
opacity: 0;
2158721586
--custom: #ff0;
21587+
opacity: 0;
2158821588
}
2158921589

2159021590
to {
21591-
opacity: 1;
2159221591
--custom: #ee00be;
21592+
opacity: 1;
2159321593
}
2159421594
}
2159521595

2159621596
@supports (color: lab(0% 0 0)) {
2159721597
@keyframes foo {
2159821598
from {
21599-
opacity: 0;
2160021599
--custom: #ff0;
21600+
opacity: 0;
2160121601
}
2160221602

2160321603
to {
21604-
opacity: 1;
2160521604
--custom: lab(50.998% 125.506 -50.7078);
21605+
opacity: 1;
2160621606
}
2160721607
}
2160821608
}
@@ -23736,8 +23736,8 @@ mod tests {
2373623736
}
2373723737

2373823738
.EgL3uq_foo {
23739-
color: var(--foo);
2374023739
--foo: red;
23740+
color: var(--foo);
2374123741
}
2374223742
"#},
2374323743
map! {
@@ -23786,10 +23786,10 @@ mod tests {
2378623786
}
2378723787

2378823788
.EgL3uq_foo {
23789-
color: var(--EgL3uq_foo);
23790-
font-palette: --EgL3uq_Cooler;
2379123789
--EgL3uq_foo: red;
2379223790
--EgL3uq_bar: green;
23791+
color: var(--EgL3uq_foo);
23792+
font-palette: --EgL3uq_Cooler;
2379323793
}
2379423794

2379523795
.EgL3uq_bar {
@@ -27646,7 +27646,7 @@ mod tests {
2764627646
);
2764727647
minify_test(
2764827648
".foo { --test: red; all: revert-layer }",
27649-
".foo{all:revert-layer;--test:red}",
27649+
".foo{--test:red;all:revert-layer}",
2765027650
);
2765127651
minify_test(
2765227652
".foo { unicode-bidi: embed; all: revert-layer }",
@@ -27660,5 +27660,10 @@ mod tests {
2766027660
".foo { direction: rtl; all: revert-layer; direction: ltr }",
2766127661
".foo{all:revert-layer;direction:ltr}",
2766227662
);
27663+
minify_test(".foo { background: var(--foo); all: unset; }", ".foo{all:unset}");
27664+
minify_test(
27665+
".foo { all: unset; background: var(--foo); }",
27666+
".foo{all:unset;background:var(--foo)}",
27667+
);
2766327668
}
2766427669
}

0 commit comments

Comments
 (0)