aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/namednodemap.rs
blob: 1bfe7034198ffcf26193b383034ed269d2a1583a (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
/* 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::attr::Attr;
use dom::bindings::codegen::Bindings::NamedNodeMapBinding;
use dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods;
use dom::bindings::global;
use dom::bindings::js::{JS, JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::element::Element;
use dom::window::Window;

#[dom_struct]
pub struct NamedNodeMap {
    reflector_: Reflector,
    owner: JS<Element>,
}

impl NamedNodeMap {
    fn new_inherited(elem: JSRef<Element>) -> NamedNodeMap {
        NamedNodeMap {
            reflector_: Reflector::new(),
            owner: JS::from_rooted(elem),
        }
    }

    pub fn new(window: JSRef<Window>, elem: JSRef<Element>) -> Temporary<NamedNodeMap> {
        reflect_dom_object(box NamedNodeMap::new_inherited(elem),
                           &global::Window(window), NamedNodeMapBinding::Wrap)
    }
}

impl<'a> NamedNodeMapMethods for JSRef<'a, NamedNodeMap> {
    fn Length(self) -> u32 {
        self.owner.root().attrs().len() as u32
    }

    fn Item(self, index: u32) -> Option<Temporary<Attr>> {
        self.owner.root().attrs().as_slice().get(index as uint).map(|x| Temporary::new(x.clone()))
    }

    fn IndexedGetter(self, index: u32, found: &mut bool) -> Option<Temporary<Attr>> {
        let item = self.Item(index);
        *found = item.is_some();
        item
    }
}

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