Skip to content

Commit 887537b

Browse files
tete17Lubrsi
authored andcommitted
LibWeb: Implement TrustedTypes spec for set_value on Attribute
1 parent e2adce8 commit 887537b

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

Libraries/LibWeb/DOM/Attr.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
33
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
4+
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
45
*
56
* SPDX-License-Identifier: BSD-2-Clause
67
*/
@@ -13,6 +14,7 @@
1314
#include <LibWeb/DOM/MutationType.h>
1415
#include <LibWeb/DOM/StaticNodeList.h>
1516
#include <LibWeb/HTML/CustomElements/CustomElementReactionNames.h>
17+
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
1618

1719
namespace Web::DOM {
1820

@@ -69,17 +71,43 @@ void Attr::set_owner_element(Element* owner_element)
6971
m_owner_element = owner_element;
7072
}
7173

72-
// https://dom.spec.whatwg.org/#set-an-existing-attribute-value
73-
void Attr::set_value(String value)
74+
// FIXME: Trusted Types integration with DOM is still under review https://github.com/whatwg/dom/pull/1268
75+
// https://whatpr.org/dom/1268.html#set-an-existing-attribute-value
76+
WebIDL::ExceptionOr<void> Attr::set_value(String value)
7477
{
7578
// 1. If attribute’s element is null, then set attribute’s value to value.
7679
if (!owner_element()) {
7780
m_value = move(value);
7881
}
79-
// 2. Otherwise, change attribute to value.
82+
// 2. Otherwise:
8083
else {
81-
change_attribute(move(value));
84+
// 1. Let element be attribute’s element.
85+
auto const* element = owner_element();
86+
87+
// 2. Let verifiedValue be the result of calling get Trusted Types-compliant attribute value with
88+
// attribute’s local name, attribute’s namespace, element, and value.
89+
auto const verified_value = TRY(TrustedTypes::get_trusted_types_compliant_attribute_value(
90+
local_name(),
91+
namespace_uri().has_value() ? Utf16String::from_utf8(namespace_uri().value()) : Optional<Utf16String>(),
92+
*element,
93+
Utf16String::from_utf8(value)));
94+
95+
// 3. If attribute’s element is null, then set attribute’s value to verifiedValue, and return.
96+
if (!owner_element()) {
97+
m_value = verified_value.to_utf8_but_should_be_ported_to_utf16();
98+
return {};
99+
}
100+
101+
// 4. If attribute’s element is not element, then return.
102+
if (owner_element() != element) {
103+
return {};
104+
}
105+
106+
// 5. Change attribute to verifiedValue.
107+
change_attribute(verified_value.to_utf8_but_should_be_ported_to_utf16());
82108
}
109+
110+
return {};
83111
}
84112

85113
// https://dom.spec.whatwg.org/#concept-element-attributes-change

Libraries/LibWeb/DOM/Attr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class WEB_API Attr final : public Node {
3434
FlyString const& lowercase_name() const { return m_lowercase_name; }
3535

3636
String const& value() const { return m_value; }
37-
void set_value(String value);
37+
WebIDL::ExceptionOr<void> set_value(String value);
3838
void change_attribute(String value);
3939

4040
Element* owner_element();

Libraries/LibWeb/DOM/NamedNodeMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ WebIDL::ExceptionOr<GC::Ptr<Attr>> NamedNodeMap::set_attribute(Attr& attribute)
220220
return &attribute;
221221

222222
// 5. Set attr’s value to verifiedValue.
223-
attribute.set_value(verifiedValue.to_utf8_but_should_be_ported_to_utf16());
223+
TRY(attribute.set_value(verifiedValue.to_utf8_but_should_be_ported_to_utf16()));
224224

225225
// 6. If oldAttr is non-null, then replace oldAttr with attr.
226226
if (old_attribute) {

Libraries/LibWeb/DOM/Node.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ void Node::set_text_content(Optional<Utf16String> const& maybe_content)
221221

222222
// If Attr, set an existing attribute value with this and the given value.
223223
else if (auto* attribute = as_if<Attr>(*this)) {
224-
attribute->set_value(content.to_utf8_but_should_be_ported_to_utf16());
224+
// FIXME: This should propagate
225+
MUST(attribute->set_value(content.to_utf8_but_should_be_ported_to_utf16()));
225226
}
226227

227228
// Otherwise, do nothing.
@@ -368,21 +369,22 @@ Optional<String> Node::node_value() const
368369
}
369370

370371
// https://dom.spec.whatwg.org/#ref-for-dom-node-nodevalue%E2%91%A0
371-
void Node::set_node_value(Optional<String> const& maybe_value)
372+
WebIDL::ExceptionOr<void> Node::set_node_value(Optional<String> const& maybe_value)
372373
{
373374
// The nodeValue setter steps are to, if the given value is null, act as if it was the empty string instead,
374375
// and then do as described below, switching on the interface this implements:
375376
auto value = maybe_value.value_or(String {});
376377

377378
// If Attr, set an existing attribute value with this and the given value.
378379
if (auto* attr = as_if<Attr>(this)) {
379-
attr->set_value(move(value));
380+
TRY(attr->set_value(move(value)));
380381
} else if (auto* character_data = as_if<CharacterData>(this)) {
381382
// If CharacterData, replace data with node this, offset 0, count this’s length, and data the given value.
382383
character_data->set_data(Utf16String::from_utf8(value));
383384
}
384385

385386
// Otherwise, do nothing.
387+
return {};
386388
}
387389

388390
// https://html.spec.whatwg.org/multipage/document-sequences.html#node-navigable

Libraries/LibWeb/DOM/Node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class WEB_API Node : public EventTarget
285285
WebIDL::ExceptionOr<void> normalize();
286286

287287
Optional<String> node_value() const;
288-
void set_node_value(Optional<String> const&);
288+
WebIDL::ExceptionOr<void> set_node_value(Optional<String> const&);
289289

290290
GC::Ptr<HTML::Navigable> navigable() const;
291291

0 commit comments

Comments
 (0)