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};
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(cx, Box::new(NamedNodeMap::new_inherited(elem)), window)
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(Some(
92                "No attribute with the given name".into(),
93            )))
94    }
95
96    /// <https://dom.spec.whatwg.org/#dom-namednodemap-removenameditemns>
97    fn RemoveNamedItemNS(
98        &self,
99        cx: &mut JSContext,
100        namespace: Option<DOMString>,
101        local_name: DOMString,
102    ) -> Fallible<DomRoot<Attr>> {
103        let ns = namespace_from_domstring(namespace);
104        self.owner
105            .remove_attribute(cx, &ns, &LocalName::from(local_name))
106            .ok_or(Error::NotFound(Some(
107                "No attribute with the given namespace and local name".into(),
108            )))
109    }
110
111    /// <https://dom.spec.whatwg.org/#dom-namednodemap-item>
112    fn IndexedGetter(&self, cx: &mut JSContext, index: u32) -> Option<DomRoot<Attr>> {
113        self.Item(cx, index)
114    }
115
116    // check-tidy: no specs after this line
117    fn NamedGetter(&self, cx: &mut JSContext, name: DOMString) -> Option<DomRoot<Attr>> {
118        self.GetNamedItem(cx, name)
119    }
120
121    /// <https://heycam.github.io/webidl/#dfn-supported-property-names>
122    fn SupportedPropertyNames(&self, _: &NoGC) -> Vec<DOMString> {
123        let mut names = vec![];
124        let html_element_in_html_document = self.owner.html_element_in_html_document();
125        for attr in self.owner.attrs().borrow().iter() {
126            let s = &**attr.name();
127            if html_element_in_html_document && !s.bytes().all(|b| b.to_ascii_lowercase() == b) {
128                continue;
129            }
130
131            if !names.iter().any(|name| name == s) {
132                names.push(DOMString::from(s));
133            }
134        }
135        names
136    }
137}