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

Side by Side Diff: lib/snippets.js

Issue 29856564: Issue 6862 - Make Snippets module emit events (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Avoid duplicate notifications Created Aug. 15, 2018, 2:52 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Snippets implementation. 21 * @fileOverview Snippets implementation.
22 */ 22 */
23 23
24 const {EventEmitter} = require("./events");
24 const {Filter} = require("./filterClasses"); 25 const {Filter} = require("./filterClasses");
25 26
26 const singleCharacterEscapes = new Map([ 27 const singleCharacterEscapes = new Map([
27 ["n", "\n"], ["r", "\r"], ["t", "\t"] 28 ["n", "\n"], ["r", "\r"], ["t", "\t"]
28 ]); 29 ]);
29 30
30 let filters = new Set(); 31 let filters = new Set();
31 32
32 /** 33 /**
33 * Container for snippet filters 34 * Container for snippet filters
34 * @class 35 * @class
35 */ 36 */
36 let Snippets = { 37 let Snippets = Object.assign(new EventEmitter(), {
37 /** 38 /**
38 * Removes all known filters 39 * Removes all known filters
39 */ 40 */
40 clear() 41 clear()
41 { 42 {
43 if (filters.size == 0)
44 return;
45
42 filters.clear(); 46 filters.clear();
47
48 this.emit("snippets.filtersCleared");
43 }, 49 },
44 50
45 /** 51 /**
46 * Add a new snippet filter 52 * Add a new snippet filter
47 * @param {SnippetFilter} filter 53 * @param {SnippetFilter} filter
48 */ 54 */
49 add(filter) 55 add(filter)
50 { 56 {
57 let {size} = filters;
58
51 filters.add(filter.text); 59 filters.add(filter.text);
60
61 if (size != filters.size)
62 this.emit("snippets.filterAdded", filter);
52 }, 63 },
53 64
54 /** 65 /**
55 * Removes a snippet filter 66 * Removes a snippet filter
56 * @param {SnippetFilter} filter 67 * @param {SnippetFilter} filter
57 */ 68 */
58 remove(filter) 69 remove(filter)
59 { 70 {
71 let {size} = filters;
72
60 filters.delete(filter.text); 73 filters.delete(filter.text);
74
75 if (size != filters.size)
76 this.emit("snippets.filterRemoved", filter);
61 }, 77 },
62 78
63 /** 79 /**
64 * Returns a list of all scripts active on a particular domain 80 * Returns a list of all scripts active on a particular domain
65 * @param {string} domain 81 * @param {string} domain
66 * @return {string[]} 82 * @return {string[]}
67 */ 83 */
68 getScriptsForDomain(domain) 84 getScriptsForDomain(domain)
69 { 85 {
70 let result = []; 86 let result = [];
71 for (let text of filters) 87 for (let text of filters)
72 { 88 {
73 let filter = Filter.fromText(text); 89 let filter = Filter.fromText(text);
74 if (filter.isActiveOnDomain(domain)) 90 if (filter.isActiveOnDomain(domain))
75 result.push(filter.script); 91 result.push(filter.script);
76 } 92 }
77 return result; 93 return result;
78 } 94 }
79 }; 95 });
80 96
81 exports.Snippets = Snippets; 97 exports.Snippets = Snippets;
82 98
83 /** 99 /**
84 * Parses a script and returns a list of all its commands and their arguments 100 * Parses a script and returns a list of all its commands and their arguments
85 * @param {string} script 101 * @param {string} script
86 * @return {Array.<string[]>} 102 * @return {Array.<string[]>}
87 */ 103 */
88 function parseScript(script) 104 function parseScript(script)
89 { 105 {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 let value = imports[name]; 204 let value = imports[name];
189 if (typeof value == "function") 205 if (typeof value == "function")
190 value(...args); 206 value(...args);
191 } 207 }
192 } 208 }
193 } 209 }
194 `; 210 `;
195 } 211 }
196 212
197 exports.compileScript = compileScript; 213 exports.compileScript = compileScript;
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld