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

Side by Side Diff: lib/filterClasses.js

Issue 29737558: Issue 6538, 6781 - Implement support for snippet filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Make snippet filters similar to element hiding filters Created April 25, 2018, 5:15 p.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
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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 } 77 }
78 }; 78 };
79 79
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 code injection filters should match
88 * @type {RegExp} 88 * @type {RegExp}
89 */ 89 */
90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/; 90 Filter.codeInjectionRegExp = /^([^/*|@"!]*?)#([@?$])?#(.+)$/;
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-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/;
101 /** 101 /**
102 * Regular expression that matches an invalid Content Security Policy 102 * Regular expression that matches an invalid Content Security Policy
103 * @type {RegExp} 103 * @type {RegExp}
104 */ 104 */
105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i; 105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i;
106 106
107 /** 107 /**
108 * Creates a filter of correct type from its text representation - does the 108 * Creates a filter of correct type from its text representation - does the
109 * basic parsing and calls the right constructor then. 109 * basic parsing and calls the right constructor then.
110 * 110 *
111 * @param {string} text as in Filter() 111 * @param {string} text as in Filter()
112 * @return {Filter} 112 * @return {Filter}
113 */ 113 */
114 Filter.fromText = function(text) 114 Filter.fromText = function(text)
115 { 115 {
116 let filter = Filter.knownFilters.get(text); 116 let filter = Filter.knownFilters.get(text);
117 if (filter) 117 if (filter)
118 return filter; 118 return filter;
119 119
120 let match = (text.includes("#") ? Filter.elemhideRegExp.exec(text) : null); 120 let match = text.includes("#") ? Filter.codeInjectionRegExp.exec(text) : null;
121 if (match) 121 if (match)
122 { 122 {
123 let propsMatch; 123 let propsMatch;
124 if (!match[2] && 124 if (!match[2] &&
125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) 125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3])))
126 { 126 {
127 // This is legacy CSS properties syntax, convert to current syntax 127 // This is legacy CSS properties syntax, convert to current syntax
128 let prefix = match[3].substr(0, propsMatch.index); 128 let prefix = match[3].substr(0, propsMatch.index);
129 let expression = propsMatch[2]; 129 let expression = propsMatch[2];
130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); 130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length);
131 return Filter.fromText(`${match[1]}#?#` + 131 return Filter.fromText(`${match[1]}#?#` +
132 `${prefix}:-abp-properties(${expression})${suffix}`); 132 `${prefix}:-abp-properties(${expression})${suffix}`);
133 } 133 }
134 134
135 if (match[2] == "$")
136 return new SnippetFilter(text, match[1], match[3]);
137
135 filter = ElemHideBase.fromText( 138 filter = ElemHideBase.fromText(
136 text, match[1], match[2], match[3] 139 text, match[1], match[2], match[3]
137 ); 140 );
138 } 141 }
139 else if (text[0] == "!") 142 else if (text[0] == "!")
140 filter = new CommentFilter(text); 143 filter = new CommentFilter(text);
141 else 144 else
142 filter = RegExpFilter.fromText(text); 145 filter = RegExpFilter.fromText(text);
143 146
144 Filter.knownFilters.set(filter.text, filter); 147 Filter.knownFilters.set(filter.text, filter);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 if (!text) 180 if (!text)
178 return text; 181 return text;
179 182
180 // Remove line breaks, tabs etc 183 // Remove line breaks, tabs etc
181 text = text.replace(/[^\S ]+/g, ""); 184 text = text.replace(/[^\S ]+/g, "");
182 185
183 // Don't remove spaces inside comments 186 // Don't remove spaces inside comments
184 if (/^ *!/.test(text)) 187 if (/^ *!/.test(text))
185 return text.trim(); 188 return text.trim();
186 189
187 // Special treatment for element hiding filters, right side is allowed to 190 // Special treatment for code injection filters, right side is allowed to
188 // contain spaces 191 // contain spaces
189 if (Filter.elemhideRegExp.test(text)) 192 if (Filter.codeInjectionRegExp.test(text))
190 { 193 {
191 let [, domain, separator, selector] = /^(.*?)(#[@?]?#?)(.*)$/.exec(text); 194 let [, domain, separator, code] = /^(.*?)(#[@?]?#?)(.*)$/.exec(text);
192 return domain.replace(/ +/g, "") + separator + selector.trim(); 195 return domain.replace(/ +/g, "") + separator + code.trim();
193 } 196 }
194 197
195 // For most regexp filters we strip all spaces, but $csp filter options 198 // For most regexp filters we strip all spaces, but $csp filter options
196 // are allowed to contain single (non trailing) spaces. 199 // are allowed to contain single (non trailing) spaces.
197 let strippedText = text.replace(/ +/g, ""); 200 let strippedText = text.replace(/ +/g, "");
198 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) 201 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText))
199 return strippedText; 202 return strippedText;
200 203
201 let optionsMatch = Filter.optionsRegExp.exec(strippedText); 204 let optionsMatch = Filter.optionsRegExp.exec(strippedText);
202 if (!optionsMatch) 205 if (!optionsMatch)
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 XBL: 1, 862 XBL: 1,
860 PING: 1024, 863 PING: 1024,
861 XMLHTTPREQUEST: 2048, 864 XMLHTTPREQUEST: 2048,
862 OBJECT_SUBREQUEST: 4096, 865 OBJECT_SUBREQUEST: 4096,
863 DTD: 1, 866 DTD: 1,
864 MEDIA: 16384, 867 MEDIA: 16384,
865 FONT: 32768, 868 FONT: 32768,
866 869
867 BACKGROUND: 4, // Backwards compat, same as IMAGE 870 BACKGROUND: 4, // Backwards compat, same as IMAGE
868 871
872 SNIPPET: 0x8000000,
Manish Jethani 2018/04/26 13:19:16 I had to take this slot since I can't go any highe
869 POPUP: 0x10000000, 873 POPUP: 0x10000000,
870 GENERICBLOCK: 0x20000000, 874 GENERICBLOCK: 0x20000000,
871 ELEMHIDE: 0x40000000, 875 ELEMHIDE: 0x40000000,
872 GENERICHIDE: 0x80000000 876 GENERICHIDE: 0x80000000
873 }; 877 };
874 878
875 // CSP, DOCUMENT, ELEMHIDE, POPUP, GENERICHIDE and GENERICBLOCK options 879 // CSP, DOCUMENT, ELEMHIDE, SNIPPET, POPUP, GENERICHIDE and GENERICBLOCK options
876 // shouldn't be there by default 880 // shouldn't be there by default
877 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP | 881 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP |
878 RegExpFilter.typeMap.DOCUMENT | 882 RegExpFilter.typeMap.DOCUMENT |
879 RegExpFilter.typeMap.ELEMHIDE | 883 RegExpFilter.typeMap.ELEMHIDE |
884 RegExpFilter.typeMap.SNIPPET |
Manish Jethani 2018/04/26 13:19:16 I'm actually not sure that this is needed, $snippe
880 RegExpFilter.typeMap.POPUP | 885 RegExpFilter.typeMap.POPUP |
881 RegExpFilter.typeMap.GENERICHIDE | 886 RegExpFilter.typeMap.GENERICHIDE |
882 RegExpFilter.typeMap.GENERICBLOCK); 887 RegExpFilter.typeMap.GENERICBLOCK);
883 888
884 /** 889 /**
885 * Class for blocking filters 890 * Class for blocking filters
886 * @param {string} text see Filter() 891 * @param {string} text see Filter()
887 * @param {string} regexpSource see RegExpFilter() 892 * @param {string} regexpSource see RegExpFilter()
888 * @param {number} contentType see RegExpFilter() 893 * @param {number} contentType see RegExpFilter()
889 * @param {boolean} matchCase see RegExpFilter() 894 * @param {boolean} matchCase see RegExpFilter()
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
943 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, 948 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
944 thirdParty, sitekeys); 949 thirdParty, sitekeys);
945 } 950 }
946 exports.WhitelistFilter = WhitelistFilter; 951 exports.WhitelistFilter = WhitelistFilter;
947 952
948 WhitelistFilter.prototype = extend(RegExpFilter, { 953 WhitelistFilter.prototype = extend(RegExpFilter, {
949 type: "whitelist" 954 type: "whitelist"
950 }); 955 });
951 956
952 /** 957 /**
953 * Base class for element hiding filters 958 * Base class for code injection filters
954 * @param {string} text see Filter() 959 * @param {string} text see Filter()
955 * @param {string} [domains] Host names or domains the filter should be 960 * @param {string} [domains] Host names or domains the filter should be
956 * restricted to 961 * restricted to
957 * @param {string} selector CSS selector for the HTML elements that should be 962 * @param {string} code Code that should be injected
958 * hidden
959 * @constructor 963 * @constructor
960 * @augments ActiveFilter 964 * @augments ActiveFilter
961 */ 965 */
962 function ElemHideBase(text, domains, selector) 966 function CodeInjectionFilter(text, domains, code)
963 { 967 {
964 ActiveFilter.call(this, text, domains || null); 968 ActiveFilter.call(this, text, domains || null);
965 969
966 if (domains) 970 if (domains)
967 { 971 {
968 this.selectorDomain = domains.replace(/,~[^,]+/g, "") 972 this.injectionDomain = domains.replace(/,~[^,]+/g, "")
969 .replace(/^~[^,]+,?/, "").toLowerCase(); 973 .replace(/^~[^,]+,?/, "").toLowerCase();
970 } 974 }
971 975
972 // Braces are being escaped to prevent CSS rule injection. 976 this.code = code;
973 this.selector = selector.replace("{", "\\7B ").replace("}", "\\7D ");
974 } 977 }
975 exports.ElemHideBase = ElemHideBase; 978 exports.CodeInjectionFilter = CodeInjectionFilter;
976 979
977 ElemHideBase.prototype = extend(ActiveFilter, { 980 CodeInjectionFilter.prototype = extend(ActiveFilter, {
978 /** 981 /**
979 * @see ActiveFilter.domainSeparator 982 * @see ActiveFilter.domainSeparator
980 */ 983 */
981 domainSeparator: ",", 984 domainSeparator: ",",
982 985
983 /** 986 /**
984 * @see ActiveFilter.ignoreTrailingDot 987 * @see ActiveFilter.ignoreTrailingDot
985 */ 988 */
986 ignoreTrailingDot: false, 989 ignoreTrailingDot: false,
987 990
988 /** 991 /**
989 * Host name or domain the filter should be restricted to (can be null for 992 * Host name or domain the filter should be restricted to (can be null for
990 * no restriction) 993 * no restriction)
991 * @type {string} 994 * @type {string}
992 */ 995 */
993 selectorDomain: null, 996 injectionDomain: null,
997
994 /** 998 /**
995 * CSS selector for the HTML elements that should be hidden 999 * Code that should be injected
996 * @type {string} 1000 * @type {string}
997 */ 1001 */
998 selector: null 1002 code: null
Manish Jethani 2018/04/26 13:19:16 CSS selectors are also code, so let's make this ge
999 }); 1003 });
1000 1004
1001 /** 1005 /**
1006 * Base class for element hiding filters
1007 * @param {string} text see Filter()
1008 * @param {string} [domains] see CodeInjectionFilter()
1009 * @param {string} selector CSS selector for the HTML elements that should be
1010 * hidden
1011 * @constructor
1012 * @augments CodeInjectionFilter
1013 */
1014 function ElemHideBase(text, domains, selector)
1015 {
1016 CodeInjectionFilter.call(this, text, domains, selector);
1017
1018 // Braces are being escaped to prevent CSS rule injection.
1019 this.code = this.code.replace("{", "\\7B ").replace("}", "\\7D ");
1020 }
1021 exports.ElemHideBase = ElemHideBase;
1022
1023 ElemHideBase.prototype = extend(CodeInjectionFilter, {});
1024
1025 /**
1002 * Creates an element hiding filter from a pre-parsed text representation 1026 * Creates an element hiding filter from a pre-parsed text representation
1003 * 1027 *
1004 * @param {string} text same as in Filter() 1028 * @param {string} text same as in Filter()
1005 * @param {string?} domain 1029 * @param {string?} domain
1006 * domain part of the text representation 1030 * domain part of the text representation
1007 * @param {string?} type 1031 * @param {string?} type
1008 * rule type, either empty or @ (exception) or ? (emulation rule) 1032 * rule type, either empty or @ (exception) or ? (emulation rule)
1009 * @param {string} selector raw CSS selector 1033 * @param {string} selector raw CSS selector
1010 * @return {ElemHideFilter|ElemHideException| 1034 * @return {ElemHideFilter|ElemHideException|
1011 * ElemHideEmulationFilter|InvalidFilter} 1035 * ElemHideEmulationFilter|InvalidFilter}
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 */ 1104 */
1081 function ElemHideEmulationFilter(text, domains, selector) 1105 function ElemHideEmulationFilter(text, domains, selector)
1082 { 1106 {
1083 ElemHideBase.call(this, text, domains, selector); 1107 ElemHideBase.call(this, text, domains, selector);
1084 } 1108 }
1085 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1109 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1086 1110
1087 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1111 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1088 type: "elemhideemulation" 1112 type: "elemhideemulation"
1089 }); 1113 });
1114
1115 /**
1116 * Class for snippet filters
1117 * @param {string} text see Filter()
1118 * @param {string} [domains] see CodeInjectionFilter()
1119 * @param {string} script Script that should be executed
1120 * @constructor
1121 * @augments CodeInjectionFilter
1122 */
1123 function SnippetFilter(text, domains, script)
1124 {
1125 CodeInjectionFilter.call(this, text, domains, script);
1126 }
1127 exports.SnippetFilter = SnippetFilter;
1128
1129 SnippetFilter.prototype = extend(CodeInjectionFilter, {
1130 type: "snippet"
1131 });
OLDNEW
« no previous file with comments | « lib/elemHide.js ('k') | lib/filterListener.js » ('j') | lib/snippets.js » ('J')

Powered by Google App Engine
This is Rietveld