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

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

Issue 8482109: ABP/Android JavaScript code (Closed)
Patch Set: ABP/Android JavaScript code Created Nov. 13, 2012, 9:44 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 | « assets/js/ElemHide.jsm ('k') | assets/js/FilterListener.jsm » ('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 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 (_patchFunc2) {
12 function Filter(text) {
13 this.text = text;
14 this.subscriptions = [];
15 }
16 Filter.prototype = {
17 text: null,
18 subscriptions: null,
19 serialize: function (buffer) {
20 buffer.push("[Filter]");
21 buffer.push("text=" + this.text);
22 }
23 ,
24 toString: function () {
25 return this.text;
26 }
27
28 };
29 Filter.knownFilters = {
30 __proto__: null
31 };
32 Filter.elemhideRegExp = /^([^\/\*\|\@"!]*?)#(?:([\w\-]+|\*)((?:\([\w\-]+(?:[$^ *]?=[^\(\)"]*)?\))*)|#([^{}]+))$/;
33 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w\-]+(?:=[^,\s]+)?(?:,~?[\w\-]+(?: =[^,\s]+)?)*)?$/;
34 Filter.optionsRegExp = /\$(~?[\w\-]+(?:=[^,\s]+)?(?:,~?[\w\-]+(?:=[^,\s]+)?)*) $/;
35 Filter.fromText = (function (text) {
36 if (text in Filter.knownFilters)
37 return Filter.knownFilters[text];
38 if (!/\S/.test(text))
39 return null;
40 var ret;
41 if (Filter.elemhideRegExp.test(text))
42 ret = ElemHideFilter.fromText(text, RegExp["$1"], RegExp["$2"], RegExp["$3 "], RegExp["$4"]);
43 else
44 if (text[0] == "!")
45 ret = new CommentFilter(text);
46 else
47 ret = RegExpFilter.fromText(text);
48 Filter.knownFilters[ret.text] = ret;
49 return ret;
50 }
51 );
52 Filter.fromObject = (function (obj) {
53 var ret = Filter.fromText(obj.text);
54 if (ret instanceof ActiveFilter) {
55 if ("disabled" in obj)
56 ret._disabled = (obj.disabled == "true");
57 if ("hitCount" in obj)
58 ret._hitCount = parseInt(obj.hitCount) || 0;
59 if ("lastHit" in obj)
60 ret._lastHit = parseInt(obj.lastHit) || 0;
61 }
62 return ret;
63 }
64 );
65 Filter.normalize = (function (text) {
66 if (!text)
67 return text;
68 text = text.replace(/[^\S ]/g, "");
69 if (/^\s*!/.test(text)) {
70 return text.replace(/^\s+/, "").replace(/\s+$/, "");
71 }
72 else
73 if (Filter.elemhideRegExp.test(text)) {
74 /^(.*?)(#+)(.*)$/.test(text);
75 var domain = RegExp["$1"];
76 var separator = RegExp["$2"];
77 var selector = RegExp["$3"];
78 return domain.replace(/\s/g, "") + separator + selector.replace(/^\s+/, "").replace(/\s+$/, "");
79 }
80 else
81 return text.replace(/\s/g, "");
82 }
83 );
84 function InvalidFilter(text, reason) {
85 Filter.call(this, text);
86 this.reason = reason;
87 }
88 InvalidFilter.prototype = {
89 __proto__: Filter.prototype,
90 reason: null,
91 serialize: function (buffer) {}
92 };
93 function CommentFilter(text) {
94 Filter.call(this, text);
95 }
96 CommentFilter.prototype = {
97 __proto__: Filter.prototype,
98 serialize: function (buffer) {}
99 };
100 function ActiveFilter(text, domains) {
101 Filter.call(this, text);
102 if (domains) {
103 this.domainSource = domains;
104 this.__defineGetter__("domains", this._getDomains);
105 }
106 }
107 ActiveFilter.prototype = {
108 __proto__: Filter.prototype,
109 _disabled: false,
110 _hitCount: 0,
111 _lastHit: 0,
112 get disabled() {
113 return this._disabled;
114 },
115 set disabled(value) {
116 if (value != this._disabled) {
117 var oldValue = this._disabled;
118 this._disabled = value;
119 FilterNotifier.triggerListeners("filter.disabled", this, value, oldValue );
120 }
121 return this._disabled;
122 }
123 ,
124 get hitCount() {
125 return this._hitCount;
126 },
127 set hitCount(value) {
128 if (value != this._hitCount) {
129 var oldValue = this._hitCount;
130 this._hitCount = value;
131 FilterNotifier.triggerListeners("filter.hitCount", this, value, oldValue );
132 }
133 return this._hitCount;
134 }
135 ,
136 get lastHit() {
137 return this._lastHit;
138 },
139 set lastHit(value) {
140 if (value != this._lastHit) {
141 var oldValue = this._lastHit;
142 this._lastHit = value;
143 FilterNotifier.triggerListeners("filter.lastHit", this, value, oldValue) ;
144 }
145 return this._lastHit;
146 }
147 ,
148 domainSource: null,
149 domainSeparator: null,
150 domains: null,
151 _getDomains: function () {
152 this._generateDomains();
153 return this.domains;
154 }
155 ,
156 _generateDomains: function () {
157 var domains = this.domainSource.split(this.domainSeparator);
158 delete this.domainSource;
159 delete this.domains;
160 if (domains.length == 1 && domains[0][0] != "~") {
161 this.domains = {
162 __proto__: null,
163 "": false
164 };
165 this.domains[domains[0]] = true;
166 }
167 else {
168 var hasIncludes = false;
169 for (var i = 0;
170 i < domains.length; i++) {
171 var domain = domains[i];
172 if (domain == "")
173 continue;
174 var include;
175 if (domain[0] == "~") {
176 include = false;
177 domain = domain.substr(1);
178 }
179 else {
180 include = true;
181 hasIncludes = true;
182 }
183 if (!this.domains)
184 this.domains = {
185 __proto__: null
186 };
187 this.domains[domain] = include;
188 }
189 this.domains[""] = !hasIncludes;
190 }
191 }
192 ,
193 isActiveOnDomain: function (docDomain) {
194 if (!this.domains)
195 return true;
196 if (!docDomain)
197 return this.domains[""];
198 docDomain = docDomain.replace(/\.+$/, "").toUpperCase();
199 while (true) {
200 if (docDomain in this.domains)
201 return this.domains[docDomain];
202 var nextDot = docDomain.indexOf(".");
203 if (nextDot < 0)
204 break;
205 docDomain = docDomain.substr(nextDot + 1);
206 }
207 return this.domains[""];
208 }
209 ,
210 isActiveOnlyOnDomain: function (docDomain) {
211 if (!docDomain || !this.domains || this.domains[""])
212 return false;
213 docDomain = docDomain.replace(/\.+$/, "").toUpperCase();
214 for (var domain in this.domains)
215 if (this.domains[domain] && domain != docDomain && (domain.length <= doc Domain.length || domain.indexOf("." + docDomain) != domain.length - docDomain.le ngth - 1))
216 return false;
217 return true;
218 }
219 ,
220 serialize: function (buffer) {
221 if (this._disabled || this._hitCount || this._lastHit) {
222 Filter.prototype.serialize.call(this, buffer);
223 if (this._disabled)
224 buffer.push("disabled=true");
225 if (this._hitCount)
226 buffer.push("hitCount=" + this._hitCount);
227 if (this._lastHit)
228 buffer.push("lastHit=" + this._lastHit);
229 }
230 }
231
232 };
233 function RegExpFilter(text, regexpSource, contentType, matchCase, domains, thi rdParty) {
234 ActiveFilter.call(this, text, domains);
235 if (contentType != null)
236 this.contentType = contentType;
237 if (matchCase)
238 this.matchCase = matchCase;
239 if (thirdParty != null)
240 this.thirdParty = thirdParty;
241 if (regexpSource.length >= 2 && regexpSource[0] == "/" && regexpSource[regex pSource.length - 1] == "/") {
242 this.regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), this.matchCase ? "" : "i");
243 }
244 else {
245 this.regexpSource = regexpSource;
246 this.__defineGetter__("regexp", this._generateRegExp);
247 }
248 }
249 RegExpFilter.prototype = {
250 __proto__: ActiveFilter.prototype,
251 domainSeparator: "|",
252 regexpSource: null,
253 regexp: null,
254 contentType: 2147483647,
255 matchCase: false,
256 thirdParty: null,
257 _generateRegExp: function () {
258 var source = this.regexpSource.replace(/\*+/g, "*");
259 if (source[0] == "*")
260 source = source.substr(1);
261 var pos = source.length - 1;
262 if (pos >= 0 && source[pos] == "*")
263 source = source.substr(0, pos);
264 source = source.replace(/\^\|$/, "^").replace(/\W/g, "\\$&").replace(/\\\* /g, ".*").replace(/\\\^/g, "(?:[\\x00-\\x24\\x26-\\x2C\\x2F\\x3A-\\x40\\x5B-\\x5 E\\x60\\x7B-\\x80]|$)").replace(/^\\\|\\\|/, "^[\\w\\-]+:\\/+(?!\\/)(?:[^.\\/]+\ \.)*?").replace(/^\\\|/, "^").replace(/\\\|$/, "$");
265 var regexp = new RegExp(source, this.matchCase ? "" : "i");
266 delete this.regexp;
267 delete this.regexpSource;
268 return (this.regexp = regexp);
269 }
270 ,
271 matches: function (location, contentType, docDomain, thirdParty) {
272 if (this.regexp.test(location) && (RegExpFilter.typeMap[contentType] & thi s.contentType) != 0 && (this.thirdParty == null || this.thirdParty == thirdParty ) && this.isActiveOnDomain(docDomain)) {
273 return true;
274 }
275 return false;
276 }
277
278 };
279 RegExpFilter.fromText = (function (text) {
280 var blocking = true;
281 var origText = text;
282 if (text.indexOf("@@") == 0) {
283 blocking = false;
284 text = text.substr(2);
285 }
286 var contentType = null;
287 var matchCase = null;
288 var domains = null;
289 var siteKeys = null;
290 var thirdParty = null;
291 var collapse = null;
292 var options;
293 if (Filter.optionsRegExp.test(text)) {
294 options = RegExp["$1"].toUpperCase().split(",");
295 text = RegExp.leftContext;
296 for (var _loopIndex0 = 0;
297 _loopIndex0 < options.length; ++ _loopIndex0) {
298 var option = options[_loopIndex0];
299 var value = null;
300 var separatorIndex = option.indexOf("=");
301 if (separatorIndex >= 0) {
302 value = option.substr(separatorIndex + 1);
303 option = option.substr(0, separatorIndex);
304 }
305 option = option.replace(/-/, "_");
306 if (option in RegExpFilter.typeMap) {
307 if (contentType == null)
308 contentType = 0;
309 contentType |= RegExpFilter.typeMap[option];
310 }
311 else
312 if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) {
313 if (contentType == null)
314 contentType = RegExpFilter.prototype.contentType;
315 contentType &= ~RegExpFilter.typeMap[option.substr(1)];
316 }
317 else
318 if (option == "MATCH_CASE")
319 matchCase = true;
320 else
321 if (option == "DOMAIN" && typeof value != "undefined")
322 domains = value;
323 else
324 if (option == "THIRD_PARTY")
325 thirdParty = true;
326 else
327 if (option == "~THIRD_PARTY")
328 thirdParty = false;
329 else
330 if (option == "COLLAPSE")
331 collapse = true;
332 else
333 if (option == "~COLLAPSE")
334 collapse = false;
335 else
336 if (option == "SITEKEY" && typeof value != "undefined")
337 siteKeys = value.split(/\|/);
338 }
339 }
340 if (!blocking && (contentType == null || (contentType & RegExpFilter.typeMap .DOCUMENT)) && (!options || options.indexOf("DOCUMENT") < 0) && !/^\|?[\w\-]+:/. test(text)) {
341 if (contentType == null)
342 contentType = RegExpFilter.prototype.contentType;
343 contentType &= ~RegExpFilter.typeMap.DOCUMENT;
344 }
345 if (!blocking && siteKeys)
346 contentType = RegExpFilter.typeMap.DOCUMENT;
347 try {
348 if (blocking)
349 return new BlockingFilter(origText, text, contentType, matchCase, domain s, thirdParty, collapse);
350 else
351 return new WhitelistFilter(origText, text, contentType, matchCase, domai ns, thirdParty, siteKeys);
352 }
353 catch (e){
354 return new InvalidFilter(text, e);
355 }
356 }
357 );
358 RegExpFilter.typeMap = {
359 OTHER: 1,
360 SCRIPT: 2,
361 IMAGE: 4,
362 STYLESHEET: 8,
363 OBJECT: 16,
364 SUBDOCUMENT: 32,
365 DOCUMENT: 64,
366 XBL: 1,
367 PING: 1,
368 XMLHTTPREQUEST: 2048,
369 OBJECT_SUBREQUEST: 4096,
370 DTD: 1,
371 MEDIA: 16384,
372 FONT: 32768,
373 BACKGROUND: 4,
374 POPUP: 268435456,
375 DONOTTRACK: 536870912,
376 ELEMHIDE: 1073741824
377 };
378 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.ELEMHIDE | RegExp Filter.typeMap.DONOTTRACK | RegExpFilter.typeMap.POPUP);
379 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, t hirdParty, collapse) {
380 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, thirdParty);
381 this.collapse = collapse;
382 }
383 BlockingFilter.prototype = {
384 __proto__: RegExpFilter.prototype,
385 collapse: null
386 };
387 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, thirdParty, siteKeys) {
388 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, thirdParty);
389 if (siteKeys != null)
390 this.siteKeys = siteKeys;
391 }
392 WhitelistFilter.prototype = {
393 __proto__: RegExpFilter.prototype,
394 siteKeys: null
395 };
396 function ElemHideFilter(text, domains, selector) {
397 ActiveFilter.call(this, text, domains ? domains.toUpperCase() : null);
398 if (domains)
399 this.selectorDomain = domains.replace(/,~[^,]+/g, "").replace(/^~[^,]+,?/, "").toLowerCase();
400 this.selector = selector;
401 }
402 ElemHideFilter.prototype = {
403 __proto__: ActiveFilter.prototype,
404 domainSeparator: ",",
405 selectorDomain: null,
406 selector: null
407 };
408 ElemHideFilter.fromText = (function (text, domain, tagName, attrRules, selecto r) {
409 if (!selector) {
410 if (tagName == "*")
411 tagName = "";
412 var id = null;
413 var additional = "";
414 if (attrRules) {
415 attrRules = attrRules.match(/\([\w\-]+(?:[$^*]?=[^\(\)"]*)?\)/g);
416 for (var _loopIndex1 = 0;
417 _loopIndex1 < attrRules.length; ++ _loopIndex1) {
418 var rule = attrRules[_loopIndex1];
419 rule = rule.substr(1, rule.length - 2);
420 var separatorPos = rule.indexOf("=");
421 if (separatorPos > 0) {
422 rule = rule.replace(/=/, "=\"") + "\"";
423 additional += "[" + rule + "]";
424 }
425 else {
426 if (id)
427 return new InvalidFilter(text, Utils.getString("filter_elemhide_du plicate_id"));
428 else
429 id = rule;
430 }
431 }
432 }
433 if (id)
434 selector = tagName + "." + id + additional + "," + tagName + "#" + id + additional;
435 else
436 if (tagName || additional)
437 selector = tagName + additional;
438 else
439 return new InvalidFilter(text, Utils.getString("filter_elemhide_nocrit eria"));
440 }
441 return new ElemHideFilter(text, domain, selector);
442 }
443 );
444 if (typeof _patchFunc2 != "undefined")
445 eval("(" + _patchFunc2.toString() + ")()");
446 window.Filter = Filter;
447 window.InvalidFilter = InvalidFilter;
448 window.CommentFilter = CommentFilter;
449 window.ActiveFilter = ActiveFilter;
450 window.RegExpFilter = RegExpFilter;
451 window.BlockingFilter = BlockingFilter;
452 window.WhitelistFilter = WhitelistFilter;
453 window.ElemHideFilter = ElemHideFilter;
454 }
455 )(window.FilterClassesPatch);
OLDNEW
« no previous file with comments | « assets/js/ElemHide.jsm ('k') | assets/js/FilterListener.jsm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld