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.
This commit is contained in:
Timothy Flynn 2025-07-26 11:00:09 -04:00 committed by Andreas Kling
commit e7b08cf291
Notes: github-actions[bot] 2025-07-28 10:26:37 +00:00
6 changed files with 14 additions and 7 deletions

View file

@ -229,6 +229,12 @@ WebIDL::ExceptionOr<void> Element::set_attribute(FlyString const& name, String c
return {}; return {};
} }
// https://dom.spec.whatwg.org/#dom-element-setattribute
WebIDL::ExceptionOr<void> 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 // https://dom.spec.whatwg.org/#valid-namespace-prefix
bool is_valid_namespace_prefix(FlyString const& prefix) bool is_valid_namespace_prefix(FlyString const& prefix)
{ {

View file

@ -144,6 +144,7 @@ public:
Optional<String> lang() const; Optional<String> lang() const;
WebIDL::ExceptionOr<void> set_attribute(FlyString const& name, String const& value); WebIDL::ExceptionOr<void> set_attribute(FlyString const& name, String const& value);
WebIDL::ExceptionOr<void> set_attribute(FlyString const& name, Utf16String const& value);
WebIDL::ExceptionOr<void> set_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& qualified_name, String const& value); WebIDL::ExceptionOr<void> set_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& qualified_name, String const& value);
void set_attribute_value(FlyString const& local_name, String const& value, Optional<FlyString> const& prefix = {}, Optional<FlyString> const& namespace_ = {}); void set_attribute_value(FlyString const& local_name, String const& value, Optional<FlyString> const& prefix = {}, Optional<FlyString> const& namespace_ = {});

View file

@ -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)); auto img = MUST(DOM::create_element(document, HTML::TagNames::img, Namespace::HTML));
// 7. Run setAttribute("src", value) on img. // 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. // 8. Run insertNode(img) on range.
MUST(range->insert_node(img)); MUST(range->insert_node(img));

View file

@ -1505,7 +1505,7 @@ void force_the_value(GC::Ref<DOM::Node> node, FlyString const& command, Optional
// ownerDocument of node, then set the face attribute of new parent to new value. // ownerDocument of node, then set the face attribute of new parent to new value.
if (command == CommandNames::fontName) { if (command == CommandNames::fontName) {
new_parent = MUST(DOM::create_element(document, HTML::TagNames::font, Namespace::HTML)); 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<DOM::Node> node, FlyString const& command, Optional
new_parent = MUST(DOM::create_element(document, HTML::TagNames::a, Namespace::HTML)); new_parent = MUST(DOM::create_element(document, HTML::TagNames::a, Namespace::HTML));
// 2. Set the href attribute of new parent to new value. // 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. // 3. Let ancestor be node's parent.
GC::Ptr<DOM::Node> ancestor = node->parent(); GC::Ptr<DOM::Node> ancestor = node->parent();

View file

@ -132,7 +132,7 @@ WebIDL::ExceptionOr<void> HTMLDialogElement::show()
queue_a_dialog_toggle_event_task("closed"_string, "open"_string, nullptr); queue_a_dialog_toggle_event_task("closed"_string, "open"_string, nullptr);
// 6. Add an open attribute to this, whose value is the empty string. // 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. // 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))); VERIFY(!m_document->open_dialogs_list().contains_slow(GC::Ref(*this)));
@ -227,7 +227,7 @@ WebIDL::ExceptionOr<void> HTMLDialogElement::show_a_modal_dialog(HTMLDialogEleme
subject.queue_a_dialog_toggle_event_task("closed"_string, "open"_string, source); 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. // 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. // 12. Set is modal of subject to true.
subject.set_is_modal(true); subject.set_is_modal(true);

View file

@ -529,7 +529,7 @@ WebIDL::ExceptionOr<void> Page::toggle_media_loop_state()
if (media_element->has_attribute(HTML::AttributeNames::loop)) if (media_element->has_attribute(HTML::AttributeNames::loop))
media_element->remove_attribute(HTML::AttributeNames::loop); media_element->remove_attribute(HTML::AttributeNames::loop);
else else
TRY(media_element->set_attribute(HTML::AttributeNames::loop, {})); TRY(media_element->set_attribute(HTML::AttributeNames::loop, String {}));
return {}; return {};
} }
@ -545,7 +545,7 @@ WebIDL::ExceptionOr<void> Page::toggle_media_controls_state()
if (media_element->has_attribute(HTML::AttributeNames::controls)) if (media_element->has_attribute(HTML::AttributeNames::controls))
media_element->remove_attribute(HTML::AttributeNames::controls); media_element->remove_attribute(HTML::AttributeNames::controls);
else else
TRY(media_element->set_attribute(HTML::AttributeNames::controls, {})); TRY(media_element->set_attribute(HTML::AttributeNames::controls, String {}));
return {}; return {};
} }