|
12 | 12 | #include <LibWeb/DOM/NamedNodeMap.h> |
13 | 13 | #include <LibWeb/Infra/Strings.h> |
14 | 14 | #include <LibWeb/Namespace.h> |
| 15 | +#include <LibWeb/TrustedTypes/TrustedTypePolicy.h> |
15 | 16 |
|
16 | 17 | namespace Web::DOM { |
17 | 18 |
|
@@ -194,31 +195,43 @@ Attr const* NamedNodeMap::get_attribute_ns(Optional<FlyString> const& namespace_ |
194 | 195 | return nullptr; |
195 | 196 | } |
196 | 197 |
|
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 |
198 | 200 | WebIDL::ExceptionOr<GC::Ptr<Attr>> NamedNodeMap::set_attribute(Attr& attribute) |
199 | 201 | { |
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. |
201 | 211 | if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &associated_element())) |
202 | 212 | return WebIDL::InUseAttributeError::create(realm(), "Attribute must not already be in use"_utf16); |
203 | 213 |
|
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. |
205 | 215 | size_t old_attribute_index = 0; |
206 | 216 | auto* old_attribute = get_attribute_ns(attribute.namespace_uri(), attribute.local_name(), &old_attribute_index); |
207 | 217 |
|
208 | | - // 3. If oldAttr is attr, return attr. |
| 218 | + // 4. If oldAttr is attr, return attr. |
209 | 219 | if (old_attribute == &attribute) |
210 | 220 | return &attribute; |
211 | 221 |
|
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. |
213 | 226 | if (old_attribute) { |
214 | 227 | replace_attribute(*old_attribute, attribute, old_attribute_index); |
215 | 228 | } |
216 | | - // 5. Otherwise, append attr to element. |
| 229 | + // 7. Otherwise, append attr to element. |
217 | 230 | else { |
218 | 231 | append_attribute(attribute); |
219 | 232 | } |
220 | 233 |
|
221 | | - // 6. Return oldAttr. |
| 234 | + // 8. Return oldAttr. |
222 | 235 | return old_attribute; |
223 | 236 | } |
224 | 237 |
|
|
0 commit comments