LibWeb/CSS: Expose method for parsing a type of StyleValue

In a few places, user code wants to parse a `<color>` or `<length>` etc,
but we didn't have a way to do so, so they would do something
similar-ish instead, like parse the value of the `color` property.
Let's make that available instead.
This commit is contained in:
Sam Atkins 2025-11-13 14:04:00 +00:00
parent fd559341e3
commit 7aef3245ea
Notes: github-actions[bot] 2025-11-14 09:56:40 +00:00
3 changed files with 16 additions and 0 deletions

View file

@ -80,6 +80,13 @@ RefPtr<CSS::StyleValue const> parse_css_value(CSS::Parser::ParsingParams const&
return CSS::Parser::Parser::create(context, string).parse_as_css_value(property_id);
}
RefPtr<CSS::StyleValue const> parse_css_type(CSS::Parser::ParsingParams const& context, StringView string, CSS::ValueType value_type)
{
if (string.is_empty())
return nullptr;
return CSS::Parser::Parser::create(context, string).parse_as_type(value_type);
}
RefPtr<CSS::StyleValue const> parse_css_descriptor(CSS::Parser::ParsingParams const& parsing_params, CSS::AtRuleID at_rule_id, CSS::DescriptorID descriptor_id, StringView string)
{
if (string.is_empty())

View file

@ -1742,6 +1742,13 @@ RefPtr<StyleValue const> Parser::parse_as_descriptor_value(AtRuleID at_rule_id,
return parsed_value.release_value();
}
RefPtr<StyleValue const> Parser::parse_as_type(ValueType value_type)
{
auto component_values = parse_a_list_of_component_values(m_token_stream);
TokenStream tokens { component_values };
return parse_value(value_type, tokens);
}
// https://html.spec.whatwg.org/multipage/images.html#parsing-a-sizes-attribute
LengthOrCalculated Parser::parse_as_sizes_attribute(DOM::Element const& element, HTML::HTMLImageElement const* img)
{

View file

@ -151,6 +151,7 @@ public:
RefPtr<StyleValue const> parse_as_css_value(PropertyID);
RefPtr<StyleValue const> parse_as_descriptor_value(AtRuleID, DescriptorID);
RefPtr<StyleValue const> parse_as_type(ValueType);
Optional<ComponentValue> parse_as_component_value();
@ -604,6 +605,7 @@ GC::Ref<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::Parser::ParsingParams cons
CSS::Parser::Parser::PropertiesAndCustomProperties parse_css_property_declaration_block(CSS::Parser::ParsingParams const&, StringView);
Vector<CSS::Descriptor> parse_css_descriptor_declaration_block(CSS::Parser::ParsingParams const&, CSS::AtRuleID, StringView);
RefPtr<CSS::StyleValue const> parse_css_value(CSS::Parser::ParsingParams const&, StringView, CSS::PropertyID);
RefPtr<CSS::StyleValue const> parse_css_type(CSS::Parser::ParsingParams const&, StringView, CSS::ValueType);
RefPtr<CSS::StyleValue const> parse_css_descriptor(CSS::Parser::ParsingParams const&, CSS::AtRuleID, CSS::DescriptorID, StringView);
Optional<CSS::SelectorList> parse_selector(CSS::Parser::ParsingParams const&, StringView);
Optional<CSS::SelectorList> parse_selector_for_nested_style_rule(CSS::Parser::ParsingParams const&, StringView);