Skip to content

Commit 76bf7c4

Browse files
committed
Have at-rule without block handled in two stages as well
1 parent ff1ba7d commit 76bf7c4

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/rules_and_declarations.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,16 @@ pub fn parse_important<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(), BasicPa
2323
/// The return value for `AtRuleParser::parse_prelude`.
2424
/// Indicates whether the at-rule is expected to have a `{ /* ... */ }` block
2525
/// or end with a `;` semicolon.
26-
pub enum AtRuleType<P, R> {
26+
pub enum AtRuleType<P> {
2727
/// The at-rule is expected to end with a `;` semicolon. Example: `@import`.
28-
///
29-
/// The value is the finished representation of an at-rule
30-
/// as returned by `RuleListParser::next` or `DeclarationListParser::next`.
31-
WithoutBlock(R),
28+
WithoutBlock(P),
3229

3330
/// The at-rule is expected to have a a `{ /* ... */ }` block. Example: `@media`
34-
///
35-
/// The value is the representation of the "prelude" part of the rule.
3631
WithBlock(P),
3732

3833
/// The at-rule may either have a block or end with a semicolon.
3934
///
4035
/// 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.
4336
OptionalBlock(P),
4437
}
4538

@@ -112,7 +105,7 @@ pub trait AtRuleParser<'i> {
112105
/// that ends wherever the prelude should end.
113106
/// (Before the next semicolon, the next `{`, or the end of the current block.)
114107
fn parse_prelude<'t>(&mut self, name: CowRcStr<'i>, input: &mut Parser<'i, 't>)
115-
-> Result<AtRuleType<Self::Prelude, Self::AtRule>, ParseError<'i, Self::Error>> {
108+
-> Result<AtRuleType<Self::Prelude>, ParseError<'i, Self::Error>> {
116109
let _ = name;
117110
let _ = input;
118111
Err(ParseError::Basic(BasicParseError::AtRuleInvalid(name)))
@@ -133,14 +126,16 @@ pub trait AtRuleParser<'i> {
133126
Err(ParseError::Basic(BasicParseError::AtRuleBodyInvalid))
134127
}
135128

136-
/// An `OptionalBlock` prelude was followed by `;`.
137-
///
138129
/// Convert the prelude into the finished representation of the at-rule
139130
/// as returned by `RuleListParser::next` or `DeclarationListParser::next`.
131+
///
132+
/// This is only called when `parse_prelude` returned `WithoutBlock` or `OptionalBlock`,
133+
/// and there is no block found following the prelude.
140134
fn rule_without_block(&mut self, prelude: Self::Prelude) -> Self::AtRule {
141135
let _ = prelude;
142136
panic!("The `AtRuleParser::rule_without_block` method must be overriden \
143-
if `AtRuleParser::parse_prelude` ever returns `AtRuleType::OptionalBlock`.")
137+
if `AtRuleParser::parse_prelude` ever returns `AtRuleType::WithoutBlock` \
138+
or `AtRuleType::OptionalBlock`.")
144139
}
145140
}
146141

@@ -440,9 +435,9 @@ fn parse_at_rule<'i: 't, 't, P, E>(start: &ParserState, name: CowRcStr<'i>,
440435
parser.parse_prelude(name, input)
441436
});
442437
match result {
443-
Ok(AtRuleType::WithoutBlock(rule)) => {
438+
Ok(AtRuleType::WithoutBlock(prelude)) => {
444439
match input.next() {
445-
Ok(&Token::Semicolon) | Err(_) => Ok(rule),
440+
Ok(&Token::Semicolon) | Err(_) => Ok(parser.rule_without_block(prelude)),
446441
Ok(&Token::CurlyBracketBlock) => Err(PreciseParseError {
447442
error: ParseError::Basic(BasicParseError::UnexpectedToken(Token::CurlyBracketBlock)),
448443
slice: input.slice_from(start.position()),

src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ impl<'i> AtRuleParser<'i> for JsonParser {
750750
type Error = ();
751751

752752
fn parse_prelude<'t>(&mut self, name: CowRcStr<'i>, input: &mut Parser<'i, 't>)
753-
-> Result<AtRuleType<Vec<Json>, Json>, ParseError<'i, ()>> {
753+
-> Result<AtRuleType<Vec<Json>>, ParseError<'i, ()>> {
754754
Ok(AtRuleType::OptionalBlock(vec![
755755
"at-rule".to_json(),
756756
name.to_json(),

0 commit comments

Comments
 (0)