Added 'parent is/was in tree' param to bind_to_tree/unbind_from_tree

According to a talk with Ms2ger, both bind_to_tree / unbind_from_tree
should be called regardless if the tree is part of a Document. This
information is now passed as a parameter to their respective virtual
methods.
This commit is contained in:
Bruno de Oliveira Abinader 2014-07-14 14:15:41 -04:00
parent e8996d5ce5
commit aea4ccf849
5 changed files with 32 additions and 25 deletions

View file

@ -836,12 +836,14 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
} }
} }
fn bind_to_tree(&self) { fn bind_to_tree(&self, tree_in_doc: bool) {
match self.super_type() { match self.super_type() {
Some(ref s) => s.bind_to_tree(), Some(ref s) => s.bind_to_tree(tree_in_doc),
_ => (), _ => (),
} }
if !tree_in_doc { return; }
match self.get_attribute(Null, "id").root() { match self.get_attribute(Null, "id").root() {
Some(attr) => { Some(attr) => {
let doc = document_from_node(self).root(); let doc = document_from_node(self).root();
@ -851,12 +853,14 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
} }
} }
fn unbind_from_tree(&self) { fn unbind_from_tree(&self, tree_in_doc: bool) {
match self.super_type() { match self.super_type() {
Some(ref s) => s.unbind_from_tree(), Some(ref s) => s.unbind_from_tree(tree_in_doc),
_ => (), _ => (),
} }
if !tree_in_doc { return; }
match self.get_attribute(Null, "id").root() { match self.get_attribute(Null, "id").root() {
Some(attr) => { Some(attr) => {
let doc = document_from_node(self).root(); let doc = document_from_node(self).root();

View file

@ -176,12 +176,14 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLIFrameElement> {
} }
} }
fn bind_to_tree(&self) { fn bind_to_tree(&self, tree_in_doc: bool) {
match self.super_type() { match self.super_type() {
Some(ref s) => s.bind_to_tree(), Some(ref s) => s.bind_to_tree(tree_in_doc),
_ => (), _ => (),
} }
if !tree_in_doc { return; }
match self.get_url() { match self.get_url() {
Some(url) => { Some(url) => {
let sandboxed = if self.is_sandboxed() { let sandboxed = if self.is_sandboxed() {

View file

@ -79,9 +79,9 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLStyleElement> {
self.parse_own_css(); self.parse_own_css();
} }
fn bind_to_tree(&self) { fn bind_to_tree(&self, tree_in_doc: bool) {
match self.super_type() { match self.super_type() {
Some(ref s) => s.bind_to_tree(), Some(ref s) => s.bind_to_tree(tree_in_doc),
_ => () _ => ()
} }
self.parse_own_css(); self.parse_own_css();

View file

@ -233,7 +233,7 @@ pub enum NodeTypeId {
trait PrivateNodeHelpers { trait PrivateNodeHelpers {
fn node_inserted(&self); fn node_inserted(&self);
fn node_removed(&self); fn node_removed(&self, parent_in_doc: bool);
fn add_child(&self, new_child: &JSRef<Node>, before: Option<JSRef<Node>>); fn add_child(&self, new_child: &JSRef<Node>, before: Option<JSRef<Node>>);
fn remove_child(&self, child: &JSRef<Node>); fn remove_child(&self, child: &JSRef<Node>);
} }
@ -243,11 +243,10 @@ impl<'a> PrivateNodeHelpers for JSRef<'a, Node> {
fn node_inserted(&self) { fn node_inserted(&self) {
assert!(self.parent_node().is_some()); assert!(self.parent_node().is_some());
let document = document_from_node(self).root(); let document = document_from_node(self).root();
let is_in_doc = self.is_in_doc();
if self.is_in_doc() {
for node in self.traverse_preorder() { for node in self.traverse_preorder() {
vtable_for(&node).bind_to_tree(); vtable_for(&node).bind_to_tree(is_in_doc);
}
} }
let parent = self.parent_node().root(); let parent = self.parent_node().root();
@ -257,13 +256,12 @@ impl<'a> PrivateNodeHelpers for JSRef<'a, Node> {
} }
// http://dom.spec.whatwg.org/#node-is-removed // http://dom.spec.whatwg.org/#node-is-removed
fn node_removed(&self) { fn node_removed(&self, parent_in_doc: bool) {
assert!(self.parent_node().is_none()); assert!(self.parent_node().is_none());
let document = document_from_node(self).root(); let document = document_from_node(self).root();
for node in self.traverse_preorder() { for node in self.traverse_preorder() {
// XXX how about if the node wasn't in the tree in the first place? vtable_for(&node).unbind_from_tree(parent_in_doc);
vtable_for(&node).unbind_from_tree();
} }
document.deref().content_changed(); document.deref().content_changed();
@ -1187,8 +1185,9 @@ impl Node {
// Step 6: mutation records. // Step 6: mutation records.
// Step 7. // Step 7.
let parent_in_doc = parent.is_in_doc();
for removedNode in removedNodes.iter() { for removedNode in removedNodes.iter() {
removedNode.node_removed(); removedNode.node_removed(parent_in_doc);
} }
for addedNode in addedNodes.iter() { for addedNode in addedNodes.iter() {
addedNode.node_inserted(); addedNode.node_inserted();
@ -1224,7 +1223,7 @@ impl Node {
// Step 9. // Step 9.
match suppress_observers { match suppress_observers {
Suppressed => (), Suppressed => (),
Unsuppressed => node.node_removed(), Unsuppressed => node.node_removed(parent.is_in_doc()),
} }
} }
@ -1713,7 +1712,7 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
// Step 12-14. // Step 12-14.
// Step 13: mutation records. // Step 13: mutation records.
child.node_removed(); child.node_removed(self.is_in_doc());
if node.type_id() == DocumentFragmentNodeTypeId { if node.type_id() == DocumentFragmentNodeTypeId {
for child_node in node.children() { for child_node in node.children() {
child_node.node_inserted(); child_node.node_inserted();

View file

@ -60,18 +60,20 @@ pub trait VirtualMethods {
} }
} }
/// Called when a Node is appended to a tree that is part of a Document. /// Called when a Node is appended to a tree, where 'tree_in_doc' indicates
fn bind_to_tree(&self) { /// whether the tree is part of a Document.
fn bind_to_tree(&self, tree_in_doc: bool) {
match self.super_type() { match self.super_type() {
Some(ref s) => s.bind_to_tree(), Some(ref s) => s.bind_to_tree(tree_in_doc),
_ => (), _ => (),
} }
} }
/// Called when a Node is removed from a tree that is part of a Document. /// Called when a Node is removed from a tree, where 'tree_in_doc'
fn unbind_from_tree(&self) { /// indicates whether the tree is part of a Document.
fn unbind_from_tree(&self, tree_in_doc: bool) {
match self.super_type() { match self.super_type() {
Some(ref s) => s.unbind_from_tree(), Some(ref s) => s.unbind_from_tree(tree_in_doc),
_ => (), _ => (),
} }
} }