1212#include < LibWeb/HTML/Parser/HTMLParser.h>
1313#include < LibWeb/HTML/Scripting/Environments.h>
1414#include < LibWeb/HTML/Window.h>
15+ #include < LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
16+ #include < LibWeb/TrustedTypes/TrustedTypePolicy.h>
1517#include < LibWeb/XML/XMLDocumentBuilder.h>
1618
1719namespace Web ::HTML {
@@ -37,9 +39,16 @@ void DOMParser::initialize(JS::Realm& realm)
3739}
3840
3941// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring
40- GC::Ref <DOM::Document> DOMParser::parse_from_string (StringView string, Bindings::DOMParserSupportedType type)
42+ WebIDL::ExceptionOr< GC::Root <DOM::Document>> DOMParser::parse_from_string (Utf16String string, Bindings::DOMParserSupportedType type)
4143{
42- // FIXME: 1. Let compliantString to the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, string, "DOMParser parseFromString", and "script".
44+ // 1. Let compliantString to the result of invoking the Get Trusted Type compliant string algorithm with
45+ // TrustedHTML, this's relevant global object, string, "DOMParser parseFromString", and "script".
46+ auto const compliant_string = TRY (TrustedTypes::get_trusted_type_compliant_string (
47+ TrustedTypes::TrustedTypeName::TrustedHTML,
48+ relevant_global_object (*this ),
49+ move (string),
50+ TrustedTypes::InjectionSink::DOMParserparseFromString,
51+ TrustedTypes::Script.to_string ()));
4352
4453 // 2. Let document be a new Document, whose content type is type and url is this's relevant global object's associated Document's URL.
4554 GC::Ptr<DOM::Document> document;
@@ -52,18 +61,19 @@ GC::Ref<DOM::Document> DOMParser::parse_from_string(StringView string, Bindings:
5261 document->set_content_type (Bindings::idl_enum_to_string (type));
5362 document->set_document_type (DOM::Document::Type::HTML);
5463
55- // 1. Parse HTML from a string given document and compliantString. FIXME: Use compliantString.
56- document->parse_html_from_a_string (string );
64+ // 1. Parse HTML from a string given document and compliantString.
65+ document->parse_html_from_a_string (compliant_string. to_utf8_but_should_be_ported_to_utf16 () );
5766 } else {
5867 // -> Otherwise
5968 document = DOM::Document::create (realm (), associated_document.url ());
6069 document->set_content_type (Bindings::idl_enum_to_string (type));
6170 document->set_document_type (DOM::Document::Type::XML);
6271
6372 // 1. Create an XML parser parse, associated with document, and with XML scripting support disabled.
64- XML::Parser parser (string, { .resolve_external_resource = resolve_xml_resource });
73+ auto const utf8_complaint_string = compliant_string.to_utf8_but_should_be_ported_to_utf16 ();
74+ XML::Parser parser (utf8_complaint_string, { .resolve_external_resource = resolve_xml_resource });
6575 XMLDocumentBuilder builder { *document, XMLScriptingSupport::Disabled };
66- // 2. Parse compliantString using parser. FIXME: Use compliantString.
76+ // 2. Parse compliantString using parser.
6777 auto result = parser.parse_with_listener (builder);
6878 // 3. If the previous step resulted in an XML well-formedness or XML namespace well-formedness error, then:
6979 if (result.is_error () || builder.has_error ()) {
@@ -84,7 +94,7 @@ GC::Ref<DOM::Document> DOMParser::parse_from_string(StringView string, Bindings:
8494 document->set_origin (associated_document.origin ());
8595
8696 // 3. Return document.
87- return * document;
97+ return document;
8898}
8999
90100}
0 commit comments