1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-2017 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/>.
*/
"use strict";
/**
* @fileOverview Element hiding implementation.
*/
const {ElemHideException} = require("filterClasses");
const {FilterNotifier} = require("filterNotifier");
/**
* Lookup table, filters by their associated key
* @type {Object}
*/
let filterByKey = [];
/**
* Lookup table, keys of the filters by filter text
* @type {Object}
*/
let keyByFilter = Object.create(null);
/**
* Nested lookup table, filter (or false if inactive) by filter key by domain.
* (Only contains filters that aren't unconditionally matched for all domains.)
* @type {Object}
*/
let filtersByDomain = Object.create(null);
/**
* Lookup table, filter key by selector. (Only used for selectors that are
* unconditionally matched for all domains.)
*/
let filterKeyBySelector = Object.create(null);
/**
* This array caches the keys of filterKeyBySelector table (selectors which
* unconditionally apply on all domains). It will be null if the cache needs to
* be rebuilt.
*/
let unconditionalSelectors = null;
/**
* This array caches the values of filterKeyBySelector table (filterIds for
* selectors which unconditionally apply on all domains). It will be null if the
* cache needs to be rebuilt.
*/
let unconditionalFilterKeys = null;
/**
* Object to be used instead when a filter has a blank domains property.
*/
let defaultDomains = Object.create(null);
defaultDomains[""] = true;
/**
* Lookup table, keys are known element hiding exceptions
* @type {Object}
*/
let knownExceptions = Object.create(null);
/**
* Lookup table, lists of element hiding exceptions by selector
* @type {Object}
*/
let exceptions = Object.create(null);
/**
* Container for element hiding filters
* @class
*/
let ElemHide = exports.ElemHide = {
/**
* Removes all known filters
*/
clear()
{
filterByKey = [];
keyByFilter = Object.create(null);
filtersByDomain = Object.create(null);
filterKeyBySelector = Object.create(null);
unconditionalSelectors = unconditionalFilterKeys = null;
knownExceptions = Object.create(null);
exceptions = Object.create(null);
FilterNotifier.emit("elemhideupdate");
},
_addToFiltersByDomain(key, filter)
{
let domains = filter.domains || defaultDomains;
for (let domain in domains)
{
let filters = filtersByDomain[domain];
if (!filters)
filters = filtersByDomain[domain] = Object.create(null);
if (domains[domain])
filters[key] = filter;
else
filters[key] = false;
}
},
/**
* Add a new element hiding filter
* @param {ElemHideFilter} filter
*/
add(filter)
{
if (filter instanceof ElemHideException)
{
if (filter.text in knownExceptions)
return;
let {selector} = filter;
if (!(selector in exceptions))
exceptions[selector] = [];
exceptions[selector].push(filter);
// If this is the first exception for a previously unconditionally
// applied element hiding selector we need to take care to update the
// lookups.
let filterKey = filterKeyBySelector[selector];
if (typeof filterKey != "undefined")
{
this._addToFiltersByDomain(filterKey, filterByKey[filterKey]);
delete filterKeyBySelector[selector];
unconditionalSelectors = unconditionalFilterKeys = null;
}
knownExceptions[filter.text] = true;
}
else
{
if (filter.text in keyByFilter)
return;
let key = filterByKey.push(filter) - 1;
keyByFilter[filter.text] = key;
if (!(filter.domains || filter.selector in exceptions))
{
// The new filter's selector is unconditionally applied to all domains
filterKeyBySelector[filter.selector] = key;
unconditionalSelectors = unconditionalFilterKeys = null;
}
else
{
// The new filter's selector only applies to some domains
this._addToFiltersByDomain(key, filter);
}
}
FilterNotifier.emit("elemhideupdate");
},
_removeFilterKey(key, filter)
{
if (filterKeyBySelector[filter.selector] == key)
{
delete filterKeyBySelector[filter.selector];
unconditionalSelectors = unconditionalFilterKeys = null;
return;
}
// We haven't found this filter in unconditional filters, look in
// filtersByDomain.
let domains = filter.domains || defaultDomains;
for (let domain in domains)
{
let filters = filtersByDomain[domain];
if (filters)
delete filters[key];
}
},
/**
* Removes an element hiding filter
* @param {ElemHideFilter} filter
*/
remove(filter)
{
if (filter instanceof ElemHideException)
{
if (!(filter.text in knownExceptions))
return;
let list = exceptions[filter.selector];
let index = list.indexOf(filter);
if (index >= 0)
list.splice(index, 1);
delete knownExceptions[filter.text];
}
else
{
if (!(filter.text in keyByFilter))
return;
let key = keyByFilter[filter.text];
delete filterByKey[key];
delete keyByFilter[filter.text];
this._removeFilterKey(key, filter);
}
FilterNotifier.emit("elemhideupdate");
},
/**
* Checks whether an exception rule is registered for a filter on a particular
* domain.
* @param {Filter} filter
* @param {string} docDomain
* @return {ElemHideException}
*/
getException(filter, docDomain)
{
if (!(filter.selector in exceptions))
return null;
let list = exceptions[filter.selector];
for (let i = list.length - 1; i >= 0; i--)
{
if (list[i].isActiveOnDomain(docDomain))
return list[i];
}
return null;
},
/**
* Retrieves an element hiding filter by the corresponding protocol key
* @param {number} key
* @return {Filter}
*/
getFilterByKey(key)
{
return (key in filterByKey ? filterByKey[key] : null);
},
/**
* Returns a list of all selectors as a nested map. On first level, the keys
* are all values of `ElemHideBase.selectorDomain` (domains on which these
* selectors should apply, ignoring exceptions). The values are maps again,
* with the keys being selectors and values the corresponding filter keys.
* @returns {Map.<String,Map<String,String>>}
*/
getSelectors()
{
let domains = new Map();
for (let key in filterByKey)
{
let filter = filterByKey[key];
if (!filter.selector)
continue;
let domain = filter.selectorDomain || "";
if (!domains.has(domain))
domains.set(domain, new Map());
domains.get(domain).set(filter.selector, key);
}
return domains;
},
/**
* Returns a list of selectors that apply on each website unconditionally.
* @returns {string[]}
*/
getUnconditionalSelectors()
{
if (!unconditionalSelectors)
unconditionalSelectors = Object.keys(filterKeyBySelector);
return unconditionalSelectors.slice();
},
/**
* Returns a list of filter keys for selectors which apply to all websites
* without exception.
* @returns {number[]}
*/
getUnconditionalFilterKeys()
{
if (!unconditionalFilterKeys)
{
let selectors = this.getUnconditionalSelectors();
unconditionalFilterKeys = [];
for (let selector of selectors)
unconditionalFilterKeys.push(filterKeyBySelector[selector]);
}
return unconditionalFilterKeys.slice();
},
/**
* Constant used by getSelectorsForDomain to return all selectors applying to
* a particular hostname.
*/
ALL_MATCHING: 0,
/**
* Constant used by getSelectorsForDomain to exclude selectors which apply to
* all websites without exception.
*/
NO_UNCONDITIONAL: 1,
/**
* Constant used by getSelectorsForDomain to return only selectors for filters
* which specifically match the given host name.
*/
SPECIFIC_ONLY: 2,
/**
* Determines from the current filter list which selectors should be applied
* on a particular host name. Optionally returns the corresponding filter
* keys.
* @param {string} domain
* @param {number} [criteria]
* One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or
* ElemHide.SPECIFIC_ONLY.
* @param {boolean} [provideFilterKeys]
* If true, the function will return a list of corresponding filter keys in
* addition to selectors.
* @returns {string[]|Array.<string[]>}
* List of selectors or an array with two elements (list of selectors and
* list of corresponding keys) if provideFilterKeys is true.
*/
getSelectorsForDomain(domain, criteria, provideFilterKeys)
{
let filterKeys = [];
let selectors = [];
if (typeof criteria == "undefined")
criteria = ElemHide.ALL_MATCHING;
if (criteria < ElemHide.NO_UNCONDITIONAL)
{
selectors = this.getUnconditionalSelectors();
if (provideFilterKeys)
filterKeys = this.getUnconditionalFilterKeys();
}
let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
let seenFilters = Object.create(null);
let currentDomain = domain ? domain.toUpperCase() : "";
while (true)
{
if (specificOnly && currentDomain == "")
break;
let filters = filtersByDomain[currentDomain];
if (filters)
{
for (let filterKey in filters)
{
if (filterKey in seenFilters)
continue;
seenFilters[filterKey] = true;
let filter = filters[filterKey];
if (filter && !this.getException(filter, domain))
{
selectors.push(filter.selector);
// It is faster to always push the key, even if not required.
filterKeys.push(filterKey);
}
}
}
if (currentDomain == "")
break;
let nextDot = currentDomain.indexOf(".");
currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
}
if (provideFilterKeys)
return [selectors, filterKeys];
return selectors;
}
};
|