Skip to content

Commit 7aef324

Browse files
committed
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.
1 parent fd55934 commit 7aef324

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Libraries/LibWeb/CSS/Parser/Helpers.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ RefPtr<CSS::StyleValue const> parse_css_value(CSS::Parser::ParsingParams const&
8080
return CSS::Parser::Parser::create(context, string).parse_as_css_value(property_id);
8181
}
8282

83+
RefPtr<CSS::StyleValue const> parse_css_type(CSS::Parser::ParsingParams const& context, StringView string, CSS::ValueType value_type)
84+
{
85+
if (string.is_empty())
86+
return nullptr;
87+
return CSS::Parser::Parser::create(context, string).parse_as_type(value_type);
88+
}
89+
8390
RefPtr<CSS::StyleValue const> parse_css_descriptor(CSS::Parser::ParsingParams const& parsing_params, CSS::AtRuleID at_rule_id, CSS::DescriptorID descriptor_id, StringView string)
8491
{
8592
if (string.is_empty())

Libraries/LibWeb/CSS/Parser/Parser.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,13 @@ RefPtr<StyleValue const> Parser::parse_as_descriptor_value(AtRuleID at_rule_id,
17421742
return parsed_value.release_value();
17431743
}
17441744

1745+
RefPtr<StyleValue const> Parser::parse_as_type(ValueType value_type)
1746+
{
1747+
auto component_values = parse_a_list_of_component_values(m_token_stream);
1748+
TokenStream tokens { component_values };
1749+
return parse_value(value_type, tokens);
1750+
}
1751+
17451752
// https://html.spec.whatwg.org/multipage/images.html#parsing-a-sizes-attribute
17461753
LengthOrCalculated Parser::parse_as_sizes_attribute(DOM::Element const& element, HTML::HTMLImageElement const* img)
17471754
{

Libraries/LibWeb/CSS/Parser/Parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class Parser {
151151

152152
RefPtr<StyleValue const> parse_as_css_value(PropertyID);
153153
RefPtr<StyleValue const> parse_as_descriptor_value(AtRuleID, DescriptorID);
154+
RefPtr<StyleValue const> parse_as_type(ValueType);
154155

155156
Optional<ComponentValue> parse_as_component_value();
156157

@@ -604,6 +605,7 @@ GC::Ref<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::Parser::ParsingParams cons
604605
CSS::Parser::Parser::PropertiesAndCustomProperties parse_css_property_declaration_block(CSS::Parser::ParsingParams const&, StringView);
605606
Vector<CSS::Descriptor> parse_css_descriptor_declaration_block(CSS::Parser::ParsingParams const&, CSS::AtRuleID, StringView);
606607
RefPtr<CSS::StyleValue const> parse_css_value(CSS::Parser::ParsingParams const&, StringView, CSS::PropertyID);
608+
RefPtr<CSS::StyleValue const> parse_css_type(CSS::Parser::ParsingParams const&, StringView, CSS::ValueType);
607609
RefPtr<CSS::StyleValue const> parse_css_descriptor(CSS::Parser::ParsingParams const&, CSS::AtRuleID, CSS::DescriptorID, StringView);
608610
Optional<CSS::SelectorList> parse_selector(CSS::Parser::ParsingParams const&, StringView);
609611
Optional<CSS::SelectorList> parse_selector_for_nested_style_rule(CSS::Parser::ParsingParams const&, StringView);

0 commit comments

Comments
 (0)