OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 /** | 80 /** |
81 * Cache for known filters, maps string representation to filter objects. | 81 * Cache for known filters, maps string representation to filter objects. |
82 * @type {Map.<string,Filter>} | 82 * @type {Map.<string,Filter>} |
83 */ | 83 */ |
84 Filter.knownFilters = new Map(); | 84 Filter.knownFilters = new Map(); |
85 | 85 |
86 /** | 86 /** |
87 * Regular expression that element hiding filters should match | 87 * Regular expression that element hiding filters should match |
88 * @type {RegExp} | 88 * @type {RegExp} |
89 */ | 89 */ |
90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/; | 90 Filter.elemhideRegExp = /^([^/*|@"!]*?)(#([@?])?#)(.+)$/; |
91 /** | 91 /** |
92 * Regular expression that RegExp filters specified as RegExps should match | 92 * Regular expression that RegExp filters specified as RegExps should match |
93 * @type {RegExp} | 93 * @type {RegExp} |
94 */ | 94 */ |
95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^,
\s]+)?)*)?$/; | 95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^,
\s]+)?)*)?$/; |
96 /** | 96 /** |
97 * Regular expression that options on a RegExp filter should match | 97 * Regular expression that options on a RegExp filter should match |
98 * @type {RegExp} | 98 * @type {RegExp} |
99 */ | 99 */ |
100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^,\s]+)?)*)$/; | 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; |
101 | 101 |
102 /** | 102 /** |
103 * Creates a filter of correct type from its text representation - does the | 103 * Creates a filter of correct type from its text representation - does the |
104 * basic parsing and calls the right constructor then. | 104 * basic normalization and parsing where possible before deferring to the right |
105 * | 105 * constructor. |
106 * @param {string} text as in Filter() | 106 * @param {string} text as in Filter() |
107 * @return {Filter} | 107 * @return {Filter} |
108 */ | 108 */ |
109 Filter.fromText = function(text) | 109 Filter.fromText = function(text) |
110 { | 110 { |
111 let filter = Filter.knownFilters.get(text); | 111 let filter = Filter.knownFilters.get(text); |
112 if (filter) | 112 if (filter) |
113 return filter; | 113 return filter; |
114 | 114 |
115 let match = (text.includes("#") ? Filter.elemhideRegExp.exec(text) : null); | 115 let match = (text.includes("#") ? Filter.elemhideRegExp.exec(text) : null); |
116 if (match) | 116 if (match) |
117 { | 117 { |
| 118 let [, domain, seperator, type, selector] = match; |
| 119 |
| 120 domain = domain.replace(/\s/g, ""); |
| 121 selector = selector.trim(); |
| 122 |
118 let propsMatch; | 123 let propsMatch; |
119 if (!match[2] && | 124 if (!type && |
120 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) | 125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(selector))) |
121 { | 126 { |
122 // This is legacy CSS properties syntax, convert to current syntax | 127 // This is legacy CSS properties syntax, convert to current syntax |
123 let prefix = match[3].substr(0, propsMatch.index); | 128 let prefix = selector.substr(0, propsMatch.index); |
124 let expression = propsMatch[2]; | 129 let expression = propsMatch[2]; |
125 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); | 130 let suffix = selector.substr(propsMatch.index + propsMatch[0].length); |
126 return Filter.fromText(`${match[1]}#?#` + | 131 return Filter.fromText(`${domain}#?#` + |
127 `${prefix}:-abp-properties(${expression})${suffix}`); | 132 `${prefix}:-abp-properties(${expression})${suffix}`); |
128 } | 133 } |
129 | 134 |
130 filter = ElemHideBase.fromText( | 135 filter = ElemHideBase.fromText( |
131 text, match[1], match[2], match[3] | 136 domain + seperator + selector, domain, type, selector |
132 ); | 137 ); |
133 } | 138 } |
134 else if (text[0] == "!") | 139 else if (text[0] == "!") |
135 filter = new CommentFilter(text); | 140 filter = new CommentFilter(text); |
136 else | 141 else |
137 filter = RegExpFilter.fromText(text); | 142 filter = RegExpFilter.fromText(text); |
138 | 143 |
139 Filter.knownFilters.set(filter.text, filter); | 144 Filter.knownFilters.set(filter.text, filter); |
140 return filter; | 145 return filter; |
141 }; | 146 }; |
(...skipping 13 matching lines...) Expand all Loading... |
155 filter._disabled = (obj.disabled == "true"); | 160 filter._disabled = (obj.disabled == "true"); |
156 if ("hitCount" in obj) | 161 if ("hitCount" in obj) |
157 filter._hitCount = parseInt(obj.hitCount, 10) || 0; | 162 filter._hitCount = parseInt(obj.hitCount, 10) || 0; |
158 if ("lastHit" in obj) | 163 if ("lastHit" in obj) |
159 filter._lastHit = parseInt(obj.lastHit, 10) || 0; | 164 filter._lastHit = parseInt(obj.lastHit, 10) || 0; |
160 } | 165 } |
161 return filter; | 166 return filter; |
162 }; | 167 }; |
163 | 168 |
164 /** | 169 /** |
165 * Removes unnecessary whitespaces from filter text, will only return null if | 170 * Strip linebreaks etc and then trim whitespace from the filter text. |
166 * the input parameter is null. | 171 * Will only return null if the input parameter is null. |
167 * @param {string} text | 172 * @param {string} text |
168 * @return {string} | 173 * @return {string} |
169 */ | 174 */ |
170 Filter.normalize = function(text) | 175 Filter.stripJunk = function(text) |
171 { | 176 { |
172 if (!text) | 177 if (!text) |
173 return text; | 178 return text; |
174 | 179 return text.replace(/[^\S ]/g, "").trim(); |
175 // Remove line breaks and such | |
176 text = text.replace(/[^\S ]/g, ""); | |
177 | |
178 if (/^\s*!/.test(text)) | |
179 { | |
180 // Don't remove spaces inside comments | |
181 return text.trim(); | |
182 } | |
183 else if (Filter.elemhideRegExp.test(text)) | |
184 { | |
185 // Special treatment for element hiding filters, right side is allowed to | |
186 // contain spaces | |
187 let [, domain, separator, selector] = /^(.*?)(#@?#?)(.*)$/.exec(text); | |
188 return domain.replace(/\s/g, "") + separator + selector.trim(); | |
189 } | |
190 return text.replace(/\s/g, ""); | |
191 }; | 180 }; |
192 | 181 |
193 /** | 182 /** |
194 * @see filterToRegExp | 183 * @see filterToRegExp |
195 */ | 184 */ |
196 Filter.toRegExp = filterToRegExp; | 185 Filter.toRegExp = filterToRegExp; |
197 | 186 |
198 /** | 187 /** |
199 * Class for invalid filters | 188 * Class for invalid filters |
200 * @param {string} text see Filter() | 189 * @param {string} text see Filter() |
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 get() { return this; } | 695 get() { return this; } |
707 }); | 696 }); |
708 | 697 |
709 /** | 698 /** |
710 * Creates a RegExp filter from its text representation | 699 * Creates a RegExp filter from its text representation |
711 * @param {string} text same as in Filter() | 700 * @param {string} text same as in Filter() |
712 * @return {Filter} | 701 * @return {Filter} |
713 */ | 702 */ |
714 RegExpFilter.fromText = function(text) | 703 RegExpFilter.fromText = function(text) |
715 { | 704 { |
716 let blocking = true; | |
717 let origText = text; | |
718 if (text.indexOf("@@") == 0) | |
719 { | |
720 blocking = false; | |
721 text = text.substr(2); | |
722 } | |
723 | |
724 let contentType = null; | 705 let contentType = null; |
725 let matchCase = null; | 706 let matchCase = null; |
726 let domains = null; | 707 let domains = null; |
727 let sitekeys = null; | 708 let sitekeys = null; |
728 let thirdParty = null; | 709 let thirdParty = null; |
729 let collapse = null; | 710 let collapse = null; |
730 let options; | 711 let origText = null; |
| 712 |
731 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); | 713 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); |
732 if (match) | 714 if (!match) |
733 { | 715 { |
734 options = match[1].toUpperCase().split(","); | 716 origText = text = text.replace(/\s/g, ""); |
735 text = match.input.substr(0, match.index); | 717 } |
736 for (let option of options) | 718 else |
| 719 { |
| 720 text = match.input.substring(0, match.index).replace(/\s/g, ""); |
| 721 let options = match[1].replace(/\s/g, ""); |
| 722 origText = text + "$" + options; |
| 723 |
| 724 for (let option of options.toUpperCase().split(",")) |
737 { | 725 { |
738 let value = null; | 726 let value = null; |
739 let separatorIndex = option.indexOf("="); | 727 let separatorIndex = option.indexOf("="); |
740 if (separatorIndex >= 0) | 728 if (separatorIndex >= 0) |
741 { | 729 { |
742 value = option.substr(separatorIndex + 1); | 730 value = option.substr(separatorIndex + 1); |
743 option = option.substr(0, separatorIndex); | 731 option = option.substring(0, separatorIndex); |
744 } | 732 } |
745 option = option.replace(/-/, "_"); | 733 option = option.replace(/-/, "_"); |
746 if (option in RegExpFilter.typeMap) | 734 if (option in RegExpFilter.typeMap) |
747 { | 735 { |
748 if (contentType == null) | 736 if (contentType == null) |
749 contentType = 0; | 737 contentType = 0; |
750 contentType |= RegExpFilter.typeMap[option]; | 738 contentType |= RegExpFilter.typeMap[option]; |
751 } | 739 } |
752 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) | 740 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) |
753 { | 741 { |
(...skipping 17 matching lines...) Expand all Loading... |
771 collapse = false; | 759 collapse = false; |
772 else if (option == "SITEKEY" && typeof value != "undefined") | 760 else if (option == "SITEKEY" && typeof value != "undefined") |
773 sitekeys = value; | 761 sitekeys = value; |
774 else | 762 else |
775 return new InvalidFilter(origText, "filter_unknown_option"); | 763 return new InvalidFilter(origText, "filter_unknown_option"); |
776 } | 764 } |
777 } | 765 } |
778 | 766 |
779 try | 767 try |
780 { | 768 { |
781 if (blocking) | 769 if (text.indexOf("@@") == 0) |
782 { | 770 { |
783 return new BlockingFilter(origText, text, contentType, matchCase, domains, | 771 return new WhitelistFilter(origText, text.substr(2), contentType, |
784 thirdParty, sitekeys, collapse); | 772 matchCase, domains, thirdParty, sitekeys); |
785 } | 773 } |
786 return new WhitelistFilter(origText, text, contentType, matchCase, domains, | 774 return new BlockingFilter(origText, text, contentType, matchCase, domains, |
787 thirdParty, sitekeys); | 775 thirdParty, sitekeys, collapse); |
788 } | 776 } |
789 catch (e) | 777 catch (e) |
790 { | 778 { |
791 return new InvalidFilter(origText, "filter_invalid_regexp"); | 779 return new InvalidFilter(origText, "filter_invalid_regexp"); |
792 } | 780 } |
793 }; | 781 }; |
794 | 782 |
795 /** | 783 /** |
796 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks | 784 * Maps type strings like "SCRIPT" or "OBJECT" to bit masks |
797 */ | 785 */ |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1019 */ | 1007 */ |
1020 function ElemHideEmulationFilter(text, domains, selector) | 1008 function ElemHideEmulationFilter(text, domains, selector) |
1021 { | 1009 { |
1022 ElemHideBase.call(this, text, domains, selector); | 1010 ElemHideBase.call(this, text, domains, selector); |
1023 } | 1011 } |
1024 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 1012 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; |
1025 | 1013 |
1026 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1014 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { |
1027 type: "elemhideemulation" | 1015 type: "elemhideemulation" |
1028 }); | 1016 }); |
OLD | NEW |