diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 9d59ea556d..cf6802d9df 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -2951,9 +2951,18 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@) if (attribute.extended_attributes.contains("Reflect")) { if (attribute.type->name() != "boolean") { - attribute_generator.append(R"~~~( + if (attribute.type->is_nullable()) { + attribute_generator.append(R"~~~( + if (!cpp_value.has_value()) + impl->remove_attribute(HTML::AttributeNames::@attribute.reflect_name@); + else + MUST(impl->set_attribute(HTML::AttributeNames::@attribute.reflect_name@, cpp_value.value())); +)~~~"); + } else { + attribute_generator.append(R"~~~( MUST(impl->set_attribute(HTML::AttributeNames::@attribute.reflect_name@, cpp_value)); )~~~"); + } } else { attribute_generator.append(R"~~~( if (!cpp_value) diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 0580e268c3..e77c260508 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -206,12 +206,9 @@ WebIDL::ExceptionOr Element::set_attribute(DeprecatedFlyString const& name return {}; } -WebIDL::ExceptionOr Element::set_attribute(DeprecatedFlyString const& name, Optional const& value) +WebIDL::ExceptionOr Element::set_attribute(DeprecatedFlyString const& name, String const& value) { - if (!value.has_value()) - return set_attribute(name, DeprecatedString {}); - - return set_attribute(name, value->to_deprecated_string()); + return set_attribute(name, value.to_deprecated_string()); } // https://dom.spec.whatwg.org/#validate-and-extract diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index d41563d761..aeb8a2482b 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -110,8 +110,8 @@ public: DeprecatedString get_attribute_value(StringView local_name, DeprecatedFlyString const& namespace_ = {}) const; WebIDL::ExceptionOr set_attribute(DeprecatedFlyString const& name, DeprecatedString const& value); - WebIDL::ExceptionOr set_attribute(DeprecatedFlyString const& name, Optional const& value); - WebIDL::ExceptionOr set_attribute(FlyString const& name, Optional const& value) + WebIDL::ExceptionOr set_attribute(DeprecatedFlyString const& name, String const& value); + WebIDL::ExceptionOr set_attribute(FlyString const& name, String const& value) { return set_attribute(name.to_deprecated_fly_string(), value); } @@ -265,7 +265,10 @@ public: \ WebIDL::ExceptionOr set_##name(Optional const& value) override \ { \ - TRY(set_attribute(attribute, value)); \ + if (value.has_value()) \ + TRY(set_attribute(attribute, *value)); \ + else \ + remove_attribute(attribute); \ return {}; \ } diff --git a/Userland/Libraries/LibWeb/Page/Page.cpp b/Userland/Libraries/LibWeb/Page/Page.cpp index b85393a672..8a197ce8f9 100644 --- a/Userland/Libraries/LibWeb/Page/Page.cpp +++ b/Userland/Libraries/LibWeb/Page/Page.cpp @@ -358,7 +358,7 @@ WebIDL::ExceptionOr Page::toggle_media_loop_state() if (media_element->has_attribute(HTML::AttributeNames::loop)) media_element->remove_attribute(HTML::AttributeNames::loop); else - TRY(media_element->set_attribute(HTML::AttributeNames::loop, OptionalNone {})); + TRY(media_element->set_attribute(HTML::AttributeNames::loop, {})); return {}; } @@ -374,7 +374,7 @@ WebIDL::ExceptionOr Page::toggle_media_controls_state() if (media_element->has_attribute(HTML::AttributeNames::controls)) media_element->remove_attribute(HTML::AttributeNames::controls); else - TRY(media_element->set_attribute(HTML::AttributeNames::controls, OptionalNone {})); + TRY(media_element->set_attribute(HTML::AttributeNames::controls, {})); return {}; }