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