From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../style/properties_and_values/registry.rs | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 servo/components/style/properties_and_values/registry.rs (limited to 'servo/components/style/properties_and_values/registry.rs') diff --git a/servo/components/style/properties_and_values/registry.rs b/servo/components/style/properties_and_values/registry.rs new file mode 100644 index 0000000000..e3cd552c9c --- /dev/null +++ b/servo/components/style/properties_and_values/registry.rs @@ -0,0 +1,104 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +//! Registered custom properties. + +use super::rule::{Inherits, InitialValue, PropertyRuleName}; +use super::syntax::Descriptor; +use crate::selector_map::PrecomputedHashMap; +use crate::stylesheets::UrlExtraData; +use crate::Atom; +use cssparser::SourceLocation; + +/// The metadata of a custom property registration that we need to do the cascade properly. +#[derive(Debug, Clone, MallocSizeOf)] +pub struct PropertyRegistrationData { + /// The syntax of the property. + pub syntax: Descriptor, + /// Whether the property inherits. + pub inherits: Inherits, + /// The initial value. Only missing for universal syntax. + #[ignore_malloc_size_of = "Arc"] + pub initial_value: Option, +} + +static UNREGISTERED: PropertyRegistrationData = PropertyRegistrationData { + syntax: Descriptor::universal(), + inherits: Inherits::True, + initial_value: None, +}; + +impl PropertyRegistrationData { + /// The data for an unregistered property. + pub fn unregistered() -> &'static Self { + &UNREGISTERED + } + + /// Returns whether this property inherits. + #[inline] + pub fn inherits(&self) -> bool { + self.inherits == Inherits::True + } +} + +/// A computed, already-validated property registration. +/// +#[derive(Debug, Clone, MallocSizeOf)] +pub struct PropertyRegistration { + /// The custom property name. + pub name: PropertyRuleName, + /// The actual information about the property. + pub data: PropertyRegistrationData, + /// The url data that is used to parse and compute the registration's initial value. Note that + /// it's not the url data that should be used to parse other values. Other values should use + /// the data of the style sheet where they came from. + pub url_data: UrlExtraData, + /// The source location of this registration, if it comes from a CSS rule. + pub source_location: SourceLocation, +} + +impl PropertyRegistration { + /// Returns whether this property inherits. + #[inline] + pub fn inherits(&self) -> bool { + self.data.inherits == Inherits::True + } +} + +/// The script registry of custom properties. +/// +#[derive(Default)] +pub struct ScriptRegistry { + properties: PrecomputedHashMap, +} + +impl ScriptRegistry { + /// Gets an already-registered custom property via script. + #[inline] + pub fn get(&self, name: &Atom) -> Option<&PropertyRegistration> { + self.properties.get(name) + } + + /// Gets already-registered custom properties via script. + #[inline] + pub fn properties(&self) -> &PrecomputedHashMap { + &self.properties + } + + /// Register a given property. As per + /// + /// we don't allow overriding the registration. + #[inline] + pub fn register(&mut self, registration: PropertyRegistration) { + let name = registration.name.0.clone(); + let old = self.properties.insert(name, registration); + debug_assert!(old.is_none(), "Already registered? Should be an error"); + } + + /// Returns the properties hashmap. + #[inline] + pub fn get_all(&self) -> &PrecomputedHashMap { + &self.properties + } +} -- cgit v1.2.3