Skip to content

Commit c591f8c

Browse files
tete17Lubrsi
authored andcommitted
LibWeb: Amend DomParser to make it compatible with TrustedTypes
1 parent 3328546 commit c591f8c

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

Libraries/LibWeb/HTML/DOMParser.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
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

1719
namespace 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
}

Libraries/LibWeb/HTML/DOMParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DOMParser final : public Bindings::PlatformObject {
2424

2525
virtual ~DOMParser() override;
2626

27-
GC::Ref<DOM::Document> parse_from_string(StringView, Bindings::DOMParserSupportedType type);
27+
WebIDL::ExceptionOr<GC::Root<DOM::Document>> parse_from_string(Utf16String, Bindings::DOMParserSupportedType type);
2828

2929
private:
3030
explicit DOMParser(JS::Realm&);

Libraries/LibWeb/HTML/DOMParser.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ enum DOMParserSupportedType {
1313
interface DOMParser {
1414
constructor();
1515

16-
Document parseFromString(DOMString string, DOMParserSupportedType type);
16+
Document parseFromString(Utf16DOMString string, DOMParserSupportedType type);
1717
};

Libraries/LibWeb/TrustedTypes/InjectionSink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace Web::TrustedTypes {
2121
__ENUMERATE_INJECTION_SINKS(Documentwrite, "Document write") \
2222
__ENUMERATE_INJECTION_SINKS(Documentwriteln, "Document writeln") \
2323
__ENUMERATE_INJECTION_SINKS(DocumentexecCommand, "Document execCommand") \
24+
__ENUMERATE_INJECTION_SINKS(DOMParserparseFromString, "DOMParser parseFromString") \
2425
__ENUMERATE_INJECTION_SINKS(ElementinnerHTML, "Element innerHTML") \
2526
__ENUMERATE_INJECTION_SINKS(ElementinsertAdjacentHTML, "Element insertAdjacentHTML") \
2627
__ENUMERATE_INJECTION_SINKS(ElementouterHTML, "Element outerHTML") \

0 commit comments

Comments
 (0)