use dom_struct::dom_struct;
use html5ever::LocalName;
use js::context::{JSContext, NoGC};
use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
use crate::dom::attr::Attr;
use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
use crate::dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods;
use crate::dom::bindings::domname::namespace_from_domstring;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::element::Element;
use crate::dom::window::Window;
#[dom_struct]
pub(crate) struct NamedNodeMap {
reflector_: Reflector,
owner: Dom<Element>,
}
impl NamedNodeMap {
fn new_inherited(elem: &Element) -> NamedNodeMap {
NamedNodeMap {
reflector_: Reflector::new(),
owner: Dom::from_ref(elem),
}
}
pub(crate) fn new(
cx: &mut JSContext,
window: &Window,
elem: &Element,
) -> DomRoot<NamedNodeMap> {
reflect_dom_object_with_cx(Box::new(NamedNodeMap::new_inherited(elem)), window, cx)
}
}
impl NamedNodeMapMethods<crate::DomTypeHolder> for NamedNodeMap {
fn Length(&self) -> u32 {
self.owner.attrs().borrow().len() as u32
}
fn Item(&self, cx: &mut JSContext, index: u32) -> Option<DomRoot<Attr>> {
let index: usize = index as _;
if self.owner.attrs().borrow().len() <= index {
None
} else {
Some(self.owner.attrs().ensure_dom(cx, index, &self.owner))
}
}
fn GetNamedItem(&self, cx: &mut JSContext, name: DOMString) -> Option<DomRoot<Attr>> {
self.owner.get_attribute_by_name(cx, name)
}
fn GetNamedItemNS(
&self,
cx: &mut JSContext,
namespace: Option<DOMString>,
local_name: DOMString,
) -> Option<DomRoot<Attr>> {
let ns = namespace_from_domstring(namespace);
self.owner
.get_attribute_with_namespace(cx, &ns, &LocalName::from(local_name))
}
fn SetNamedItem(&self, cx: &mut JSContext, attr: &Attr) -> Fallible<Option<DomRoot<Attr>>> {
self.owner.SetAttributeNode(cx, attr)
}
fn SetNamedItemNS(&self, cx: &mut JSContext, attr: &Attr) -> Fallible<Option<DomRoot<Attr>>> {
self.SetNamedItem(cx, attr)
}
fn RemoveNamedItem(&self, cx: &mut JSContext, name: DOMString) -> Fallible<DomRoot<Attr>> {
let name = self.owner.parsed_name(name);
self.owner
.remove_attribute_by_name(cx, &name)
.ok_or(Error::NotFound(None))
}
fn RemoveNamedItemNS(
&self,
cx: &mut JSContext,
namespace: Option<DOMString>,
local_name: DOMString,
) -> Fallible<DomRoot<Attr>> {
let ns = namespace_from_domstring(namespace);
self.owner
.remove_attribute(cx, &ns, &LocalName::from(local_name))
.ok_or(Error::NotFound(None))
}
fn IndexedGetter(&self, cx: &mut JSContext, index: u32) -> Option<DomRoot<Attr>> {
self.Item(cx, index)
}
fn NamedGetter(&self, cx: &mut JSContext, name: DOMString) -> Option<DomRoot<Attr>> {
self.GetNamedItem(cx, name)
}
fn SupportedPropertyNames(&self, _: &NoGC) -> Vec<DOMString> {
let mut names = vec![];
let html_element_in_html_document = self.owner.html_element_in_html_document();
for attr in self.owner.attrs().borrow().iter() {
let s = &**attr.name();
if html_element_in_html_document && !s.bytes().all(|b| b.to_ascii_lowercase() == b) {
continue;
}
if !names.iter().any(|name| name == s) {
names.push(DOMString::from(s));
}
}
names
}
}