165165#include < LibWeb/SVG/SVGStyleElement.h>
166166#include < LibWeb/SVG/SVGTitleElement.h>
167167#include < LibWeb/Selection/Selection.h>
168+ #include < LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
169+ #include < LibWeb/TrustedTypes/TrustedTypePolicy.h>
168170#include < LibWeb/UIEvents/CompositionEvent.h>
169171#include < LibWeb/UIEvents/EventNames.h>
170172#include < LibWeb/UIEvents/FocusEvent.h>
@@ -644,41 +646,56 @@ GC::Ptr<Selection::Selection> Document::get_selection() const
644646}
645647
646648// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-write
647- WebIDL::ExceptionOr<void > Document::write (Vector<String > const & text)
649+ WebIDL::ExceptionOr<void > Document::write (Vector<TrustedTypes::TrustedHTMLOrString > const & text)
648650{
649651 // The document.write(...text) method steps are to run the document write steps with this, text, false, and "Document write".
650652 return run_the_document_write_steps (text, AddLineFeed::No, TrustedTypes::InjectionSink::Documentwrite);
651653}
652654
653655// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-writeln
654- WebIDL::ExceptionOr<void > Document::writeln (Vector<String > const & text)
656+ WebIDL::ExceptionOr<void > Document::writeln (Vector<TrustedTypes::TrustedHTMLOrString > const & text)
655657{
656658 // The document.writeln(...text) method steps are to run the document write steps with this, text, true, and "Document writeln".
657659 return run_the_document_write_steps (text, AddLineFeed::Yes, TrustedTypes::InjectionSink::Documentwriteln);
658660}
659661
660662// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#document-write-steps
661- WebIDL::ExceptionOr<void > Document::run_the_document_write_steps (Vector<String > const & text, AddLineFeed line_feed, TrustedTypes::InjectionSink sink)
663+ WebIDL::ExceptionOr<void > Document::run_the_document_write_steps (Vector<TrustedTypes::TrustedHTMLOrString > const & text, AddLineFeed line_feed, TrustedTypes::InjectionSink sink)
662664{
663665 // 1. Let string be the empty string.
664666 StringBuilder string;
665667
666668 // 2. Let isTrusted be false if text contains a string; otherwise true.
667- // FIXME: We currently only accept strings. Revisit this once we support the TrustedHTML type.
668669 auto is_trusted = true ;
670+ for (auto const & value : text) {
671+ if (value.has <Utf16String>()) {
672+ is_trusted = false ;
673+ break ;
674+ }
675+ }
669676
670677 // 3. For each value of text:
671678 for (auto const & value : text) {
672- // FIXME: 1. If value is a TrustedHTML object, then append value's associated data to string.
673-
674- // 2. Otherwise, append value to string.
675- string.append (value);
679+ string.append (value.visit (
680+ // 1. If value is a TrustedHTML object, then append value's associated data to string.
681+ [](GC::Root<TrustedTypes::TrustedHTML> const & value) { return value->to_string (); },
682+ // 2. Otherwise, append value to string.
683+ [](Utf16String const & value) { return value; })
684+ .to_utf8_but_should_be_ported_to_utf16 ());
676685 }
677686
678- // FIXME: 4. If isTrusted is false, set string to the result of invoking the Get Trusted Type compliant string algorithm
687+ // 4. If isTrusted is false, set string to the result of invoking the Get Trusted Type compliant string algorithm
679688 // with TrustedHTML, this's relevant global object, string, sink, and "script".
680- (void )is_trusted;
681- (void )sink;
689+ if (!is_trusted) {
690+ auto const new_string = TRY (TrustedTypes::get_trusted_type_compliant_string (
691+ TrustedTypes::TrustedTypeName::TrustedHTML,
692+ relevant_global_object (*this ),
693+ Utf16String::from_utf8 (MUST (string.to_string ())),
694+ sink,
695+ TrustedTypes::Script.to_string ()));
696+ string.clear ();
697+ string.append (new_string.to_utf8_but_should_be_ported_to_utf16 ());
698+ }
682699
683700 // 5. If lineFeed is true, append U+000A LINE FEED to string.
684701 if (line_feed == AddLineFeed::Yes)
@@ -6345,10 +6362,19 @@ void Document::parse_html_from_a_string(StringView html)
63456362}
63466363
63476364// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-parsehtmlunsafe
6348- GC::Ref< Document> Document::parse_html_unsafe (JS::VM& vm, StringView html)
6365+ WebIDL::ExceptionOr< GC::Root<DOM:: Document>> Document::parse_html_unsafe (JS::VM& vm, TrustedTypes::TrustedHTMLOrString const & html)
63496366{
63506367 auto & realm = *vm.current_realm ();
6351- // FIXME: 1. Let compliantHTML to the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, html, "Document parseHTMLUnsafe", and "script".
6368+
6369+ // FIXME: update description once https://github.com/whatwg/html/issues/11778 gets solved
6370+ // 1. Let compliantHTML to the result of invoking the Get Trusted Type compliant string algorithm with
6371+ // TrustedHTML, this's relevant global object, html, "Document parseHTMLUnsafe", and "script".
6372+ auto const compliant_html = TRY (TrustedTypes::get_trusted_type_compliant_string (
6373+ TrustedTypes::TrustedTypeName::TrustedHTML,
6374+ HTML::current_principal_global_object (),
6375+ html,
6376+ TrustedTypes::InjectionSink::DocumentparseHTMLUnsafe,
6377+ TrustedTypes::Script.to_string ()));
63526378
63536379 // 2. Let document be a new Document, whose content type is "text/html".
63546380 auto document = Document::create_for_fragment_parsing (realm);
@@ -6357,8 +6383,8 @@ GC::Ref<Document> Document::parse_html_unsafe(JS::VM& vm, StringView html)
63576383 // 3. Set document's allow declarative shadow roots to true.
63586384 document->set_allow_declarative_shadow_roots (true );
63596385
6360- // 4. Parse HTML from a string given document and compliantHTML. // FIXME: Use compliantHTML.
6361- document->parse_html_from_a_string (html );
6386+ // 4. Parse HTML from a string given document and compliantHTML.
6387+ document->parse_html_from_a_string (compliant_html. to_utf8_but_should_be_ported_to_utf16 () );
63626388
63636389 // AD-HOC: Setting the origin to match that of the associated document matches the behavior of existing browsers.
63646390 auto & associated_document = as<HTML::Window>(realm.global_object ()).associated_document ();
0 commit comments