diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2019-04-29 08:38:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-29 08:38:50 -0400 |
commit | 799490a02e9bea575bf34c39f045ef0883539f05 (patch) | |
tree | 278cada683564db36997cf3f96c92513b89c3096 /components/script/dom/documentfragment.rs | |
parent | d58ea974baff1b51a43d2e2bf4b287ff11991a8d (diff) | |
parent | 37e88e77cdf00e3555599dd4004d03548bd95dcf (diff) | |
download | servo-799490a02e9bea575bf34c39f045ef0883539f05.tar.gz servo-799490a02e9bea575bf34c39f045ef0883539f05.zip |
Auto merge of #22743 - ferjm:shadowdom, r=emilio
Partial ShadowDOM support
This is just an early WIP, not to take it very seriously yet.
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [x] These changes fix #22719
- [x] There are tests for these changes
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/22743)
<!-- Reviewable:end -->
Diffstat (limited to 'components/script/dom/documentfragment.rs')
-rw-r--r-- | components/script/dom/documentfragment.rs | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/components/script/dom/documentfragment.rs b/components/script/dom/documentfragment.rs index 2e504750947..1561b0d007e 100644 --- a/components/script/dom/documentfragment.rs +++ b/components/script/dom/documentfragment.rs @@ -2,13 +2,14 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * 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; 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::DomRoot; +use crate::dom::bindings::root::{Dom, DomRoot}; use crate::dom::bindings::str::DOMString; use crate::dom::document::Document; use crate::dom::element::Element; @@ -18,18 +19,22 @@ 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()), } } @@ -46,6 +51,10 @@ impl DocumentFragment { Ok(DocumentFragment::new(&document)) } + + pub fn id_map(&self) -> &DomRefCell<HashMap<Atom, Vec<Dom<Element>>>> { + &self.id_map + } } impl DocumentFragmentMethods for DocumentFragment { @@ -57,16 +66,11 @@ impl DocumentFragmentMethods for DocumentFragment { // https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid fn GetElementById(&self, id: DOMString) -> Option<DomRoot<Element>> { - let node = self.upcast::<Node>(); let id = Atom::from(id); - node.traverse_preorder() - .filter_map(DomRoot::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 |