Skip to content

Commit

Permalink
Merge @supports declarations with the same property (minus prefix) an…
Browse files Browse the repository at this point in the history
…d value
  • Loading branch information
devongovett committed Mar 15, 2024
1 parent e37b9ed commit 6bd2761
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
62 changes: 62 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13123,6 +13123,68 @@ mod tests {
..Default::default()
},
);
prefix_test(
r#"
@supports ((-webkit-backdrop-filter: blur(10px)) or (backdrop-filter: blur(10px))) {
div {
backdrop-filter: blur(10px);
}
}
"#,
indoc! { r#"
@supports ((-webkit-backdrop-filter: blur(10px)) or (backdrop-filter: blur(10px))) {
div {
-webkit-backdrop-filter: blur(10px);
backdrop-filter: blur(10px);
}
}
"#},
Browsers {
safari: Some(14 << 16),
..Default::default()
},
);
prefix_test(
r#"
@supports ((-webkit-backdrop-filter: blur(20px)) or (backdrop-filter: blur(10px))) {
div {
backdrop-filter: blur(10px);
}
}
"#,
indoc! { r#"
@supports ((-webkit-backdrop-filter: blur(20px))) or ((-webkit-backdrop-filter: blur(10px)) or (backdrop-filter: blur(10px))) {
div {
-webkit-backdrop-filter: blur(10px);
backdrop-filter: blur(10px);
}
}
"#},
Browsers {
safari: Some(14 << 16),
..Default::default()
},
);
prefix_test(
r#"
@supports ((-webkit-backdrop-filter: blur(10px)) or (backdrop-filter: blur(10px))) {
div {
backdrop-filter: blur(10px);
}
}
"#,
indoc! { r#"
@supports (backdrop-filter: blur(10px)) {
div {
backdrop-filter: blur(10px);
}
}
"#},
Browsers {
chrome: Some(80 << 16),
..Default::default()
},
);
minify_test(
r#"
@supports (width: calc(10px * 2)) {
Expand Down
22 changes: 21 additions & 1 deletion src/properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ macro_rules! define_properties {
}
}

fn with_prefix(&self, prefix: VendorPrefix) -> PropertyId<'i> {
pub(crate) fn with_prefix(&self, prefix: VendorPrefix) -> PropertyId<'i> {
use PropertyId::*;
match self {
$(
Expand All @@ -344,6 +344,26 @@ macro_rules! define_properties {
}
}

pub(crate) fn add_prefix(&mut self, prefix: VendorPrefix) {
use PropertyId::*;
match self {
$(
$(#[$meta])*
$property$((vp_name!($vp, p)))? => {
macro_rules! get_prefixed {
($v: ty) => {{
*p |= prefix;
}};
() => {{}};
}

get_prefixed!($($vp)?)
},
)+
_ => {}
}
}

pub(crate) fn set_prefixes_for_targets(&mut self, targets: Targets) {
match self {
$(
Expand Down
33 changes: 31 additions & 2 deletions src/rules/supports.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! The `@supports` rule.

use std::collections::HashMap;

use super::Location;
use super::{CssRuleList, MinifyContext};
use crate::error::{MinifyError, ParserError, PrinterError};
Expand Down Expand Up @@ -159,6 +161,7 @@ impl<'i> Parse<'i> for SupportsCondition<'i> {
let in_parens = Self::parse_in_parens(input)?;
let mut expected_type = None;
let mut conditions = Vec::new();
let mut seen_declarations = HashMap::new();

loop {
let condition = input.try_parse(|input| {
Expand All @@ -185,14 +188,40 @@ impl<'i> Parse<'i> for SupportsCondition<'i> {

if let Ok(condition) = condition {
if conditions.is_empty() {
conditions.push(in_parens.clone())
conditions.push(in_parens.clone());
if let SupportsCondition::Declaration { property_id, value } = &in_parens {
seen_declarations.insert((property_id.with_prefix(VendorPrefix::None), value.clone()), 0);
}
}

if let SupportsCondition::Declaration { property_id, value } = condition {
// Merge multiple declarations with the same property id (minus prefix) and value together.
let property_id = property_id.with_prefix(VendorPrefix::None);
let key = (property_id.clone(), value.clone());
if let Some(index) = seen_declarations.get(&key) {
if let SupportsCondition::Declaration {
property_id: cur_property,
..
} = &mut conditions[*index]
{
cur_property.add_prefix(property_id.prefix());
}
} else {
seen_declarations.insert(key, conditions.len());
conditions.push(SupportsCondition::Declaration { property_id, value });
}
} else {
conditions.push(condition);
}
conditions.push(condition)
} else {
break;
}
}

if conditions.len() == 1 {
return Ok(conditions.pop().unwrap());
}

match expected_type {
Some(1) => Ok(SupportsCondition::And(conditions)),
Some(2) => Ok(SupportsCondition::Or(conditions)),
Expand Down

0 comments on commit 6bd2761

Please sign in to comment.