Bug 1335905 Timer Auto-Search. Highlights Checkboxes, Text and radio objects. Clears highlighted fileds. Empty string input fixed. draft
authorFirefox <manotejmeka@gmail.com>
Sun, 12 Feb 2017 15:56:52 -0500
changeset 486796 eef0c8404252000b9f38440d23aed2425fa60552
parent 486795 57912fd645df994f25d15d727b25b9d8d2f5ec55
child 486797 4be0f69a012f9a66d75b90c907c6fa03ba96af4e
push id46062
push userbmo:manotejmeka@gmail.com
push dateMon, 20 Feb 2017 00:32:40 +0000
bugs1335905
milestone54.0a1
Bug 1335905 Timer Auto-Search. Highlights Checkboxes, Text and radio objects. Clears highlighted fileds. Empty string input fixed. MozReview-Commit-ID: AcpryZtM9dS
browser/components/preferences/in-content/SearchEach.js
browser/components/preferences/in-content/preferences.xul
--- a/browser/components/preferences/in-content/SearchEach.js
+++ b/browser/components/preferences/in-content/SearchEach.js
@@ -1,143 +1,207 @@
 console.log('in the SearchEach file');
 
-let mainSeachEmelemt = document.querySelectorAll('#searchTextIMF');
+let mainSearchElement = document.querySelectorAll('#searchTextIMF');
+
+document.getElementById("searchTextIMF").addEventListener("command", searchFunc);
 
 function hideShowSearch () {
-
+    
     let mainSearchEnable = Services.prefs.getBoolPref('browser.preference.search')
 
-    mainSeachEmelemt.forEach(function (node){
+    mainSearchElement.forEach(function (node){
         let vis = node.style;
 
         if (mainSearchEnable)
         {
             vis.display = 'block';
         }
         else
         {
             vis.display = 'none';
         }
     });
     
+    //if (!TransientPrefs.prefShouldBeVisible("browser.tabs.warnOnOpen"))
+    //  document.getElementById("warnOpenMany").hidden = true
+    
+
+    
 
 }
 
-function searchFunc(element) {
-    if(mainSeachEmelemt[0].value){
-        console.time('SearchTime');
+function searchFunc(event) {
+    let element = event.target;
+    //Controller
+    let controller = getSelectionController();
+    let findSelection = controller.getSelection(Ci.nsISelectionController.SELECTION_FIND);
+    findSelection.removeAllRanges();
 
-        //Controller
-        let controller = getSelectionController();
-        let findSelection = controller.getSelection(Ci.nsISelectionController.SELECTION_FIND);
-        findSelection.removeAllRanges()
+    if(element.value){
+        console.time('SearchTime');
 
         //Building the range for highlighted areas
         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[0].value, findSelection)){
+            if(nodeRecursion(rootPreferencesChildren[i], element.value, findSelection)){
                 foundCounter++; //<Remove this 
                 rootPreferencesChildren[i].setAttribute('hidden', 'false');
             }else{
                 rootPreferencesChildren[i].setAttribute('hidden', 'true');
             }
         }
         console.log('found and highlighted: ', foundCounter) //<Remove later
         console.timeEnd('SearchTime')
     } else{
-        console.log("Empty string not searching")
+        console.log("Empty string not searching, clearing shit instead");
     }
 
 }
-
-mainSeachEmelemt.innerHTML = ''
+mainSearchElement.innerHTML = ''
+mainSearchElement.value = ''
 hideShowSearch();
 
 
-  /**
-   * Check that the passed string matches the filter arguments.
-   *
-   * @param String str
-   *        to search for filter words in.
-   * @param String filter
-   *        is a string containing all of the words to filter on.
-   * @returns boolean
-   */
-  function stringMatchesFilters (str, filter) {
+/**
+ * Check that the passed string matches the filter arguments.
+ *
+ * @param String str
+ *        to search for filter words in.
+ * @param String filter
+ *        is a string containing all of the words to filter on.
+ * @returns boolean
+ */
+function stringMatchesFilters (str, filter) {
     if (!filter || !str) {
-      return true;
+        return true;
     }
 
     let searchStr = str.toLowerCase();
     let filterStrings = filter.toLowerCase().split(/\s+/);
     return !filterStrings.some(function (f) {
-      return searchStr.indexOf(f) == -1;
+        return searchStr.indexOf(f) == -1;
     });
-  }
+}
 
+function nodeRecursion(nodeObject, searchPhrase, findSelection) {
+    let foundIn = false
+    if(nodeObject.childElementCount == 0){
+        let leafTextNodes, otherTextNodes = []
+        let listOfWords = searchPhrase.trim().split()
+        if(nodeObject){
+        leafTextNodes = textNodesUnder(nodeObject)
+        }
+        if(nodeObject.boxObject){
+        otherTextNodes = textNodesUnder(nodeObject.boxObject)
+        }
+        
+        if (leafTextNodes || otherTextNodes){
+            //console.log(nodeObject)
+            //console.log(leafTextNodes,otherTextNodes)
+        }
+        
+        leafTextNodes.forEach(function (node) {
+            //console.log(node
+            listOfWords.forEach(function (word){
+                let boolAns = searchWord(node,node.textContent,word,findSelection)
+                foundIn = foundIn || boolAns
+            });
+        });
+        otherTextNodes.forEach(function (node) {
+            //console.log(node)
+            listOfWords.forEach(function (word){
+                let boolAns = searchWord(node,node.textContent,word,findSelection)
+                foundIn = foundIn || boolAns
+            });
+        });
+    }
+    for(let i=0; i<nodeObject.childElementCount; i++){
+        let boolAns = nodeRecursion(nodeObject.childNodes[i], searchPhrase, findSelection)
+        foundIn = foundIn || boolAns
+        //foundIn = foundIn || nodeRecursion(nodeObject.childNodes[i], searchPhrase, findSelection)
+    }
+    return foundIn
+}
+/*
   function nodeRecursion(nodeObject, searchPhrase, findSelection) {
-      if(nodeObject.childElementCount == 0 && 
-      (getTextContentAttribute(nodeObject,searchPhrase) //|| 
+      console.log(nodeObject)
+      if(nodeObject.childElementCount == 0 //&& 
+      //(getTextContentAttribute(nodeObject,searchPhrase) || 
        //getLabelAttribute(nodeObject,searchPhrase))
-      )) {
+     // (textNodesUnder(nodeObject) || textNodesUnder(nodeObject.boxObject))
+      ) {
           //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)
+          //console.log("Leaf")
+          //console.log(leaf)
           leaf.forEach(function (node) {
-              console.log(node)
+              //console.log(node)
               listOfWords.forEach(function (word){
                 searchWord(node,node.textContent,word,findSelection)
             });
           });
           
-
+          // Checkboxes, radio buttons
           let anonymous = textNodesUnder(nodeObject.boxObject)
           let anonymousTextContent = ''
+          let anonymousLabelContent = ''
+          let nodeSizes = []
+          let nodeLengthCounter = 0
+          //console.log("anonymousTextContent", nodeObject, anonymous)
           anonymous.forEach(function (node) {
               anonymousTextContent += node.textContent
+              anonymousLabelContent += node.label
+              nodeLengthCounter += node.length
+              //console.log("itter:",node, anonymousTextContent,anonymousLabelContent,nodeLengthCounter)
+              nodeSizes.push(nodeLengthCounter)
           });
-          console.log("anonymousTextContent")
-          console.log(anonymousTextContent)
-
+          
+          //console.log(anonymousTextContent)
+          
+          listOfWords.forEach(function (word){
+                multiSearch(anonymous,nodeSizes,anonymousTextContent,word,findSelection)
+          });
+          
           
           /*
           allTextNodes.forEach(function (node) {
               listOfWords.forEach(function (word){
                 searchWord(node,node.textContent,word,range)
             });
           });
-          */
+          *
           
           return true
       }
       let foundInChild = false
       for (let i = 0; i<nodeObject.childElementCount; i++){
           foundInChild = foundInChild || nodeRecursion(nodeObject.childNodes[i],searchPhrase, findSelection)
       }
 
-      return foundInChild
+      //return foundInChild
   }
+  */
 
   //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);
+        if (node.nodeType===node.TEXT_NODE) all.push(node);
         else all = all.concat(textNodesUnder(node));
     }
     return all;
   }
 
   /*
   function getRangeObject(elementObject, listOfWords, range){
     let firstChildElement = elementObject.firstChild
@@ -165,19 +229,31 @@ hideShowSearch();
 
       // 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
       }
+      return indices.length > 0
+  }
 
-      
+  function multiSearch(textNodes, nodeSizes, textSearch, word, findSelection){
+      let regExp = new RegExp(word,'gi')
+      let result,indices = []
 
+      while( (result=regExp.exec(textSearch)) ){
+          indices.push(result.index)
+      }
+
+      //console.log("multiSearch")
+      //console.log(textNodes)
+      //console.log(nodeSizes)
+      //console.log(indices)      
   }
 
   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++){
--- a/browser/components/preferences/in-content/preferences.xul
+++ b/browser/components/preferences/in-content/preferences.xul
@@ -185,19 +185,17 @@
            Remove this keyset once bug 1094240 ("disablefastfind" attribute
            broken in e10s mode) is fixed. -->
       <key key="&focusSearch1.key;" modifiers="accel" id="focusSearch1" oncommand=";"/>
     </keyset>
 
 
     <vbox class="main-content" flex="1">
       <div align="right">  
-        <input xmlns="http://www.w3.org/1999/xhtml" name="q" value="" id="searchTextIMF" maxlength="500" 
-        placeholder="Search" type="text"></input>
-        <button id="searchTextIMF" onclick="searchFunc(this)" type="button">Click Me!</button>
+        <textbox type="search" name="q" value="" id="searchTextIMF" placeholder="Search" oncommand=";"/>
       </div>
       <prefpane id="mainPrefPane">
 #include main.xul
 #include search.xul
 #include privacy.xul
 #include containers.xul
 #include advanced.xul
 #include applications.xul