tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

registry.rs (3735B)


      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 
      7 use super::rule::{Inherits, InitialValue, PropertyRuleName};
      8 use super::syntax::Descriptor;
      9 use crate::derives::*;
     10 use crate::selector_map::PrecomputedHashMap;
     11 use crate::stylesheets::UrlExtraData;
     12 use crate::Atom;
     13 use cssparser::SourceLocation;
     14 
     15 /// The metadata of a custom property registration that we need to do the cascade properly.
     16 #[derive(Debug, Clone, MallocSizeOf)]
     17 pub 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 
     27 static UNREGISTERED: PropertyRegistrationData = PropertyRegistrationData {
     28    syntax: Descriptor::universal(),
     29    inherits: Inherits::True,
     30    initial_value: None,
     31 };
     32 
     33 impl 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)]
     49 pub 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 
     62 impl 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))]
     74 pub struct ScriptRegistry {
     75    properties: PrecomputedHashMap<Atom, PropertyRegistration>,
     76 }
     77 
     78 impl 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 }