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
66 .get_attribute_with_namespace(&ns, &LocalName::from(local_name))
67 }
68
69 fn SetNamedItem(
71 &self,
72 cx: &mut js::context::JSContext,
73 attr: &Attr,
74 ) -> Fallible<Option<DomRoot<Attr>>> {
75 self.owner.SetAttributeNode(cx, attr)
76 }
77
78 fn SetNamedItemNS(
80 &self,
81 cx: &mut js::context::JSContext,
82 attr: &Attr,
83 ) -> Fallible<Option<DomRoot<Attr>>> {
84 self.SetNamedItem(cx, attr)
85 }
86
87 fn RemoveNamedItem(
89 &self,
90 cx: &mut js::context::JSContext,
91 name: DOMString,
92 ) -> Fallible<DomRoot<Attr>> {
93 let name = self.owner.parsed_name(name);
94 self.owner
95 .remove_attribute_by_name(&name, CanGc::from_cx(cx))
96 .ok_or(Error::NotFound(None))
97 }
98
99 fn RemoveNamedItemNS(
101 &self,
102 cx: &mut js::context::JSContext,
103 namespace: Option<DOMString>,
104 local_name: DOMString,
105 ) -> Fallible<DomRoot<Attr>> {
106 let ns = namespace_from_domstring(namespace);
107 self.owner
108 .remove_attribute(&ns, &LocalName::from(local_name), CanGc::from_cx(cx))
109 .ok_or(Error::NotFound(None))
110 }
111
112 fn IndexedGetter(&self, index: u32) -> Option<DomRoot<Attr>> {
114 self.Item(index)
115 }
116
117 fn NamedGetter(&self, name: DOMString) -> Option<DomRoot<Attr>> {
119 self.GetNamedItem(name)
120 }
121
122 fn SupportedPropertyNames(&self) -> Vec<DOMString> {
124 let mut names = vec![];
125 let html_element_in_html_document = self.owner.html_element_in_html_document();
126 for attr in self.owner.attrs().iter() {
127 let s = &**attr.name();
128 if html_element_in_html_document && !s.bytes().all(|b| b.to_ascii_lowercase() == b) {
129 continue;
130 }
131
132 if !names.iter().any(|name| name == s) {
133 names.push(DOMString::from(s));
134 }
135 }
136 names
137 }
138}