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