script/dom/
namednodemap.rs1use dom_struct::dom_struct;
6use html5ever::LocalName;
7
8use crate::dom::attr::Attr;
9use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
10use crate::dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods;
11use crate::dom::bindings::domname::namespace_from_domstring;
12use crate::dom::bindings::error::{Error, Fallible};
13use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
14use crate::dom::bindings::root::{Dom, DomRoot};
15use crate::dom::bindings::str::DOMString;
16use crate::dom::element::Element;
17use crate::dom::window::Window;
18use crate::script_runtime::CanGc;
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(window: &Window, elem: &Element, can_gc: CanGc) -> DomRoot<NamedNodeMap> {
35 reflect_dom_object(Box::new(NamedNodeMap::new_inherited(elem)), window, can_gc)
36 }
37}
38
39impl NamedNodeMapMethods<crate::DomTypeHolder> for NamedNodeMap {
40 fn Length(&self) -> u32 {
42 self.owner.attrs().len() as u32
43 }
44
45 fn Item(&self, index: u32) -> Option<DomRoot<Attr>> {
47 self.owner
48 .attrs()
49 .get(index as usize)
50 .map(|js| DomRoot::from_ref(&**js))
51 }
52
53 fn GetNamedItem(&self, name: DOMString) -> Option<DomRoot<Attr>> {
55 self.owner.get_attribute_by_name(name)
56 }
57
58 fn GetNamedItemNS(
60 &self,
61 namespace: Option<DOMString>,
62 local_name: DOMString,
63 ) -> Option<DomRoot<Attr>> {
64 let ns = namespace_from_domstring(namespace);
65 self.owner.get_attribute(&ns, &LocalName::from(local_name))
66 }
67
68 fn SetNamedItem(
70 &self,
71 cx: &mut js::context::JSContext,
72 attr: &Attr,
73 ) -> Fallible<Option<DomRoot<Attr>>> {
74 self.owner.SetAttributeNode(cx, attr)
75 }
76
77 fn SetNamedItemNS(
79 &self,
80 cx: &mut js::context::JSContext,
81 attr: &Attr,
82 ) -> Fallible<Option<DomRoot<Attr>>> {
83 self.SetNamedItem(cx, attr)
84 }
85
86 fn RemoveNamedItem(
88 &self,
89 cx: &mut js::context::JSContext,
90 name: DOMString,
91 ) -> Fallible<DomRoot<Attr>> {
92 let name = self.owner.parsed_name(name);
93 self.owner
94 .remove_attribute_by_name(&name, CanGc::from_cx(cx))
95 .ok_or(Error::NotFound(None))
96 }
97
98 fn RemoveNamedItemNS(
100 &self,
101 cx: &mut js::context::JSContext,
102 namespace: Option<DOMString>,
103 local_name: DOMString,
104 ) -> Fallible<DomRoot<Attr>> {
105 let ns = namespace_from_domstring(namespace);
106 self.owner
107 .remove_attribute(&ns, &LocalName::from(local_name), CanGc::from_cx(cx))
108 .ok_or(Error::NotFound(None))
109 }
110
111 fn IndexedGetter(&self, index: u32) -> Option<DomRoot<Attr>> {
113 self.Item(index)
114 }
115
116 fn NamedGetter(&self, name: DOMString) -> Option<DomRoot<Attr>> {
118 self.GetNamedItem(name)
119 }
120
121 fn SupportedPropertyNames(&self) -> 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().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}