Bug 1335905 Searching direct Text node works. Removes old searches draft
authorFirefox <manotejmeka@gmail.com>
Sun, 12 Feb 2017 11:56:54 -0500
changeset 482426 1cb48f5dee846a4bd65f00a8c3336b8402ee5701
parent 482425 0354ce910aeb38782708503e156cf6200aad7c3a
child 545428 9cd53d4836c3e543e242168288ab18f41376721f
push id45076
push usermanotejmeka@gmail.com
push dateSun, 12 Feb 2017 16:57:29 +0000
bugs1335905
milestone54.0a1
Bug 1335905 Searching direct Text node works. Removes old searches MozReview-Commit-ID: Cfl9LS1Fm33
browser/components/preferences/in-content/SearchEach.js
--- a/browser/components/preferences/in-content/SearchEach.js
+++ b/browser/components/preferences/in-content/SearchEach.js
@@ -18,39 +18,38 @@ function hideShowSearch () {
             vis.display = 'none';
         }
     });
     
 
 }
 
 function searchFunc(element) {
-    if(mainSeachEmelemt.value){
+    if(mainSeachEmelemt[0].value){
         console.time('SearchTime');
 
         //Controller
         let controller = getSelectionController();
         let findSelection = controller.getSelection(Ci.nsISelectionController.SELECTION_FIND);
+        findSelection.removeAllRanges()
 
         //Building the range for highlighted areas
-        let range = document.createRange();
         let rootPreferences = document.getElementById('mainPrefPane')//.querySelectorAll('*')
         let rootPreferencesChildren = rootPreferences.childNodes
         //Remove later, used for the debugs
         let foundCounter = 0;
 
         for (let i = 0; i<rootPreferences.childElementCount; i++){
-            if(nodeRecursion(rootPreferencesChildren[i], mainSeachEmelemt.value, range)){
+            if(nodeRecursion(rootPreferencesChildren[i], mainSeachEmelemt[0].value, findSelection)){
                 foundCounter++; //<Remove this 
                 rootPreferencesChildren[i].setAttribute('hidden', 'false');
             }else{
                 rootPreferencesChildren[i].setAttribute('hidden', 'true');
             }
         }
-        findSelection.addRange(range); // Select it all ones done searching
         console.log('found and highlighted: ', foundCounter) //<Remove later
         console.timeEnd('SearchTime')
     } else{
         console.log("Empty string not searching")
     }
 
 }
 
@@ -74,38 +73,42 @@ hideShowSearch();
 
     let searchStr = str.toLowerCase();
     let filterStrings = filter.toLowerCase().split(/\s+/);
     return !filterStrings.some(function (f) {
       return searchStr.indexOf(f) == -1;
     });
   }
 
-  function nodeRecursion(nodeObject, searchPhrase, range) {
+  function nodeRecursion(nodeObject, searchPhrase, findSelection) {
       if(nodeObject.childElementCount == 0 && 
       (getTextContentAttribute(nodeObject,searchPhrase) //|| 
        //getLabelAttribute(nodeObject,searchPhrase))
       )) {
           //Drill down deeper 
           //if(nodeObject.boxObject){
           //    getRangeObject(nodeObject.boxObject, searchPhrase.trim().split(), range);
           //}
           // Finding Text nodes in leaf node
           let leaf = textNodesUnder(nodeObject)
           let listOfWords = searchPhrase.trim().split()
 
+          console.log("Leaf")
+          console.log(leaf)
           leaf.forEach(function (node) {
+              console.log(node)
               listOfWords.forEach(function (word){
-                searchWord(node,node.textContent,word,range)
+                searchWord(node,node.textContent,word,findSelection)
             });
           });
+          
 
           let anonymous = textNodesUnder(nodeObject.boxObject)
           let anonymousTextContent = ''
-          leaf.forEach(function (node) {
+          anonymous.forEach(function (node) {
               anonymousTextContent += node.textContent
           });
           console.log("anonymousTextContent")
           console.log(anonymousTextContent)
 
           
           /*
           allTextNodes.forEach(function (node) {
@@ -114,17 +117,17 @@ hideShowSearch();
             });
           });
           */
           
           return true
       }
       let foundInChild = false
       for (let i = 0; i<nodeObject.childElementCount; i++){
-          foundInChild = foundInChild || nodeRecursion(nodeObject.childNodes[i],searchPhrase, range)
+          foundInChild = foundInChild || nodeRecursion(nodeObject.childNodes[i],searchPhrase, findSelection)
       }
 
       return foundInChild
   }
 
   //http://stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page
   function textNodesUnder(node) {
     let all = [];
@@ -147,30 +150,34 @@ hideShowSearch();
     }
 
     
   }
   */
 
   //nodeObject.boxObject.firstChild.nextSibling.firstChild.nextSibling.firstChild.textContent.includes("Night")
 
-  function searchWord(textNode, textSearch, word, range){
+  function searchWord(textNode, textSearch, word, findSelection){
       let regExp = new RegExp(word,'gi')
       let result,indices = []
 
       while( (result=regExp.exec(textSearch)) ){
           indices.push(result.index)
       }
 
       // Add each found word to range
       for(let i=0;i<indices.length; i++){
+        let range = document.createRange();
         range.setStart(textNode, indices[i]);
         range.setEnd(textNode, (indices[i]+word.length) );
+        findSelection.addRange(range); // Add each range to be highlighted
       }
 
+      
+
   }
 
   function getTextContentAttribute(nodeObject,searchPhrase){
       if(typeof nodeObject.textContent == 'string' && nodeObject.textContent != ''
       && stringMatchesFilters(nodeObject.textContent, searchPhrase)){
           /*
           words = searchPhrase.trim().split()
           for(let i=0; i<words.length; i++){