summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/Parser
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-03-29 16:01:38 +0200
committerAndreas Kling <kling@serenityos.org>2022-03-29 16:35:46 +0200
commit427beb97b51be36e29797a76419405589b9f4cdd (patch)
tree63162361f45c9225387102a423679c313c283244 /Userland/Libraries/LibWeb/CSS/Parser
parent3efa6cedec027647a2e4d6fcb1408102a6b3a98c (diff)
downloadserenity-427beb97b51be36e29797a76419405589b9f4cdd.zip
LibWeb: Streamline how inline CSS style declarations are constructed
When parsing the "style" attribute on elements, we'd previously ask the CSS parser for a PropertyOwningCSSStyleDeclaration. Then we'd create a new ElementCSSInlineStyleDeclaration and transfer the properties from the first object to the second object. This patch teaches the parser to make ElementCSSInlineStyleDeclaration objects directly.
Diffstat (limited to 'Userland/Libraries/LibWeb/CSS/Parser')
-rw-r--r--Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp34
-rw-r--r--Userland/Libraries/LibWeb/CSS/Parser/Parser.h11
2 files changed, 27 insertions, 18 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
index e85c4c2991..fa52c3ad0b 100644
--- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
+++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
@@ -1997,10 +1997,11 @@ Vector<Vector<StyleComponentValueRule>> Parser::parse_a_comma_separated_list_of_
return lists;
}
-RefPtr<PropertyOwningCSSStyleDeclaration> Parser::parse_as_style_attribute()
+RefPtr<ElementInlineCSSStyleDeclaration> Parser::parse_as_style_attribute(DOM::Element& element)
{
auto declarations_and_at_rules = parse_a_list_of_declarations(m_token_stream);
- return convert_to_style_declaration(declarations_and_at_rules);
+ auto [properties, custom_properties] = extract_properties(declarations_and_at_rules);
+ return ElementInlineCSSStyleDeclaration::create(element, move(properties), move(custom_properties));
}
Optional<AK::URL> Parser::parse_url_function(StyleComponentValueRule const& component_value, AllowedDataUrlType allowed_data_url_type)
@@ -2169,30 +2170,32 @@ RefPtr<CSSRule> Parser::convert_to_rule(NonnullRefPtr<StyleRule> rule)
return {};
}
-RefPtr<PropertyOwningCSSStyleDeclaration> Parser::convert_to_style_declaration(Vector<DeclarationOrAtRule> declarations_and_at_rules)
+auto Parser::extract_properties(Vector<DeclarationOrAtRule> const& declarations_and_at_rules) -> PropertiesAndCustomProperties
{
- Vector<StyleProperty> properties;
- HashMap<String, StyleProperty> custom_properties;
-
+ PropertiesAndCustomProperties result;
for (auto& declaration_or_at_rule : declarations_and_at_rules) {
if (declaration_or_at_rule.is_at_rule()) {
dbgln_if(CSS_PARSER_DEBUG, "!!! CSS at-rule is not allowed here!");
continue;
}
- auto& declaration = declaration_or_at_rule.m_declaration;
+ auto const& declaration = declaration_or_at_rule.declaration();
- auto maybe_property = convert_to_style_property(declaration);
- if (maybe_property.has_value()) {
- auto property = maybe_property.value();
+ if (auto maybe_property = convert_to_style_property(declaration); maybe_property.has_value()) {
+ auto property = maybe_property.release_value();
if (property.property_id == PropertyID::Custom) {
- custom_properties.set(property.custom_name, property);
+ result.custom_properties.set(property.custom_name, property);
} else {
- properties.append(property);
+ result.properties.append(move(property));
}
}
}
+ return result;
+}
+RefPtr<PropertyOwningCSSStyleDeclaration> Parser::convert_to_style_declaration(Vector<DeclarationOrAtRule> declarations_and_at_rules)
+{
+ auto [properties, custom_properties] = extract_properties(declarations_and_at_rules);
return PropertyOwningCSSStyleDeclaration::create(move(properties), move(custom_properties));
}
@@ -5206,13 +5209,12 @@ RefPtr<CSS::CSSStyleSheet> parse_css(CSS::ParsingContext const& context, StringV
return parser.parse_as_stylesheet();
}
-RefPtr<CSS::PropertyOwningCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const& context, StringView css)
+RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const& context, StringView css, DOM::Element& element)
{
- // FIXME: We could save some effort by creating an ElementInlineCSSStyleDeclaration right here.
if (css.is_empty())
- return CSS::PropertyOwningCSSStyleDeclaration::create({}, {});
+ return CSS::ElementInlineCSSStyleDeclaration::create(element, {}, {});
CSS::Parser parser(context, css);
- return parser.parse_as_style_attribute();
+ return parser.parse_as_style_attribute(element);
}
RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const& context, StringView string, CSS::PropertyID property_id)
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h
index 786619b029..4e7f51348f 100644
--- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h
+++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h
@@ -105,7 +105,7 @@ public:
Vector<StyleComponentValueRule> parse_as_list_of_component_values();
Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
- RefPtr<PropertyOwningCSSStyleDeclaration> parse_as_style_attribute();
+ RefPtr<ElementInlineCSSStyleDeclaration> parse_as_style_attribute(DOM::Element&);
enum class SelectorParsingMode {
Standard,
@@ -343,6 +343,13 @@ private:
static bool has_ignored_vendor_prefix(StringView);
static bool is_builtin(StringView);
+ struct PropertiesAndCustomProperties {
+ Vector<StyleProperty> properties;
+ HashMap<String, StyleProperty> custom_properties;
+ };
+
+ PropertiesAndCustomProperties extract_properties(Vector<DeclarationOrAtRule> const&);
+
ParsingContext m_context;
Tokenizer m_tokenizer;
@@ -355,7 +362,7 @@ private:
namespace Web {
RefPtr<CSS::CSSStyleSheet> parse_css(CSS::ParsingContext const&, StringView);
-RefPtr<CSS::PropertyOwningCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const&, StringView);
+RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const&, StringView, DOM::Element&);
RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const&, StringView);
RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const&, StringView);