Skip to content

Remove the first rule if it’s @charset in "parse a stylesheet" #107

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

Merged
merged 1 commit into from
Aug 23, 2016
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "cssparser"
version = "0.5.7"
version = "0.5.8"
authors = [ "Simon Sapin <[email protected]>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
5 changes: 5 additions & 0 deletions src/css-parsing-tests/rule_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

"@foo", [["at-rule", "foo", [], null]],

"@charset; @foo", [
["at-rule", "charset", [], null],
["at-rule", "foo", [], null]
],

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

" /**/ @foo bar{[(4", [["at-rule", "foo",
Expand Down
7 changes: 7 additions & 0 deletions src/css-parsing-tests/stylesheet.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

"@foo", [["at-rule", "foo", [], null]],

"@charset 4 {} @foo", [["at-rule", "foo", [], null]],

"@foo; @charset 4 {}", [
["at-rule", "foo", [], null],
["at-rule", "charset", [" ", ["number", "4", 4, "integer"], " "], []]
],

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

" /**/ @foo bar{[(4", [["at-rule", "foo",
Expand Down
36 changes: 12 additions & 24 deletions src/css-parsing-tests/stylesheet_bytes.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,27 @@


{"css_bytes": "@charset \"ISO-8859-5\"; @\u00E9"},
[[["at-rule", "charset", [" ", ["string", "ISO-8859-5"]], null],
["at-rule", "щ", [], null]],
[[["at-rule", "щ", [], null]],
"iso-8859-5"],

{"css_bytes": "@Charset \"ISO-8859-5\"; @\u00E9",
"comment": "@charset has to match an exact byte pattern"},
[[["at-rule", "Charset", [" ", ["string", "ISO-8859-5"]], null],
["at-rule", "�", [], null]],
[[["at-rule", "�", [], null]],
"utf-8"],

{"css_bytes": "@charset \"ISO-8859-5\"; @\u00E9",
"comment": "@charset has to match an exact byte pattern"},
[[["at-rule", "charset", [" ", ["string", "ISO-8859-5"]], null],
["at-rule", "�", [], null]],
[[["at-rule", "�", [], null]],
"utf-8"],

{"css_bytes": "@charset 'ISO-8859-5'; @\u00E9",
"comment": "@charset has to match an exact byte pattern"},
[[["at-rule", "charset", [" ", ["string", "ISO-8859-5"]], null],
["at-rule", "�", [], null]],
[[["at-rule", "�", [], null]],
"utf-8"],

{"css_bytes": "@charset \"ISO-8859-5\" ; @\u00E9",
"comment": "@charset has to match an exact byte pattern"},
[[["at-rule", "charset", [" ", ["string", "ISO-8859-5"], " "], null],
["at-rule", "�", [], null]],
[[["at-rule", "�", [], null]],
"utf-8"],


Expand All @@ -79,35 +74,30 @@

{"css_bytes": "@charset \"UTF-16LE\"; @\u00C3\u00A9",
"comment": "@charset can only specify ASCII-compatible encodings"},
[[["at-rule", "charset", [" ", ["string", "UTF-16LE"]], null],
["at-rule", "é", [], null]],
[[["at-rule", "é", [], null]],
"utf-8"],


{"css_bytes": "\u00EF\u00BB\u00BF @charset \"ISO-8859-5\"; @\u00E9",
"comment": "BOM takes precedence over @charset"},
[[["at-rule", "charset", [" ", ["string", "ISO-8859-5"]], null],
["at-rule", "�", [], null]],
[[["at-rule", "�", [], null]],
"utf-8"],

{"css_bytes": "\u00EF\u00BB\u00BF @charset \"ISO-8859-5\"; @\u00C3\u00A9",
"comment": "BOM takes precedence over @charset"},
[[["at-rule", "charset", [" ", ["string", "ISO-8859-5"]], null],
["at-rule", "é", [], null]],
[[["at-rule", "é", [], null]],
"utf-8"],

{"css_bytes": "@charset \"ISO-8859-5\"; @\u00E9",
"protocol_encoding": " Iso-8859-2",
"comment": "Protocol takes precedence over @charset"},
[[["at-rule", "charset", [" ", ["string", "ISO-8859-5"]], null],
["at-rule", "é", [], null]],
[[["at-rule", "é", [], null]],
"iso-8859-2"],

{"css_bytes": "@charset \"ISO-8859-5\"; @\u00E9",
"protocol_encoding": "kamoulox",
"comment": "Unknow protocol encoding falls back to @charset"},
[[["at-rule", "charset", [" ", ["string", "ISO-8859-5"]], null],
["at-rule", "щ", [], null]],
[[["at-rule", "щ", [], null]],
"iso-8859-5"],


Expand All @@ -120,15 +110,13 @@
{"css_bytes": "@charset \"ISO-8859-5\"; @\u00E9",
"environment_encoding": "ISO-8859-2",
"comment": "@character takes precedence over environment"},
[[["at-rule", "charset", [" ", ["string", "ISO-8859-5"]], null],
["at-rule", "щ", [], null]],
[[["at-rule", "щ", [], null]],
"iso-8859-5"],

{"css_bytes": "@charset \"kamoulox\"; @\u00E9",
"environment_encoding": "ISO-8859-2",
"comment": "@character with unknown encoding falls back to environment encoding"},
[[["at-rule", "charset", [" ", ["string", "kamoulox"]], null],
["at-rule", "é", [], null]],
[[["at-rule", "é", [], null]],
"iso-8859-2"],

{"css_bytes": "@\u00E9",
Expand Down
3 changes: 3 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ impl<'i, 't> Parser<'i, 't> {
if next_byte.is_some() && !self.stop_before.contains(Delimiters::from_byte(next_byte)) {
debug_assert!(delimiters.contains(Delimiters::from_byte(next_byte)));
self.tokenizer.advance(1);
if next_byte == Some(b'{') {
consume_until_end_of_block(BlockType::CurlyBracket, &mut *self.tokenizer);
}
}
result
}
Expand Down
14 changes: 13 additions & 1 deletion src/rules_and_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// https://drafts.csswg.org/css-syntax/#parsing

use std::ascii::AsciiExt;
use std::ops::Range;
use std::borrow::Cow;
use super::{Token, Parser, Delimiter, SourcePosition};
Expand Down Expand Up @@ -264,6 +265,7 @@ where P: QualifiedRuleParser<QualifiedRule = R> + AtRuleParser<AtRule = R> {
pub parser: P,

is_stylesheet: bool,
any_rule_so_far: bool,
}


Expand All @@ -285,6 +287,7 @@ where P: QualifiedRuleParser<QualifiedRule = R> + AtRuleParser<AtRule = R> {
input: input,
parser: parser,
is_stylesheet: true,
any_rule_so_far: false,
}
}

Expand All @@ -300,6 +303,7 @@ where P: QualifiedRuleParser<QualifiedRule = R> + AtRuleParser<AtRule = R> {
input: input,
parser: parser,
is_stylesheet: false,
any_rule_so_far: false,
}
}
}
Expand All @@ -318,9 +322,17 @@ where P: QualifiedRuleParser<QualifiedRule = R> + AtRuleParser<AtRule = R> {
Ok(Token::WhiteSpace(_)) | Ok(Token::Comment(_)) => {}
Ok(Token::CDO) | Ok(Token::CDC) if self.is_stylesheet => {}
Ok(Token::AtKeyword(name)) => {
return Some(parse_at_rule(start_position, name, self.input, &mut self.parser))
let first_stylesheet_rule = self.is_stylesheet && !self.any_rule_so_far;
self.any_rule_so_far = true;
if first_stylesheet_rule && name.eq_ignore_ascii_case("charset") {
let delimiters = Delimiter::Semicolon | Delimiter::CurlyBracketBlock;
let _ = self.input.parse_until_after(delimiters, |_input| Ok(()));
} else {
return Some(parse_at_rule(start_position, name, self.input, &mut self.parser))
}
}
Ok(_) => {
self.any_rule_so_far = true;
self.input.reset(start_position);
return Some(parse_qualified_rule(self.input, &mut self.parser)
.map_err(|()| start_position..self.input.position()))
Expand Down