1212#include < LibWeb/Editing/Commands.h>
1313#include < LibWeb/Editing/Internal/Algorithms.h>
1414#include < LibWeb/Selection/Selection.h>
15+ #include < LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
16+ #include < LibWeb/TrustedTypes/TrustedHTML.h>
17+ #include < LibWeb/TrustedTypes/TrustedTypePolicy.h>
1518#include < LibWeb/UIEvents/InputEvent.h>
1619
1720namespace Web ::DOM {
1821
1922// https://w3c.github.io/editing/docs/execCommand/#execcommand()
20- WebIDL::ExceptionOr<bool > Document::exec_command (FlyString const & command, [[maybe_unused]] bool show_ui, Utf16String const & value)
23+ WebIDL::ExceptionOr<bool > Document::exec_command (FlyString const & command, [[maybe_unused]] bool show_ui, TrustedTypes::TrustedHTMLOrString const & value)
2124{
25+ Utf16String compliant_string;
26+ if (command.equals_ignoring_ascii_case (Editing::CommandNames::insertHTML)) {
27+ // AD-HOC: The spec has been abandoned but there is a WPT tests checking weather this api follows TrustedTypes
28+ compliant_string = TRY (TrustedTypes::get_trusted_type_compliant_string (
29+ TrustedTypes::TrustedTypeName::TrustedHTML,
30+ relevant_global_object (*this ),
31+ value.downcast <TrustedTypes::TrustedHTMLOrString>(),
32+ TrustedTypes::InjectionSink::DocumentexecCommand,
33+ TrustedTypes::Script.to_string ()));
34+ } else {
35+ compliant_string = value.downcast <TrustedTypes::TrustedHTMLOrString>().visit (
36+ [](auto const & value) { return value->to_string (); },
37+ [](Utf16String const & value) { return value; });
38+ }
39+
2240 // AD-HOC: This is not directly mentioned in the spec, but all major browsers limit editing API calls to HTML documents
2341 if (!is_html_document ())
2442 return WebIDL::InvalidStateError::create (realm (), " execCommand is only supported on HTML documents" _utf16);
@@ -101,7 +119,7 @@ WebIDL::ExceptionOr<bool> Document::exec_command(FlyString const& command, [[may
101119 auto old_character_data_version = character_data_version ();
102120
103121 // 5. Take the action for command, passing value to the instructions as an argument.
104- auto command_result = command_definition.action (*this , value );
122+ auto command_result = command_definition.action (*this , compliant_string );
105123
106124 // https://w3c.github.io/editing/docs/execCommand/#preserves-overrides
107125 // After taking the action, if the active range is collapsed, it must restore states and values from the recorded
@@ -125,7 +143,7 @@ WebIDL::ExceptionOr<bool> Document::exec_command(FlyString const& command, [[may
125143
126144 // AD-HOC: For insertText, we do what other browsers do and set data to value.
127145 if (command == Editing::CommandNames::insertText)
128- event_init.data = value ;
146+ event_init.data = compliant_string ;
129147
130148 auto event = UIEvents::InputEvent::create_from_platform_event (realm (), HTML::EventNames::input, event_init);
131149 event->set_is_trusted (true );
0 commit comments