Skip to content

Commit e2adce8

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

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

Libraries/LibWeb/DOM/Element.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,25 @@ WebIDL::ExceptionOr<QualifiedName> validate_and_extract(JS::Realm& realm, Option
362362
return QualifiedName { local_name, prefix, namespace_ };
363363
}
364364

365-
// https://dom.spec.whatwg.org/#dom-element-setattributens
366-
WebIDL::ExceptionOr<void> Element::set_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& qualified_name, String const& value)
365+
// FIXME: Trusted Types integration with DOM is still under review https://github.com/whatwg/dom/pull/1268
366+
// https://whatpr.org/dom/1268.html#dom-element-setattributens
367+
WebIDL::ExceptionOr<void> Element::set_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& qualified_name, Variant<GC::Root<TrustedTypes::TrustedHTML>, GC::Root<TrustedTypes::TrustedScript>, GC::Root<TrustedTypes::TrustedScriptURL>, Utf16String> const& value)
367368
{
368369
// 1. Let (namespace, prefix, localName) be the result of validating and extracting namespace and qualifiedName given "element".
369370
auto extracted_qualified_name = TRY(validate_and_extract(realm(), namespace_, qualified_name, ValidationContext::Element));
370371

371-
// 2. Set an attribute value for this using localName, value, and also prefix and namespace.
372-
set_attribute_value(extracted_qualified_name.local_name(), value, extracted_qualified_name.prefix(), extracted_qualified_name.namespace_());
372+
// 2. Let verifiedValue be the result of calling get Trusted Types-compliant attribute value
373+
// with localName, namespace, element, and value.
374+
auto const verified_value = TRY(TrustedTypes::get_trusted_types_compliant_attribute_value(
375+
extracted_qualified_name.local_name(),
376+
extracted_qualified_name.namespace_().has_value() ? Utf16String::from_utf8(extracted_qualified_name.namespace_().value()) : Optional<Utf16String>(),
377+
*this,
378+
value.visit(
379+
[](auto const& trusted_type) -> Variant<GC::Root<TrustedTypes::TrustedHTML>, GC::Root<TrustedTypes::TrustedScript>, GC::Root<TrustedTypes::TrustedScriptURL>, Utf16String> { return trusted_type; },
380+
[](String const& string) -> Variant<GC::Root<TrustedTypes::TrustedHTML>, GC::Root<TrustedTypes::TrustedScript>, GC::Root<TrustedTypes::TrustedScriptURL>, Utf16String> { return Utf16String::from_utf8(string); })));
381+
382+
// 3. Set an attribute value for this using localName, verifiedValue, and also prefix and namespace.
383+
set_attribute_value(extracted_qualified_name.local_name(), verified_value.to_utf8_but_should_be_ported_to_utf16(), extracted_qualified_name.prefix(), extracted_qualified_name.namespace_());
373384

374385
return {};
375386
}

Libraries/LibWeb/DOM/Element.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class WEB_API Element
152152
WebIDL::ExceptionOr<void> set_attribute(FlyString qualified_name, Variant<GC::Root<TrustedTypes::TrustedHTML>, GC::Root<TrustedTypes::TrustedScript>, GC::Root<TrustedTypes::TrustedScriptURL>, String> const& value);
153153
WebIDL::ExceptionOr<void> set_attribute(FlyString qualified_name, Variant<GC::Root<TrustedTypes::TrustedHTML>, GC::Root<TrustedTypes::TrustedScript>, GC::Root<TrustedTypes::TrustedScriptURL>, Utf16String> const& value);
154154

155-
WebIDL::ExceptionOr<void> set_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& qualified_name, String const& value);
155+
WebIDL::ExceptionOr<void> set_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& qualified_name, Variant<GC::Root<TrustedTypes::TrustedHTML>, GC::Root<TrustedTypes::TrustedScript>, GC::Root<TrustedTypes::TrustedScriptURL>, Utf16String> const& value);
156156
void set_attribute_value(FlyString const& local_name, String const& value, Optional<FlyString> const& prefix = {}, Optional<FlyString> const& namespace_ = {});
157157
WebIDL::ExceptionOr<GC::Ptr<Attr>> set_attribute_node(Attr&);
158158
WebIDL::ExceptionOr<GC::Ptr<Attr>> set_attribute_node_ns(Attr&);

Libraries/LibWeb/DOM/Element.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ interface Element : Node {
5454
DOMString? getAttribute(DOMString qualifiedName);
5555
DOMString? getAttributeNS([FlyString] DOMString? namespace, [FlyString] DOMString localName);
5656
[CEReactions] undefined setAttribute(DOMString qualifiedName, (TrustedType or Utf16DOMString) value);
57-
[CEReactions] undefined setAttributeNS([FlyString] DOMString? namespace , [FlyString] DOMString qualifiedName , DOMString value);
57+
[CEReactions] undefined setAttributeNS([FlyString] DOMString? namespace , [FlyString] DOMString qualifiedName , (TrustedType or Utf16DOMString) value);
5858
[CEReactions] undefined removeAttribute([FlyString] DOMString qualifiedName);
5959
[CEReactions] undefined removeAttributeNS([FlyString] DOMString? namespace, [FlyString] DOMString localName);
6060
[CEReactions] boolean toggleAttribute(DOMString qualifiedName, optional boolean force);

Libraries/LibWeb/XML/XMLDocumentBuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,16 @@ void XMLDocumentBuilder::element_start(XML::Name const& name, OrderedHashMap<XML
164164
if (attribute.key == "xmlns" || attribute.key.starts_with("xmlns:"sv)) {
165165
// The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/.
166166
if (!attribute.key.is_one_of("xmlns:"sv, "xmlns:xmlns"sv)) {
167-
if (!node->set_attribute_ns(Namespace::XMLNS, MUST(String::from_byte_string(attribute.key)), MUST(String::from_byte_string(attribute.value))).is_error())
167+
if (!node->set_attribute_ns(Namespace::XMLNS, MUST(String::from_byte_string(attribute.key)), Utf16String::from_utf8(attribute.value)).is_error())
168168
continue;
169169
}
170170
m_has_error = true;
171171
} else if (attribute.key.contains(':')) {
172172
if (auto ns = namespace_for_name(attribute.key); ns.has_value()) {
173-
if (!node->set_attribute_ns(ns.value(), MUST(String::from_byte_string(attribute.key)), MUST(String::from_byte_string(attribute.value))).is_error())
173+
if (!node->set_attribute_ns(ns.value(), MUST(String::from_byte_string(attribute.key)), Utf16String::from_utf8(attribute.value)).is_error())
174174
continue;
175175
} else if (attribute.key.starts_with("xml:"sv)) {
176-
if (auto maybe_error = node->set_attribute_ns(Namespace::XML, MUST(String::from_byte_string(attribute.key)), MUST(String::from_byte_string(attribute.value))); !maybe_error.is_error())
176+
if (auto maybe_error = node->set_attribute_ns(Namespace::XML, MUST(String::from_byte_string(attribute.key)), Utf16String::from_utf8(attribute.value)); !maybe_error.is_error())
177177
continue;
178178
}
179179
m_has_error = true;

0 commit comments

Comments
 (0)