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

Side by Side Diff: lib/filterValidation.js

Issue 5279235799252992: Issue 491 - Validate custom filters (Closed)
Patch Set: Created Nov. 19, 2014, 3:26 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « block.js ('k') | metadata.common » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 let {Filter, InvalidFilter, ElemHideBase} = require("filterClasses");
19
20 function parseFilter(text)
21 {
22 text = Filter.normalize(text);
23 if (!text)
24 return null;
25
26 let filter = Filter.fromText(text);
27
28 if (filter instanceof InvalidFilter)
29 throw filter.reason;
30
31 if (filter instanceof ElemHideBase)
32 {
33 let style = document.createElement("style");
34 document.documentElement.appendChild(style);
35 let sheet = style.sheet;
36 document.documentElement.removeChild(style);
37
38 try
39 {
40 document.querySelector(filter.selector);
41 sheet.insertRule(filter.selector + "{}", 0);
42 }
43 catch (error)
44 {
45 throw ext.i18n.getMessage("invalid_css_selector", "'" + filter.selector + "'");
46 }
47 }
48
49 return filter;
50 }
51 exports.parseFilter = parseFilter;
52
53 function parseFilters(text)
54 {
55 let lines = text.split("\n");
56 let filters = [];
57
58 for (let i = 0; i < lines.length; i++)
59 {
60 let filter;
61 try
62 {
63 filter = parseFilter(lines[i]);
64 }
65 catch (error)
66 {
67 throw ext.i18n.getMessage("line", (i + 1).toString()) + ": " + error;
68 }
69
70 if (filter)
71 filters.push(filter);
72 }
73
74 return filters;
75 }
76 exports.parseFilters = parseFilters;
OLDNEW
« no previous file with comments | « block.js ('k') | metadata.common » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld