Bug 1335905 - Fixed special character inputs and added test cases draft
authorFirefox <manotejmeka@gmail.com>
Tue, 21 Mar 2017 18:21:44 -0400
changeset 502517 583c5cb21b9ca35a1a91d3b4c18a7cc43d6d719e
parent 501737 33ef225bf67c16ee88e6f4e9233caf92a9f93b18
child 504031 bc8a02bd5720ef2f5453016ab9dc5d9de2657c38
child 554463 33b00c6356c490ffd64bfe0611fce3e62e554852
push id50308
push usermanotejmeka@gmail.com
push dateTue, 21 Mar 2017 22:25:21 +0000
bugs1335905
milestone55.0a1
Bug 1335905 - Fixed special character inputs and added test cases MozReview-Commit-ID: BwiUk5p6ehA
browser/components/preferences/in-content/findInPage.js
browser/components/preferences/in-content/tests/browser_search_within_preferences.js
--- a/browser/components/preferences/in-content/findInPage.js
+++ b/browser/components/preferences/in-content/findInPage.js
@@ -113,17 +113,17 @@ var gSearchResultsPane = {
    *    nodeSizes = "This is an example"
    *    This is used when executing the regular expression
    * @param String searchPhrase
    *    word or words to search for
    * @returns boolean
    *      Returns true when atleast one instance of search phrase is found, otherwise false
    */
   highlightMatches(textNodes, nodeSizes, textSearch, searchPhrase) {
-    let regExp = new RegExp(searchPhrase, "gi");
+    let regExp = new RegExp(searchPhrase.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"), "gi");
     let result, indices = [];
 
     while ((result = regExp.exec(textSearch))) {
       indices.push(result.index);
     }
 
     // Looping through each spot the searchPhrase is found in the concatenated string
     for (let startValue of indices) {
--- a/browser/components/preferences/in-content/tests/browser_search_within_preferences.js
+++ b/browser/components/preferences/in-content/tests/browser_search_within_preferences.js
@@ -157,8 +157,62 @@ add_task(function*() {
   searchInput.value = "";
   searchInput.doCommand()
 
   // Checks if back to normal
   is_element_visible(generalPane, "Should be in generalPane");
 
   yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
 });
+
+/**
+ * Test for regular expressions
+ */
+add_task(function*() {
+  yield openPreferencesViaOpenPreferencesAPI("general", undefined, {leaveOpen: true});
+
+  // The first 2 Ids [0,1] contain a "+"
+  // The next 2 Ids [2,3] contain a "-"
+  // The next 2 Ids [4,5] contain neither "+" or "-"
+  // The last Id [6] contain a sorry message that mean no results were found
+  let testIdArray = ["ctrlTabRecentlyUsedOrder", "linkTargeting", "e10sAutoStart", "warnAddonInstall",
+  "playDRMContent", "savePasswords", "sorry-message"];
+
+  // Performs search for the "+" test case
+  let searchInput = gBrowser.contentDocument.getElementById("searchInput");
+  searchInput.doCommand()
+  searchInput.value = "+";
+  searchInput.doCommand()
+
+  for (let elementId of testIdArray) {
+    if (elementId == "ctrlTabRecentlyUsedOrder" || elementId == "linkTargeting") {
+      is_element_visible(gBrowser.contentDocument.getElementById(elementId), elementId + "Should be visible");
+    } else {
+        is_element_hidden(gBrowser.contentDocument.getElementById(elementId), elementId + "Should be hidden");
+    }
+  }
+
+  // Performs search for the "-" test case
+  searchInput.value = "-";
+  searchInput.doCommand()
+
+  for (let elementId of testIdArray) {
+    if (elementId == "e10sAutoStart" || elementId == "warnAddonInstall") {
+      is_element_visible(gBrowser.contentDocument.getElementById(elementId), elementId + "Should be visible");
+    } else {
+        is_element_hidden(gBrowser.contentDocument.getElementById(elementId), elementId + "Should be hidden");
+    }
+  }
+
+  // Performs search for the "?" test case, should not find "?" anywhere
+  searchInput.value = "?";
+  searchInput.doCommand()
+
+  for (let elementId of testIdArray) {
+    if (elementId == "sorry-message") {
+      is_element_visible(gBrowser.contentDocument.getElementById(elementId), elementId + "Should be visible");
+    } else {
+        is_element_hidden(gBrowser.contentDocument.getElementById(elementId), elementId + "Should be hidden");
+    }
+  }
+
+  yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
+});