From adaad653ca9bc866b872c3469252fe31936dac9d Mon Sep 17 00:00:00 2001 From: Tete17 Date: Thu, 31 Jul 2025 17:08:30 +0200 Subject: [PATCH] LibWeb: Implement TrustedScriptURL class --- Libraries/LibWeb/CMakeLists.txt | 1 + Libraries/LibWeb/Forward.h | 1 + .../LibWeb/TrustedTypes/TrustedScriptURL.cpp | 43 +++++++++++++++++++ .../LibWeb/TrustedTypes/TrustedScriptURL.h | 32 ++++++++++++++ .../LibWeb/TrustedTypes/TrustedScriptURL.idl | 6 +++ Libraries/LibWeb/idl_files.cmake | 1 + .../BindingsGenerator/IDLGenerators.cpp | 1 + .../Text/expected/all-window-properties.txt | 1 + 8 files changed, 86 insertions(+) create mode 100644 Libraries/LibWeb/TrustedTypes/TrustedScriptURL.cpp create mode 100644 Libraries/LibWeb/TrustedTypes/TrustedScriptURL.h create mode 100644 Libraries/LibWeb/TrustedTypes/TrustedScriptURL.idl diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 0349ad9c046..11c816f3330 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -899,6 +899,7 @@ set(SOURCES SVG/ViewBox.cpp TrustedTypes/TrustedHTML.cpp TrustedTypes/TrustedScript.cpp + TrustedTypes/TrustedScriptURL.cpp TrustedTypes/TrustedTypePolicy.cpp TrustedTypes/TrustedTypePolicyFactory.cpp UIEvents/CompositionEvent.cpp diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index 2f106552683..3d6eddd9082 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -1222,6 +1222,7 @@ namespace Web::TrustedTypes { class TrustedHTML; class TrustedScript; +class TrustedScriptURL; class TrustedTypePolicy; class TrustedTypePolicyFactory; struct TrustedTypePolicyOptions; diff --git a/Libraries/LibWeb/TrustedTypes/TrustedScriptURL.cpp b/Libraries/LibWeb/TrustedTypes/TrustedScriptURL.cpp new file mode 100644 index 00000000000..6cef8b0065d --- /dev/null +++ b/Libraries/LibWeb/TrustedTypes/TrustedScriptURL.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025, Miguel Sacristán Izcue + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include +#include +#include + +namespace Web::TrustedTypes { + +GC_DEFINE_ALLOCATOR(TrustedScriptURL); + +TrustedScriptURL::TrustedScriptURL(JS::Realm& realm, Utf16String data) + : PlatformObject(realm) + , m_data(move(data)) +{ +} + +void TrustedScriptURL::initialize(JS::Realm& realm) +{ + WEB_SET_PROTOTYPE_FOR_INTERFACE(TrustedScriptURL); + Base::initialize(realm); +} + +// https://w3c.github.io/trusted-types/dist/spec/#trustedscripturl-stringification-behavior +Utf16String const& TrustedScriptURL::to_string() const +{ + // 1. return the associated data value. + return m_data; +} + +// https://w3c.github.io/trusted-types/dist/spec/#dom-trustedscripturl-tojson +Utf16String const& TrustedScriptURL::to_json() const +{ + // 1. return the associated data value. + return to_string(); +} + +} diff --git a/Libraries/LibWeb/TrustedTypes/TrustedScriptURL.h b/Libraries/LibWeb/TrustedTypes/TrustedScriptURL.h new file mode 100644 index 00000000000..d784c8cba81 --- /dev/null +++ b/Libraries/LibWeb/TrustedTypes/TrustedScriptURL.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2025, Miguel Sacristán Izcue + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::TrustedTypes { + +class TrustedScriptURL final : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(TrustedScriptURL, Bindings::PlatformObject); + GC_DECLARE_ALLOCATOR(TrustedScriptURL); + +public: + virtual ~TrustedScriptURL() override = default; + + Utf16String const& to_string() const; + Utf16String const& to_json() const; + +private: + explicit TrustedScriptURL(JS::Realm&, Utf16String); + virtual void initialize(JS::Realm&) override; + + Utf16String const m_data; +}; + +} diff --git a/Libraries/LibWeb/TrustedTypes/TrustedScriptURL.idl b/Libraries/LibWeb/TrustedTypes/TrustedScriptURL.idl new file mode 100644 index 00000000000..4ed37028ec4 --- /dev/null +++ b/Libraries/LibWeb/TrustedTypes/TrustedScriptURL.idl @@ -0,0 +1,6 @@ +// https://w3c.github.io/trusted-types/dist/spec/#trused-script-url +[Exposed=(Window,Worker)] +interface TrustedScriptURL { + stringifier; + Utf16DOMString toJSON(); +}; diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake index 1657ecef276..5cf126e0dd6 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -330,6 +330,7 @@ libweb_js_bindings(Streams/WritableStreamDefaultController) libweb_js_bindings(Streams/WritableStreamDefaultWriter) libweb_js_bindings(TrustedTypes/TrustedHTML) libweb_js_bindings(TrustedTypes/TrustedScript) +libweb_js_bindings(TrustedTypes/TrustedScriptURL) libweb_js_bindings(TrustedTypes/TrustedTypePolicy) libweb_js_bindings(TrustedTypes/TrustedTypePolicyFactory) libweb_js_bindings(SVG/SVGAElement) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 804d36f7242..89ac6d8772e 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -118,6 +118,7 @@ static bool is_platform_object(Type const& type) "TimeRanges"sv, "TrustedHTML"sv, "TrustedScript"sv, + "TrustedScriptURL"sv, "TrustedTypePolicy"sv, "TrustedTypePolicyFactory"sv, "URLSearchParams"sv, diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index 8c98d18ec93..2faae0f5e08 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -434,6 +434,7 @@ TransitionEvent TreeWalker TrustedHTML TrustedScript +TrustedScriptURL TrustedTypePolicy TrustedTypePolicyFactory TypeError