From 465ecf37c201ee893aac536869ea40cddd629186 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 10 May 2023 13:01:30 +0100 Subject: [PATCH] LibWeb: Make `property_id_from_string()` return Optional --- .../LibWeb/GenerateCSSPropertyID.cpp | 12 +++++----- Userland/Libraries/LibWeb/CSS/CSS.cpp | 4 ++-- .../LibWeb/CSS/CSSStyleDeclaration.cpp | 24 +++++++++---------- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 10 ++++---- .../WebContent/WebDriverConnection.cpp | 8 +++---- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp index 425cbd81da..19d936a780 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp @@ -109,8 +109,8 @@ enum class PropertyID { generator.append(R"~~~( }; -PropertyID property_id_from_camel_case_string(StringView); -PropertyID property_id_from_string(StringView); +Optional property_id_from_camel_case_string(StringView); +Optional property_id_from_string(StringView); StringView string_from_property_id(PropertyID); bool is_inherited_property(PropertyID); ErrorOr> property_initial_value(JS::Realm&, PropertyID); @@ -186,7 +186,7 @@ ErrorOr generate_implementation_file(JsonObject& properties, Core::File& f namespace Web::CSS { -PropertyID property_id_from_camel_case_string(StringView string) +Optional property_id_from_camel_case_string(StringView string) { )~~~"); @@ -204,10 +204,10 @@ PropertyID property_id_from_camel_case_string(StringView string) }); generator.append(R"~~~( - return PropertyID::Invalid; + return {}; } -PropertyID property_id_from_string(StringView string) +Optional property_id_from_string(StringView string) { )~~~"); @@ -224,7 +224,7 @@ PropertyID property_id_from_string(StringView string) }); generator.append(R"~~~( - return PropertyID::Invalid; + return {}; } StringView string_from_property_id(PropertyID property_id) { diff --git a/Userland/Libraries/LibWeb/CSS/CSS.cpp b/Userland/Libraries/LibWeb/CSS/CSS.cpp index 443830ff34..010a44e7c2 100644 --- a/Userland/Libraries/LibWeb/CSS/CSS.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSS.cpp @@ -27,8 +27,8 @@ bool supports(JS::VM& vm, StringView property, StringView value) // 1. If property is an ASCII case-insensitive match for any defined CSS property that the UA supports, // and value successfully parses according to that property’s grammar, return true. - if (auto property_id = property_id_from_string(property); property_id != PropertyID::Invalid) { - if (parse_css_value(Parser::ParsingContext { realm }, value, property_id).release_value_but_fixme_should_propagate_errors()) + if (auto property_id = property_id_from_string(property); property_id.has_value()) { + if (parse_css_value(Parser::ParsingContext { realm }, value, property_id.value()).release_value_but_fixme_should_propagate_errors()) return true; } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp index b734a2304f..dd37e2e9c9 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp @@ -206,9 +206,9 @@ bool PropertyOwningCSSStyleDeclaration::set_a_css_declaration(PropertyID propert DeprecatedString CSSStyleDeclaration::get_property_value(StringView property_name) const { auto property_id = property_id_from_string(property_name); - if (property_id == CSS::PropertyID::Invalid) + if (!property_id.has_value()) return {}; - auto maybe_property = property(property_id); + auto maybe_property = property(property_id.value()); if (!maybe_property.has_value()) return {}; return maybe_property->value->to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string(); @@ -218,9 +218,9 @@ DeprecatedString CSSStyleDeclaration::get_property_value(StringView property_nam DeprecatedString CSSStyleDeclaration::get_property_priority(StringView property_name) const { auto property_id = property_id_from_string(property_name); - if (property_id == CSS::PropertyID::Invalid) + if (!property_id.has_value()) return {}; - auto maybe_property = property(property_id); + auto maybe_property = property(property_id.value()); if (!maybe_property.has_value()) return {}; return maybe_property->important == Important::Yes ? "important" : ""; @@ -229,17 +229,17 @@ DeprecatedString CSSStyleDeclaration::get_property_priority(StringView property_ WebIDL::ExceptionOr CSSStyleDeclaration::set_property(StringView property_name, StringView css_text, StringView priority) { auto property_id = property_id_from_string(property_name); - if (property_id == CSS::PropertyID::Invalid) + if (!property_id.has_value()) return {}; - return set_property(property_id, css_text, priority); + return set_property(property_id.value(), css_text, priority); } WebIDL::ExceptionOr CSSStyleDeclaration::remove_property(StringView property_name) { auto property_id = property_id_from_string(property_name); - if (property_id == CSS::PropertyID::Invalid) + if (!property_id.has_value()) return DeprecatedString::empty(); - return remove_property(property_id); + return remove_property(property_id.value()); } // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext @@ -326,11 +326,11 @@ static CSS::PropertyID property_id_from_name(StringView name) if (name == "cssFloat"sv) return CSS::PropertyID::Float; - if (auto property_id = CSS::property_id_from_camel_case_string(name); property_id != CSS::PropertyID::Invalid) - return property_id; + if (auto property_id = CSS::property_id_from_camel_case_string(name); property_id.has_value()) + return property_id.value(); - if (auto property_id = CSS::property_id_from_string(name); property_id != CSS::PropertyID::Invalid) - return property_id; + if (auto property_id = CSS::property_id_from_string(name); property_id.has_value()) + return property_id.value(); return CSS::PropertyID::Invalid; } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 7fb3862da3..9237950e6e 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -3235,7 +3235,7 @@ Optional Parser::convert_to_style_property(Declaration const& dec auto property_name = declaration.name(); auto property_id = property_id_from_string(property_name); - if (property_id == PropertyID::Invalid) { + if (!property_id.has_value()) { if (property_name.starts_with("--"sv)) { property_id = PropertyID::Custom; } else if (has_ignored_vendor_prefix(property_name)) { @@ -3247,7 +3247,7 @@ Optional Parser::convert_to_style_property(Declaration const& dec } auto value_token_stream = TokenStream(declaration.values()); - auto value = parse_css_value(property_id, value_token_stream); + auto value = parse_css_value(property_id.value(), value_token_stream); if (value.is_error()) { if (value.error() == ParseError::SyntaxError) { dbgln_if(CSS_PARSER_DEBUG, "Unable to parse value for CSS property '{}'.", property_name); @@ -3258,10 +3258,10 @@ Optional Parser::convert_to_style_property(Declaration const& dec return {}; } - if (property_id == PropertyID::Custom) - return StyleProperty { declaration.importance(), property_id, value.release_value(), declaration.name() }; + if (property_id.value() == PropertyID::Custom) + return StyleProperty { declaration.importance(), property_id.value(), value.release_value(), declaration.name() }; - return StyleProperty { declaration.importance(), property_id, value.release_value(), {} }; + return StyleProperty { declaration.importance(), property_id.value(), value.release_value(), {} }; } ErrorOr> Parser::parse_builtin_value(ComponentValue const& component_value) diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index ec7db5c925..6fd5cdd383 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -1132,10 +1132,10 @@ Messages::WebDriverClient::GetElementCssValueResponse WebDriverConnection::get_e // -> current browsing context’s active document’s type is not "xml" if (!m_page_client.page().top_level_browsing_context().active_document()->is_xml_document()) { // computed value of parameter property name from element’s style declarations. property name is obtained from url variables. - auto property = Web::CSS::property_id_from_string(name); - - if (auto* computed_values = element->computed_css_values()) - computed_value = computed_values->property(property)->to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string(); + if (auto property = Web::CSS::property_id_from_string(name); property.has_value()) { + if (auto* computed_values = element->computed_css_values()) + computed_value = computed_values->property(property.value())->to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string(); + } } // -> Otherwise else {