1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::cmp::Ordering;
use std::iter::Iterator;

use dom_struct::dom_struct;
use servo_atoms::Atom;
use style::custom_properties;

use super::bindings::trace::HashMapTracedValues;
use crate::dom::bindings::codegen::Bindings::StylePropertyMapReadOnlyBinding::StylePropertyMapReadOnlyMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::cssstylevalue::CSSStyleValue;
use crate::dom::globalscope::GlobalScope;

#[dom_struct]
pub struct StylePropertyMapReadOnly {
    reflector: Reflector,
    entries: HashMapTracedValues<Atom, Dom<CSSStyleValue>>,
}

impl StylePropertyMapReadOnly {
    fn new_inherited<Entries>(entries: Entries) -> StylePropertyMapReadOnly
    where
        Entries: IntoIterator<Item = (Atom, Dom<CSSStyleValue>)>,
    {
        StylePropertyMapReadOnly {
            reflector: Reflector::new(),
            entries: HashMapTracedValues(entries.into_iter().collect()),
        }
    }

    pub fn from_iter<Entries>(
        global: &GlobalScope,
        entries: Entries,
    ) -> DomRoot<StylePropertyMapReadOnly>
    where
        Entries: IntoIterator<Item = (Atom, String)>,
    {
        let mut keys = Vec::new();
        rooted_vec!(let mut values);
        let iter = entries.into_iter();
        let (lo, _) = iter.size_hint();
        keys.reserve(lo);
        values.reserve(lo);
        for (key, value) in iter {
            let value = CSSStyleValue::new(global, value);
            keys.push(key);
            values.push(Dom::from_ref(&*value));
        }
        let iter = keys.drain(..).zip(values.iter().cloned());
        reflect_dom_object(
            Box::new(StylePropertyMapReadOnly::new_inherited(iter)),
            global,
        )
    }
}

impl StylePropertyMapReadOnlyMethods for StylePropertyMapReadOnly {
    /// <https://drafts.css-houdini.org/css-typed-om-1/#dom-stylepropertymapreadonly-get>
    fn Get(&self, property: DOMString) -> Option<DomRoot<CSSStyleValue>> {
        // TODO: avoid constructing an Atom
        self.entries
            .get(&Atom::from(property))
            .map(|value| DomRoot::from_ref(&**value))
    }

    /// <https://drafts.css-houdini.org/css-typed-om-1/#dom-stylepropertymapreadonly-has>
    fn Has(&self, property: DOMString) -> bool {
        // TODO: avoid constructing an Atom
        self.entries.contains_key(&Atom::from(property))
    }

    /// <https://drafts.css-houdini.org/css-typed-om-1/#dom-stylepropertymapreadonly-getproperties>
    fn GetProperties(&self) -> Vec<DOMString> {
        let mut result: Vec<DOMString> = self
            .entries
            .0
            .keys()
            .map(|key| DOMString::from(&**key))
            .collect();
        // https://drafts.css-houdini.org/css-typed-om-1/#dom-stylepropertymap-getproperties
        // requires this sort order
        result.sort_by(|key1, key2| {
            if let Ok(key1) = custom_properties::parse_name(key1) {
                if let Ok(key2) = custom_properties::parse_name(key2) {
                    key1.cmp(key2)
                } else {
                    Ordering::Greater
                }
            } else if custom_properties::parse_name(key2).is_ok() {
                Ordering::Less
            } else {
                key1.cmp(key2)
            }
        });
        result
    }
}