diff options
Diffstat (limited to 'components')
-rw-r--r-- | components/config/prefs.rs | 5 | ||||
-rw-r--r-- | components/script/dom/bindings/reflector.rs | 14 | ||||
-rw-r--r-- | components/script/dom/urlpattern.rs | 182 | ||||
-rw-r--r-- | components/script_bindings/interface.rs | 18 | ||||
-rw-r--r-- | components/script_bindings/record.rs | 10 | ||||
-rw-r--r-- | components/script_bindings/reflector.rs | 13 | ||||
-rw-r--r-- | components/script_bindings/webidls/URLPattern.webidl | 40 |
7 files changed, 196 insertions, 86 deletions
diff --git a/components/config/prefs.rs b/components/config/prefs.rs index 64dd9659e56..896a4fe0bc4 100644 --- a/components/config/prefs.rs +++ b/components/config/prefs.rs @@ -116,10 +116,6 @@ pub struct Preferences { // https://testutils.spec.whatwg.org#availability pub dom_testutils_enabled: bool, pub dom_trusted_types_enabled: bool, - /// Enable the [URLPattern] API. - /// - /// [URLPattern]: https://developer.mozilla.org/en-US/docs/Web/API/URLPattern - pub dom_urlpattern_enabled: bool, pub dom_xpath_enabled: bool, /// Enable WebGL2 APIs. pub dom_webgl2_enabled: bool, @@ -292,7 +288,6 @@ impl Preferences { dom_testperf_enabled: false, dom_testutils_enabled: false, dom_trusted_types_enabled: false, - dom_urlpattern_enabled: false, dom_webgl2_enabled: false, dom_webgpu_enabled: false, dom_webgpu_wgpu_backend: String::new(), diff --git a/components/script/dom/bindings/reflector.rs b/components/script/dom/bindings/reflector.rs index 0a5afbce487..c888686974e 100644 --- a/components/script/dom/bindings/reflector.rs +++ b/components/script/dom/bindings/reflector.rs @@ -11,7 +11,7 @@ use crate::DomTypes; use crate::dom::bindings::conversions::DerivedFrom; use crate::dom::bindings::root::DomRoot; use crate::dom::globalscope::GlobalScope; -use crate::realms::InRealm; +use crate::realms::{InRealm, enter_realm}; use crate::script_runtime::CanGc; /// Create the reflector for a new DOM object and yield ownership to the @@ -42,7 +42,16 @@ where } pub(crate) trait DomGlobal { + /// Returns the [relevant global] in whatever realm is currently active. + /// + /// [relevant global]: https://html.spec.whatwg.org/multipage/#concept-relevant-global fn global_(&self, realm: InRealm) -> DomRoot<GlobalScope>; + + /// Returns the [relevant global] in the same realm as the callee object. + /// If you know the callee's realm is already the current realm, it is + /// more efficient to call [DomGlobal::global_] instead. + /// + /// [relevant global]: https://html.spec.whatwg.org/multipage/#concept-relevant-global fn global(&self) -> DomRoot<GlobalScope>; } @@ -51,7 +60,8 @@ impl<T: DomGlobalGeneric<crate::DomTypeHolder>> DomGlobal for T { <Self as DomGlobalGeneric<crate::DomTypeHolder>>::global_(self, realm) } fn global(&self) -> DomRoot<GlobalScope> { - <Self as DomGlobalGeneric<crate::DomTypeHolder>>::global(self) + let realm = enter_realm(self); + <Self as DomGlobalGeneric<crate::DomTypeHolder>>::global_(self, InRealm::entered(&realm)) } } diff --git a/components/script/dom/urlpattern.rs b/components/script/dom/urlpattern.rs index c811d3a9a70..63665f6df0b 100644 --- a/components/script/dom/urlpattern.rs +++ b/components/script/dom/urlpattern.rs @@ -4,6 +4,7 @@ use dom_struct::dom_struct; use js::rust::HandleObject; +use script_bindings::codegen::GenericBindings::URLPatternBinding::URLPatternResult; use script_bindings::codegen::GenericUnionTypes::USVStringOrURLPatternInit; use script_bindings::error::{Error, Fallible}; use script_bindings::reflector::Reflector; @@ -46,7 +47,7 @@ impl URLPattern { ) -> Fallible<DomRoot<URLPattern>> { // The section below converts from servos types to the types used in the urlpattern crate let base_url = base_url.map(|usv_string| usv_string.0); - let input = bindings_to_third_party::map_urlpattern_input(input, base_url.clone()); + let input = bindings_to_third_party::map_urlpattern_input(input); let options = urlpattern::UrlPatternOptions { ignore_case: options.ignoreCase, }; @@ -94,6 +95,50 @@ impl URLPatternMethods<crate::DomTypeHolder> for URLPattern { URLPattern::initialize(global, proto, input, None, options, can_gc) } + /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-test> + fn Test( + &self, + input: USVStringOrURLPatternInit, + base_url: Option<USVString>, + ) -> Fallible<bool> { + let input = bindings_to_third_party::map_urlpattern_input(input); + let inputs = urlpattern::quirks::process_match_input(input, base_url.as_deref()) + .map_err(|error| Error::Type(format!("{error}")))?; + let Some((match_input, _)) = inputs else { + return Ok(false); + }; + + self.associated_url_pattern + .test(match_input) + .map_err(|error| Error::Type(format!("{error}"))) + } + + /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec> + fn Exec( + &self, + input: USVStringOrURLPatternInit, + base_url: Option<USVString>, + ) -> Fallible<Option<URLPatternResult>> { + let input = bindings_to_third_party::map_urlpattern_input(input); + let inputs = urlpattern::quirks::process_match_input(input, base_url.as_deref()) + .map_err(|error| Error::Type(format!("{error}")))?; + let Some((match_input, inputs)) = inputs else { + return Ok(None); + }; + + let result = self + .associated_url_pattern + .exec(match_input) + .map_err(|error| Error::Type(format!("{error}")))?; + let Some(result) = result else { + return Ok(None); + }; + + Ok(Some(third_party_to_bindings::map_urlpattern_result( + result, inputs, + ))) + } + /// <https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol> fn Protocol(&self) -> USVString { // Step 1. Return this’s associated URL pattern’s protocol component’s pattern string. @@ -151,54 +196,115 @@ impl URLPatternMethods<crate::DomTypeHolder> for URLPattern { } mod bindings_to_third_party { + use script_bindings::codegen::GenericBindings::URLPatternBinding::URLPatternInit; + use crate::dom::urlpattern::USVStringOrURLPatternInit; + fn map_urlpatterninit(pattern_init: URLPatternInit) -> urlpattern::quirks::UrlPatternInit { + urlpattern::quirks::UrlPatternInit { + protocol: pattern_init.protocol.map(|protocol| protocol.0), + username: pattern_init.username.map(|username| username.0), + password: pattern_init.password.map(|password| password.0), + hostname: pattern_init.hostname.map(|hostname| hostname.0), + port: pattern_init.port.map(|hash| hash.0), + pathname: pattern_init + .pathname + .as_ref() + .map(|usv_string| usv_string.to_string()), + search: pattern_init.search.map(|search| search.0), + hash: pattern_init.hash.map(|hash| hash.0), + base_url: pattern_init.baseURL.map(|base_url| base_url.0), + } + } + pub(super) fn map_urlpattern_input( input: USVStringOrURLPatternInit, - base_url: Option<String>, ) -> urlpattern::quirks::StringOrInit { match input { USVStringOrURLPatternInit::USVString(usv_string) => { urlpattern::quirks::StringOrInit::String(usv_string.0) }, USVStringOrURLPatternInit::URLPatternInit(pattern_init) => { - let pattern_init = urlpattern::quirks::UrlPatternInit { - protocol: pattern_init - .protocol - .as_ref() - .map(|usv_string| usv_string.to_string()), - username: pattern_init - .username - .as_ref() - .map(|usv_string| usv_string.to_string()), - password: pattern_init - .password - .as_ref() - .map(|usv_string| usv_string.to_string()), - hostname: pattern_init - .hostname - .as_ref() - .map(|usv_string| usv_string.to_string()), - port: pattern_init - .port - .as_ref() - .map(|usv_string| usv_string.to_string()), - pathname: pattern_init - .pathname - .as_ref() - .map(|usv_string| usv_string.to_string()), - search: pattern_init - .search - .as_ref() - .map(|usv_string| usv_string.to_string()), - hash: pattern_init - .hash - .as_ref() - .map(|usv_string| usv_string.to_string()), - base_url, - }; - urlpattern::quirks::StringOrInit::Init(pattern_init) + urlpattern::quirks::StringOrInit::Init(map_urlpatterninit(pattern_init)) + }, + } + } +} + +mod third_party_to_bindings { + use script_bindings::codegen::GenericBindings::URLPatternBinding::{ + URLPatternComponentResult, URLPatternInit, URLPatternResult, + }; + use script_bindings::codegen::GenericUnionTypes::USVStringOrUndefined; + use script_bindings::record::Record; + use script_bindings::str::USVString; + + use crate::dom::bindings::codegen::UnionTypes::USVStringOrURLPatternInit; + + // FIXME: For some reason codegen puts a lot of options into these types that don't make sense + + fn map_component_result( + component_result: urlpattern::UrlPatternComponentResult, + ) -> URLPatternComponentResult { + let mut groups = Record::new(); + for (key, value) in component_result.groups.iter() { + let value = match value { + Some(value) => USVStringOrUndefined::USVString(USVString(value.to_owned())), + None => USVStringOrUndefined::Undefined(()), + }; + + groups.insert(USVString(key.to_owned()), value); + } + + URLPatternComponentResult { + input: Some(component_result.input.into()), + groups: Some(groups), + } + } + + fn map_urlpatterninit(pattern_init: urlpattern::quirks::UrlPatternInit) -> URLPatternInit { + URLPatternInit { + baseURL: pattern_init.base_url.map(USVString), + protocol: pattern_init.protocol.map(USVString), + username: pattern_init.username.map(USVString), + password: pattern_init.password.map(USVString), + hostname: pattern_init.hostname.map(USVString), + port: pattern_init.port.map(USVString), + pathname: pattern_init.pathname.map(USVString), + search: pattern_init.search.map(USVString), + hash: pattern_init.hash.map(USVString), + } + } + + pub(super) fn map_urlpattern_result( + result: urlpattern::UrlPatternResult, + (string_or_init, base_url): urlpattern::quirks::Inputs, + ) -> URLPatternResult { + let string_or_init = match string_or_init { + urlpattern::quirks::StringOrInit::String(string) => { + USVStringOrURLPatternInit::USVString(USVString(string)) + }, + urlpattern::quirks::StringOrInit::Init(pattern_init) => { + USVStringOrURLPatternInit::URLPatternInit(map_urlpatterninit(pattern_init)) }, + }; + + let mut inputs = vec![string_or_init]; + + if let Some(base_url) = base_url { + inputs.push(USVStringOrURLPatternInit::USVString(USVString(base_url))); + } + + URLPatternResult { + inputs: Some(inputs), + protocol: Some(map_component_result(result.protocol)), + username: Some(map_component_result(result.username)), + password: Some(map_component_result(result.password)), + hostname: Some(map_component_result(result.hostname)), + port: Some(map_component_result(result.port)), + pathname: Some(map_component_result(result.pathname)), + search: Some(map_component_result(result.search)), + hash: Some(map_component_result(result.hash)), } } } diff --git a/components/script_bindings/interface.rs b/components/script_bindings/interface.rs index 08ee0a4f420..e185cfe9cfd 100644 --- a/components/script_bindings/interface.rs +++ b/components/script_bindings/interface.rs @@ -16,11 +16,11 @@ use js::jsapi::{ GetFunctionRealm, GetNonCCWObjectGlobal, GetRealmGlobalOrNull, GetWellKnownSymbol, HandleObject as RawHandleObject, IsSharableCompartment, IsSystemCompartment, JS_AtomizeAndPinString, JS_GetFunctionObject, JS_GetProperty, JS_IterateCompartments, - JS_NewFunction, JS_NewGlobalObject, JS_NewObject, JS_NewPlainObject, JS_NewStringCopyN, - JS_SetReservedSlot, JS_WrapObject, JSAutoRealm, JSClass, JSClassOps, JSContext, - JSFUN_CONSTRUCTOR, JSFunctionSpec, JSObject, JSPROP_PERMANENT, JSPROP_READONLY, - JSPROP_RESOLVING, JSPropertySpec, JSString, JSTracer, ObjectOps, OnNewGlobalHookOption, - SymbolCode, TrueHandleValue, Value, jsid, + JS_NewFunction, JS_NewGlobalObject, JS_NewObject, JS_NewStringCopyN, JS_SetReservedSlot, + JS_WrapObject, JSAutoRealm, JSClass, JSClassOps, JSContext, JSFUN_CONSTRUCTOR, JSFunctionSpec, + JSObject, JSPROP_ENUMERATE, JSPROP_PERMANENT, JSPROP_READONLY, JSPROP_RESOLVING, + JSPropertySpec, JSString, JSTracer, ObjectOps, OnNewGlobalHookOption, SymbolCode, + TrueHandleValue, Value, jsid, }; use js::jsval::{JSVal, NullValue, PrivateValue}; use js::rust::wrappers::{ @@ -473,7 +473,11 @@ fn create_unscopable_object(cx: SafeJSContext, names: &[&CStr], mut rval: Mutabl assert!(!names.is_empty()); assert!(rval.is_null()); unsafe { - rval.set(JS_NewPlainObject(*cx)); + rval.set(JS_NewObjectWithGivenProto( + *cx, + ptr::null(), + HandleObject::null(), + )); assert!(!rval.is_null()); for &name in names { assert!(JS_DefineProperty( @@ -481,7 +485,7 @@ fn create_unscopable_object(cx: SafeJSContext, names: &[&CStr], mut rval: Mutabl rval.handle(), name.as_ptr(), HandleValue::from_raw(TrueHandleValue), - JSPROP_READONLY as u32, + JSPROP_ENUMERATE as u32, )); } } diff --git a/components/script_bindings/record.rs b/components/script_bindings/record.rs index 2668a84f42c..d469faefaf2 100644 --- a/components/script_bindings/record.rs +++ b/components/script_bindings/record.rs @@ -7,7 +7,7 @@ use std::cmp::Eq; use std::hash::Hash; use std::marker::Sized; -use std::ops::Deref; +use std::ops::{Deref, DerefMut}; use indexmap::IndexMap; use js::conversions::{ConversionResult, FromJSValConvertible, ToJSValConvertible}; @@ -94,11 +94,17 @@ impl<K: RecordKey, V> Record<K, V> { impl<K: RecordKey, V> Deref for Record<K, V> { type Target = IndexMap<K, V>; - fn deref(&self) -> &IndexMap<K, V> { + fn deref(&self) -> &Self::Target { &self.map } } +impl<K: RecordKey, V> DerefMut for Record<K, V> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.map + } +} + impl<K, V, C> FromJSValConvertible for Record<K, V> where K: RecordKey, diff --git a/components/script_bindings/reflector.rs b/components/script_bindings/reflector.rs index 6b6ae03cb69..4b91b0536fc 100644 --- a/components/script_bindings/reflector.rs +++ b/components/script_bindings/reflector.rs @@ -8,7 +8,7 @@ use malloc_size_of_derive::MallocSizeOf; use crate::interfaces::GlobalScopeHelpers; use crate::iterable::{Iterable, IterableIterator}; -use crate::realms::{AlreadyInRealm, InRealm}; +use crate::realms::InRealm; use crate::root::{Dom, DomRoot, Root}; use crate::script_runtime::{CanGc, JSContext}; use crate::{DomTypes, JSTraceable}; @@ -108,17 +108,6 @@ pub trait DomGlobalGeneric<D: DomTypes>: DomObject { { D::GlobalScope::from_reflector(self, realm) } - - /// Returns the [`GlobalScope`] of the realm that the [`DomObject`] was created in. If this - /// object is a `Node`, this will be different from it's owning `Document` if adopted by. For - /// `Node`s it's almost always better to use `NodeTraits::owning_global`. - fn global(&self) -> DomRoot<D::GlobalScope> - where - Self: Sized, - { - let realm = AlreadyInRealm::assert_for_cx(D::GlobalScope::get_cx()); - D::GlobalScope::from_reflector(self, InRealm::already(&realm)) - } } impl<D: DomTypes, T: DomObject> DomGlobalGeneric<D> for T {} diff --git a/components/script_bindings/webidls/URLPattern.webidl b/components/script_bindings/webidls/URLPattern.webidl index f61b65702bc..a29cb650c23 100644 --- a/components/script_bindings/webidls/URLPattern.webidl +++ b/components/script_bindings/webidls/URLPattern.webidl @@ -6,14 +6,14 @@ typedef (USVString or URLPatternInit) URLPatternInput; -[Exposed=(Window,Worker), Pref="dom_urlpattern_enabled"] +[Exposed=(Window,Worker)] interface URLPattern { [Throws] constructor(URLPatternInput input, USVString baseURL, optional URLPatternOptions options = {}); [Throws] constructor(optional URLPatternInput input = {}, optional URLPatternOptions options = {}); - // [Throws] boolean test(optional URLPatternInput input = {}, optional USVString baseURL); + [Throws] boolean test(optional URLPatternInput input = {}, optional USVString baseURL); - // [Throws] URLPatternResult? exec(optional URLPatternInput input = {}, optional USVString baseURL); + [Throws] URLPatternResult? exec(optional URLPatternInput input = {}, optional USVString baseURL); readonly attribute USVString protocol; readonly attribute USVString username; @@ -43,20 +43,20 @@ dictionary URLPatternOptions { boolean ignoreCase = false; }; -// dictionary URLPatternResult { -// sequence<URLPatternInput> inputs; - -// URLPatternComponentResult protocol; -// URLPatternComponentResult username; -// URLPatternComponentResult password; -// URLPatternComponentResult hostname; -// URLPatternComponentResult port; -// URLPatternComponentResult pathname; -// URLPatternComponentResult search; -// URLPatternComponentResult hash; -// }; - -// dictionary URLPatternComponentResult { -// USVString input; -// record<USVString, (USVString or undefined)> groups; -// }; +dictionary URLPatternResult { + sequence<URLPatternInput> inputs; + + URLPatternComponentResult protocol; + URLPatternComponentResult username; + URLPatternComponentResult password; + URLPatternComponentResult hostname; + URLPatternComponentResult port; + URLPatternComponentResult pathname; + URLPatternComponentResult search; + URLPatternComponentResult hash; +}; + +dictionary URLPatternComponentResult { + USVString input; + record<USVString, (USVString or undefined)> groups; +}; |