Skip to content

Commit 4b00a61

Browse files
tete17Lubrsi
authored andcommitted
LibWeb: Implement TrustedTypes spec for the concept of set_attribute
1 parent df543cf commit 4b00a61

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

Libraries/LibWeb/DOM/NamedNodeMap.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <LibWeb/DOM/NamedNodeMap.h>
1313
#include <LibWeb/Infra/Strings.h>
1414
#include <LibWeb/Namespace.h>
15+
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
1516

1617
namespace Web::DOM {
1718

@@ -194,31 +195,43 @@ Attr const* NamedNodeMap::get_attribute_ns(Optional<FlyString> const& namespace_
194195
return nullptr;
195196
}
196197

197-
// https://dom.spec.whatwg.org/#concept-element-attributes-set
198+
// FIXME: Trusted Types integration with DOM is still under review https://github.com/whatwg/dom/pull/1268
199+
// https://whatpr.org/dom/1268.html#concept-element-attributes-set
198200
WebIDL::ExceptionOr<GC::Ptr<Attr>> NamedNodeMap::set_attribute(Attr& attribute)
199201
{
200-
// 1. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
202+
// 1. Let verifiedValue be the result of calling get Trusted Types-compliant attribute value
203+
// with attr’s local name, attr’s namespace, element, and attr’s value
204+
auto const verifiedValue = TRY(TrustedTypes::get_trusted_types_compliant_attribute_value(
205+
attribute.local_name(),
206+
attribute.namespace_uri().has_value() ? Utf16String::from_utf8(attribute.namespace_uri().value()) : Optional<Utf16String>(),
207+
associated_element(),
208+
Utf16String::from_utf8(attribute.value())));
209+
210+
// 2. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
201211
if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &associated_element()))
202212
return WebIDL::InUseAttributeError::create(realm(), "Attribute must not already be in use"_utf16);
203213

204-
// 2. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
214+
// 3. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
205215
size_t old_attribute_index = 0;
206216
auto* old_attribute = get_attribute_ns(attribute.namespace_uri(), attribute.local_name(), &old_attribute_index);
207217

208-
// 3. If oldAttr is attr, return attr.
218+
// 4. If oldAttr is attr, return attr.
209219
if (old_attribute == &attribute)
210220
return &attribute;
211221

212-
// 4. If oldAttr is non-null, then replace oldAttr with attr.
222+
// 5. Set attr’s value to verifiedValue.
223+
attribute.set_value(verifiedValue.to_utf8_but_should_be_ported_to_utf16());
224+
225+
// 6. If oldAttr is non-null, then replace oldAttr with attr.
213226
if (old_attribute) {
214227
replace_attribute(*old_attribute, attribute, old_attribute_index);
215228
}
216-
// 5. Otherwise, append attr to element.
229+
// 7. Otherwise, append attr to element.
217230
else {
218231
append_attribute(attribute);
219232
}
220233

221-
// 6. Return oldAttr.
234+
// 8. Return oldAttr.
222235
return old_attribute;
223236
}
224237

0 commit comments

Comments
 (0)