Add overridable tree binding/unbinding behaviour.

This commit is contained in:
Josh Matthews 2014-02-13 08:50:28 +05:30 committed by Ms2ger
parent f5d1907195
commit 6388dec996
3 changed files with 39 additions and 10 deletions

View file

@ -727,4 +727,22 @@ impl VirtualMethods for JS<Element> {
self.notify_attribute_changed(name);
}
fn bind_to_tree(&mut self) {
match self.super_type() {
Some(ref mut s) => s.bind_to_tree(),
_ => (),
}
self.bind_to_tree_impl();
}
fn unbind_from_tree(&mut self) {
match self.super_type() {
Some(ref mut s) => s.unbind_from_tree(),
_ => (),
}
self.unbind_from_tree_impl();
}
}

View file

@ -19,12 +19,12 @@ use dom::comment::Comment;
use dom::document::{Document, HTMLDocument, NonHTMLDocument};
use dom::documentfragment::DocumentFragment;
use dom::documenttype::DocumentType;
use dom::element::{Element, ElementTypeId, HTMLAnchorElementTypeId, IElement};
use dom::element::{Element, ElementTypeId, HTMLAnchorElementTypeId};
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::nodelist::{NodeList};
use dom::processinginstruction::ProcessingInstruction;
use dom::text::Text;
use dom::virtualmethods::VirtualMethods;
use dom::virtualmethods::{VirtualMethods, vtable_for};
use dom::window::Window;
use html::hubbub_html_parser::build_element_from_tag;
use layout_interface::{LayoutChan, ReapLayoutDataMsg, UntrustedNodeAddress};
@ -404,10 +404,7 @@ impl NodeHelpers for JS<Node> {
if self.is_in_doc() {
for node in self.traverse_preorder() {
if node.is_element() {
let element: JS<Element> = ElementCast::to(&node).unwrap();
element.bind_to_tree_impl();
}
vtable_for(&node).bind_to_tree();
}
}
@ -420,10 +417,8 @@ impl NodeHelpers for JS<Node> {
let document = document_from_node(self);
for node in self.traverse_preorder() {
if node.is_element() {
let element: JS<Element> = ElementCast::to(&node).unwrap();
element.unbind_from_tree_impl();
}
// XXX how about if the node wasn't in the tree in the first place?
vtable_for(&node).unbind_from_tree();
}
document.get().content_changed();

View file

@ -42,6 +42,22 @@ pub trait VirtualMethods {
_ => (),
}
}
/// Called when a Node is appended to a tree that is part of a Document.
fn bind_to_tree(&mut self) {
match self.super_type() {
Some(ref mut s) => s.bind_to_tree(),
_ => (),
}
}
/// Called when a Node is removed from a tree that is part of a Document.
fn unbind_from_tree(&mut self) {
match self.super_type() {
Some(ref mut s) => s.unbind_from_tree(),
_ => (),
}
}
}
/// Obtain a VirtualMethods instance for a given Node-derived object. Any