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

fix(biome_css_parser): fixed css @document parser error #4342

Closed
wants to merge 2 commits into from
Closed
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
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
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/biome_css_parser/tests/spec_test.rs
assertion_line: 169
expression: snapshot
---
## Input
Expand Down Expand Up @@ -30,6 +31,22 @@ expression: snapshot
}
}

@-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;
}
}

```


Expand Down Expand Up @@ -385,17 +402,173 @@ CssRoot {
},
},
},
CssAtRule {
at_token: [email protected] "@" [Newline("\n"), Newline("\n")] [],
rule: CssDocumentAtRule {
document_token: [email protected] "-moz-document" [] [Whitespace(" ")],
matchers: CssDocumentMatcherList [
CssDocumentCustomMatcher {
name: [email protected] "url-prefix" [] [],
l_paren_token: [email protected] "(" [] [],
value: missing (optional),
r_paren_token: [email protected] ")" [] [Whitespace(" ")],
},
],
block: CssRuleBlock {
l_curly_token: [email protected] "{" [] [],
rules: CssRuleList [],
r_curly_token: [email protected] "}" [] [],
},
},
},
CssAtRule {
at_token: [email protected] "@" [Newline("\n"), Newline("\n")] [],
rule: CssDocumentAtRule {
document_token: [email protected] "-moz-document" [] [Whitespace(" ")],
matchers: CssDocumentMatcherList [
CssDocumentCustomMatcher {
name: [email protected] "url-prefix" [] [],
l_paren_token: [email protected] "(" [] [],
value: CssString {
value_token: [email protected] "\"https://www.example.com/\"" [] [],
},
r_paren_token: [email protected] ")" [] [Whitespace(" ")],
},
],
block: CssRuleBlock {
l_curly_token: [email protected] "{" [] [],
rules: CssRuleList [],
r_curly_token: [email protected] "}" [] [],
},
},
},
CssAtRule {
at_token: [email protected] "@" [Newline("\n"), Newline("\n")] [],
rule: CssDocumentAtRule {
document_token: [email protected] "-moz-document" [] [Whitespace(" ")],
matchers: CssDocumentMatcherList [
CssDocumentCustomMatcher {
name: [email protected] "url-prefix" [] [],
l_paren_token: [email protected] "(" [] [],
value: missing (optional),
r_paren_token: [email protected] ")" [] [Whitespace(" ")],
},
],
block: CssRuleBlock {
l_curly_token: [email protected] "{" [] [],
rules: CssRuleList [
CssQualifiedRule {
prelude: CssSelectorList [
CssCompoundSelector {
nesting_selectors: CssNestedSelectorList [],
simple_selector: CssTypeSelector {
namespace: missing (optional),
ident: CssIdentifier {
value_token: [email protected] "body" [Newline("\n"), Whitespace("\t")] [Whitespace(" ")],
},
},
sub_selectors: CssSubSelectorList [],
},
],
block: CssDeclarationOrRuleBlock {
l_curly_token: [email protected] "{" [] [],
items: CssDeclarationOrRuleList [
CssDeclarationWithSemicolon {
declaration: CssDeclaration {
property: CssGenericProperty {
name: CssIdentifier {
value_token: [email protected] "background-color" [Newline("\n"), Whitespace("\t\t")] [],
},
colon_token: [email protected] ":" [] [Whitespace(" ")],
value: CssGenericComponentValueList [
CssIdentifier {
value_token: [email protected] "green" [] [],
},
],
},
important: missing (optional),
},
semicolon_token: [email protected] ";" [] [],
},
],
r_curly_token: [email protected] "}" [Newline("\n"), Whitespace("\t")] [],
},
},
],
r_curly_token: [email protected] "}" [Newline("\n")] [],
},
},
},
CssAtRule {
at_token: [email protected] "@" [Newline("\n"), Newline("\n")] [],
rule: CssDocumentAtRule {
document_token: [email protected] "-moz-document" [] [Whitespace(" ")],
matchers: CssDocumentMatcherList [
CssDocumentCustomMatcher {
name: [email protected] "url-prefix" [] [],
l_paren_token: [email protected] "(" [] [],
value: CssString {
value_token: [email protected] "\"https://www.example.com/\"" [] [],
},
r_paren_token: [email protected] ")" [] [Whitespace(" ")],
},
],
block: CssRuleBlock {
l_curly_token: [email protected] "{" [] [],
rules: CssRuleList [
CssQualifiedRule {
prelude: CssSelectorList [
CssCompoundSelector {
nesting_selectors: CssNestedSelectorList [],
simple_selector: CssTypeSelector {
namespace: missing (optional),
ident: CssIdentifier {
value_token: [email protected] "body" [Newline("\n"), Whitespace("\t")] [Whitespace(" ")],
},
},
sub_selectors: CssSubSelectorList [],
},
],
block: CssDeclarationOrRuleBlock {
l_curly_token: [email protected] "{" [] [],
items: CssDeclarationOrRuleList [
CssDeclarationWithSemicolon {
declaration: CssDeclaration {
property: CssGenericProperty {
name: CssIdentifier {
value_token: [email protected] "background-color" [Newline("\n"), Whitespace("\t\t")] [],
},
colon_token: [email protected] ":" [] [Whitespace(" ")],
value: CssGenericComponentValueList [
CssIdentifier {
value_token: [email protected] "green" [] [],
},
],
},
important: missing (optional),
},
semicolon_token: [email protected] ";" [] [],
},
],
r_curly_token: [email protected] "}" [Newline("\n"), Whitespace("\t")] [],
},
},
],
r_curly_token: [email protected] "}" [Newline("\n")] [],
},
},
},
],
eof_token: EOF@562..563 "" [Newline("\n")] [],
eof_token: EOF@820..821 "" [Newline("\n")] [],
}
```

## CST

```
0: CSS_ROOT@0..563
0: CSS_ROOT@0..821
0: (empty)
1: CSS_RULE_LIST@0..562
1: CSS_RULE_LIST@0..820
0: [email protected]
0: [email protected] "@" [] []
1: [email protected]
Expand Down Expand Up @@ -635,6 +808,112 @@ CssRoot {
2: [email protected] "}" [Newline("\n"), Whitespace("\t\t")] []
2: [email protected] "}" [Newline("\n"), Whitespace("\t")] []
2: [email protected] "}" [Newline("\n")] []
2: [email protected] "" [Newline("\n")] []
6: [email protected]
0: [email protected] "@" [Newline("\n"), Newline("\n")] []
1: [email protected]
0: [email protected] "-moz-document" [] [Whitespace(" ")]
1: [email protected]
0: [email protected]
0: [email protected] "url-prefix" [] []
1: [email protected] "(" [] []
2: (empty)
3: [email protected] ")" [] [Whitespace(" ")]
2: [email protected]
0: [email protected] "{" [] []
1: [email protected]
2: [email protected] "}" [] []
7: [email protected]
0: [email protected] "@" [Newline("\n"), Newline("\n")] []
1: [email protected]
0: [email protected] "-moz-document" [] [Whitespace(" ")]
1: [email protected]
0: [email protected]
0: [email protected] "url-prefix" [] []
1: [email protected] "(" [] []
2: [email protected]
0: [email protected] "\"https://www.example.com/\"" [] []
3: [email protected] ")" [] [Whitespace(" ")]
2: [email protected]
0: [email protected] "{" [] []
1: [email protected]
2: [email protected] "}" [] []
8: [email protected]
0: [email protected] "@" [Newline("\n"), Newline("\n")] []
1: [email protected]
0: [email protected] "-moz-document" [] [Whitespace(" ")]
1: [email protected]
0: [email protected]
0: [email protected] "url-prefix" [] []
1: [email protected] "(" [] []
2: (empty)
3: [email protected] ")" [] [Whitespace(" ")]
2: [email protected]
0: [email protected] "{" [] []
1: [email protected]
0: [email protected]
0: [email protected]
0: [email protected]
0: [email protected]
1: [email protected]
0: (empty)
1: [email protected]
0: [email protected] "body" [Newline("\n"), Whitespace("\t")] [Whitespace(" ")]
2: [email protected]
1: [email protected]
0: [email protected] "{" [] []
1: [email protected]
0: [email protected]
0: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "background-color" [Newline("\n"), Whitespace("\t\t")] []
1: [email protected] ":" [] [Whitespace(" ")]
2: [email protected]
0: [email protected]
0: [email protected] "green" [] []
1: (empty)
1: [email protected] ";" [] []
2: [email protected] "}" [Newline("\n"), Whitespace("\t")] []
2: [email protected] "}" [Newline("\n")] []
9: [email protected]
0: [email protected] "@" [Newline("\n"), Newline("\n")] []
1: [email protected]
0: [email protected] "-moz-document" [] [Whitespace(" ")]
1: [email protected]
0: [email protected]
0: [email protected] "url-prefix" [] []
1: [email protected] "(" [] []
2: [email protected]
0: [email protected] "\"https://www.example.com/\"" [] []
3: [email protected] ")" [] [Whitespace(" ")]
2: [email protected]
0: [email protected] "{" [] []
1: [email protected]
0: [email protected]
0: [email protected]
0: [email protected]
0: [email protected]
1: [email protected]
0: (empty)
1: [email protected]
0: [email protected] "body" [Newline("\n"), Whitespace("\t")] [Whitespace(" ")]
2: [email protected]
1: [email protected]
0: [email protected] "{" [] []
1: [email protected]
0: [email protected]
0: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "background-color" [Newline("\n"), Whitespace("\t\t")] []
1: [email protected] ":" [] [Whitespace(" ")]
2: [email protected]
0: [email protected]
0: [email protected] "green" [] []
1: (empty)
1: [email protected] ";" [] []
2: [email protected] "}" [Newline("\n"), Whitespace("\t")] []
2: [email protected] "}" [Newline("\n")] []
2: [email protected] "" [Newline("\n")] []

```
Loading
Loading