aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2018-05-18 18:27:50 -0400
committerGitHub <noreply@github.com>2018-05-18 18:27:50 -0400
commitfe1a057bd1dac069998c7f4fd23027bb0629025e (patch)
treeb11a6594c15091f8d1c24278acfb5e0472fb47d0 /components/script/dom
parent4ff3389c90fcfd93a2a66cf4467f2cffefaf100a (diff)
parent5bfa81903885712373e2747e138f2abaf4a83d95 (diff)
downloadservo-fe1a057bd1dac069998c7f4fd23027bb0629025e.tar.gz
servo-fe1a057bd1dac069998c7f4fd23027bb0629025e.zip
Auto merge of #20740 - fabricedesre:mutation-observer-disconnect, r=jdm
Implement MutationObserver.disconnect() <!-- Please describe your changes on the following line: --> This implements https://dom.spec.whatwg.org/#dom-mutationobserver-disconnect I added a `node_list` to the `MutationObserver` struct to keep track of the nodes involved in an observer because otherwise I could not find a way to unregister the observers on the node themselves without traversing the whole document tree. Let me know if there's something I missed. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [x] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- 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/20740) <!-- Reviewable:end -->
Diffstat (limited to 'components/script/dom')
-rw-r--r--components/script/dom/mutationobserver.rs18
-rw-r--r--components/script/dom/node.rs7
-rw-r--r--components/script/dom/webidls/MutationObserver.webidl2
3 files changed, 25 insertions, 2 deletions
diff --git a/components/script/dom/mutationobserver.rs b/components/script/dom/mutationobserver.rs
index 97b4ca36d27..d0dc201f12e 100644
--- a/components/script/dom/mutationobserver.rs
+++ b/components/script/dom/mutationobserver.rs
@@ -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> {
@@ -38,7 +39,7 @@ pub enum Mutation<'a> {
#[derive(JSTraceable, MallocSizeOf)]
pub struct RegisteredObserver {
- observer: DomRoot<MutationObserver>,
+ pub observer: DomRoot<MutationObserver>,
options: ObserverOptions,
}
@@ -64,6 +65,7 @@ impl MutationObserver {
reflector_: Reflector::new(),
callback: callback,
record_queue: DomRefCell::new(vec![]),
+ node_list: DomRefCell::new(vec![]),
}
}
@@ -286,6 +288,8 @@ impl MutationObserverMethods for MutationObserver {
child_list
},
});
+
+ self.node_list.borrow_mut().push(DomRoot::from_ref(target));
}
Ok(())
@@ -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();
+ }
}
diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs
index 64b808c3859..edc1b6d8366 100644
--- a/components/script/dom/node.rs
+++ b/components/script/dom/node.rs
@@ -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);
diff --git a/components/script/dom/webidls/MutationObserver.webidl b/components/script/dom/webidls/MutationObserver.webidl
index 015686c47d9..426e995fc63 100644
--- a/components/script/dom/webidls/MutationObserver.webidl
+++ b/components/script/dom/webidls/MutationObserver.webidl
@@ -11,7 +11,7 @@
interface MutationObserver {
[Throws]
void observe(Node target, optional MutationObserverInit options);
- //void disconnect();
+ void disconnect();
sequence<MutationRecord> takeRecords();
};