Skip to main content

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::{Descriptors, PropertyRuleName};
8use crate::derives::*;
9use crate::selector_map::PrecomputedHashMap;
10use crate::stylesheets::UrlExtraData;
11use crate::Atom;
12use cssparser::SourceLocation;
13
14/// A computed, already-validated property registration.
15/// <https://drafts.css-houdini.org/css-properties-values-api-1/#custom-property-registration>
16#[derive(Debug, Clone, MallocSizeOf)]
17pub struct PropertyRegistration {
18    /// The custom property name.
19    pub name: PropertyRuleName,
20    /// The actual information about the property.
21    pub descriptors: Descriptors,
22    /// The url data that is used to parse and compute the registration's initial value. Note that
23    /// it's not the url data that should be used to parse other values. Other values should use
24    /// the data of the style sheet where they came from.
25    pub url_data: UrlExtraData,
26    /// The source location of this registration, if it comes from a CSS rule.
27    pub source_location: SourceLocation,
28}
29
30/// The script registry of custom properties.
31/// <https://drafts.css-houdini.org/css-properties-values-api-1/#dom-window-registeredpropertyset-slot>
32#[derive(Default, MallocSizeOf)]
33pub struct ScriptRegistry {
34    properties: PrecomputedHashMap<Atom, PropertyRegistration>,
35}
36
37impl ScriptRegistry {
38    /// Gets an already-registered custom property via script.
39    #[inline]
40    pub fn get(&self, name: &Atom) -> Option<&PropertyRegistration> {
41        self.properties.get(name)
42    }
43
44    /// Gets already-registered custom properties via script.
45    #[inline]
46    pub fn properties(&self) -> &PrecomputedHashMap<Atom, PropertyRegistration> {
47        &self.properties
48    }
49
50    /// Register a given property. As per
51    /// <https://drafts.css-houdini.org/css-properties-values-api-1/#the-registerproperty-function>
52    /// we don't allow overriding the registration.
53    #[inline]
54    pub fn register(&mut self, registration: PropertyRegistration) {
55        let name = registration.name.0.clone();
56        let old = self.properties.insert(name, registration);
57        debug_assert!(old.is_none(), "Already registered? Should be an error");
58    }
59
60    /// Returns the properties hashmap.
61    #[inline]
62    pub fn get_all(&self) -> &PrecomputedHashMap<Atom, PropertyRegistration> {
63        &self.properties
64    }
65}