diff options
Diffstat (limited to 'components/script/dom/documentfragment.rs')
-rw-r--r-- | components/script/dom/documentfragment.rs | 87 |
1 files changed, 51 insertions, 36 deletions
diff --git a/components/script/dom/documentfragment.rs b/components/script/dom/documentfragment.rs index 6f2a123dd06..ac87a9a786e 100644 --- a/components/script/dom/documentfragment.rs +++ b/components/script/dom/documentfragment.rs @@ -1,78 +1,88 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -use dom::bindings::codegen::Bindings::DocumentFragmentBinding; -use dom::bindings::codegen::Bindings::DocumentFragmentBinding::DocumentFragmentMethods; -use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; -use dom::bindings::codegen::UnionTypes::NodeOrString; -use dom::bindings::error::{ErrorResult, Fallible}; -use dom::bindings::inheritance::Castable; -use dom::bindings::js::Root; -use dom::bindings::str::DOMString; -use dom::document::Document; -use dom::element::Element; -use dom::htmlcollection::HTMLCollection; -use dom::node::{Node, window_from_node}; -use dom::nodelist::NodeList; -use dom::window::Window; + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use crate::dom::bindings::cell::DomRefCell; +use crate::dom::bindings::codegen::Bindings::DocumentFragmentBinding::DocumentFragmentMethods; +use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; +use crate::dom::bindings::codegen::UnionTypes::NodeOrString; +use crate::dom::bindings::error::{ErrorResult, Fallible}; +use crate::dom::bindings::inheritance::Castable; +use crate::dom::bindings::root::{Dom, DomRoot}; +use crate::dom::bindings::str::DOMString; +use crate::dom::document::Document; +use crate::dom::element::Element; +use crate::dom::htmlcollection::HTMLCollection; +use crate::dom::node::{window_from_node, Node}; +use crate::dom::nodelist::NodeList; +use crate::dom::window::Window; use dom_struct::dom_struct; use servo_atoms::Atom; +use std::collections::HashMap; // https://dom.spec.whatwg.org/#documentfragment #[dom_struct] pub struct DocumentFragment { node: Node, + /// Caches for the getElement methods + id_map: DomRefCell<HashMap<Atom, Vec<Dom<Element>>>>, } impl DocumentFragment { /// Creates a new DocumentFragment. - fn new_inherited(document: &Document) -> DocumentFragment { + pub fn new_inherited(document: &Document) -> DocumentFragment { DocumentFragment { node: Node::new_inherited(document), + id_map: DomRefCell::new(HashMap::new()), } } - pub fn new(document: &Document) -> Root<DocumentFragment> { - Node::reflect_node(box DocumentFragment::new_inherited(document), - document, - DocumentFragmentBinding::Wrap) + pub fn new(document: &Document) -> DomRoot<DocumentFragment> { + Node::reflect_node( + Box::new(DocumentFragment::new_inherited(document)), + document, + ) } - pub fn Constructor(window: &Window) -> Fallible<Root<DocumentFragment>> { + #[allow(non_snake_case)] + pub fn Constructor(window: &Window) -> Fallible<DomRoot<DocumentFragment>> { let document = window.Document(); Ok(DocumentFragment::new(&document)) } + + pub fn id_map(&self) -> &DomRefCell<HashMap<Atom, Vec<Dom<Element>>>> { + &self.id_map + } } impl DocumentFragmentMethods for DocumentFragment { // https://dom.spec.whatwg.org/#dom-parentnode-children - fn Children(&self) -> Root<HTMLCollection> { + fn Children(&self) -> DomRoot<HTMLCollection> { let window = window_from_node(self); HTMLCollection::children(&window, self.upcast()) } // https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid - fn GetElementById(&self, id: DOMString) -> Option<Root<Element>> { - let node = self.upcast::<Node>(); + fn GetElementById(&self, id: DOMString) -> Option<DomRoot<Element>> { let id = Atom::from(id); - node.traverse_preorder().filter_map(Root::downcast::<Element>).find(|descendant| { - match descendant.get_attribute(&ns!(), &local_name!("id")) { - None => false, - Some(attr) => *attr.value().as_atom() == id, - } - }) + self.id_map + .borrow() + .get(&id) + .map(|ref elements| DomRoot::from_ref(&*(*elements)[0])) } // https://dom.spec.whatwg.org/#dom-parentnode-firstelementchild - fn GetFirstElementChild(&self) -> Option<Root<Element>> { + fn GetFirstElementChild(&self) -> Option<DomRoot<Element>> { self.upcast::<Node>().child_elements().next() } // https://dom.spec.whatwg.org/#dom-parentnode-lastelementchild - fn GetLastElementChild(&self) -> Option<Root<Element>> { - self.upcast::<Node>().rev_children().filter_map(Root::downcast::<Element>).next() + fn GetLastElementChild(&self) -> Option<DomRoot<Element>> { + self.upcast::<Node>() + .rev_children() + .filter_map(DomRoot::downcast::<Element>) + .next() } // https://dom.spec.whatwg.org/#dom-parentnode-childelementcount @@ -90,13 +100,18 @@ impl DocumentFragmentMethods for DocumentFragment { self.upcast::<Node>().append(nodes) } + // https://dom.spec.whatwg.org/#dom-parentnode-replacechildren + fn ReplaceChildren(&self, nodes: Vec<NodeOrString>) -> ErrorResult { + self.upcast::<Node>().replace_children(nodes) + } + // https://dom.spec.whatwg.org/#dom-parentnode-queryselector - fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<Root<Element>>> { + fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> { self.upcast::<Node>().query_selector(selectors) } // https://dom.spec.whatwg.org/#dom-parentnode-queryselectorall - fn QuerySelectorAll(&self, selectors: DOMString) -> Fallible<Root<NodeList>> { + fn QuerySelectorAll(&self, selectors: DOMString) -> Fallible<DomRoot<NodeList>> { self.upcast::<Node>().query_selector_all(selectors) } } |