Bug 1335905 - Commented the code and rearranged code and pulled central draft
authormanotejmeka <manotejmeka@gmail.com>
Wed, 22 Feb 2017 20:38:42 -0500
changeset 488399 3079d89f8da016f011e238a0f25680f3d04d2593
parent 488398 07f04cbbe33591da0c6d57fb52409f3a031db8b8
child 489341 662d34d51f8c77868cb7dc46dc471154f2c1a70e
push id46506
push userbmo:manotejmeka@gmail.com
push dateThu, 23 Feb 2017 01:53:57 +0000
bugs1335905
milestone54.0a1
Bug 1335905 - Commented the code and rearranged code and pulled central MozReview-Commit-ID: 6u3nFOzJbRA
browser/components/preferences/in-content/SearchEach.js
browser/components/preferences/in-content/searchResults.xul
--- a/browser/components/preferences/in-content/SearchEach.js
+++ b/browser/components/preferences/in-content/SearchEach.js
@@ -1,112 +1,48 @@
-// No lint errors
-// console.log("in the SearchEach file");
-
+// Search Object to load the 6th pin properly
 var gSearchResults = {
     init(){}
 }
+
+// Obtaining the search field
 let mainSearchElement = document.querySelectorAll("#searchTextIMF");
 
+// Boolean flag to init JavaScript bindings
 let firstTimeSearch = true;
 
 document.getElementById("searchTextIMF").addEventListener("command", searchFunc);
 
+// Obtaining the Search Results tag
 const searchResultsCategory = document.getElementById("categories").firstElementChild;
 
-function searchOnClick(){
-    // console.log("Search was clicked on");
-    //  Initializing all the JS for all the tabs
-    if (firstTimeSearch) {
-        firstTimeSearch = false;
-        gCategoryInits.forEach( function(eachTab) {
-            if (!eachTab.inited) {
-                eachTab.init();
-            }
-        });
-    }
-}
-
+/**
+ * Toggling Seachbar on and off according to browser.preference.search
+ * 
+ * @returns null
+ */
 function hideShowSearch() {
     let mainSearchEnable = Services.prefs.getBoolPref("browser.preference.search");
+    // Change this implementation is not put in each of the tabs. No loop
     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(event) {
-    let element = event.target;
-    // Controller
-    let controller = getSelectionController();
-    let findSelection = controller.getSelection(Ci.nsISelectionController.SELECTION_FIND);
-    findSelection.removeAllRanges();
-
-    let srHeader = document.getElementById("header-search-results");
-    let noResults = document.getElementById("noResultsFound");
-
-    if (element.value.trim()) {
-        console.time("SearchTime");
-        gotoPref("paneSearchResults");
-
-        srHeader.setAttribute("hidden", "false");
-
-        searchResultsCategory.setAttribute("hidden", "false");
-        let notFound = true;
-        
-        // 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++) {
-            rootPreferencesChildren[i].setAttribute("hidden", "false");
-        }
-
-        for (let i = 0; i < rootPreferences.childElementCount; i++) {
-            if (nodeRecursion(rootPreferencesChildren[i], element.value.trim(), findSelection)) {
-                // foundCounter++; //  < Remove this
-                rootPreferencesChildren[i].setAttribute("hidden", "false");
-                notFound = false;
-            } else {
-                rootPreferencesChildren[i].setAttribute("hidden", "true");
-            }
-        }
-        srHeader.setAttribute("hidden", "false");
-        if(notFound){
-            
-            noResults.setAttribute("hidden", "false");
-            //noResults.childNodes[0].innerHTML("Sorry! No results are found for '" + element.value.trim() + "'");
-            //console.log('no results found');
-        }
-        // console.log("found and highlighted: ", foundCounter) //  < Remove later
-        console.timeEnd("SearchTime");
-    } else {
-        //console.log("Empty string not searching, clearing shit instead");
-        searchResultsCategory.setAttribute("hidden", "true");
-
-        srHeader.setAttribute("hidden", "true");
-        noResults.setAttribute("hidden", "true");
-        
-        gotoPref("paneGeneral");
-    }
-
-}
+// Clearning the search filed when before enable or disable
 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
@@ -118,219 +54,339 @@ function stringMatchesFilters(str, filte
 
     let searchStr = str.toLowerCase();
     let filterStrings = filter.toLowerCase().split(/\s+/);
     return !filterStrings.some(function(f) {
         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(); //  List of words to search from
-
-        //  Searching in the Text Nodes
-        if (nodeObject) {
-            leafTextNodes = textNodesUnder(nodeObject);
-        }
-        if (nodeObject.boxObject) {
-            otherTextNodes = textNodesUnder(nodeObject.boxObject);
-        }
-        leafTextNodes.forEach(function(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
-            });
+/**
+ * When search bar is clicked initializes all the JS bindings
+ * 
+ * @returns null
+ */
+function searchOnClick(){
+    //  Initializing all the JS for all the tabs
+    if (firstTimeSearch) {
+        firstTimeSearch = false;
+        gCategoryInits.forEach( function(eachTab) {
+            if (!eachTab.inited) {
+                eachTab.init();
+            }
         });
-        */
-        //  Searching in Text boxes, radio buttons
-        let nodeSizes = [];
-        let allNodeText = "";
-        let runningSize = 0;
-
-        otherTextNodes.forEach(function(node) {
-            runningSize += node.textContent.length;
-            allNodeText += node.textContent;
-            nodeSizes.push(runningSize);
-        });
-
-        listOfWords.forEach(function(word) {
-            let boolAns = multiSearch(otherTextNodes, nodeSizes, allNodeText, word, findSelection);
-            // searchWord(node,node.textContent,word,findSelection)
-            foundIn = foundIn || boolAns;
-        });
-
-        
-        //  Searching in the buttons
-        listOfWords.forEach(function(word) {
-            let boolAns = getLabelAttribute(nodeObject, word);
-            foundIn = foundIn || boolAns;
-            // if (getLabelAttribute(nodeObject,word)){
-            //     console.log(nodeObject)
-            //     foundIn = true
-            // }
-        });
-
-        //  Label tag that does not have textCont but text is in Value attr
-        listOfWords.forEach(function(word) {
-            let boolAns = getValueAttribute(nodeObject, word, findSelection);
-            foundIn = foundIn || boolAns;
-            // if (getLabelAttribute(nodeObject,word)){
-            //     console.log(nodeObject)
-            //     foundIn = true
-            // }
-        });
-        
     }
-    for (let i = 0; i < nodeObject.childNodes.length; i++) {
-        let boolAns = nodeRecursion(nodeObject.childNodes[i], searchPhrase, findSelection);
-        foundIn = foundIn || boolAns;
-        // foundIn = foundIn || nodeRecursion(nodeObject.childNodes[i], searchPhrase, findSelection)
-    }
-    return foundIn;
 }
 
-// http:// stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page
+/**
+ * Finding text nodes from the nodes
+ * Source - http:// stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page
+ * 
+ * @param Node nodeObject
+ *        Html element
+ * @returns array of text nodes
+ */
 function textNodesUnder(node) {
     let all = [];
     for (node = node.firstChild; node; node = node.nextSibling) {
         if (node.nodeType === node.TEXT_NODE) all.push(node);
         else all = all.concat(textNodesUnder(node));
     }
     return all;
 }
 
+/**
+ * Finding words in the text node
+ *
+ * @param Node textNode
+ *        Html element
+ * @param String textSearch
+ *        textNodes's text content
+ * @param String word
+ *        work to search for
+ * @param Object findSelection
+ *        Selection tool
+ * @returns boolean
+ */
 function searchWord(textNode, textSearch, word, findSelection) {
     let regExp = new RegExp(word, "gi");
     let result, indices = [];
 
     while ((result = regExp.exec(textSearch))) {
         indices.push(result.index);
     }
 
-    if (indices.length > 0 ){
-        console.log("Search Word",textNode, textSearch)
-    }
-
     //  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;
 }
 
+/**
+ * Finding words in the text nodes
+ * Cases where the word is split up due to access keys
+ * @param Array textNodes
+ *        List of Html elements
+ * @param Array nodeSizes
+ *        Running size of text nodes
+ * @param String textSearch
+ *        Concatination of textNodes's text content
+ * @param String word
+ *        Word to search for
+ * @param Object findSelection
+ *        Selection tool
+ * @returns boolean
+ */
 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);
     }
 
-    /*
-    if (indices.length > 0){
-        console.log("Multi Searcb",textNodes,textSearch)
-        //console.log("Other words")
-        //console.log(textNodes)
-        //console.log(nodeSizes)
-        //console.log(textSearch)
-        //console.log("End other words")
-    } 
-    */
+    // Looping through each spot the word is found in the concatinated string
     indices.forEach(function(startValue, startIndex) {
         let endValue = startValue + word.length;
         let startNode = null;
         let endNode = null;
         let nodeStartIndex = null;
 
+        // Determining the start and end node to highlight from
         nodeSizes.forEach(function(lengthNodes, index) {
+            // Determining the start node
             if (!startNode && lengthNodes >= startValue) {
                 startNode = textNodes[index];
                 nodeStartIndex = index;
                 if (index > 0 ) {
                     startValue -= nodeSizes[index - 1];
                 }
             }
-
+            // Determining the end node
             if (!endNode && lengthNodes >= endValue) {
                 endNode = textNodes[index];
                 if (index != nodeStartIndex || index > 0 ) {
                     endValue -= nodeSizes[index - 1];
                 }
             }
         });
+        // Selection the section to higlight
         let range = document.createRange();
         range.setStart(startNode, startValue);
         range.setEnd(endNode, endValue);
         findSelection.addRange(range);
     });
 
     return indices.length > 0;
-
 }
 
+/**
+ * Finding if search phrase is in TextContent Attribute
+ * 
+ * @param Node nodeObject
+ *        Html element
+ * @param String searchPhrase
+ *        word or words to search for
+ * @returns boolean
+ */
 function getTextContentAttribute(nodeObject, searchPhrase) {
     if (typeof nodeObject.textContent == "string" && nodeObject.textContent != ""
     && stringMatchesFilters(nodeObject.textContent, searchPhrase)) {
         return true;
     }
     return false;
 }
 
+/**
+ * Finding if search phrase is in Label Attribute
+ * 
+ * @param Node nodeObject
+ *        Html element
+ * @param String searchPhrase
+ *        word or words to search for
+ * @returns boolean
+ */
 function getLabelAttribute(nodeObject, searchPhrase) {
     if (typeof nodeObject.label == "string" && nodeObject.label != ""
     && stringMatchesFilters(nodeObject.label, searchPhrase)) {
-        console.log("Found it in label", nodeObject);
-        // nodeObject.classList.add("search-bubble");
-
-        // var creatSpanTag = document.createElement("span");
-        // creatSpanTag.classList.add("search-bubble-text");
-        // creatSpanTag.innerHTML = searchPhrase;
-       //  nodeObject.appendChild(creatSpanTag);
         return true;
     }
     return false;
 }
 
+/**
+ * Finding if search phrase is in Value Attribute
+ * 
+ * @param Node nodeObject
+ *        Html element
+ * @param String searchPhrase
+ *        word or words to search for
+ * @returns boolean
+ */
 function getValueAttribute(nodeObject, searchPhrase, findSelection) {
     if (typeof nodeObject.value == "string" && nodeObject.value != ""
     && stringMatchesFilters(nodeObject.value, searchPhrase)) {
-        console.log("Found it in value", nodeObject);
-        //nodeObject.textContent = nodeObject.value
-        //nodeObject.innerHTML = nodeObject.value
-        // nodeObject.classList.add("search-bubble");
-
-        //var createTextNode = document.createTextNode(nodeObject.value);
-        //nodeObject.appendChild(createTextNode);
-        //console.log(nodeObject, nodeObject.childNodes[0])
-        //searchWord(nodeObject.childNodes[0], nodeObject.value, word, findSelection)
         return true;
     }
     return false;
 }
 
-
+/**
+ * Getter for Selection Controller
+ * 
+ * @returns controller
+ */
 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;
 }
+
+/**
+ * SHows or hides content according to search input
+ *
+ * @param String event
+ *        to search for filter words in
+ * @returns boolean
+ */
+function searchFunc(event) {
+    let words = event.target.value.trim();
+
+    // Controller
+    let controller = getSelectionController();
+    let findSelection = controller.getSelection(Ci.nsISelectionController.SELECTION_FIND);
+    findSelection.removeAllRanges();
+
+    // Init for Search Results tab
+    let srHeader = document.getElementById("header-search-results");
+    let noResults = document.getElementById("noResultsFound");
+
+    if (words) {
+        console.time("SearchTime");
+
+        // Showing the Search Results Tag
+        gotoPref("paneSearchResults");
+        srHeader.setAttribute("hidden", "false");
+        searchResultsCategory.setAttribute("hidden", "false");
+
+        let notSearchFound = true;
+        
+        // Building the range for highlighted areas
+        let rootPreferences = document.getElementById("mainPrefPane")
+        let rootPreferencesChildren = rootPreferences.childNodes;
+
+        // Showing all the children to bind JS, Access Keys, etc
+        for (let i = 0; i < rootPreferences.childElementCount; i++) {
+            rootPreferencesChildren[i].setAttribute("hidden", "false");
+        }
+
+        // Showing or Hiding specific section depending on if words are found
+        for (let i = 0; i < rootPreferences.childElementCount; i++) {
+            if (nodeRecursion(rootPreferencesChildren[i], words, findSelection)) {
+                rootPreferencesChildren[i].setAttribute("hidden", "false");
+                notSearchFound = false;
+            } else {
+                rootPreferencesChildren[i].setAttribute("hidden", "true");
+            }
+        }
+        srHeader.setAttribute("hidden", "false");
+
+        if(notSearchFound){
+            noResults.setAttribute("hidden", "false");
+            for (let i = 0; i < noResults.childNodes.length; i++) {
+                noResults.childNodes[i].setAttribute("hidden", "false");
+            }
+        }
+        console.timeEnd("SearchTime");
+    } else {
+        searchResultsCategory.setAttribute("hidden", "true");
+
+        srHeader.setAttribute("hidden", "true");
+        noResults.setAttribute("hidden", "true");
+        // Hiding the child nodes so they will not be searched
+        for (let i = 0; i < noResults.childNodes.length; i++) {
+            noResults.childNodes[i].setAttribute("hidden", "true");
+        }
+        
+        // Going back to General when cleared
+        gotoPref("paneGeneral");
+    }
+
+}
+
+/**
+ * Finding leaf nodes and checking their content for words to search
+ *
+ * @param Node nodeObject
+ *        Html element
+ * @param String searchPhrase
+ *        The list of words to search for
+ * @param Object findSelection
+ *        Selection tool
+ * @returns boolean
+ */
+function nodeRecursion(nodeObject, searchPhrase, findSelection) {
+    let foundIn = false;
+    if (nodeObject.childElementCount == 0) {
+        let leafTextNodes, otherTextNodes = [];
+        //  List of words to search on
+        let listOfWords = searchPhrase.trim().split(' ').filter(function(i){return i});
+
+        if (nodeObject) {
+            leafTextNodes = textNodesUnder(nodeObject);
+        }
+        if (nodeObject.boxObject) {
+            otherTextNodes = textNodesUnder(nodeObject.boxObject);
+        }
+
+         //  Searching in the Text Nodes
+        leafTextNodes.forEach(function(node) {
+            listOfWords.forEach(function(word) {
+                let boolAns = searchWord(node, node.textContent, word, findSelection);
+                foundIn = foundIn || boolAns;
+            });
+        });
+
+        //  Collecting data from boxObject
+        let nodeSizes = [];
+        let allNodeText = "";
+        let runningSize = 0;
+
+        otherTextNodes.forEach(function(node) {
+            runningSize += node.textContent.length;
+            allNodeText += node.textContent;
+            nodeSizes.push(runningSize);
+        });
+
+        listOfWords.forEach(function(word) {
+            // Linux machines where access key is presented
+            let splitAns = multiSearch(otherTextNodes, nodeSizes, allNodeText, word, findSelection);
+            
+            //  Searching in the buttons
+            let buttonAns = getLabelAttribute(nodeObject, word);
+
+            //  Label tag that does not have textContent but text is in Value attr
+            let labelAns = getValueAttribute(nodeObject, word, findSelection);
+            
+            foundIn = foundIn || splitAns || buttonAns || labelAns;
+        });        
+    }
+
+    for (let i = 0; i < nodeObject.childNodes.length; i++) {
+        // Search only if child node is not hidden
+        if (!nodeObject.childNodes[i].hidden) {
+            let boolAns = nodeRecursion(nodeObject.childNodes[i], searchPhrase, findSelection);
+            foundIn = foundIn || boolAns;
+        }
+    }
+    return foundIn;
+}
\ No newline at end of file
--- a/browser/components/preferences/in-content/searchResults.xul
+++ b/browser/components/preferences/in-content/searchResults.xul
@@ -1,12 +1,12 @@
 <hbox id="header-search-results"
       class="header"
       hidden="true"
       data-category="paneSearchResults">
   <label class="header-name" flex="1">&paneSearchResults.title;</label>
   <html:a class="help-button" target="_blank" aria-label="&helpButton.label;"></html:a>
 </hbox>
 
-<groupbox id="noResultsFound" align="start" data-category="paneSearchResults">
-  <label >Sorry! No results are found for the searched word</label>
-  <label> Need help? Visit <a href="https://support.mozilla.org/t5/Mozilla-Support-English/ct-p/Mozilla-EN" class="text-link">Firefox Support</a></label>
+<groupbox id="noResultsFound" align="start" data-category="paneSearchResults" hidden="true">
+  <label hidden="true">Sorry! No results are found for the searched word</label>
+  <label hidden="true"> Need help? Visit <a href="https://support.mozilla.org/t5/Mozilla-Support-English/ct-p/Mozilla-EN" class="text-link">Firefox Support</a></label>
 </groupbox>