Bug 1657759 - Fix TextPoints lesser-than operator. r=Jamie
authorEitan Isaacson <eitan@monotonous.org>
Tue, 11 Aug 2020 16:26:04 +0000
changeset 544292 2437e4c3bd865c8401e5e90623c1a8ab8ca8f9a7
parent 544291 8b3429b0e8da8d8d239611184f8d314cc2518fb6
child 544293 585a5613ef449295838f74f234cab582ff928b18
push id37692
push usercbrindusan@mozilla.com
push dateWed, 12 Aug 2020 03:44:18 +0000
treeherdermozilla-central@08471023c834 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersJamie
bugs1657759
milestone81.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1657759 - Fix TextPoints lesser-than operator. r=Jamie Differential Revision: https://phabricator.services.mozilla.com/D86276
accessible/base/TextRange.cpp
accessible/tests/mochitest/textrange/test_selection.html
--- a/accessible/base/TextRange.cpp
+++ b/accessible/base/TextRange.cpp
@@ -36,16 +36,36 @@ bool TextPoint::operator<(const TextPoin
   uint32_t pos1 = parents1.Length(), pos2 = parents2.Length();
   for (uint32_t len = std::min(pos1, pos2); len > 0; --len) {
     Accessible* child1 = parents1.ElementAt(--pos1);
     Accessible* child2 = parents2.ElementAt(--pos2);
     if (child1 != child2)
       return child1->IndexInParent() < child2->IndexInParent();
   }
 
+  if (pos1 != 0) {
+    // If parents1 is a superset of parents2 then mContainer is a
+    // descendant of aPoint.mContainer. The next element down in parents1
+    // is mContainer's ancestor that is the child of aPoint.mContainer.
+    // We compare its end offset in aPoint.mContainer with aPoint.mOffset.
+    Accessible* child = parents1.ElementAt(pos1 - 1);
+    MOZ_ASSERT(child->Parent() == aPoint.mContainer);
+    return child->EndOffset() < static_cast<uint32_t>(aPoint.mOffset);
+  }
+
+  if (pos2 != 0) {
+    // If parents2 is a superset of parents1 then aPoint.mContainer is a
+    // descendant of mContainer. The next element down in parents2
+    // is aPoint.mContainer's ancestor that is the child of mContainer.
+    // We compare its start offset in mContainer with mOffset.
+    Accessible* child = parents2.ElementAt(pos2 - 1);
+    MOZ_ASSERT(child->Parent() == mContainer);
+    return static_cast<uint32_t>(mOffset) < child->StartOffset();
+  }
+
   NS_ERROR("Broken tree?!");
   return false;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 // TextRange
 
 TextRange::TextRange(HyperTextAccessible* aRoot,
--- a/accessible/tests/mochitest/textrange/test_selection.html
+++ b/accessible/tests/mochitest/textrange/test_selection.html
@@ -89,16 +89,27 @@
       a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
       a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);
 
       testTextRange(a11yrange, "selection range #7", document, 4, document, 5);
 
       ok(a11yrange.crop(getAccessible("table")), "Range failed to crop #7.");
       testTextRange(a11yrange, "cropped range #7", "table", 0, "table", 1);
 
+      // test compare points for selection with start in nested node
+      range.setStart(a.firstChild, 2);
+      range.setEnd(p.lastChild, 3);
+      a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges;
+      a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange);
+      var res = a11yrange.compareEndPoints(EndPoint_Start, a11yrange, EndPoint_End);
+      is(res, -1, "start must be lesser than end");
+
+      res = a11yrange.compareEndPoints(EndPoint_End, a11yrange, EndPoint_Start);
+      is(res, 1, "end must be greater than start");
+
       SimpleTest.finish();
     }
 
     SimpleTest.waitForExplicitFinish();
     addA11yLoadEvent(doTest);
   </script>
 </head>
 <body>