Skip to main content

script/dom/element/
namednodemap.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 dom_struct::dom_struct;
6use html5ever::LocalName;
7use js::context::{JSContext, NoGC};
8use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
9
10use crate::dom::attr::Attr;
11use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
12use crate::dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods;
13use crate::dom::bindings::domname::namespace_from_domstring;
14use crate::dom::bindings::error::{Error, Fallible};
15use crate::dom::bindings::root::{Dom, DomRoot};
16use crate::dom::bindings::str::DOMString;
17use crate::dom::element::Element;
18use crate::dom::window::Window;
19
20#[dom_struct]
21pub(crate) struct NamedNodeMap {
22    reflector_: Reflector,
23    owner: Dom<Element>,
24}
25
26impl NamedNodeMap {
27    fn new_inherited(elem: &Element) -> NamedNodeMap {
28        NamedNodeMap {
29            reflector_: Reflector::new(),
30            owner: Dom::from_ref(elem),
31        }
32    }
33
34    pub(crate) fn new(
35        cx: &mut JSContext,
36        window: &Window,
37        elem: &Element,
38    ) -> DomRoot<NamedNodeMap> {
39        reflect_dom_object_with_cx(Box::new(NamedNodeMap::new_inherited(elem)), window, cx)
40    }
41}
42
43impl NamedNodeMapMethods<crate::DomTypeHolder> for NamedNodeMap {
44    /// <https://dom.spec.whatwg.org/#dom-namednodemap-length>
45    fn Length(&self) -> u32 {
46        self.owner.attrs().borrow().len() as u32
47    }
48
49    /// <https://dom.spec.whatwg.org/#dom-namednodemap-item>
50    fn Item(&self, cx: &mut JSContext, index: u32) -> Option<DomRoot<Attr>> {
51        let index: usize = index as _;
52        if self.owner.attrs().borrow().len() <= index {
53            None
54        } else {
55            Some(self.owner.attrs().ensure_dom(cx, index, &self.owner))
56        }
57    }
58
59    /// <https://dom.spec.whatwg.org/#dom-namednodemap-getnameditem>
60    fn GetNamedItem(&self, cx: &mut JSContext, name: DOMString) -> Option<DomRoot<Attr>> {
61        self.owner.get_attribute_by_name(cx, name)
62    }
63
64    /// <https://dom.spec.whatwg.org/#dom-namednodemap-getnameditemns>
65    fn GetNamedItemNS(
66        &self,
67        cx: &mut JSContext,
68        namespace: Option<DOMString>,
69        local_name: DOMString,
70    ) -> Option<DomRoot<Attr>> {
71        let ns = namespace_from_domstring(namespace);
72        self.owner
73            .get_attribute_with_namespace(cx, &ns, &LocalName::from(local_name))
74    }
75
76    /// <https://dom.spec.whatwg.org/#dom-namednodemap-setnameditem>
77    fn SetNamedItem(&self, cx: &mut JSContext, attr: &Attr) -> Fallible<Option<DomRoot<Attr>>> {
78        self.owner.SetAttributeNode(cx, attr)
79    }
80
81    /// <https://dom.spec.whatwg.org/#dom-namednodemap-setnameditemns>
82    fn SetNamedItemNS(&self, cx: &mut JSContext, attr: &Attr) -> Fallible<Option<DomRoot<Attr>>> {
83        self.SetNamedItem(cx, attr)
84    }
85
86    /// <https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem>
87    fn RemoveNamedItem(&self, cx: &mut JSContext, name: DOMString) -> Fallible<DomRoot<Attr>> {
88        let name = self.owner.parsed_name(name);
89        self.owner
90            .remove_attribute_by_name(cx, &name)
91            .ok_or(Error::NotFound(None))
92    }
93
94    /// <https://dom.spec.whatwg.org/#dom-namednodemap-removenameditemns>
95    fn RemoveNamedItemNS(
96        &self,
97        cx: &mut JSContext,
98        namespace: Option<DOMString>,
99        local_name: DOMString,
100    ) -> Fallible<DomRoot<Attr>> {
101        let ns = namespace_from_domstring(namespace);
102        self.owner
103            .remove_attribute(cx, &ns, &LocalName::from(local_name))
104            .ok_or(Error::NotFound(None))
105    }
106
107    /// <https://dom.spec.whatwg.org/#dom-namednodemap-item>
108    fn IndexedGetter(&self, cx: &mut JSContext, index: u32) -> Option<DomRoot<Attr>> {
109        self.Item(cx, index)
110    }
111
112    // check-tidy: no specs after this line
113    fn NamedGetter(&self, cx: &mut JSContext, name: DOMString) -> Option<DomRoot<Attr>> {
114        self.GetNamedItem(cx, name)
115    }
116
117    /// <https://heycam.github.io/webidl/#dfn-supported-property-names>
118    fn SupportedPropertyNames(&self, _: &NoGC) -> Vec<DOMString> {
119        let mut names = vec![];
120        let html_element_in_html_document = self.owner.html_element_in_html_document();
121        for attr in self.owner.attrs().borrow().iter() {
122            let s = &**attr.name();
123            if html_element_in_html_document && !s.bytes().all(|b| b.to_ascii_lowercase() == b) {
124                continue;
125            }
126
127            if !names.iter().any(|name| name == s) {
128                names.push(DOMString::from(s));
129            }
130        }
131        names
132    }
133}