Bug 1399091 - Avoid adding the same range to multiple selections. r=mats, a=sledru
authorCatalin Badea <catalin.badea392@gmail.com>
Sun, 24 Sep 2017 16:44:51 -0400
changeset 431807 3cfa5cf991a1c18a72929b2bc0b646580be24b3e
parent 431806 d0b9e449e7fb833b2d531c773c82cc81b5e6bbd9
child 431808 aa837b37b60cef2f61a086867f0111780d2d6700
push id7819
push userryanvm@gmail.com
push dateMon, 25 Sep 2017 13:25:08 +0000
treeherdermozilla-beta@078e47662790 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmats, sledru
bugs1399091
milestone57.0
Bug 1399091 - Avoid adding the same range to multiple selections. r=mats, a=sledru
dom/base/nsRange.cpp
--- a/dom/base/nsRange.cpp
+++ b/dom/base/nsRange.cpp
@@ -1043,22 +1043,29 @@ IndexOf(nsINode* aChild)
 }
 
 void
 nsRange::SetSelection(mozilla::dom::Selection* aSelection)
 {
   if (mSelection == aSelection) {
     return;
   }
+
   // At least one of aSelection and mSelection must be null
   // aSelection will be null when we are removing from a selection
   // and a range can't be in more than one selection at a time,
   // thus mSelection must be null too.
   MOZ_ASSERT(!aSelection || !mSelection);
 
+  // Extra step in case our parent failed to ensure the above
+  // invariant.
+  if (aSelection && mSelection) {
+    mSelection->RemoveRange(this);
+  }
+
   mSelection = aSelection;
   if (mSelection) {
     nsINode* commonAncestor = GetCommonAncestor();
     NS_ASSERTION(commonAncestor, "unexpected disconnected nodes");
     RegisterCommonAncestor(commonAncestor);
   } else {
     UnregisterCommonAncestor(mRegisteredCommonAncestor, false);
     MOZ_DIAGNOSTIC_ASSERT(!mRegisteredCommonAncestor,