Bug 1406912 - Fix _serializeRangeData does not handle ranges found in same node properly. r=mixedpuppy
authorKevin Jones <kevinhowjones@gmail.com>
Mon, 09 Oct 2017 12:42:00 -0400
changeset 427828 969e0b72c2eea55d49c3dc057ab3ccad1c1a1865
parent 427827 244f2649c736dc5dc9652f9ab839cd3dc25cd820
child 427829 314590a2d58878e952f209723153b341ea2a2d9c
push id97
push userfmarier@mozilla.com
push dateSat, 14 Oct 2017 01:12:59 +0000
reviewersmixedpuppy
bugs1406912
milestone58.0a1
Bug 1406912 - Fix _serializeRangeData does not handle ranges found in same node properly. r=mixedpuppy
browser/components/extensions/test/browser/browser_ext_find.js
browser/components/extensions/test/browser/file_find_frames.html
toolkit/components/extensions/FindContent.jsm
--- a/browser/components/extensions/test/browser/browser_ext_find.js
+++ b/browser/components/extensions/test/browser/browser_ext_find.js
@@ -73,16 +73,31 @@ add_task(async function testDuplicatePin
     browser.test.log("Test that case insensitive match works properly.");
     data = await browser.find.find("banana", {caseSensitive: false});
     browser.test.assertEq(6, data.count, "The number of matches found:");
 
     browser.test.log("Test that entire word match works properly.");
     data = await browser.find.find("banana", {entireWord: true});
     browser.test.assertEq(4, data.count, "The number of matches found, should skip 2 matches, \"banaNaland\" and \"bananAland\":");
 
+    let expectedRangeData = [{framePos: 0, text: "example", startTextNodePos: 15, startOffset: 11, endTextNodePos: 15, endOffset: 18},
+                             {framePos: 0, text: "example", startTextNodePos: 15, startOffset: 25, endTextNodePos: 15, endOffset: 32},
+                             {framePos: 0, text: "example", startTextNodePos: 18, startOffset: 6, endTextNodePos: 18, endOffset: 13},
+                             {framePos: 0, text: "example", startTextNodePos: 20, startOffset: 3, endTextNodePos: 20, endOffset: 10},
+                             {framePos: 1, text: "example", startTextNodePos: 0, startOffset: 0, endTextNodePos: 0, endOffset: 7},
+                             {framePos: 2, text: "example", startTextNodePos: 0, startOffset: 0, endTextNodePos: 0, endOffset: 7}];
+
+    browser.test.log("Test that word found in the same node, different nodes and different frames returns the correct rangeData results.");
+    data = await browser.find.find("example", {includeRangeData: true});
+    for (let i = 0; i < data.rangeData.length; i++) {
+      for (let name in data.rangeData[i]) {
+        browser.test.assertEq(expectedRangeData[i][name], data.rangeData[i][name], `rangeData[${i}].${name}:`);
+      }
+    }
+
     browser.test.log("Test that `rangeData` is not returned if `includeRangeData` is false.");
     data = await browser.find.find("banana", {caseSensitive: false, includeRangeData: false});
     browser.test.assertEq(false, !!data.rangeData, "The boolean cast value of `rangeData`:");
 
     browser.test.log("Test that `rectData` is not returned if `includeRectData` is false.");
     data = await browser.find.find("banana", {caseSensitive: false, includeRectData: false});
     browser.test.assertEq(false, !!data.rectData, "The boolean cast value of `rectData`:");
 
--- a/browser/components/extensions/test/browser/file_find_frames.html
+++ b/browser/components/extensions/test/browser/file_find_frames.html
@@ -5,11 +5,16 @@
 <p>Banana 0</p>
 <iframe src="data:text/html,<p>baNana 2</p>
                             <iframe src='data:text/html,banaNaland 4' height='50' width='100%'></iframe>
                             <iframe src='data:text/html,bananAland 5' height='50' width='100%'></iframe>
                             <p>banAna 3</p>" height="250" width="100%"></iframe>
 <p>bAnana 1</p>
 <p><b>fru</b>it<i><b>ca</b>ke</i></p>
 <p>ang<div>elf</div>ood</p>
+<p>This is an example of an example in the same node.</p>
+<p>This <span>is an example</span> of <span>an example</span> of ranges in separate nodes.</p>
+<iframe src="data:text/html,<p>example within a frame.</p>
+  <iframe src='data:text/html,example within a sub-frame' height='50' width='100%'></iframe>" height="250" width="100%">
+</iframe>
 </body>
 </html>
 
--- a/toolkit/components/extensions/FindContent.jsm
+++ b/toolkit/components/extensions/FindContent.jsm
@@ -131,30 +131,30 @@ class FindContent {
         framePos++;
       }
       lastDoc = doc;
 
       let data = {framePos, text: range.toString()};
       rangeData.push(data);
 
       if (node != range.startContainer) {
-        let node = walker.nextNode();
+        node = walker.nextNode();
         while (node) {
           nodeCountWin++;
           if (node == range.startContainer) {
             break;
           }
           node = walker.nextNode();
         }
       }
       data.startTextNodePos = nodeCountWin;
       data.startOffset = range.startOffset;
 
       if (range.startContainer != range.endContainer) {
-        let node = walker.nextNode();
+        node = walker.nextNode();
         while (node) {
           nodeCountWin++;
           if (node == range.endContainer) {
             break;
           }
           node = walker.nextNode();
         }
       }