Bug 1335905 - Fixed special character inputs and added test cases
MozReview-Commit-ID: BwiUk5p6ehA
--- 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);
+});