Skip to content
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

Add @container support #89

Merged
merged 3 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions packages/stylist-core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ impl Parser {
/// Parses a Rule Block
fn rule_block(i: &str, kind: RuleBlockKind) -> IResult<&str, Rule, VerboseError<&str>> {
let cond = |i| match kind {
RuleBlockKind::Other => Self::at_rule_condition(i, (tag("@media"), tag("@supports"))),
RuleBlockKind::Other => {
Self::at_rule_condition(i, (tag("@container"), tag("@media"), tag("@supports")))
}
RuleBlockKind::Keyframes => map(recognize(Self::condition), |m| {
vec![m.trim().to_string().into()]
})(i),
Expand Down Expand Up @@ -459,7 +461,12 @@ impl Parser {
Self::trimmed(expect_non_empty(map(
separated_pair(
// Collect at Rules.
|i| Self::at_rule_condition(i, (tag("@supports"), tag("@media"))),
|i| {
Self::at_rule_condition(
i,
(tag("@container"), tag("@supports"), tag("@media")),
)
},
tag("{"),
// Collect contents with-in rules.
terminated(Parser::scope_contents, tag("}")),
Expand Down Expand Up @@ -859,6 +866,41 @@ mod tests {
Ok(())
}

#[test]
fn test_container_rule() -> Result<()> {
init();

let test_str = r#"
@container (min-width: 200px) {
background: #000;
}

"#;
let parsed = Parser::parse(test_str)?;
log::debug!("{:?}", parsed);

let expected = Sheet::from(vec![ScopeContent::Rule(Rule {
condition: vec!["@container ".into(), "(min-width: 200px)".into()].into(),
content: vec![RuleBlockContent::Block(
Block {
condition: Cow::Borrowed(&[]),
content: vec![StyleAttribute {
key: "background".into(),
value: vec!["#000".into()].into(),
}
.into()]
.into(),
}
.into(),
)]
.into(),
})]);

assert_eq!(parsed, expected);

Ok(())
}

#[test]
fn test_supports_rule() -> Result<()> {
init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ error[E0277]: `NoDisplay` doesn't implement `std::fmt::Display`
|
= help: the trait `std::fmt::Display` is not implemented for `NoDisplay`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required for the cast to the object type `dyn std::fmt::Display`
= note: required for the cast from `NoDisplay` to the object type `dyn std::fmt::Display`