8989#include < LibWeb/Painting/ViewportPaintable.h>
9090#include < LibWeb/SVG/SVGAElement.h>
9191#include < LibWeb/Selection/Selection.h>
92+ #include < LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
9293#include < LibWeb/TrustedTypes/TrustedTypePolicy.h>
9394#include < LibWeb/WebIDL/AbstractOperations.h>
9495#include < LibWeb/WebIDL/DOMException.h>
@@ -370,7 +371,7 @@ WebIDL::ExceptionOr<void> Element::set_attribute_ns(Optional<FlyString> const& n
370371 auto extracted_qualified_name = TRY (validate_and_extract (realm (), namespace_, qualified_name, ValidationContext::Element));
371372
372373 // 2. Let verifiedValue be the result of calling get Trusted Types-compliant attribute value
373- // with localName, namespace, element , and value.
374+ // with localName, namespace, this , and value.
374375 auto const verified_value = TRY (TrustedTypes::get_trusted_types_compliant_attribute_value (
375376 extracted_qualified_name.local_name (),
376377 extracted_qualified_name.namespace_ ().has_value () ? Utf16String::from_utf8 (extracted_qualified_name.namespace_ ().value ()) : Optional<Utf16String>(),
@@ -1057,15 +1058,22 @@ WebIDL::ExceptionOr<DOM::Element const*> Element::closest(StringView selectors)
10571058}
10581059
10591060// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-innerhtml
1060- WebIDL::ExceptionOr<void > Element::set_inner_html (StringView value)
1061+ WebIDL::ExceptionOr<void > Element::set_inner_html (TrustedTypes::TrustedHTMLOrString const & value)
10611062{
1062- // FIXME: 1. Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, the given value, "Element innerHTML", and "script".
1063+ // 1. Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with
1064+ // TrustedHTML, this's relevant global object, the given value, "Element innerHTML", and "script".
1065+ auto const compliant_string = TRY (TrustedTypes::get_trusted_type_compliant_string (
1066+ TrustedTypes::TrustedTypeName::TrustedHTML,
1067+ HTML::relevant_global_object (*this ),
1068+ value,
1069+ TrustedTypes::InjectionSink::ElementinnerHTML,
1070+ TrustedTypes::Script.to_string ()));
10631071
10641072 // 2. Let context be this.
10651073 DOM::Node* context = this ;
10661074
1067- // 3. Let fragment be the result of invoking the fragment parsing algorithm steps with context and compliantString. FIXME: Use compliantString.
1068- auto fragment = TRY (as<Element>(*context).parse_fragment (value ));
1075+ // 3. Let fragment be the result of invoking the fragment parsing algorithm steps with context and compliantString.
1076+ auto fragment = TRY (as<Element>(*context).parse_fragment (compliant_string. to_utf8_but_should_be_ported_to_utf16 () ));
10691077
10701078 // 4. If context is a template element, then set context to the template element's template contents (a DocumentFragment).
10711079 auto * template_element = as_if<HTML::HTMLTemplateElement>(*context);
@@ -1089,9 +1097,9 @@ WebIDL::ExceptionOr<void> Element::set_inner_html(StringView value)
10891097}
10901098
10911099// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-innerhtml
1092- WebIDL::ExceptionOr<String > Element::inner_html () const
1100+ WebIDL::ExceptionOr<TrustedTypes::TrustedHTMLOrString > Element::inner_html () const
10931101{
1094- return serialize_fragment (HTML::RequireWellFormed::Yes);
1102+ return TRY ( serialize_fragment (HTML::RequireWellFormed::Yes) );
10951103}
10961104
10971105bool Element::is_focused () const
@@ -2098,15 +2106,22 @@ WebIDL::ExceptionOr<GC::Ref<DOM::DocumentFragment>> Element::parse_fragment(Stri
20982106}
20992107
21002108// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-outerhtml
2101- WebIDL::ExceptionOr<String > Element::outer_html () const
2109+ WebIDL::ExceptionOr<TrustedTypes::TrustedHTMLOrString > Element::outer_html () const
21022110{
2103- return serialize_fragment (HTML::RequireWellFormed::Yes, FragmentSerializationMode::Outer);
2111+ return TRY ( serialize_fragment (HTML::RequireWellFormed::Yes, FragmentSerializationMode::Outer) );
21042112}
21052113
21062114// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-outerhtml
2107- WebIDL::ExceptionOr<void > Element::set_outer_html (String const & value)
2115+ WebIDL::ExceptionOr<void > Element::set_outer_html (TrustedTypes::TrustedHTMLOrString const & value)
21082116{
2109- // 1. FIXME: Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, the given value, "Element outerHTML", and "script".
2117+ // 1. Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with
2118+ // TrustedHTML, this's relevant global object, the given value, "Element outerHTML", and "script".
2119+ auto const compliant_string = TRY (TrustedTypes::get_trusted_type_compliant_string (
2120+ TrustedTypes::TrustedTypeName::TrustedHTML,
2121+ HTML::relevant_global_object (*this ),
2122+ value,
2123+ TrustedTypes::InjectionSink::ElementouterHTML,
2124+ TrustedTypes::Script.to_string ()));
21102125
21112126 // 2. Let parent be this's parent.
21122127 auto * parent = this ->parent ();
@@ -2123,8 +2138,8 @@ WebIDL::ExceptionOr<void> Element::set_outer_html(String const& value)
21232138 if (parent->is_document_fragment ())
21242139 parent = TRY (create_element (document (), HTML::TagNames::body, Namespace::HTML));
21252140
2126- // 6. Let fragment be the result of invoking the fragment parsing algorithm steps given parent and compliantString. FIXME: Use compliantString.
2127- auto fragment = TRY (as<Element>(*parent).parse_fragment (value ));
2141+ // 6. Let fragment be the result of invoking the fragment parsing algorithm steps given parent and compliantString.
2142+ auto fragment = TRY (as<Element>(*parent).parse_fragment (compliant_string. to_utf8_but_should_be_ported_to_utf16 () ));
21282143
21292144 // 6. Replace this with fragment within this's parent.
21302145 TRY (parent->replace_child (fragment, *this ));
@@ -2133,12 +2148,21 @@ WebIDL::ExceptionOr<void> Element::set_outer_html(String const& value)
21332148}
21342149
21352150// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#the-insertadjacenthtml()-method
2136- WebIDL::ExceptionOr<void > Element::insert_adjacent_html (String const & position, String const & string)
2137- {
2138- // 1. Let context be null.
2151+ WebIDL::ExceptionOr<void > Element::insert_adjacent_html (String const & position, TrustedTypes::TrustedHTMLOrString const & string)
2152+ {
2153+ // 1. Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with
2154+ // TrustedHTML, this's relevant global object, string, "Element insertAdjacentHTML", and "script".
2155+ auto const compliant_string = TRY (TrustedTypes::get_trusted_type_compliant_string (
2156+ TrustedTypes::TrustedTypeName::TrustedHTML,
2157+ HTML::relevant_global_object (*this ),
2158+ string,
2159+ TrustedTypes::InjectionSink::ElementinsertAdjacentHTML,
2160+ TrustedTypes::Script.to_string ()));
2161+
2162+ // 2. Let context be null.
21392163 GC::Ptr<Node> context;
21402164
2141- // 2 . Use the first matching item from this list:
2165+ // 3 . Use the first matching item from this list:
21422166 // - If position is an ASCII case-insensitive match for the string "beforebegin"
21432167 // - If position is an ASCII case-insensitive match for the string "afterend"
21442168 if (position.equals_ignoring_ascii_case (" beforebegin" sv)
@@ -2163,7 +2187,7 @@ WebIDL::ExceptionOr<void> Element::insert_adjacent_html(String const& position,
21632187 return WebIDL::SyntaxError::create (realm (), " insertAdjacentHTML: invalid position argument" _utf16);
21642188 }
21652189
2166- // 3 . If context is not an Element or the following are all true:
2190+ // 4 . If context is not an Element or the following are all true:
21672191 // - context's node document is an HTML document,
21682192 // - context's local name is "html", and
21692193 // - context's namespace is the HTML namespace;
@@ -2175,10 +2199,10 @@ WebIDL::ExceptionOr<void> Element::insert_adjacent_html(String const& position,
21752199 context = TRY (create_element (document (), HTML::TagNames::body, Namespace::HTML));
21762200 }
21772201
2178- // 4 . Let fragment be the result of invoking the fragment parsing algorithm steps with context and string .
2179- auto fragment = TRY (as<Element>(*context).parse_fragment (string ));
2202+ // 5 . Let fragment be the result of invoking the fragment parsing algorithm steps with context and compliantString .
2203+ auto fragment = TRY (as<Element>(*context).parse_fragment (compliant_string. to_utf8_but_should_be_ported_to_utf16 () ));
21802204
2181- // 5 . Use the first matching item from this list:
2205+ // 6 . Use the first matching item from this list:
21822206
21832207 // - If position is an ASCII case-insensitive match for the string "beforebegin"
21842208 if (position.equals_ignoring_ascii_case (" beforebegin" sv)) {
@@ -3936,17 +3960,24 @@ WebIDL::ExceptionOr<String> Element::get_html(GetHTMLOptions const& options) con
39363960}
39373961
39383962// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-sethtmlunsafe
3939- WebIDL::ExceptionOr<void > Element::set_html_unsafe (StringView html)
3963+ WebIDL::ExceptionOr<void > Element::set_html_unsafe (TrustedTypes::TrustedHTMLOrString const & html)
39403964{
3941- // FIXME: 1. Let compliantHTML be the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, html, "Element setHTMLUnsafe", and "script".
3965+ // 1. Let compliantHTML be the result of invoking the Get Trusted Type compliant string algorithm with
3966+ // TrustedHTML, this's relevant global object, html, "Element setHTMLUnsafe", and "script".
3967+ auto const compliant_html = TRY (TrustedTypes::get_trusted_type_compliant_string (
3968+ TrustedTypes::TrustedTypeName::TrustedHTML,
3969+ HTML::relevant_global_object (*this ),
3970+ html,
3971+ TrustedTypes::InjectionSink::ElementsetHTMLUnsafe,
3972+ TrustedTypes::Script.to_string ()));
39423973
39433974 // 2. Let target be this's template contents if this is a template element; otherwise this.
39443975 DOM::Node* target = this ;
39453976 if (is<HTML::HTMLTemplateElement>(*this ))
39463977 target = as<HTML::HTMLTemplateElement>(*this ).content ().ptr ();
39473978
3948- // 3. Unsafe set HTML given target, this, and compliantHTML. FIXME: Use compliantHTML.
3949- TRY (target->unsafely_set_html (*this , html ));
3979+ // 3. Unsafe set HTML given target, this, and compliantHTML.
3980+ TRY (target->unsafely_set_html (*this , compliant_html. to_utf8_but_should_be_ported_to_utf16 () ));
39503981
39513982 return {};
39523983}
0 commit comments