srctree

Robin Linden parent ccf8486b 74199108
css: Parse (and ignore) charset at-rules

These are in use on e.g. https://www.iana.org/help/example-domains andthe parser didn't like just running over them, causing valid CSS rulesafter the charset at-rule to be discarded due to the parser thinkingthey're nested rules.

inlinesplit
css/parser.cpp added: 26, removed: 2, total 24
@@ -266,6 +266,21 @@ StyleSheet Parser::parse_rules() {
 
skip_whitespace_and_comments();
while (!is_eof()) {
if (starts_with("@charset ")) {
advance(std::strlen("@charset"));
skip_whitespace_and_comments();
if (auto charset = consume_while([](char c) { return c != ';'; }); charset) {
spdlog::warn("Ignoring charset: {}", *charset);
} else {
spdlog::error("Eof while parsing charset");
return style;
}
 
consume_char(); // ;
skip_whitespace_and_comments();
continue;
}
 
if (starts_with("@media ") || starts_with("@media(")) {
advance(std::strlen("@media"));
skip_whitespace_and_comments();
 
css/parser_test.cpp added: 26, removed: 2, total 24
@@ -1248,5 +1248,14 @@ int main() {
std::map<css::PropertyId, std::string>{{css::PropertyId::Unknown, "3px"}});
});
 
etest::test("parser: @charset", [] {
expect_eq(css::parse("@charset 'shift-jis'; p { font-size: 3px; }").rules.at(0),
css::Rule{.selectors = {{"p"}}, .declarations{{css::PropertyId::FontSize, "3px"}}});
});
 
etest::test("parser: @charset eof", [] {
expect(css::parse("@charset 'shi").rules.empty()); //
});
 
return etest::run_all_tests();
}