script/dom/
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 style::custom_properties;
10use stylo_atoms::Atom;
11
12use super::bindings::trace::HashMapTracedValues;
13use crate::dom::bindings::codegen::Bindings::StylePropertyMapReadOnlyBinding::StylePropertyMapReadOnlyMethods;
14use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
15use crate::dom::bindings::root::{Dom, DomRoot};
16use crate::dom::bindings::str::DOMString;
17use crate::dom::cssstylevalue::CSSStyleValue;
18use crate::dom::globalscope::GlobalScope;
19use crate::script_runtime::CanGc;
20
21#[dom_struct]
22pub(crate) struct StylePropertyMapReadOnly {
23    reflector: Reflector,
24    entries: HashMapTracedValues<Atom, Dom<CSSStyleValue>>,
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        global: &GlobalScope,
40        entries: Entries,
41        can_gc: CanGc,
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(global, value, can_gc);
54            keys.push(key);
55            values.push(Dom::from_ref(&*value));
56        }
57        let iter = keys.drain(..).zip(values.iter().cloned());
58        reflect_dom_object(
59            Box::new(StylePropertyMapReadOnly::new_inherited(iter)),
60            global,
61            can_gc,
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) {
93                if let Ok(key2) = custom_properties::parse_name(key2) {
94                    key1.cmp(key2)
95                } else {
96                    Ordering::Greater
97                }
98            } else if custom_properties::parse_name(key2).is_ok() {
99                Ordering::Less
100            } else {
101                key1.cmp(key2)
102            }
103        });
104        result
105    }
106}