diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 7aca27c004c..9dc981e42c0 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -897,6 +897,7 @@ set(SOURCES SVG/TagNames.cpp SVG/ViewBox.cpp TrustedTypes/TrustedHTML.cpp + TrustedTypes/TrustedTypePolicy.cpp TrustedTypes/TrustedTypePolicyFactory.cpp UIEvents/CompositionEvent.cpp UIEvents/EventNames.cpp diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index 8f3ff311e54..b9e1c9b61b1 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -1221,6 +1221,8 @@ ErrorOr decode(Decoder&); namespace Web::TrustedTypes { class TrustedHTML; +class TrustedTypePolicy; class TrustedTypePolicyFactory; +struct TrustedTypePolicyOptions; } diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp new file mode 100644 index 00000000000..096bbfa6b59 --- /dev/null +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025, Miguel Sacristán Izcue + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include +#include +#include + +namespace Web::TrustedTypes { + +GC_DEFINE_ALLOCATOR(TrustedTypePolicy); + +TrustedTypePolicy::TrustedTypePolicy(JS::Realm& realm) + : PlatformObject(realm) +{ +} + +void TrustedTypePolicy::initialize(JS::Realm& realm) +{ + WEB_SET_PROTOTYPE_FOR_INTERFACE(TrustedTypePolicy); + Base::initialize(realm); +} + +} diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h new file mode 100644 index 00000000000..f33471ae801 --- /dev/null +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025, Miguel Sacristán Izcue + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::TrustedTypes { + +struct TrustedTypePolicyOptions { + GC::Root create_html; + GC::Root create_script; + GC::Root create_script_url; +}; + +class TrustedTypePolicy final : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(TrustedTypePolicy, Bindings::PlatformObject); + GC_DECLARE_ALLOCATOR(TrustedTypePolicy); + +public: + virtual ~TrustedTypePolicy() override = default; + +private: + explicit TrustedTypePolicy(JS::Realm&); + virtual void initialize(JS::Realm&) override; +}; + +} diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl new file mode 100644 index 00000000000..b98cb21a107 --- /dev/null +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl @@ -0,0 +1,18 @@ +// https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy +[Exposed=(Window,Worker)] +interface TrustedTypePolicy { + [FIXME] readonly attribute DOMString name; + [FIXME] TrustedHTML createHTML(DOMString input, any... arguments); + [FIXME] TrustedScript createScript(DOMString input, any... arguments); + [FIXME] TrustedScriptURL createScriptURL(DOMString input, any... arguments); +}; + +// https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy-options +dictionary TrustedTypePolicyOptions { + CreateHTMLCallback createHTML; + CreateScriptCallback createScript; + CreateScriptURLCallback createScriptURL; +}; +callback CreateHTMLCallback = DOMString? (DOMString input, any... arguments); +callback CreateScriptCallback = DOMString? (DOMString input, any... arguments); +callback CreateScriptURLCallback = USVString? (DOMString input, any... arguments); diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake index 5dedd37a66d..1e435fdfb56 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -329,6 +329,7 @@ libweb_js_bindings(Streams/WritableStream) libweb_js_bindings(Streams/WritableStreamDefaultController) libweb_js_bindings(Streams/WritableStreamDefaultWriter) libweb_js_bindings(TrustedTypes/TrustedHTML) +libweb_js_bindings(TrustedTypes/TrustedTypePolicy) libweb_js_bindings(TrustedTypes/TrustedTypePolicyFactory) libweb_js_bindings(SVG/SVGAElement) libweb_js_bindings(SVG/SVGAnimatedEnumeration) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 444d0d0b123..f0b23fb4c73 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -117,6 +117,7 @@ static bool is_platform_object(Type const& type) "TextTrack"sv, "TimeRanges"sv, "TrustedHTML"sv, + "TrustedTypePolicy"sv, "TrustedTypePolicyFactory"sv, "URLSearchParams"sv, "VTTRegion"sv, diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index 311cb31f739..684e1e25c0d 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -433,6 +433,7 @@ TransformStreamDefaultController TransitionEvent TreeWalker TrustedHTML +TrustedTypePolicy TrustedTypePolicyFactory TypeError UIEvent