Skip to content

Commit 1368744

Browse files
tete17Lubrsi
authored andcommitted
LibWeb: Amend Document interface to make it compatible with TrustedTypes
1 parent 2fa84f1 commit 1368744

File tree

4 files changed

+50
-25
lines changed

4 files changed

+50
-25
lines changed

Libraries/LibWeb/DOM/Document.cpp

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@
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();

Libraries/LibWeb/DOM/Document.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,8 @@ class WEB_API Document
472472

473473
void set_window(HTML::Window&);
474474

475-
WebIDL::ExceptionOr<void> write(Vector<String> const& strings);
476-
WebIDL::ExceptionOr<void> writeln(Vector<String> const& strings);
475+
WebIDL::ExceptionOr<void> write(Vector<TrustedTypes::TrustedHTMLOrString> const& text);
476+
WebIDL::ExceptionOr<void> writeln(Vector<TrustedTypes::TrustedHTMLOrString> const& text);
477477

478478
WebIDL::ExceptionOr<Document*> open(Optional<String> const& = {}, Optional<String> const& = {});
479479
WebIDL::ExceptionOr<GC::Ptr<HTML::WindowProxy>> open(StringView url, StringView name, StringView features);
@@ -828,7 +828,7 @@ class WEB_API Document
828828
Vector<GC::Root<Range>> find_matching_text(String const&, CaseSensitivity);
829829

830830
void parse_html_from_a_string(StringView);
831-
static GC::Ref<Document> parse_html_unsafe(JS::VM&, StringView);
831+
static WebIDL::ExceptionOr<GC::Root<DOM::Document>> parse_html_unsafe(JS::VM&, TrustedTypes::TrustedHTMLOrString const&);
832832

833833
void set_console_client(GC::Ptr<JS::ConsoleClient> console_client) { m_console_client = console_client; }
834834
GC::Ptr<JS::ConsoleClient> console_client() const { return m_console_client; }
@@ -972,7 +972,7 @@ class WEB_API Document
972972
Yes,
973973
No,
974974
};
975-
WebIDL::ExceptionOr<void> run_the_document_write_steps(Vector<String> const& text, AddLineFeed line_feed, TrustedTypes::InjectionSink sink);
975+
WebIDL::ExceptionOr<void> run_the_document_write_steps(Vector<TrustedTypes::TrustedHTMLOrString> const& text, AddLineFeed line_feed, TrustedTypes::InjectionSink sink);
976976

977977
void queue_intersection_observer_task();
978978
void queue_an_intersection_observer_entry(IntersectionObserver::IntersectionObserver&, HighResolutionTime::DOMHighResTimeStamp time, GC::Ref<Geometry::DOMRectReadOnly> root_bounds, GC::Ref<Geometry::DOMRectReadOnly> bounding_client_rect, GC::Ref<Geometry::DOMRectReadOnly> intersection_rect, bool is_intersecting, double intersection_ratio, GC::Ref<Element> target);

Libraries/LibWeb/DOM/Document.idl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import <HTML/HTMLScriptElement.idl>
2727
#import <HTML/Location.idl>
2828
#import <Selection/Selection.idl>
29+
#import <TrustedTypes/TrustedHTML.idl>
2930
#import <ViewTransition/ViewTransition.idl>
3031
#import <XPath/XPathResult.idl>
3132
#import <XPath/XPathExpression.idl>
@@ -56,13 +57,10 @@ interface Document : Node {
5657
[CEReactions] Document open(optional DOMString unused1, optional DOMString unused2);
5758
WindowProxy? open(USVString url, DOMString name, DOMString features);
5859
[CEReactions] undefined close();
59-
// FIXME: [CEReactions] undefined write((TrustedHTML or DOMString)... text);
60-
[CEReactions] undefined write(DOMString... text);
61-
// FIXME: [CEReactions] undefined writeln((TrustedHTML or DOMString)... text);
62-
[CEReactions] undefined writeln(DOMString... text);
60+
[CEReactions] undefined write((TrustedHTML or Utf16DOMString)... text);
61+
[CEReactions] undefined writeln((TrustedHTML or Utf16DOMString)... text);
6362

64-
// FIXME: static Document parseHTMLUnsafe((TrustedHTML or DOMString) html);
65-
static Document parseHTMLUnsafe(DOMString html);
63+
static Document parseHTMLUnsafe((TrustedHTML or Utf16DOMString) html);
6664

6765
attribute DOMString cookie;
6866

Libraries/LibWeb/TrustedTypes/InjectionSink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Web::TrustedTypes {
1717

1818
// https://w3c.github.io/trusted-types/dist/spec/#injection-sink
1919
#define ENUMERATE_INJECTION_SINKS \
20+
__ENUMERATE_INJECTION_SINKS(DocumentparseHTMLUnsafe, "Document parseHTMLUnsafe") \
2021
__ENUMERATE_INJECTION_SINKS(Documentwrite, "Document write") \
2122
__ENUMERATE_INJECTION_SINKS(Documentwriteln, "Document writeln") \
2223
__ENUMERATE_INJECTION_SINKS(DocumentexecCommand, "Document execCommand") \

0 commit comments

Comments
 (0)