LibWeb: Implement methods dependant on TrustedScriptURL

This commit is contained in:
Tete17 2025-08-08 23:42:42 +02:00 committed by Luke Wilde
commit bc9e67a0dc
Notes: github-actions[bot] 2025-08-11 11:22:53 +00:00
6 changed files with 34 additions and 3 deletions

View file

@ -11,6 +11,7 @@
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/TrustedTypes/TrustedHTML.h>
#include <LibWeb/TrustedTypes/TrustedScript.h>
#include <LibWeb/TrustedTypes/TrustedScriptURL.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/CallbackType.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
@ -64,6 +65,22 @@ WebIDL::ExceptionOr<GC::Root<TrustedScript>> TrustedTypePolicy::create_script(St
return trusted_type.get<GC::Root<TrustedScript>>();
}
// https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createscripturl
WebIDL::ExceptionOr<GC::Root<TrustedScriptURL>> TrustedTypePolicy::create_script_url(String const& input, GC::RootVector<JS::Value> const& arguments)
{
// 1. Returns the result of executing the Create a Trusted Type algorithm, with the following arguments:
// policy
// this value
// trustedTypeName
// "TrustedScriptURL"
// value
// input
// arguments
// arguments
auto const trusted_type = TRY(create_a_trusted_type(TrustedTypeName::TrustedScriptURL, input, arguments));
return trusted_type.get<GC::Root<TrustedScriptURL>>();
}
// https://w3c.github.io/trusted-types/dist/spec/#create-a-trusted-type-algorithm
TrustedTypesVariants TrustedTypePolicy::create_a_trusted_type(TrustedTypeName trusted_type_name, String const& value, GC::RootVector<JS::Value> const& arguments)
{
@ -99,6 +116,8 @@ TrustedTypesVariants TrustedTypePolicy::create_a_trusted_type(TrustedTypeName tr
return realm.create<TrustedHTML>(realm, move(data_string));
case TrustedTypeName::TrustedScript:
return realm.create<TrustedScript>(realm, move(data_string));
case TrustedTypeName::TrustedScriptURL:
return realm.create<TrustedScriptURL>(realm, move(data_string));
default:
VERIFY_NOT_REACHED();
}

View file

@ -14,7 +14,8 @@ namespace Web::TrustedTypes {
using TrustedTypesVariants = WebIDL::ExceptionOr<Variant<
GC::Root<TrustedHTML>,
GC::Root<TrustedScript>>>;
GC::Root<TrustedScript>,
GC::Root<TrustedScriptURL>>>;
enum class TrustedTypeName {
TrustedHTML,
@ -44,6 +45,7 @@ public:
WebIDL::ExceptionOr<GC::Root<TrustedHTML>> create_html(String const&, GC::RootVector<JS::Value> const&);
WebIDL::ExceptionOr<GC::Root<TrustedScript>> create_script(String const&, GC::RootVector<JS::Value> const&);
WebIDL::ExceptionOr<GC::Root<TrustedScriptURL>> create_script_url(String const&, GC::RootVector<JS::Value> const&);
private:
explicit TrustedTypePolicy(JS::Realm&, String const&, TrustedTypePolicyOptions const&);

View file

@ -1,5 +1,6 @@
#import <TrustedTypes/TrustedHTML.idl>
#import <TrustedTypes/TrustedScript.idl>
#import <TrustedTypes/TrustedScriptURL.idl>
// https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy
[Exposed=(Window,Worker)]
@ -7,7 +8,7 @@ interface TrustedTypePolicy {
readonly attribute DOMString name;
TrustedHTML createHTML(DOMString input, any... arguments);
TrustedScript createScript(DOMString input, any... arguments);
[FIXME] TrustedScriptURL createScriptURL(DOMString input, any... arguments);
TrustedScriptURL createScriptURL(DOMString input, any... arguments);
};
// https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy-options

View file

@ -22,6 +22,7 @@
#include <LibWeb/SVG/TagNames.h>
#include <LibWeb/TrustedTypes/TrustedHTML.h>
#include <LibWeb/TrustedTypes/TrustedScript.h>
#include <LibWeb/TrustedTypes/TrustedScriptURL.h>
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
@ -172,6 +173,13 @@ bool TrustedTypePolicyFactory::is_script(JS::Value value)
return value.is_object() && is<TrustedScript>(value.as_object());
}
// https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-isscripturl
bool TrustedTypePolicyFactory::is_script_url(JS::Value value)
{
// 1. Returns true if value is an instance of TrustedScriptURL and has an associated data value set, false otherwise.
return value.is_object() && is<TrustedScriptURL>(value.as_object());
}
// https://w3c.github.io/trusted-types/dist/spec/#create-trusted-type-policy-algorithm
WebIDL::ExceptionOr<GC::Ref<TrustedTypePolicy>> TrustedTypePolicyFactory::create_a_trusted_type_policy(String const& policy_name, TrustedTypePolicyOptions const& options, JS::Object& global)
{

View file

@ -27,6 +27,7 @@ public:
bool is_html(JS::Value);
bool is_script(JS::Value);
bool is_script_url(JS::Value);
Optional<String> get_attribute_type(String const& tag_name, String& attribute, Optional<String> element_ns, Optional<String> attr_ns);
Optional<String> get_property_type(String const& tag_name, String const& property, Optional<String> element_ns);

View file

@ -7,7 +7,7 @@ interface TrustedTypePolicyFactory {
DOMString policyName, optional TrustedTypePolicyOptions policyOptions = {});
boolean isHTML(any value);
boolean isScript(any value);
[FIXME] boolean isScriptURL(any value);
boolean isScriptURL(any value);
[FIXME] readonly attribute TrustedHTML emptyHTML;
[FIXME] readonly attribute TrustedScript emptyScript;
DOMString? getAttributeType(