Skip to content

Commit

Permalink
Implement MutationObserver.disconnect()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricedesre committed May 9, 2018
1 parent 2c9ef9e commit 5bfa819
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
18 changes: 17 additions & 1 deletion components/script/dom/mutationobserver.rs
Expand Up @@ -27,6 +27,7 @@ pub struct MutationObserver {
#[ignore_malloc_size_of = "can't measure Rc values"]
callback: Rc<MutationCallback>,
record_queue: DomRefCell<Vec<DomRoot<MutationRecord>>>,
node_list: DomRefCell<Vec<DomRoot<Node>>>,
}

pub enum Mutation<'a> {
Expand All @@ -38,7 +39,7 @@ pub enum Mutation<'a> {

#[derive(JSTraceable, MallocSizeOf)]
pub struct RegisteredObserver {
observer: DomRoot<MutationObserver>,
pub observer: DomRoot<MutationObserver>,
options: ObserverOptions,
}

Expand All @@ -64,6 +65,7 @@ impl MutationObserver {
reflector_: Reflector::new(),
callback: callback,
record_queue: DomRefCell::new(vec![]),
node_list: DomRefCell::new(vec![]),
}
}

Expand Down Expand Up @@ -286,6 +288,8 @@ impl MutationObserverMethods for MutationObserver {
child_list
},
});

self.node_list.borrow_mut().push(DomRoot::from_ref(target));
}

Ok(())
Expand All @@ -297,4 +301,16 @@ impl MutationObserverMethods for MutationObserver {
self.record_queue.borrow_mut().clear();
records
}

/// https://dom.spec.whatwg.org/#dom-mutationobserver-disconnect
fn Disconnect(&self) {
// Step 1
let mut nodes = self.node_list.borrow_mut();
for node in nodes.drain(..) {
node.remove_mutation_observer(self);
}

// Step 2
self.record_queue.borrow_mut().clear();
}
}
7 changes: 7 additions & 0 deletions components/script/dom/node.rs
Expand Up @@ -392,6 +392,13 @@ impl Node {
self.mutation_observers.borrow_mut()
}

/// Removes the mutation observer for a given node.
pub fn remove_mutation_observer(&self, observer: &MutationObserver) {
self.mutation_observers.borrow_mut().retain(|reg_obs| {
&*reg_obs.observer != observer
})
}

/// Dumps the subtree rooted at this node, for debugging.
pub fn dump(&self) {
self.dump_indent(0);
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/webidls/MutationObserver.webidl
Expand Up @@ -11,7 +11,7 @@
interface MutationObserver {
[Throws]
void observe(Node target, optional MutationObserverInit options);
//void disconnect();
void disconnect();
sequence<MutationRecord> takeRecords();
};

Expand Down
1 change: 0 additions & 1 deletion tests/wpt/metadata/dom/nodes/Element-classlist.html.ini
@@ -1,6 +1,5 @@
[Element-classlist.html]
type: testharness
expected: TIMEOUT
[classList.remove("a") with attribute value null (HTML node)]
expected: FAIL

Expand Down

This file was deleted.

@@ -1,7 +1,5 @@
[MutationObserver-document.html]
type: testharness
[setup test]
expected: FAIL

[parser insertion mutations]
expected: FAIL
Expand Down

This file was deleted.

0 comments on commit 5bfa819

Please sign in to comment.