Skip to content

Commit 7635551

Browse files
committed
Remove AtRuleType::OptionalBlock
1 parent cf3d20d commit 7635551

File tree

5 files changed

+30
-50
lines changed

5 files changed

+30
-50
lines changed

src/css-parsing-tests/one_rule.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
"@foo", ["at-rule", "foo", [], null],
88

99
"@foo bar; \t/* comment */", ["at-rule", "foo", [" ", ["ident", "bar"]], null],
10-
" /**/ @foo bar{[(4", ["at-rule", "foo",
10+
" /**/ @foo-with-block bar{[(4", ["at-rule", "foo-with-block",
1111
[" ", ["ident", "bar"]],
1212
[["[]", ["()", ["number", "4", 4, "integer"]]]]
1313
],
1414

15-
"@foo { bar", ["at-rule", "foo", [" "], [" ", ["ident", "bar"]]],
16-
"@foo [ bar", ["at-rule", "foo", [" ", ["[]", " ", ["ident", "bar"]]], null],
15+
"@foo-with-block { bar", ["at-rule", "foo-with-block", [" "], [
16+
" ", ["ident", "bar"]]
17+
],
18+
"@foo [ bar", ["at-rule", "foo",
19+
[" ", ["[]", " ", ["ident", "bar"]]], null
20+
],
1721

1822
" /**/ div > p { color: #aaa; } /**/ ", ["qualified rule",
1923
[["ident", "div"], " ", ">", " ", ["ident", "p"], " "],

src/css-parsing-tests/rule_list.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
"@foo", [["at-rule", "foo", [], null]],
88

99
"@charset; @foo", [
10-
["at-rule", "charset", [], null],
10+
["error", "invalid"],
1111
["at-rule", "foo", [], null]
1212
],
1313

1414
"@foo bar; \t/* comment */", [["at-rule", "foo", [" ", ["ident", "bar"]], null]],
1515

16-
" /**/ @foo bar{[(4", [["at-rule", "foo",
16+
" /**/ @foo-with-block bar{[(4", [["at-rule", "foo-with-block",
1717
[" ", ["ident", "bar"]],
1818
[["[]", ["()", ["number", "4", 4, "integer"]]]]
1919
]],
2020

21-
"@foo { bar", [["at-rule", "foo", [" "], [" ", ["ident", "bar"]]]],
21+
"@foo-with-block { bar", [["at-rule", "foo-with-block", [" "], [
22+
" ", ["ident", "bar"]]]
23+
],
2224
"@foo [ bar", [["at-rule", "foo", [" ", ["[]", " ", ["ident", "bar"]]], null]],
2325

2426
" /**/ div > p { color: #aaa; } /**/ ", [["qualified rule",

src/css-parsing-tests/stylesheet.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@
1010

1111
"@foo; @charset 4 {}", [
1212
["at-rule", "foo", [], null],
13-
["at-rule", "charset", [" ", ["number", "4", 4, "integer"], " "], []]
13+
["error", "invalid"],
14+
["error", "invalid"]
1415
],
1516

1617
"@foo bar; \t/* comment */", [["at-rule", "foo", [" ", ["ident", "bar"]], null]],
1718

18-
" /**/ @foo bar{[(4", [["at-rule", "foo",
19+
" /**/ @foo-with-block bar{[(4", [["at-rule", "foo-with-block",
1920
[" ", ["ident", "bar"]],
2021
[["[]", ["()", ["number", "4", 4, "integer"]]]]
2122
]],
2223

23-
"@foo { bar", [["at-rule", "foo", [" "], [" ", ["ident", "bar"]]]],
24+
"@foo-with-block { bar", [["at-rule", "foo-with-block", [" "], [
25+
" ", ["ident", "bar"]]]
26+
],
2427
"@foo [ bar", [["at-rule", "foo", [" ", ["[]", " ", ["ident", "bar"]]], null]],
2528

2629
" /**/ div > p { color: #aaa; } /**/ ", [["qualified rule",

src/rules_and_declarations.rs

+2-34
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ pub enum AtRuleType<P, R> {
3434
///
3535
/// The value is the representation of the "prelude" part of the rule.
3636
WithBlock(P),
37-
38-
/// The at-rule may either have a block or end with a semicolon.
39-
///
40-
/// This is mostly for testing. As of this writing no real CSS at-rule behaves like this.
41-
///
42-
/// The value is the representation of the "prelude" part of the rule.
43-
OptionalBlock(P),
4437
}
4538

4639
/// A trait to provide various parsing of declaration values.
@@ -124,24 +117,14 @@ pub trait AtRuleParser<'i> {
124117
/// as returned by `RuleListParser::next` or `DeclarationListParser::next`,
125118
/// or `Err(())` to ignore the entire at-rule as invalid.
126119
///
127-
/// This is only called when `parse_prelude` returned `WithBlock` or `OptionalBlock`,
128-
/// and a block was indeed found following the prelude.
120+
/// This is only called when `parse_prelude` returned `WithBlock`, and a block
121+
/// was indeed found following the prelude.
129122
fn parse_block<'t>(&mut self, prelude: Self::Prelude, input: &mut Parser<'i, 't>)
130123
-> Result<Self::AtRule, ParseError<'i, Self::Error>> {
131124
let _ = prelude;
132125
let _ = input;
133126
Err(ParseError::Basic(BasicParseError::AtRuleBodyInvalid))
134127
}
135-
136-
/// An `OptionalBlock` prelude was followed by `;`.
137-
///
138-
/// Convert the prelude into the finished representation of the at-rule
139-
/// as returned by `RuleListParser::next` or `DeclarationListParser::next`.
140-
fn rule_without_block(&mut self, prelude: Self::Prelude) -> Self::AtRule {
141-
let _ = prelude;
142-
panic!("The `AtRuleParser::rule_without_block` method must be overriden \
143-
if `AtRuleParser::parse_prelude` ever returns `AtRuleType::OptionalBlock`.")
144-
}
145128
}
146129

147130
/// A trait to provide various parsing of qualified rules.
@@ -495,21 +478,6 @@ fn parse_at_rule<'i: 't, 't, P, E>(start: &ParserState, name: CowRcStr<'i>,
495478
Ok(_) => unreachable!()
496479
}
497480
}
498-
Ok(AtRuleType::OptionalBlock(prelude)) => {
499-
match input.next() {
500-
Ok(&Token::Semicolon) | Err(_) => Ok(parser.rule_without_block(prelude)),
501-
Ok(&Token::CurlyBracketBlock) => {
502-
// FIXME: https://github.com/rust-lang/rust/issues/42508
503-
parse_nested_block::<'i, 't, _, _, _>(input, move |input| parser.parse_block(prelude, input))
504-
.map_err(|e| PreciseParseError {
505-
error: e,
506-
slice: input.slice_from(start.position()),
507-
location: start.source_location(),
508-
})
509-
}
510-
_ => unreachable!()
511-
}
512-
}
513481
Err(error) => {
514482
let end_position = input.position();
515483
match input.next() {

src/tests.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -751,23 +751,26 @@ impl<'i> AtRuleParser<'i> for JsonParser {
751751

752752
fn parse_prelude<'t>(&mut self, name: CowRcStr<'i>, input: &mut Parser<'i, 't>)
753753
-> Result<AtRuleType<Vec<Json>, Json>, ParseError<'i, ()>> {
754-
Ok(AtRuleType::OptionalBlock(vec![
754+
let mut prelude = vec![
755755
"at-rule".to_json(),
756756
name.to_json(),
757757
Json::Array(component_values_to_json(input)),
758-
]))
758+
];
759+
match_ignore_ascii_case! { &*name,
760+
"media" | "foo-with-block" => Ok(AtRuleType::WithBlock(prelude)),
761+
"charset" => Err(BasicParseError::AtRuleInvalid(name.clone()).into()),
762+
_ => {
763+
prelude.push(Json::Null);
764+
Ok(AtRuleType::WithoutBlock(Json::Array(prelude)))
765+
}
766+
}
759767
}
760768

761769
fn parse_block<'t>(&mut self, mut prelude: Vec<Json>, input: &mut Parser<'i, 't>)
762770
-> Result<Json, ParseError<'i, ()>> {
763771
prelude.push(Json::Array(component_values_to_json(input)));
764772
Ok(Json::Array(prelude))
765773
}
766-
767-
fn rule_without_block(&mut self, mut prelude: Vec<Json>) -> Json {
768-
prelude.push(Json::Null);
769-
Json::Array(prelude)
770-
}
771774
}
772775

773776
impl<'i> QualifiedRuleParser<'i> for JsonParser {

0 commit comments

Comments
 (0)