Skip to content

Commit 5c105fb

Browse files
committed
Split selector lists when some selectors aren't supported by targets
Fixes parcel-bundler#311
1 parent df1bae2 commit 5c105fb

File tree

16 files changed

+302
-51
lines changed

16 files changed

+302
-51
lines changed

node/src/at_rule_parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub struct Prelude<'i> {
4949
prelude: Option<ParsedComponent<'i>>,
5050
}
5151

52-
#[derive(Serialize, Deserialize)]
52+
#[derive(Serialize, Deserialize, Clone)]
5353
pub struct AtRule<'i> {
5454
#[serde(borrow)]
5555
pub name: CowArcStr<'i>,
@@ -58,7 +58,7 @@ pub struct AtRule<'i> {
5858
pub loc: Location,
5959
}
6060

61-
#[derive(Serialize, Deserialize)]
61+
#[derive(Serialize, Deserialize, Clone)]
6262
#[serde(tag = "type", content = "value", rename_all = "kebab-case")]
6363
pub enum AtRuleBody<'i> {
6464
#[serde(borrow)]

node/test/composeVisitors.test.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@ test('known rules', () => {
510510
margin-right: @margin-left;
511511
}
512512
`),
513+
targets: {
514+
safari: 14 << 16
515+
},
513516
visitor: composeVisitors([
514517
{
515518
Rule: {

node/test/visitor.test.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ test('focus visible', () => {
401401
color: red;
402402
}
403403
`),
404+
targets: {
405+
safari: 14 << 16
406+
},
404407
visitor: {
405408
Rule: {
406409
style(rule) {

src/bundler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl<'a, 'o, 's, P: SourceProvider> Bundler<'a, 'o, 's, P, DefaultAtRuleParser>
218218

219219
impl<'a, 'o, 's, P: SourceProvider, T: AtRuleParser<'a> + Clone + Sync + Send> Bundler<'a, 'o, 's, P, T>
220220
where
221-
T::AtRule: Sync + Send + ToCss,
221+
T::AtRule: Sync + Send + ToCss + Clone,
222222
{
223223
/// Creates a new Bundler using the given source provider.
224224
/// If a source map is given, the content of each source file included in the bundle will

src/context.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'i, 'o> PropertyHandlerContext<'i, 'o> {
6565
self.rtl.push(rtl);
6666
}
6767

68-
pub fn get_logical_rules<T>(&mut self, style_rule: &StyleRule<'i, T>) -> Vec<CssRule<'i, T>> {
68+
pub fn get_logical_rules<T>(&self, style_rule: &StyleRule<'i, T>) -> Vec<CssRule<'i, T>> {
6969
// TODO: :dir/:lang raises the specificity of the selector. Use :where to lower it?
7070
let mut dest = Vec::new();
7171

@@ -82,7 +82,7 @@ impl<'i, 'o> PropertyHandlerContext<'i, 'o> {
8282
selectors,
8383
vendor_prefix: VendorPrefix::None,
8484
declarations: DeclarationBlock {
85-
declarations: std::mem::take(&mut self.$decls),
85+
declarations: self.$decls.clone(),
8686
important_declarations: vec![],
8787
},
8888
rules: CssRuleList(vec![]),
@@ -148,22 +148,21 @@ impl<'i, 'o> PropertyHandlerContext<'i, 'o> {
148148
}
149149
}
150150

151-
pub fn get_supports_rules<T>(&mut self, style_rule: &StyleRule<'i, T>) -> Vec<CssRule<'i, T>> {
151+
pub fn get_supports_rules<T>(&self, style_rule: &StyleRule<'i, T>) -> Vec<CssRule<'i, T>> {
152152
if self.supports.is_empty() {
153153
return Vec::new();
154154
}
155155

156156
let mut dest = Vec::new();
157-
let supports = std::mem::take(&mut self.supports);
158-
for entry in supports {
157+
for entry in &self.supports {
159158
dest.push(CssRule::Supports(SupportsRule {
160-
condition: entry.condition,
159+
condition: entry.condition.clone(),
161160
rules: CssRuleList(vec![CssRule::Style(StyleRule {
162161
selectors: style_rule.selectors.clone(),
163162
vendor_prefix: VendorPrefix::None,
164163
declarations: DeclarationBlock {
165-
declarations: entry.declarations,
166-
important_declarations: entry.important_declarations,
164+
declarations: entry.declarations.clone(),
165+
important_declarations: entry.important_declarations.clone(),
167166
},
168167
rules: CssRuleList(vec![]),
169168
loc: style_rule.loc.clone(),
@@ -174,4 +173,10 @@ impl<'i, 'o> PropertyHandlerContext<'i, 'o> {
174173

175174
dest
176175
}
176+
177+
pub fn reset(&mut self) {
178+
self.supports.clear();
179+
self.ltr.clear();
180+
self.rtl.clear();
181+
}
177182
}

src/lib.rs

Lines changed: 169 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8691,11 +8691,7 @@ mod tests {
86918691
}
86928692
"#,
86938693
indoc! {r#"
8694-
[foo="bar"] {
8695-
color: red;
8696-
}
8697-
8698-
.bar {
8694+
[foo="bar"], .bar {
86998695
color: red;
87008696
}
87018697
"#},
@@ -9335,6 +9331,174 @@ mod tests {
93359331
}
93369332
"#},
93379333
);
9334+
9335+
prefix_test(
9336+
r#"
9337+
:hover, :focus-visible {
9338+
color: red;
9339+
}
9340+
"#,
9341+
indoc! {r#"
9342+
:hover {
9343+
color: red;
9344+
}
9345+
9346+
:focus-visible {
9347+
color: red;
9348+
}
9349+
"#},
9350+
Browsers {
9351+
safari: Some(13 << 16),
9352+
..Browsers::default()
9353+
},
9354+
);
9355+
9356+
prefix_test(
9357+
r#"
9358+
.foo {
9359+
color: red;
9360+
}
9361+
9362+
:hover, :focus-visible {
9363+
color: red;
9364+
}
9365+
"#,
9366+
indoc! {r#"
9367+
.foo, :hover {
9368+
color: red;
9369+
}
9370+
9371+
:focus-visible {
9372+
color: red;
9373+
}
9374+
"#},
9375+
Browsers {
9376+
safari: Some(13 << 16),
9377+
..Browsers::default()
9378+
},
9379+
);
9380+
9381+
prefix_test(
9382+
r#"
9383+
:hover, :focus-visible {
9384+
margin-inline-start: 24px;
9385+
}
9386+
"#,
9387+
indoc! {r#"
9388+
:hover:not(:lang(ae, ar, arc, bcc, bqi, ckb, dv, fa, glk, he, ku, mzn, nqo, pnb, ps, sd, ug, ur, yi)) {
9389+
margin-left: 24px;
9390+
}
9391+
9392+
:hover:lang(ae, ar, arc, bcc, bqi, ckb, dv, fa, glk, he, ku, mzn, nqo, pnb, ps, sd, ug, ur, yi) {
9393+
margin-right: 24px;
9394+
}
9395+
9396+
:focus-visible:not(:lang(ae, ar, arc, bcc, bqi, ckb, dv, fa, glk, he, ku, mzn, nqo, pnb, ps, sd, ug, ur, yi)) {
9397+
margin-left: 24px;
9398+
}
9399+
9400+
:focus-visible:lang(ae, ar, arc, bcc, bqi, ckb, dv, fa, glk, he, ku, mzn, nqo, pnb, ps, sd, ug, ur, yi) {
9401+
margin-right: 24px;
9402+
}
9403+
"#},
9404+
Browsers {
9405+
safari: Some(11 << 16),
9406+
..Browsers::default()
9407+
},
9408+
);
9409+
9410+
prefix_test(
9411+
r#"
9412+
:focus-within, :focus-visible {
9413+
color: red;
9414+
}
9415+
"#,
9416+
indoc! {r#"
9417+
:focus-within {
9418+
color: red;
9419+
}
9420+
9421+
:focus-visible {
9422+
color: red;
9423+
}
9424+
"#},
9425+
Browsers {
9426+
safari: Some(9 << 16),
9427+
..Browsers::default()
9428+
},
9429+
);
9430+
9431+
prefix_test(
9432+
r#"
9433+
:hover, :focus-visible {
9434+
color: red;
9435+
}
9436+
"#,
9437+
indoc! {r#"
9438+
:is(:hover, :focus-visible) {
9439+
color: red;
9440+
}
9441+
"#},
9442+
Browsers {
9443+
safari: Some(14 << 16),
9444+
..Browsers::default()
9445+
},
9446+
);
9447+
9448+
prefix_test(
9449+
r#"
9450+
a::after:hover, a::after:focus-visible {
9451+
color: red;
9452+
}
9453+
"#,
9454+
indoc! {r#"
9455+
a:after:hover {
9456+
color: red;
9457+
}
9458+
9459+
a:after:focus-visible {
9460+
color: red;
9461+
}
9462+
"#},
9463+
Browsers {
9464+
safari: Some(14 << 16),
9465+
..Browsers::default()
9466+
},
9467+
);
9468+
9469+
prefix_test(
9470+
r#"
9471+
a:not(:hover), a:not(:focus-visible) {
9472+
color: red;
9473+
}
9474+
"#,
9475+
indoc! {r#"
9476+
:is(a:not(:hover), a:not(:focus-visible)) {
9477+
color: red;
9478+
}
9479+
"#},
9480+
Browsers {
9481+
safari: Some(14 << 16),
9482+
..Browsers::default()
9483+
},
9484+
);
9485+
9486+
prefix_test(
9487+
r#"
9488+
a:has(:hover), a:has(:focus-visible) {
9489+
color: red;
9490+
}
9491+
"#,
9492+
indoc! {r#"
9493+
:is(a:has(:hover), a:has(:focus-visible)) {
9494+
color: red;
9495+
}
9496+
"#},
9497+
Browsers {
9498+
safari: Some(14 << 16),
9499+
..Browsers::default()
9500+
},
9501+
);
93389502
}
93399503

93409504
#[test]

src/rules/container.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<'i> ToCss for ContainerName<'i> {
271271
}
272272
}
273273

274-
impl<'i, T> ContainerRule<'i, T> {
274+
impl<'i, T: Clone> ContainerRule<'i, T> {
275275
pub(crate) fn minify(
276276
&mut self,
277277
context: &mut MinifyContext<'_, 'i>,

src/rules/document.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct MozDocumentRule<'i, R = DefaultAtRule> {
2626
pub loc: Location,
2727
}
2828

29-
impl<'i, T> MozDocumentRule<'i, T> {
29+
impl<'i, T: Clone> MozDocumentRule<'i, T> {
3030
pub(crate) fn minify(&mut self, context: &mut MinifyContext<'_, 'i>) -> Result<(), MinifyError> {
3131
self.rules.minify(context, false)
3232
}

src/rules/layer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub struct LayerBlockRule<'i, R = DefaultAtRule> {
128128
pub loc: Location,
129129
}
130130

131-
impl<'i, T> LayerBlockRule<'i, T> {
131+
impl<'i, T: Clone> LayerBlockRule<'i, T> {
132132
pub(crate) fn minify(
133133
&mut self,
134134
context: &mut MinifyContext<'_, 'i>,

src/rules/media.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct MediaRule<'i, R = DefaultAtRule> {
2626
pub loc: Location,
2727
}
2828

29-
impl<'i, T> MediaRule<'i, T> {
29+
impl<'i, T: Clone> MediaRule<'i, T> {
3030
pub(crate) fn minify(
3131
&mut self,
3232
context: &mut MinifyContext<'_, 'i>,

0 commit comments

Comments
 (0)