LibWeb: Implement TrustedTypes spec for set_value on Attribute

This commit is contained in:
Tete17 2025-08-08 02:12:15 +02:00 committed by Luke Wilde
parent e2adce84e7
commit 887537b061
5 changed files with 40 additions and 10 deletions

View File

@ -1,6 +1,7 @@
/*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -13,6 +14,7 @@
#include <LibWeb/DOM/MutationType.h>
#include <LibWeb/DOM/StaticNodeList.h>
#include <LibWeb/HTML/CustomElements/CustomElementReactionNames.h>
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
namespace Web::DOM {
@ -69,17 +71,43 @@ void Attr::set_owner_element(Element* owner_element)
m_owner_element = owner_element;
}
// https://dom.spec.whatwg.org/#set-an-existing-attribute-value
void Attr::set_value(String value)
// FIXME: Trusted Types integration with DOM is still under review https://github.com/whatwg/dom/pull/1268
// https://whatpr.org/dom/1268.html#set-an-existing-attribute-value
WebIDL::ExceptionOr<void> Attr::set_value(String value)
{
// 1. If attributes element is null, then set attributes value to value.
if (!owner_element()) {
m_value = move(value);
}
// 2. Otherwise, change attribute to value.
// 2. Otherwise:
else {
change_attribute(move(value));
// 1. Let element be attributes element.
auto const* element = owner_element();
// 2. Let verifiedValue be the result of calling get Trusted Types-compliant attribute value with
// attributes local name, attributes namespace, element, and value.
auto const verified_value = TRY(TrustedTypes::get_trusted_types_compliant_attribute_value(
local_name(),
namespace_uri().has_value() ? Utf16String::from_utf8(namespace_uri().value()) : Optional<Utf16String>(),
*element,
Utf16String::from_utf8(value)));
// 3. If attributes element is null, then set attributes value to verifiedValue, and return.
if (!owner_element()) {
m_value = verified_value.to_utf8_but_should_be_ported_to_utf16();
return {};
}
// 4. If attributes element is not element, then return.
if (owner_element() != element) {
return {};
}
// 5. Change attribute to verifiedValue.
change_attribute(verified_value.to_utf8_but_should_be_ported_to_utf16());
}
return {};
}
// https://dom.spec.whatwg.org/#concept-element-attributes-change

View File

@ -34,7 +34,7 @@ public:
FlyString const& lowercase_name() const { return m_lowercase_name; }
String const& value() const { return m_value; }
void set_value(String value);
WebIDL::ExceptionOr<void> set_value(String value);
void change_attribute(String value);
Element* owner_element();

View File

@ -220,7 +220,7 @@ WebIDL::ExceptionOr<GC::Ptr<Attr>> NamedNodeMap::set_attribute(Attr& attribute)
return &attribute;
// 5. Set attrs value to verifiedValue.
attribute.set_value(verifiedValue.to_utf8_but_should_be_ported_to_utf16());
TRY(attribute.set_value(verifiedValue.to_utf8_but_should_be_ported_to_utf16()));
// 6. If oldAttr is non-null, then replace oldAttr with attr.
if (old_attribute) {

View File

@ -221,7 +221,8 @@ void Node::set_text_content(Optional<Utf16String> const& maybe_content)
// If Attr, set an existing attribute value with this and the given value.
else if (auto* attribute = as_if<Attr>(*this)) {
attribute->set_value(content.to_utf8_but_should_be_ported_to_utf16());
// FIXME: This should propagate
MUST(attribute->set_value(content.to_utf8_but_should_be_ported_to_utf16()));
}
// Otherwise, do nothing.
@ -368,7 +369,7 @@ Optional<String> Node::node_value() const
}
// https://dom.spec.whatwg.org/#ref-for-dom-node-nodevalue%E2%91%A0
void Node::set_node_value(Optional<String> const& maybe_value)
WebIDL::ExceptionOr<void> Node::set_node_value(Optional<String> const& maybe_value)
{
// The nodeValue setter steps are to, if the given value is null, act as if it was the empty string instead,
// and then do as described below, switching on the interface this implements:
@ -376,13 +377,14 @@ void Node::set_node_value(Optional<String> const& maybe_value)
// If Attr, set an existing attribute value with this and the given value.
if (auto* attr = as_if<Attr>(this)) {
attr->set_value(move(value));
TRY(attr->set_value(move(value)));
} else if (auto* character_data = as_if<CharacterData>(this)) {
// If CharacterData, replace data with node this, offset 0, count thiss length, and data the given value.
character_data->set_data(Utf16String::from_utf8(value));
}
// Otherwise, do nothing.
return {};
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#node-navigable

View File

@ -285,7 +285,7 @@ public:
WebIDL::ExceptionOr<void> normalize();
Optional<String> node_value() const;
void set_node_value(Optional<String> const&);
WebIDL::ExceptionOr<void> set_node_value(Optional<String> const&);
GC::Ptr<HTML::Navigable> navigable() const;