Skip to content

Commit

Permalink
fix(biome_css_parser): fixed css @document parser error (#4350)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryue0220 authored Oct 23, 2024
1 parent e3e37ab commit d59d43d
Show file tree
Hide file tree
Showing 8 changed files with 360 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

### Analyzer

#### Bug fixes

- Fix [#4258](https://github.com/biomejs/biome/issues/4258), where fixed css parse error with @-moz-document url-prefix(). Contributed by @eryue0220

### CLI

### Configuration
Expand Down
42 changes: 31 additions & 11 deletions crates/biome_css_factory/src/generated/node_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion crates/biome_css_parser/src/syntax/at_rule/document.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::lexer::CssLexContext;
use crate::parser::CssParser;
use crate::syntax::at_rule::parse_error::expected_any_document_matcher;
use crate::syntax::block::parse_rule_block;
use crate::syntax::parse_error::expected_string;
use crate::syntax::parse_string;
use crate::syntax::value::url::{is_at_url_function, parse_url_function};
use crate::syntax::value::url::{is_at_url_function, parse_url_function, parse_url_value};
use biome_css_syntax::CssSyntaxKind::*;
use biome_css_syntax::{CssSyntaxKind, T};
use biome_parser::parse_lists::ParseSeparatedList;
Expand Down Expand Up @@ -158,6 +159,12 @@ pub(crate) fn is_at_document_custom_matcher(p: &mut CssParser) -> bool {
p.at_ts(DOCUMENT_CUSTOM_MATCHER_SET) && p.nth_at(1, T!['('])
}

const URL_PREFIX_SET: TokenSet<CssSyntaxKind> = token_set!(T![url_prefix]);

pub(crate) fn is_at_url_prefix(p: &mut CssParser) -> bool {
p.at_ts(URL_PREFIX_SET) && p.nth_at(1, T!['('])
}

/// Parses a custom matcher for the `@document` at-rule in a CSS stylesheet.
/// # Example
/// Basic usage in CSS:
Expand All @@ -177,6 +184,14 @@ pub(crate) fn parse_document_custom_matcher(p: &mut CssParser) -> ParsedSyntax {

let m = p.start();

if is_at_url_prefix(p) {
p.bump_ts(URL_PREFIX_SET);
p.bump_with_context(T!['('], CssLexContext::UrlRawValue);
parse_url_value(p).ok();
p.expect(T![')']);
return Present(m.complete(p, CSS_DOCUMENT_CUSTOM_MATCHER));
}

p.bump_ts(DOCUMENT_CUSTOM_MATCHER_SET);
p.bump(T!['(']);
parse_string(p).or_add_diagnostic(p, expected_string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@
}
}
}

@-moz-document url-prefix() {}

@-moz-document url-prefix("https://www.example.com/") {}

@-moz-document url-prefix() {
body {
background-color: green;
}
}

@-moz-document url-prefix("https://www.example.com/") {
body {
background-color: green;
}
}
Loading

0 comments on commit d59d43d

Please sign in to comment.