style/properties_and_values/
registry.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Registered custom properties.
6
7use super::rule::{Inherits, InitialValue, PropertyRuleName};
8use super::syntax::Descriptor;
9use crate::selector_map::PrecomputedHashMap;
10use crate::stylesheets::UrlExtraData;
11use crate::Atom;
12use cssparser::SourceLocation;
13
14/// The metadata of a custom property registration that we need to do the cascade properly.
15#[derive(Debug, Clone, MallocSizeOf)]
16pub struct PropertyRegistrationData {
17    /// The syntax of the property.
18    pub syntax: Descriptor,
19    /// Whether the property inherits.
20    pub inherits: Inherits,
21    /// The initial value. Only missing for universal syntax.
22    #[ignore_malloc_size_of = "Arc"]
23    pub initial_value: Option<InitialValue>,
24}
25
26static UNREGISTERED: PropertyRegistrationData = PropertyRegistrationData {
27    syntax: Descriptor::universal(),
28    inherits: Inherits::True,
29    initial_value: None,
30};
31
32impl PropertyRegistrationData {
33    /// The data for an unregistered property.
34    pub fn unregistered() -> &'static Self {
35        &UNREGISTERED
36    }
37
38    /// Returns whether this property inherits.
39    #[inline]
40    pub fn inherits(&self) -> bool {
41        self.inherits == Inherits::True
42    }
43}
44
45/// A computed, already-validated property registration.
46/// <https://drafts.css-houdini.org/css-properties-values-api-1/#custom-property-registration>
47#[derive(Debug, Clone, MallocSizeOf)]
48pub struct PropertyRegistration {
49    /// The custom property name.
50    pub name: PropertyRuleName,
51    /// The actual information about the property.
52    pub data: PropertyRegistrationData,
53    /// The url data that is used to parse and compute the registration's initial value. Note that
54    /// it's not the url data that should be used to parse other values. Other values should use
55    /// the data of the style sheet where they came from.
56    pub url_data: UrlExtraData,
57    /// The source location of this registration, if it comes from a CSS rule.
58    pub source_location: SourceLocation,
59}
60
61impl PropertyRegistration {
62    /// Returns whether this property inherits.
63    #[inline]
64    pub fn inherits(&self) -> bool {
65        self.data.inherits == Inherits::True
66    }
67}
68
69/// The script registry of custom properties.
70/// <https://drafts.css-houdini.org/css-properties-values-api-1/#dom-window-registeredpropertyset-slot>
71#[derive(Default)]
72#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
73pub struct ScriptRegistry {
74    properties: PrecomputedHashMap<Atom, PropertyRegistration>,
75}
76
77impl ScriptRegistry {
78    /// Gets an already-registered custom property via script.
79    #[inline]
80    pub fn get(&self, name: &Atom) -> Option<&PropertyRegistration> {
81        self.properties.get(name)
82    }
83
84    /// Gets already-registered custom properties via script.
85    #[inline]
86    pub fn properties(&self) -> &PrecomputedHashMap<Atom, PropertyRegistration> {
87        &self.properties
88    }
89
90    /// Register a given property. As per
91    /// <https://drafts.css-houdini.org/css-properties-values-api-1/#the-registerproperty-function>
92    /// we don't allow overriding the registration.
93    #[inline]
94    pub fn register(&mut self, registration: PropertyRegistration) {
95        let name = registration.name.0.clone();
96        let old = self.properties.insert(name, registration);
97        debug_assert!(old.is_none(), "Already registered? Should be an error");
98    }
99
100    /// Returns the properties hashmap.
101    #[inline]
102    pub fn get_all(&self) -> &PrecomputedHashMap<Atom, PropertyRegistration> {
103        &self.properties
104    }
105}