Bug 1335905 Implementing Highlighting a new way draft
authorFirefox <herrickz@msu.edu>
Sun, 12 Feb 2017 10:59:51 -0500
changeset 482424 2c1a32b3046ddd63592ea1fdd9d31aa13041417a
parent 482238 e57b208bbe7d84c9808c1fa639c38a0b46bcf5aa
child 482425 0354ce910aeb38782708503e156cf6200aad7c3a
push id45074
push usermanotejmeka@gmail.com
push dateSun, 12 Feb 2017 16:00:37 +0000
bugs1335905
milestone54.0a1
Bug 1335905 Implementing Highlighting a new way MozReview-Commit-ID: LKpXLAVKNeN
browser/components/preferences/in-content/SearchEach.js
--- a/browser/components/preferences/in-content/SearchEach.js
+++ b/browser/components/preferences/in-content/SearchEach.js
@@ -1,17 +1,17 @@
 console.log('in the SearchEach file');
 
-var mainSeachEmelemt = document.getElementById('searchTextIMF');
+let mainSeachEmelemt = document.getElementById('searchTextIMF');
 
 function hideShowSearch () {
 
-    var mainSearchEnable = Services.prefs.getBoolPref('browser.preference.searchxx')
+    let mainSearchEnable = Services.prefs.getBoolPref('browser.preference.search')
 
-    var vis = mainSeachEmelemt.style;
+    let vis = mainSeachEmelemt.style;
 
     if (mainSearchEnable)
     {
         vis.display = 'block';
     }
     else
     {
         vis.display = 'none';
@@ -29,17 +29,17 @@ function searchFunc(element) {
     //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 (var i = 0; i<rootPreferences.childElementCount; i++){
+    for (let i = 0; i<rootPreferences.childElementCount; i++){
        if(nodeRecursion(rootPreferencesChildren[i], mainSeachEmelemt.value, range)){
            foundCounter++; //<Remove this 
            rootPreferencesChildren[i].setAttribute('hidden', 'false');
        }else{
            rootPreferencesChildren[i].setAttribute('hidden', 'true');
        }
     }
     findSelection.addRange(range); // Select it all ones done searching
@@ -70,96 +70,150 @@ hideShowSearch();
     let filterStrings = filter.toLowerCase().split(/\s+/);
     return !filterStrings.some(function (f) {
       return searchStr.indexOf(f) == -1;
     });
   }
 
   function nodeRecursion(nodeObject, searchPhrase, range) {
       if(nodeObject.childElementCount == 0 && 
-      (getTextContentAttribute(nodeObject,searchPhrase, range) || 
-       getLabelAttribute(nodeObject,searchPhrase, range))
-      ) {
-          //search to find exactly where it is
+      (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)
+          
+
+          leaf.forEach(function (node) {
+              listOfWords.forEach(function (word){
+                searchWord(node,node.textContent,word,range)
+            });
+          });
+
+          let anonymous = textNodesUnder(nodeObject.boxObject)
+          let anonymousTextContent = ''
+          leaf.forEach(function (node) {
+              anonymousTextContent += node.textContent
+          });
+
+
+          
+          /*
+          allTextNodes.forEach(function (node) {
+              listOfWords.forEach(function (word){
+                searchWord(node,node.textContent,word,range)
+            });
+          });
+          */
+          
           return true
       }
-      var foundInChild = false
-      for (var i = 0; i<nodeObject.childElementCount; i++){
+      let foundInChild = false
+      for (let i = 0; i<nodeObject.childElementCount; i++){
           foundInChild = foundInChild || nodeRecursion(nodeObject.childNodes[i],searchPhrase, range)
       }
 
       return foundInChild
   }
 
-  nodeObject.boxObject.firstChild.nextSibling.firstChild.nextSibling.firstChild.textContent.includes("Night")
-
-  function findTextNode(elementObject,word) {
-      stringMatchesFilters()
-      //if (elementObject.firstChild.textContent) 
+  //http://stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page
+  function textNodesUnder(node) {
+    let all = [];
+    for (node=node.firstChild;node;node=node.nextSibling){
+        if (node.nodeType==3) all.push(node);
+        else all = all.concat(textNodesUnder(node));
+    }
+    return all;
   }
 
-  function searchWord(nodeObject, textSearch, word, range){
+  /*
+  function getRangeObject(elementObject, listOfWords, range){
+    let firstChildElement = elementObject.firstChild
+    if(firstChildElement.nodeType === firstChildElement.TEXT_NODE){
+        listOfWords.forEach(function (word){
+            searchWord(firstChildElement,nodeObject.textContent,word,range)
+        });
+    } else if(firstChildElement.nextSibling){
+        getRangeObject(firstChildElement.nextSibling, listOfWords, range)
+    }
+
+    
+  }
+  */
+
+  //nodeObject.boxObject.firstChild.nextSibling.firstChild.nextSibling.firstChild.textContent.includes("Night")
+
+  function searchWord(textNode, textSearch, word, range){
       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++){
-        range.setStart(nodeObject, indices[i]);
-        range.setEnd(nodeObject, (indices[i]+word.length) );
+        range.setStart(textNode, indices[i]);
+        range.setEnd(textNode, (indices[i]+word.length) );
       }
 
   }
 
-  function getTextContentAttribute(nodeObject,searchPhrase, range){
+  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++){
             searchWord(nodeObject,nodeObject.textContent,words[i],range)
           }
+          */
           return true
       }
       return false
   }
 
-  function getLabelAttribute(nodeObject,searchPhrase,range){
+  function getLabelAttribute(nodeObject,searchPhrase){
       if(typeof nodeObject.label == 'string' && nodeObject.label != ''
       && stringMatchesFilters(nodeObject.label, searchPhrase)){
+          /*
           words = searchPhrase.trim().split()
           for(let i=0; i<words.length; i++){
             searchWord(nodeObject,nodeObject.label,words[i],range)
           }
+          */
           return true
       }
       return false
   }
 
+  /*
   function replaceText(originalText,words){
       words = words.replace(/ /g,'|')
-      var regExp = new RegExp(words,'gi')
+      let regExp = new RegExp(words,'gi')
       originalText = originalText.replace(regExp, function subFunction(x){return '<span class=\'search-highlighted\'>'+x+'</span>';});
       return originalText
 }
 
 function removeHighlightedSearch() {
-    var x = document.getElementsByClassName('search-highlighted');
+    let x = document.getElementsByClassName('search-highlighted');
     while(x.length){
         x[0].outerHTML = x[0].textContent
     }
     
 }
-
+*/
 function getSelectionController() {
     // Yuck. See bug 138068.
     let docShell = window.QueryInterface(Ci.nsIInterfaceRequestor)
                           .getInterface(Ci.nsIWebNavigation)
                           .QueryInterface(Ci.nsIDocShell);
 
     let controller = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
                              .getInterface(Ci.nsISelectionDisplay)
                              .QueryInterface(Ci.nsISelectionController);
 
     return controller
-} 
\ No newline at end of file
+}