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)]
33#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
34pub struct ScriptRegistry {
35    properties: PrecomputedHashMap<Atom, PropertyRegistration>,
36}
37
38impl ScriptRegistry {
39    /// Gets an already-registered custom property via script.
40    #[inline]
41    pub fn get(&self, name: &Atom) -> Option<&PropertyRegistration> {
42        self.properties.get(name)
43    }
44
45    /// Gets already-registered custom properties via script.
46    #[inline]
47    pub fn properties(&self) -> &PrecomputedHashMap<Atom, PropertyRegistration> {
48        &self.properties
49    }
50
51    /// Register a given property. As per
52    /// <https://drafts.css-houdini.org/css-properties-values-api-1/#the-registerproperty-function>
53    /// we don't allow overriding the registration.
54    #[inline]
55    pub fn register(&mut self, registration: PropertyRegistration) {
56        let name = registration.name.0.clone();
57        let old = self.properties.insert(name, registration);
58        debug_assert!(old.is_none(), "Already registered? Should be an error");
59    }
60
61    /// Returns the properties hashmap.
62    #[inline]
63    pub fn get_all(&self) -> &PrecomputedHashMap<Atom, PropertyRegistration> {
64        &self.properties
65    }
66}