Skip to main content

script/dom/css/
stylepropertymapreadonly.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
5use std::cmp::Ordering;
6use std::iter::Iterator;
7
8use dom_struct::dom_struct;
9use rustc_hash::FxBuildHasher;
10use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
11use style::custom_properties;
12use stylo_atoms::Atom;
13
14use super::cssstylevalue::CSSStyleValue;
15use crate::dom::bindings::codegen::Bindings::StylePropertyMapReadOnlyBinding::StylePropertyMapReadOnlyMethods;
16use crate::dom::bindings::root::{Dom, DomRoot};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::bindings::trace::HashMapTracedValues;
19use crate::dom::globalscope::GlobalScope;
20
21#[dom_struct]
22pub(crate) struct StylePropertyMapReadOnly {
23    reflector: Reflector,
24    entries: HashMapTracedValues<Atom, Dom<CSSStyleValue>, FxBuildHasher>,
25}
26
27impl StylePropertyMapReadOnly {
28    fn new_inherited<Entries>(entries: Entries) -> StylePropertyMapReadOnly
29    where
30        Entries: IntoIterator<Item = (Atom, Dom<CSSStyleValue>)>,
31    {
32        StylePropertyMapReadOnly {
33            reflector: Reflector::new(),
34            entries: HashMapTracedValues(entries.into_iter().collect()),
35        }
36    }
37
38    pub(crate) fn from_iter<Entries>(
39        cx: &mut js::context::JSContext,
40        global: &GlobalScope,
41        entries: Entries,
42    ) -> DomRoot<StylePropertyMapReadOnly>
43    where
44        Entries: IntoIterator<Item = (Atom, String)>,
45    {
46        let mut keys = Vec::new();
47        rooted_vec!(let mut values);
48        let iter = entries.into_iter();
49        let (lo, _) = iter.size_hint();
50        keys.reserve(lo);
51        values.reserve(lo);
52        for (key, value) in iter {
53            let value = CSSStyleValue::new(cx, global, value);
54            keys.push(key);
55            values.push(Dom::from_ref(&*value));
56        }
57        let iter = keys.into_iter().zip(values.iter().cloned());
58        reflect_dom_object_with_cx(
59            Box::new(StylePropertyMapReadOnly::new_inherited(iter)),
60            global,
61            cx,
62        )
63    }
64}
65
66impl StylePropertyMapReadOnlyMethods<crate::DomTypeHolder> for StylePropertyMapReadOnly {
67    /// <https://drafts.css-houdini.org/css-typed-om-1/#dom-stylepropertymapreadonly-get>
68    fn Get(&self, property: DOMString) -> Option<DomRoot<CSSStyleValue>> {
69        // TODO: avoid constructing an Atom
70        self.entries
71            .get(&Atom::from(property))
72            .map(|value| DomRoot::from_ref(&**value))
73    }
74
75    /// <https://drafts.css-houdini.org/css-typed-om-1/#dom-stylepropertymapreadonly-has>
76    fn Has(&self, property: DOMString) -> bool {
77        // TODO: avoid constructing an Atom
78        self.entries.contains_key(&Atom::from(property))
79    }
80
81    /// <https://drafts.css-houdini.org/css-typed-om-1/#dom-stylepropertymapreadonly-getproperties>
82    fn GetProperties(&self) -> Vec<DOMString> {
83        let mut result: Vec<DOMString> = self
84            .entries
85            .0
86            .keys()
87            .map(|key| DOMString::from(&**key))
88            .collect();
89        // https://drafts.css-houdini.org/css-typed-om-1/#dom-stylepropertymap-getproperties
90        // requires this sort order
91        result.sort_by(|key1, key2| {
92            if let Ok(key1) = custom_properties::parse_name(&key1.str()) {
93                if let Ok(key2) = custom_properties::parse_name(&key2.str()) {
94                    key1.cmp(key2)
95                } else {
96                    Ordering::Greater
97                }
98            } else if custom_properties::parse_name(&key2.str()).is_ok() {
99                Ordering::Less
100            } else {
101                key1.cmp(key2)
102            }
103        });
104        result
105    }
106}