From 5623fef0581fd460380c48e8a5f453f7aa810886 Mon Sep 17 00:00:00 2001 From: Jason Barnabe Date: Fri, 2 Nov 2012 22:25:04 -0500 Subject: [PATCH] issue 57 Bad regexp prevents some other styles from applying --- _locales/en/messages.json | 6 +++++- background.js | 8 +++++++- edit.js | 24 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 64e407be..90890e7e 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -157,6 +157,10 @@ "message": "Remove section", "description": "Label for the button to remove a section" }, + "styleBadRegexp": { + "message": "Regexp is invalid.", + "description": "Validation message for a bad regexp in a style" + }, "styleCancelEditLabel": { "message": "Back to manage", "description": "Label for cancel button for style editing" @@ -223,4 +227,4 @@ "message": "Update completed.", "description": "Text that displays when an update completed" } -} \ No newline at end of file +} diff --git a/background.js b/background.js index 1c257e73..84ac06bd 100644 --- a/background.js +++ b/background.js @@ -159,7 +159,13 @@ function sectionAppliesToUrl(section, url) { if (regexp[regexp.length - 1] != "$") { regexp += "$"; } - return (new RegExp(regexp)).test(url); + try { + var re = new RegExp(regexp); + } catch (ex) { + console.log(section.id + "'s regexp '" + regexp + "' is not valid"); + return false; + } + return (re).test(url); })) { console.log(section.id + " applies to " + url + " due to regexp rules"); return true; diff --git a/edit.js b/edit.js index a1fc8857..44f4b032 100644 --- a/edit.js +++ b/edit.js @@ -149,6 +149,30 @@ function validate() { if (name == "") { return t("styleMissingName"); } + // validate the regexps + if (Array.prototype.some.call(document.querySelectorAll(".applies-to-list"), function(list) { + return Array.prototype.some.call(list.childNodes, function(li) { + if (li.className == appliesToEverythingTemplate.className) { + return false; + } + var valueElement = li.querySelector("[name=applies-value]"); + var a = li.querySelector("[name=applies-type]").value; + var b = valueElement.value; + if (a && b) { + if (a == "regexp") { + try { + new RegExp(b); + } catch (ex) { + valueElement.focus(); + return true; + } + } + } + return false; + }); + })) { + return t("styleBadRegexp"); + } return null; }