aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/nodelist.rs
diff options
context:
space:
mode:
authorPatrick Shaughnessy <pshaughn@comcast.net>2020-01-03 16:45:29 -0500
committerPatrick Shaughnessy <pshaughn@comcast.net>2020-02-15 10:16:29 -0500
commit007a6cd05083c16abb0d0b129fe0dc7544530cda (patch)
treeba61f3fdbe6ad3b96870a25eb374870710ec75f7 /components/script/dom/nodelist.rs
parent3475790fc23f939560f546901bf3082ebf20508a (diff)
downloadservo-007a6cd05083c16abb0d0b129fe0dc7544530cda.tar.gz
servo-007a6cd05083c16abb0d0b129fe0dc7544530cda.zip
make Document.getElementsByName a live collection
Diffstat (limited to 'components/script/dom/nodelist.rs')
-rw-r--r--components/script/dom/nodelist.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/components/script/dom/nodelist.rs b/components/script/dom/nodelist.rs
index 8c4f6d39358..5218d1b3026 100644
--- a/components/script/dom/nodelist.rs
+++ b/components/script/dom/nodelist.rs
@@ -7,6 +7,8 @@ use crate::dom::bindings::codegen::Bindings::NodeListBinding;
use crate::dom::bindings::codegen::Bindings::NodeListBinding::NodeListMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
+use crate::dom::bindings::str::DOMString;
+use crate::dom::document::Document;
use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlformelement::HTMLFormElement;
use crate::dom::node::{ChildrenMutation, Node};
@@ -22,6 +24,7 @@ pub enum NodeListType {
Children(ChildrenList),
Labels(LabelsList),
Radio(RadioList),
+ ElementsByName(ElementsByNameList),
}
// https://dom.spec.whatwg.org/#interface-nodelist
@@ -74,6 +77,17 @@ impl NodeList {
NodeList::new(window, NodeListType::Labels(LabelsList::new(element)))
}
+ pub fn new_elements_by_name_list(
+ window: &Window,
+ document: &Document,
+ name: DOMString,
+ ) -> DomRoot<NodeList> {
+ NodeList::new(
+ window,
+ NodeListType::ElementsByName(ElementsByNameList::new(document, name)),
+ )
+ }
+
pub fn empty(window: &Window) -> DomRoot<NodeList> {
NodeList::new(window, NodeListType::Simple(vec![]))
}
@@ -87,6 +101,7 @@ impl NodeListMethods for NodeList {
NodeListType::Children(ref list) => list.len(),
NodeListType::Labels(ref list) => list.len(),
NodeListType::Radio(ref list) => list.len(),
+ NodeListType::ElementsByName(ref list) => list.len(),
}
}
@@ -99,6 +114,7 @@ impl NodeListMethods for NodeList {
NodeListType::Children(ref list) => list.item(index),
NodeListType::Labels(ref list) => list.item(index),
NodeListType::Radio(ref list) => list.item(index),
+ NodeListType::ElementsByName(ref list) => list.item(index),
}
}
@@ -401,3 +417,29 @@ impl RadioList {
self.form.nth_for_radio_list(index, self.mode, &self.name)
}
}
+
+#[derive(JSTraceable, MallocSizeOf)]
+#[unrooted_must_root_lint::must_root]
+pub struct ElementsByNameList {
+ document: Dom<Document>,
+ name: DOMString,
+}
+
+impl ElementsByNameList {
+ pub fn new(document: &Document, name: DOMString) -> ElementsByNameList {
+ ElementsByNameList {
+ document: Dom::from_ref(document),
+ name: name,
+ }
+ }
+
+ pub fn len(&self) -> u32 {
+ self.document.elements_by_name_count(&self.name)
+ }
+
+ pub fn item(&self, index: u32) -> Option<DomRoot<Node>> {
+ self.document
+ .nth_element_by_name(index, &self.name)
+ .and_then(|n| Some(DomRoot::from_ref(&*n)))
+ }
+}