Skip to content

Commit 3328546

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

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

Libraries/LibWeb/DOM/ShadowRoot.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <LibWeb/HTML/HTMLTemplateElement.h>
1414
#include <LibWeb/HTML/Parser/HTMLParser.h>
1515
#include <LibWeb/Layout/BlockContainer.h>
16+
#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
17+
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
1618

1719
namespace Web::DOM {
1820

@@ -63,22 +65,29 @@ EventTarget* ShadowRoot::get_parent(Event const& event)
6365
}
6466

6567
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-innerhtml
66-
WebIDL::ExceptionOr<String> ShadowRoot::inner_html() const
68+
WebIDL::ExceptionOr<TrustedTypes::TrustedHTMLOrString> ShadowRoot::inner_html() const
6769
{
68-
return TRY(serialize_fragment(HTML::RequireWellFormed::Yes)).to_utf8_but_should_be_ported_to_utf16();
70+
return TRY(serialize_fragment(HTML::RequireWellFormed::Yes));
6971
}
7072

7173
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-innerhtml
72-
WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(StringView value)
74+
WebIDL::ExceptionOr<void> ShadowRoot::set_inner_html(TrustedTypes::TrustedHTMLOrString const& value)
7375
{
74-
// 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, "ShadowRoot innerHTML", and "script".
76+
// 1. Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with
77+
// TrustedHTML, this's relevant global object, the given value, "ShadowRoot innerHTML", and "script".
78+
auto const compliant_string = TRY(TrustedTypes::get_trusted_type_compliant_string(
79+
TrustedTypes::TrustedTypeName::TrustedHTML,
80+
HTML::relevant_global_object(*this),
81+
value,
82+
TrustedTypes::InjectionSink::ShadowRootinnerHTML,
83+
TrustedTypes::Script.to_string()));
7584

7685
// 2. Let context be this's host.
7786
auto context = this->host();
7887
VERIFY(context);
7988

80-
// 3. Let fragment be the result of invoking the fragment parsing algorithm steps with context and compliantString. FIXME: Use compliantString instead of markup.
81-
auto fragment = TRY(context->parse_fragment(value));
89+
// 3. Let fragment be the result of invoking the fragment parsing algorithm steps with context and compliantString.
90+
auto fragment = TRY(context->parse_fragment(compliant_string.to_utf8_but_should_be_ported_to_utf16()));
8291

8392
// 4. Replace all with fragment within this.
8493
this->replace_all(fragment);
@@ -110,12 +119,19 @@ WebIDL::ExceptionOr<String> ShadowRoot::get_html(GetHTMLOptions const& options)
110119
}
111120

112121
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-shadowroot-sethtmlunsafe
113-
WebIDL::ExceptionOr<void> ShadowRoot::set_html_unsafe(StringView html)
114-
{
115-
// FIXME: 1. Let compliantHTML be the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, html, "ShadowRoot setHTMLUnsafe", and "script".
116-
117-
// 3. Unsafe set HTML given this, this's shadow host, and compliantHTML. FIXME: Use compliantHTML.
118-
TRY(unsafely_set_html(*this->host(), html));
122+
WebIDL::ExceptionOr<void> ShadowRoot::set_html_unsafe(TrustedTypes::TrustedHTMLOrString const& html)
123+
{
124+
// 1. Let compliantHTML be the result of invoking the Get Trusted Type compliant string algorithm with
125+
// TrustedHTML, this's relevant global object, html, "ShadowRoot setHTMLUnsafe", and "script".
126+
auto const compliant_html = TRY(TrustedTypes::get_trusted_type_compliant_string(
127+
TrustedTypes::TrustedTypeName::TrustedHTML,
128+
HTML::relevant_global_object(*this),
129+
html,
130+
TrustedTypes::InjectionSink::ShadowRootsetHTMLUnsafe,
131+
TrustedTypes::Script.to_string()));
132+
133+
// 2. Unsafely set HTML given this, this's shadow host, and compliantHTML.
134+
TRY(unsafely_set_html(*this->host(), compliant_html.to_utf8_but_should_be_ported_to_utf16()));
119135

120136
return {};
121137
}

Libraries/LibWeb/DOM/ShadowRoot.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ class WEB_API ShadowRoot final : public DocumentFragment {
4545
// ^EventTarget
4646
virtual EventTarget* get_parent(Event const&) override;
4747

48-
WebIDL::ExceptionOr<String> inner_html() const;
49-
WebIDL::ExceptionOr<void> set_inner_html(StringView);
48+
WebIDL::ExceptionOr<TrustedTypes::TrustedHTMLOrString> inner_html() const;
49+
WebIDL::ExceptionOr<void> set_inner_html(TrustedTypes::TrustedHTMLOrString const&);
5050

51-
WebIDL::ExceptionOr<void> set_html_unsafe(StringView);
51+
WebIDL::ExceptionOr<void> set_html_unsafe(TrustedTypes::TrustedHTMLOrString const&);
5252

5353
WebIDL::ExceptionOr<String> get_html(GetHTMLOptions const&) const;
5454

Libraries/LibWeb/DOM/ShadowRoot.idl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import <DOM/DocumentFragment.idl>
22
#import <DOM/DocumentOrShadowRoot.idl>
33
#import <DOM/Element.idl>
4+
#import <TrustedTypes/TrustedHTML.idl>
45

56
// https://dom.spec.whatwg.org/#shadowroot
67
[Exposed=Window]
@@ -15,12 +16,10 @@ interface ShadowRoot : DocumentFragment {
1516

1617
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-parsing-and-serialization
1718

18-
// FIXME: [CEReactions] undefined setHTMLUnsafe((TrustedHTML or DOMString) html);
19-
[CEReactions] undefined setHTMLUnsafe(DOMString html);
19+
[CEReactions] undefined setHTMLUnsafe((TrustedHTML or Utf16DOMString) html);
2020
DOMString getHTML(optional GetHTMLOptions options = {});
2121

22-
// FIXME: [CEReactions] attribute (TrustedHTML or [LegacyNullToEmptyString] DOMString) innerHTML;
23-
[CEReactions, LegacyNullToEmptyString] attribute DOMString innerHTML;
22+
[CEReactions, LegacyNullToEmptyString] attribute (TrustedHTML or Utf16DOMString) innerHTML;
2423
};
2524

2625
ShadowRoot includes DocumentOrShadowRoot;

Libraries/LibWeb/TrustedTypes/InjectionSink.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ namespace Web::TrustedTypes {
3434
__ENUMERATE_INJECTION_SINKS(Locationhref, "Location href") \
3535
__ENUMERATE_INJECTION_SINKS(RangecreateContextualFragment, "Range createContextualFragment") \
3636
__ENUMERATE_INJECTION_SINKS(ServiceWorkerContainerregister, "ServiceWorkerContainer register") \
37+
__ENUMERATE_INJECTION_SINKS(ShadowRootinnerHTML, "ShadowRoot innerHTML") \
38+
__ENUMERATE_INJECTION_SINKS(ShadowRootsetHTMLUnsafe, "ShadowRoot setHTMLUnsafe") \
3739
__ENUMERATE_INJECTION_SINKS(SharedWorkerconstructor, "SharedWorker constructor") \
3840
__ENUMERATE_INJECTION_SINKS(SVGScriptElementhref, "SVGScriptElement href") \
3941
__ENUMERATE_INJECTION_SINKS(Workerconstructor, "Worker constructor") \

0 commit comments

Comments
 (0)