From e7b08cf291e1c92616c74125fdb2c7b4ccb7295c Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 26 Jul 2025 11:00:09 -0400 Subject: [PATCH] LibWeb: Add a DOM::Element::set_attribute override for UTF-16 strings This just transcodes to UTF-8 for now, but primarily serves to to keep compatibility with generated IDL definitions. --- Libraries/LibWeb/DOM/Element.cpp | 6 ++++++ Libraries/LibWeb/DOM/Element.h | 1 + Libraries/LibWeb/Editing/Commands.cpp | 2 +- Libraries/LibWeb/Editing/Internal/Algorithms.cpp | 4 ++-- Libraries/LibWeb/HTML/HTMLDialogElement.cpp | 4 ++-- Libraries/LibWeb/Page/Page.cpp | 4 ++-- 6 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp index 72212e9b169..5f464ae5725 100644 --- a/Libraries/LibWeb/DOM/Element.cpp +++ b/Libraries/LibWeb/DOM/Element.cpp @@ -229,6 +229,12 @@ WebIDL::ExceptionOr Element::set_attribute(FlyString const& name, String c return {}; } +// https://dom.spec.whatwg.org/#dom-element-setattribute +WebIDL::ExceptionOr Element::set_attribute(FlyString const& name, Utf16String const& value) +{ + return set_attribute(name, value.to_utf8_but_should_be_ported_to_utf16()); +} + // https://dom.spec.whatwg.org/#valid-namespace-prefix bool is_valid_namespace_prefix(FlyString const& prefix) { diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h index acd39a46591..da24c609bc6 100644 --- a/Libraries/LibWeb/DOM/Element.h +++ b/Libraries/LibWeb/DOM/Element.h @@ -144,6 +144,7 @@ public: Optional lang() const; WebIDL::ExceptionOr set_attribute(FlyString const& name, String const& value); + WebIDL::ExceptionOr set_attribute(FlyString const& name, Utf16String const& value); WebIDL::ExceptionOr set_attribute_ns(Optional const& namespace_, FlyString const& qualified_name, String const& value); void set_attribute_value(FlyString const& local_name, String const& value, Optional const& prefix = {}, Optional const& namespace_ = {}); diff --git a/Libraries/LibWeb/Editing/Commands.cpp b/Libraries/LibWeb/Editing/Commands.cpp index 0279fbed5f8..3212fdbf541 100644 --- a/Libraries/LibWeb/Editing/Commands.cpp +++ b/Libraries/LibWeb/Editing/Commands.cpp @@ -1306,7 +1306,7 @@ bool command_insert_image_action(DOM::Document& document, Utf16String const& val auto img = MUST(DOM::create_element(document, HTML::TagNames::img, Namespace::HTML)); // 7. Run setAttribute("src", value) on img. - MUST(img->set_attribute(HTML::AttributeNames::src, value.to_utf8_but_should_be_ported_to_utf16())); + MUST(img->set_attribute(HTML::AttributeNames::src, value)); // 8. Run insertNode(img) on range. MUST(range->insert_node(img)); diff --git a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp index 42ef5083f78..acd859bd92e 100644 --- a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp @@ -1505,7 +1505,7 @@ void force_the_value(GC::Ref node, FlyString const& command, Optional // ownerDocument of node, then set the face attribute of new parent to new value. if (command == CommandNames::fontName) { new_parent = MUST(DOM::create_element(document, HTML::TagNames::font, Namespace::HTML)); - MUST(new_parent->set_attribute(HTML::AttributeNames::face, new_value.value().to_utf8_but_should_be_ported_to_utf16())); + MUST(new_parent->set_attribute(HTML::AttributeNames::face, *new_value)); } } @@ -1515,7 +1515,7 @@ void force_the_value(GC::Ref node, FlyString const& command, Optional new_parent = MUST(DOM::create_element(document, HTML::TagNames::a, Namespace::HTML)); // 2. Set the href attribute of new parent to new value. - MUST(new_parent->set_attribute(HTML::AttributeNames::href, new_value.value().to_utf8_but_should_be_ported_to_utf16())); + MUST(new_parent->set_attribute(HTML::AttributeNames::href, *new_value)); // 3. Let ancestor be node's parent. GC::Ptr ancestor = node->parent(); diff --git a/Libraries/LibWeb/HTML/HTMLDialogElement.cpp b/Libraries/LibWeb/HTML/HTMLDialogElement.cpp index f9ea7ff1c39..e7405c69d1e 100644 --- a/Libraries/LibWeb/HTML/HTMLDialogElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLDialogElement.cpp @@ -132,7 +132,7 @@ WebIDL::ExceptionOr HTMLDialogElement::show() queue_a_dialog_toggle_event_task("closed"_string, "open"_string, nullptr); // 6. Add an open attribute to this, whose value is the empty string. - TRY(set_attribute(AttributeNames::open, {})); + TRY(set_attribute(AttributeNames::open, String {})); // 7. Assert: this's node document's open dialogs list does not contain this. VERIFY(!m_document->open_dialogs_list().contains_slow(GC::Ref(*this))); @@ -227,7 +227,7 @@ WebIDL::ExceptionOr HTMLDialogElement::show_a_modal_dialog(HTMLDialogEleme subject.queue_a_dialog_toggle_event_task("closed"_string, "open"_string, source); // 11. Add an open attribute to subject, whose value is the empty string. - TRY(subject.set_attribute(AttributeNames::open, {})); + TRY(subject.set_attribute(AttributeNames::open, String {})); // 12. Set is modal of subject to true. subject.set_is_modal(true); diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp index f3cf3062d92..c55c93d3829 100644 --- a/Libraries/LibWeb/Page/Page.cpp +++ b/Libraries/LibWeb/Page/Page.cpp @@ -529,7 +529,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, {})); + TRY(media_element->set_attribute(HTML::AttributeNames::loop, String {})); return {}; } @@ -545,7 +545,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, {})); + TRY(media_element->set_attribute(HTML::AttributeNames::controls, String {})); return {}; }