LibWeb: Parse and serialize @counter-style rule

We don't yet parse or serialize any of the descriptors in the rule, just
the rule itself and the name
This commit is contained in:
Callum Law
2026-01-31 01:03:29 +13:00
committed by Sam Atkins
parent c7f288bf97
commit 703259a24c
Notes: github-actions[bot] 2026-02-06 10:38:18 +00:00
18 changed files with 187 additions and 21 deletions

View File

@@ -12,6 +12,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/CSSCounterStyleRule.h>
#include <LibWeb/CSS/CSSFontFaceRule.h>
#include <LibWeb/CSS/CSSImportRule.h>
#include <LibWeb/CSS/CSSKeyframeRule.h>
@@ -99,6 +100,9 @@ GC::Ptr<CSSRule> Parser::convert_to_rule(Rule const& rule, Nested nested)
if (has_ignored_vendor_prefix(at_rule.name))
return {};
if (at_rule.name.equals_ignoring_ascii_case("counter-style"sv))
return convert_to_counter_style_rule(at_rule);
if (at_rule.name.equals_ignoring_ascii_case("font-face"sv))
return convert_to_font_face_rule(at_rule);
@@ -814,6 +818,65 @@ GC::Ptr<CSSPropertyRule> Parser::convert_to_property_rule(AtRule const& rule)
return CSSPropertyRule::create(realm(), name, syntax_maybe.value(), inherits_maybe.value(), move(initial_value_maybe));
}
GC::Ptr<CSSCounterStyleRule> Parser::convert_to_counter_style_rule(AtRule const& rule)
{
// https://drafts.csswg.org/css-counter-styles-3/#the-counter-style-rule
TokenStream prelude_stream { rule.prelude };
if (!rule.is_block_rule) {
ErrorReporter::the().report(CSS::Parser::InvalidRuleError {
.rule_name = "@counter-style"_fly_string,
.prelude = prelude_stream.dump_string(),
.description = "Must be a block, not a statement."_string,
});
return nullptr;
}
if (rule.prelude.is_empty()) {
ErrorReporter::the().report(CSS::Parser::InvalidRuleError {
.rule_name = "@counter-style"_fly_string,
.prelude = prelude_stream.dump_string(),
.description = "Empty prelude."_string,
});
return nullptr;
}
auto name = parse_counter_style_name(prelude_stream);
if (!name.has_value()) {
ErrorReporter::the().report(CSS::Parser::InvalidRuleError {
.rule_name = "@counter-style"_fly_string,
.prelude = prelude_stream.dump_string(),
.description = "Missing counter style name."_string,
});
return nullptr;
}
prelude_stream.discard_whitespace();
if (prelude_stream.has_next_token()) {
ErrorReporter::the().report(CSS::Parser::InvalidRuleError {
.rule_name = "@counter-style"_fly_string,
.prelude = prelude_stream.dump_string(),
.description = "Trailing tokens after name in prelude."_string,
});
return nullptr;
}
// https://drafts.csswg.org/css-counter-styles-3/#typedef-counter-style-name
// When used here, to define a counter style, it also cannot be any of the non-overridable counter-style names
// FIXME: We should allow these in the UA stylesheet in order to initially define them.
if (CSSCounterStyleRule::matches_non_overridable_counter_style_name(name.value())) {
ErrorReporter::the().report(CSS::Parser::InvalidRuleError {
.rule_name = "@counter-style"_fly_string,
.prelude = prelude_stream.dump_string(),
.description = "Non-overridable counter style name."_string,
});
return nullptr;
}
// TODO: Parse descriptors
return CSSCounterStyleRule::create(realm(), name.release_value());
}
GC::Ptr<CSSFontFaceRule> Parser::convert_to_font_face_rule(AtRule const& rule)
{
// https://drafts.csswg.org/css-fonts/#font-face-rule