OLD | NEW |
| (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 | |
19 // | |
20 // This file has been generated automatically from Adblock Plus source code | |
21 // | |
22 | |
23 (function (_patchFunc0) { | |
24 function Matcher() { | |
25 this.clear(); | |
26 } | |
27 Matcher.prototype = { | |
28 filterByKeyword: null, | |
29 keywordByFilter: null, | |
30 clear: function () { | |
31 this.filterByKeyword = { | |
32 __proto__: null | |
33 }; | |
34 this.keywordByFilter = { | |
35 __proto__: null | |
36 }; | |
37 } | |
38 , | |
39 add: function (filter) { | |
40 if (filter.text in this.keywordByFilter) | |
41 return ; | |
42 var keyword = this.findKeyword(filter); | |
43 switch (typeof this.filterByKeyword[keyword]) { | |
44 case "undefined": { | |
45 this.filterByKeyword[keyword] = filter.text; | |
46 break; | |
47 } | |
48 case "string": { | |
49 this.filterByKeyword[keyword] = [this.filterByKeyword[keyword], filter
.text]; | |
50 break; | |
51 } | |
52 default: { | |
53 this.filterByKeyword[keyword].push(filter.text); | |
54 break; | |
55 } | |
56 } | |
57 this.keywordByFilter[filter.text] = keyword; | |
58 } | |
59 , | |
60 remove: function (filter) { | |
61 if (!(filter.text in this.keywordByFilter)) | |
62 return ; | |
63 var keyword = this.keywordByFilter[filter.text]; | |
64 var list = this.filterByKeyword[keyword]; | |
65 if (typeof list == "string") | |
66 delete this.filterByKeyword[keyword]; | |
67 else { | |
68 var index = list.indexOf(filter.text); | |
69 if (index >= 0) { | |
70 list.splice(index, 1); | |
71 if (list.length == 1) | |
72 this.filterByKeyword[keyword] = list[0]; | |
73 } | |
74 } | |
75 delete this.keywordByFilter[filter.text]; | |
76 } | |
77 , | |
78 findKeyword: function (filter) { | |
79 var defaultResult = (filter.contentType & RegExpFilter.typeMap.DONOTTRACK
? "donottrack" : ""); | |
80 var text = filter.text; | |
81 if (Filter.regexpRegExp.test(text)) | |
82 return defaultResult; | |
83 if (Filter.optionsRegExp.test(text)) | |
84 text = RegExp.leftContext; | |
85 if (text.substr(0, 2) == "@@") | |
86 text = text.substr(2); | |
87 var candidates = text.toLowerCase().match(/[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-
z0-9%*])/g); | |
88 if (!candidates) | |
89 return defaultResult; | |
90 var hash = this.filterByKeyword; | |
91 var result = defaultResult; | |
92 var resultCount = 16777215; | |
93 var resultLength = 0; | |
94 for (var i = 0, l = candidates.length; | |
95 i < l; i++) { | |
96 var candidate = candidates[i].substr(1); | |
97 var count; | |
98 switch (typeof hash[candidate]) { | |
99 case "undefined": { | |
100 count = 0; | |
101 break; | |
102 } | |
103 case "string": { | |
104 count = 1; | |
105 break; | |
106 } | |
107 default: { | |
108 count = hash[candidate].length; | |
109 break; | |
110 } | |
111 } | |
112 if (count < resultCount || (count == resultCount && candidate.length > r
esultLength)) { | |
113 result = candidate; | |
114 resultCount = count; | |
115 resultLength = candidate.length; | |
116 } | |
117 } | |
118 return result; | |
119 } | |
120 , | |
121 hasFilter: function (filter) { | |
122 return (filter.text in this.keywordByFilter); | |
123 } | |
124 , | |
125 getKeywordForFilter: function (filter) { | |
126 if (filter.text in this.keywordByFilter) | |
127 return this.keywordByFilter[filter.text]; | |
128 else | |
129 return null; | |
130 } | |
131 , | |
132 _checkEntryMatch: function (keyword, location, contentType, docDomain, third
Party) { | |
133 var list = this.filterByKeyword[keyword]; | |
134 if (typeof list == "string") { | |
135 var filter = Filter.knownFilters[list]; | |
136 if (!filter) { | |
137 delete this.filterByKeyword[keyword]; | |
138 return null; | |
139 } | |
140 return (filter.matches(location, contentType, docDomain, thirdParty) ? f
ilter : null); | |
141 } | |
142 else { | |
143 for (var i = 0; | |
144 i < list.length; i++) { | |
145 var filter = Filter.knownFilters[list[i]]; | |
146 if (!filter) { | |
147 if (list.length == 1) { | |
148 delete this.filterByKeyword[keyword]; | |
149 return null; | |
150 } | |
151 else { | |
152 list.splice(i--, 1); | |
153 continue; | |
154 } | |
155 } | |
156 if (filter.matches(location, contentType, docDomain, thirdParty)) | |
157 return filter; | |
158 } | |
159 return null; | |
160 } | |
161 } | |
162 , | |
163 matchesAny: function (location, contentType, docDomain, thirdParty) { | |
164 var candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); | |
165 if (candidates === null) | |
166 candidates = []; | |
167 if (contentType == "DONOTTRACK") | |
168 candidates.unshift("donottrack"); | |
169 else | |
170 candidates.push(""); | |
171 for (var i = 0, l = candidates.length; | |
172 i < l; i++) { | |
173 var substr = candidates[i]; | |
174 if (substr in this.filterByKeyword) { | |
175 var result = this._checkEntryMatch(substr, location, contentType, docD
omain, thirdParty); | |
176 if (result) | |
177 return result; | |
178 } | |
179 } | |
180 return null; | |
181 } | |
182 , | |
183 toCache: function (cache) { | |
184 cache.filterByKeyword = this.filterByKeyword; | |
185 } | |
186 , | |
187 fromCache: function (cache) { | |
188 this.filterByKeyword = cache.filterByKeyword; | |
189 this.filterByKeyword.__proto__ = null; | |
190 delete this.keywordByFilter; | |
191 this.__defineGetter__("keywordByFilter", function () { | |
192 var result = { | |
193 __proto__: null | |
194 }; | |
195 for (var k in this.filterByKeyword) { | |
196 var list = this.filterByKeyword[k]; | |
197 if (typeof list == "string") | |
198 result[list] = k; | |
199 else | |
200 for (var i = 0, l = list.length; | |
201 i < l; i++) | |
202 result[list[i]] = k; | |
203 } | |
204 return this.keywordByFilter = result; | |
205 } | |
206 ); | |
207 this.__defineSetter__("keywordByFilter", function (value) { | |
208 delete this.keywordByFilter; | |
209 return this.keywordByFilter = value; | |
210 } | |
211 ); | |
212 } | |
213 | |
214 }; | |
215 function CombinedMatcher() { | |
216 this.blacklist = new Matcher(); | |
217 this.whitelist = new Matcher(); | |
218 this.keys = { | |
219 __proto__: null | |
220 }; | |
221 this.resultCache = { | |
222 __proto__: null | |
223 }; | |
224 } | |
225 CombinedMatcher.maxCacheEntries = 1000; | |
226 CombinedMatcher.prototype = { | |
227 blacklist: null, | |
228 whitelist: null, | |
229 keys: null, | |
230 resultCache: null, | |
231 cacheEntries: 0, | |
232 clear: function () { | |
233 this.blacklist.clear(); | |
234 this.whitelist.clear(); | |
235 this.keys = { | |
236 __proto__: null | |
237 }; | |
238 this.resultCache = { | |
239 __proto__: null | |
240 }; | |
241 this.cacheEntries = 0; | |
242 } | |
243 , | |
244 add: function (filter) { | |
245 if (filter instanceof WhitelistFilter) { | |
246 if (filter.siteKeys) { | |
247 for (var i = 0; | |
248 i < filter.siteKeys.length; i++) | |
249 this.keys[filter.siteKeys[i]] = filter.text; | |
250 } | |
251 else | |
252 this.whitelist.add(filter); | |
253 } | |
254 else | |
255 this.blacklist.add(filter); | |
256 if (this.cacheEntries > 0) { | |
257 this.resultCache = { | |
258 __proto__: null | |
259 }; | |
260 this.cacheEntries = 0; | |
261 } | |
262 } | |
263 , | |
264 remove: function (filter) { | |
265 if (filter instanceof WhitelistFilter) { | |
266 if (filter.siteKeys) { | |
267 for (var i = 0; | |
268 i < filter.siteKeys.length; i++) | |
269 delete this.keys[filter.siteKeys[i]]; | |
270 } | |
271 else | |
272 this.whitelist.remove(filter); | |
273 } | |
274 else | |
275 this.blacklist.remove(filter); | |
276 if (this.cacheEntries > 0) { | |
277 this.resultCache = { | |
278 __proto__: null | |
279 }; | |
280 this.cacheEntries = 0; | |
281 } | |
282 } | |
283 , | |
284 findKeyword: function (filter) { | |
285 if (filter instanceof WhitelistFilter) | |
286 return this.whitelist.findKeyword(filter); | |
287 else | |
288 return this.blacklist.findKeyword(filter); | |
289 } | |
290 , | |
291 hasFilter: function (filter) { | |
292 if (filter instanceof WhitelistFilter) | |
293 return this.whitelist.hasFilter(filter); | |
294 else | |
295 return this.blacklist.hasFilter(filter); | |
296 } | |
297 , | |
298 getKeywordForFilter: function (filter) { | |
299 if (filter instanceof WhitelistFilter) | |
300 return this.whitelist.getKeywordForFilter(filter); | |
301 else | |
302 return this.blacklist.getKeywordForFilter(filter); | |
303 } | |
304 , | |
305 isSlowFilter: function (filter) { | |
306 var matcher = (filter instanceof WhitelistFilter ? this.whitelist : this.b
lacklist); | |
307 if (matcher.hasFilter(filter)) | |
308 return !matcher.getKeywordForFilter(filter); | |
309 else | |
310 return !matcher.findKeyword(filter); | |
311 } | |
312 , | |
313 matchesAnyInternal: function (location, contentType, docDomain, thirdParty)
{ | |
314 var candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); | |
315 if (candidates === null) | |
316 candidates = []; | |
317 if (contentType == "DONOTTRACK") | |
318 candidates.unshift("donottrack"); | |
319 else | |
320 candidates.push(""); | |
321 var blacklistHit = null; | |
322 for (var i = 0, l = candidates.length; | |
323 i < l; i++) { | |
324 var substr = candidates[i]; | |
325 if (substr in this.whitelist.filterByKeyword) { | |
326 var result = this.whitelist._checkEntryMatch(substr, location, content
Type, docDomain, thirdParty); | |
327 if (result) | |
328 return result; | |
329 } | |
330 if (substr in this.blacklist.filterByKeyword && blacklistHit === null) | |
331 blacklistHit = this.blacklist._checkEntryMatch(substr, location, conte
ntType, docDomain, thirdParty); | |
332 } | |
333 return blacklistHit; | |
334 } | |
335 , | |
336 matchesAny: function (location, contentType, docDomain, thirdParty) { | |
337 var key = location + " " + contentType + " " + docDomain + " " + thirdPart
y; | |
338 if (key in this.resultCache) | |
339 return this.resultCache[key]; | |
340 var result = this.matchesAnyInternal(location, contentType, docDomain, thi
rdParty); | |
341 if (this.cacheEntries >= CombinedMatcher.maxCacheEntries) { | |
342 this.resultCache = { | |
343 __proto__: null | |
344 }; | |
345 this.cacheEntries = 0; | |
346 } | |
347 this.resultCache[key] = result; | |
348 this.cacheEntries++; | |
349 return result; | |
350 } | |
351 , | |
352 matchesByKey: function (location, key, docDomain) { | |
353 key = key.toUpperCase(); | |
354 if (key in this.keys) { | |
355 var filter = Filter.knownFilters[this.keys[key]]; | |
356 if (filter && filter.matches(location, "DOCUMENT", docDomain, false)) | |
357 return filter; | |
358 else | |
359 return null; | |
360 } | |
361 else | |
362 return null; | |
363 } | |
364 , | |
365 toCache: function (cache) { | |
366 cache.matcher = { | |
367 whitelist: { | |
368 | |
369 }, | |
370 blacklist: { | |
371 | |
372 }, | |
373 keys: this.keys | |
374 }; | |
375 this.whitelist.toCache(cache.matcher.whitelist); | |
376 this.blacklist.toCache(cache.matcher.blacklist); | |
377 } | |
378 , | |
379 fromCache: function (cache) { | |
380 this.whitelist.fromCache(cache.matcher.whitelist); | |
381 this.blacklist.fromCache(cache.matcher.blacklist); | |
382 this.keys = cache.matcher.keys; | |
383 } | |
384 | |
385 }; | |
386 var defaultMatcher = new CombinedMatcher(); | |
387 if (typeof _patchFunc0 != "undefined") | |
388 eval("(" + _patchFunc0.toString() + ")()"); | |
389 window.Matcher = Matcher; | |
390 window.CombinedMatcher = CombinedMatcher; | |
391 window.defaultMatcher = defaultMatcher; | |
392 } | |
393 )(window.MatcherPatch); | |
OLD | NEW |