diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2019-06-10 10:08:40 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-10 10:08:40 -0400 |
commit | 4c776f33d407c1ef84952967925318476ac9e3f6 (patch) | |
tree | c69848fc547afd925aa24b07523667cb5813c4fb /components/script | |
parent | 973a3448a459464b79ea0ef5fb46141176cc7643 (diff) | |
parent | 3df8d6891e0fe47d4af3d7ee59ba05ea64ee628c (diff) | |
download | servo-4c776f33d407c1ef84952967925318476ac9e3f6.tar.gz servo-4c776f33d407c1ef84952967925318476ac9e3f6.zip |
Auto merge of #23529 - gatoWololo:omar_split_mutation_observer, r=ferjm
Split getter for mutation_observers() into two methods.
Please let me know if these API changes look good:
registered_mutation_observers() returns immutable references and does
not init mutation observers.
registered_mutation_observers_mut() lazily initializes raredata if it
does not exist.
Updated code that uses this methods to call appropriate method when
mutation is not necessary.
<!-- Please describe your changes on the following line: -->
---
<!-- 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
- [X] These changes fix #23286
<!-- Either: -->
- [X] These changes do not require tests because: this is a small change to the API, and existing tests for Partial ShadowDOM support cover this API use/changes.
<!-- 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/23529)
<!-- Reviewable:end -->
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/dom/mutationobserver.rs | 9 | ||||
-rw-r--r-- | components/script/dom/node.rs | 21 |
2 files changed, 21 insertions, 9 deletions
diff --git a/components/script/dom/mutationobserver.rs b/components/script/dom/mutationobserver.rs index bb87a10f4b2..6320c0497d2 100644 --- a/components/script/dom/mutationobserver.rs +++ b/components/script/dom/mutationobserver.rs @@ -132,7 +132,12 @@ impl MutationObserver { // Step 2 & 3 for node in target.inclusive_ancestors(ShadowIncluding::No) { - for registered in &*node.registered_mutation_observers() { + let registered = node.registered_mutation_observers(); + if registered.is_none() { + continue; + } + + for registered in &*registered.unwrap() { if &*node != target && !registered.options.subtree { continue; } @@ -297,7 +302,7 @@ impl MutationObserverMethods for MutationObserver { // Step 7 let add_new_observer = { let mut replaced = false; - for registered in &mut *target.registered_mutation_observers() { + for registered in &mut *target.registered_mutation_observers_mut() { if &*registered.observer as *const MutationObserver != self as *const MutationObserver { diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 814378aca88..fe7f9ec29c2 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -451,18 +451,25 @@ impl Node { cmp & NodeConstants::DOCUMENT_POSITION_PRECEDING != 0 } - /// Return all registered mutation observers for this node. - /// XXX(ferjm) This should probably be split into two functions, - /// `registered_mutation_observers`, which returns an Option or - /// an empty slice or something, and doesn't create the rare data, - /// and `registered_mutation_observers_mut`, which does lazily - /// initialize the raredata. - pub fn registered_mutation_observers(&self) -> RefMut<Vec<RegisteredObserver>> { + /// Return all registered mutation observers for this node. Lazily initialize the + /// raredata if it does not exist. + pub fn registered_mutation_observers_mut(&self) -> RefMut<Vec<RegisteredObserver>> { RefMut::map(self.ensure_rare_data(), |rare_data| { &mut rare_data.mutation_observers }) } + pub fn registered_mutation_observers(&self) -> Option<Ref<Vec<RegisteredObserver>>> { + let rare_data: Ref<_> = self.rare_data.borrow(); + + if rare_data.is_none() { + return None; + } + Some(Ref::map(rare_data, |rare_data| { + &rare_data.as_ref().unwrap().mutation_observers + })) + } + /// Add a new mutation observer for a given node. pub fn add_mutation_observer(&self, observer: RegisteredObserver) { self.ensure_rare_data().mutation_observers.push(observer); |