Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/filterValidation.js

Issue 5279235799252992: Issue 491 - Validate custom filters (Closed)
Patch Set: Improved test messages Created Nov. 21, 2014, 8:07 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « block.js ('k') | metadata.common » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/filterValidation.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/filterValidation.js
@@ -0,0 +1,84 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2014 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+let {Filter, InvalidFilter, ElemHideBase} = require("filterClasses");
+
+function parseFilter(text, ignore_headers)
+{
+ text = Filter.normalize(text);
+ if (!text)
+ return null;
+
+ if (text[0] == "[")
+ {
+ if (ignore_headers)
+ return null;
+
+ throw ext.i18n.getMessage("unexpected_filter_list_header");
+ }
+
+ let filter = Filter.fromText(text);
+
+ if (filter instanceof InvalidFilter)
+ throw filter.reason;
+
+ if (filter instanceof ElemHideBase)
+ {
+ let style = document.createElement("style");
+ document.documentElement.appendChild(style);
+ let sheet = style.sheet;
+ document.documentElement.removeChild(style);
+
+ try
+ {
+ document.querySelector(filter.selector);
+ sheet.insertRule(filter.selector + "{}", 0);
+ }
+ catch (error)
+ {
+ throw ext.i18n.getMessage("invalid_css_selector", "'" + filter.selector + "'");
+ }
+ }
+
+ return filter;
+}
+exports.parseFilter = parseFilter;
+
+function parseFilters(text, ignore_headers)
+{
+ let lines = text.split("\n");
+ let filters = [];
+
+ for (let i = 0; i < lines.length; i++)
+ {
+ let filter;
+ try
+ {
+ filter = parseFilter(lines[i], ignore_headers);
+ }
+ catch (error)
+ {
+ throw ext.i18n.getMessage("line", (i + 1).toString()) + ": " + error;
+ }
+
+ if (filter)
+ filters.push(filter);
+ }
+
+ return filters;
+}
+exports.parseFilters = parseFilters;
« no previous file with comments | « block.js ('k') | metadata.common » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld