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

Side by Side Diff: lib/elemHideExceptions.js

Issue 29783618: Issue 6665 - Split out element hiding exceptions into their own module (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created May 17, 2018, 3:46 a.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 | « lib/elemHideEmulation.js ('k') | lib/filterListener.js » ('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 <https://adblockplus.org/>,
3 * Copyright (C) 2006-present 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 "use strict";
19
20 /**
21 * @fileOverview Element hiding exceptions implementation.
22 */
23
24 const {EventEmitter} = require("./events");
25 const {FilterNotifier} = require("./filterNotifier");
26
27 /**
28 * Lookup table, lists of element hiding exceptions by selector
29 * @type {Map.<string,ElemHideException[]>}
30 */
31 let exceptions = new Map();
32
33 /**
34 * Set containing known element exceptions
35 * @type {Set.<ElemHideException>}
36 */
37 let knownExceptions = new Set();
38
39 /**
40 * Container for element hiding exceptions
41 * @class
42 */
43 exports.ElemHideExceptions = Object.assign(Object.create(new EventEmitter()), {
44 /**
45 * Removes all known exceptions
46 */
47 clear()
48 {
49 exceptions.clear();
50 knownExceptions.clear();
51
52 FilterNotifier.emit("elemhideupdate");
53 },
54
55 /**
56 * Add a new element hiding exception
57 * @param {ElemHideException} exception
58 */
59 add(exception)
60 {
61 if (knownExceptions.has(exception))
62 return;
63
64 let {selector} = exception;
65 let list = exceptions.get(selector);
66 if (list)
67 list.push(exception);
68 else
69 exceptions.set(selector, [exception]);
70
71 knownExceptions.add(exception);
72
73 this.emit("added", exception);
74 },
75
76 /**
77 * Removes an element hiding exception
78 * @param {ElemHideException} exception
79 */
80 remove(exception)
81 {
82 if (!knownExceptions.has(exception))
83 return;
84
85 let list = exceptions.get(exception.selector);
86 let index = list.indexOf(exception);
87 if (index >= 0)
88 list.splice(index, 1);
89
90 knownExceptions.delete(exception);
91
92 this.emit("removed", exception);
93 },
94
95 /**
96 * Checks whether any exception rules are registered for a selector
97 * @param {string} selector
98 * @returns {boolean}
99 */
100 hasExceptions(selector)
101 {
102 return exceptions.has(selector);
103 },
104
105 /**
106 * Checks whether an exception rule is registered for a filter on a particular
107 * domain.
108 * @param {ElemHideFilter|ElemHideEmulationFilter} filter
109 * @param {?string} docDomain
110 * @return {?ElemHideException}
111 */
112 getException(filter, docDomain)
113 {
114 let list = exceptions.get(filter.selector);
115 if (!list)
116 return null;
117
118 for (let i = list.length - 1; i >= 0; i--)
119 {
120 if (list[i].isActiveOnDomain(docDomain))
121 return list[i];
122 }
123
124 return null;
125 }
126 });
OLDNEW
« no previous file with comments | « lib/elemHideEmulation.js ('k') | lib/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld