aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/domstringmap.rs
blob: 26218445c808920168e5b847c47a31fa3905601c (plain) (blame)
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
/* 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 http://mozilla.org/MPL/2.0/. */

use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::DOMStringMapBinding;
use dom::bindings::codegen::Bindings::DOMStringMapBinding::DOMStringMapMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use servo_util::str::DOMString;

use std::collections::HashMap;

#[dom_struct]
pub struct DOMStringMap {
    map: DOMRefCell<HashMap<DOMString, DOMString>>,
    reflector_: Reflector,
}

impl DOMStringMap {
    fn new_inherited() -> DOMStringMap {
        DOMStringMap {
            map: DOMRefCell::new(HashMap::new()),
            reflector_: Reflector::new(),
        }
    }

    pub fn new(global: GlobalRef) -> Temporary<DOMStringMap> {
        reflect_dom_object(box DOMStringMap::new_inherited(),
                           global, DOMStringMapBinding::Wrap)
    }
}

impl<'a> DOMStringMapMethods for JSRef<'a, DOMStringMap> {
    fn NamedCreator(self, name: DOMString, value: DOMString) {
        self.map.borrow_mut().insert(name, value);
    }

    fn NamedDeleter(self, name: DOMString) {
        self.map.borrow_mut().remove(&name);
    }

    fn NamedSetter(self, name: DOMString, value: DOMString) {
        self.map.borrow_mut().insert(name, value);
    }

    fn NamedGetter(self, name: DOMString, found: &mut bool) -> DOMString {
        match self.map.borrow().get(&name) {
            Some(value) => {
                *found = true;
                value.clone()
            },
            None => {
                *found = false;
                String::new()
            }
        }
    }
}

impl Reflectable for DOMStringMap {
    fn reflector<'a>(&'a self) -> &'a Reflector {
        &self.reflector_
    }
}