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

Side by Side Diff: assets/js/Matcher.jsm

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

Powered by Google App Engine
This is Rietveld