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

Delta Between Two Patch Sets: lib/content/elemHideEmulation.js

Issue 29613805: Issue 6034 - :-abp-contains() accept a regular expression (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Do all matches with RegExp. Created Jan. 9, 2018, 10:25 p.m.
Right Patch Set: Some comments editing, Created Feb. 22, 2018, 2:05 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/common.js ('k') | test/browser/elemHideEmulation.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 const {filterToRegExp, splitSelector} = require("../common"); 20 const {textToRegExp, filterToRegExp, splitSelector} = require("../common");
21 21
22 let MIN_INVOCATION_INTERVAL = 3000; 22 let MIN_INVOCATION_INTERVAL = 3000;
23 const MAX_SYNCHRONOUS_PROCESSING_TIME = 50; 23 const MAX_SYNCHRONOUS_PROCESSING_TIME = 50;
24 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; 24 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i;
25 25
26 /** Return position of node from parent. 26 /** Return position of node from parent.
27 * @param {Node} node the node to find the position of. 27 * @param {Node} node the node to find the position of.
28 * @return {number} One-based index like for :nth-child(), or 0 on error. 28 * @return {number} One-based index like for :nth-child(), or 0 on error.
29 */ 29 */
30 function positionInParent(node) 30 function positionInParent(node)
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 let priority = rule.style.getPropertyPriority(property); 115 let priority = rule.style.getPropertyPriority(property);
116 styles.push(`${property}: ${value}${priority ? " !" + priority : ""};`); 116 styles.push(`${property}: ${value}${priority ? " !" + priority : ""};`);
117 } 117 }
118 styles.sort(); 118 styles.sort();
119 return { 119 return {
120 style: styles.join(" "), 120 style: styles.join(" "),
121 subSelectors: splitSelector(rule.selectorText) 121 subSelectors: splitSelector(rule.selectorText)
122 }; 122 };
123 } 123 }
124 124
125 let scopeSupported = null;
126
127 function tryQuerySelector(subtree, selector, all)
128 {
129 let elements = null;
130 try
131 {
132 elements = all ? subtree.querySelectorAll(selector) :
133 subtree.querySelector(selector);
134 scopeSupported = true;
135 }
136 catch (e)
137 {
138 // Edge doesn't support ":scope"
139 scopeSupported = false;
140 }
141 return elements;
142 }
143
125 /** 144 /**
126 * Make a regular expresson from a text argument. 145 * Query selector. If it is relative, will try :scope.
146 * @param {Node} subtree the element to query selector
147 * @param {string} selector the selector to query
148 * @param {bool} [all=false] true to perform querySelectorAll()
149 * @returns {?(Node|NodeList)} result of the query. null in case of error.
150 */
151 function scopedQuerySelector(subtree, selector, all)
152 {
153 if (selector[0] == ">")
154 {
155 selector = ":scope" + selector;
156 if (scopeSupported)
157 {
158 return all ? subtree.querySelectorAll(selector) :
159 subtree.querySelector(selector);
160 }
161 if (scopeSupported == null)
162 return tryQuerySelector(subtree, selector, all);
163 return null;
164 }
165 return all ? subtree.querySelectorAll(selector) :
166 subtree.querySelector(selector);
167 }
168
169 function scopedQuerySelectorAll(subtree, selector)
170 {
171 return scopedQuerySelector(subtree, selector, true);
172 }
173
174 const regexpRegexp = /^\/(.*)\/([im]*)$/;
175
176 /**
177 * Make a regular expression from a text argument. If it can be parsed as a
178 * regular expression, parse it and the flags.
127 * @param {string} text the text argument. 179 * @param {string} text the text argument.
128 * @param {?string} flag the regexp flag passed to the RegExp constructor. 180 * @return {?RegExp} a RegExp object or null in case of error.
129 * @return {RegExp} a RegExp object.
130 */ 181 */
131 function makeRegExpParameter(text, flag) 182 function makeRegExpParameter(text)
132 { 183 {
133 let regexpString = null; 184 let [, pattern, flags] =
134 if (text.length >= 2 && text[0] == "/" && 185 regexpRegexp.exec(text) || [undefined, textToRegExp(text)];
135 text[text.length - 1] == "/") 186
136 { 187 try
137 regexpString = text.slice(1, -1) 188 {
138 .replace("\\7B ", "{").replace("\\7D ", "}"); 189 return new RegExp(pattern, flags);
139 } 190 }
140 else 191 catch (e)
141 regexpString = filterToRegExp(text); 192 {
142 193 }
143 return new RegExp(regexpString, flag); 194 return null;
144 } 195 }
145 196
146 function* evaluate(chain, index, prefix, subtree, styles) 197 function* evaluate(chain, index, prefix, subtree, styles)
147 { 198 {
148 if (index >= chain.length) 199 if (index >= chain.length)
149 { 200 {
150 yield prefix; 201 yield prefix;
151 return; 202 return;
152 } 203 }
153 for (let [selector, element] of 204 for (let [selector, element] of
(...skipping 23 matching lines...) Expand all
177 * @param {Node} subtree the subtree we work on. 228 * @param {Node} subtree the subtree we work on.
178 * @param {StringifiedStyle[]} styles the stringified style objects. 229 * @param {StringifiedStyle[]} styles the stringified style objects.
179 */ 230 */
180 *getSelectors(prefix, subtree, styles) 231 *getSelectors(prefix, subtree, styles)
181 { 232 {
182 yield [prefix + this._selector, subtree]; 233 yield [prefix + this._selector, subtree];
183 } 234 }
184 }; 235 };
185 236
186 const incompletePrefixRegexp = /[\s>+~]$/; 237 const incompletePrefixRegexp = /[\s>+~]$/;
187 const relativeSelectorRegexp = /^[>+~]/;
188 238
189 function HasSelector(selectors) 239 function HasSelector(selectors)
190 { 240 {
191 this._innerSelectors = selectors; 241 this._innerSelectors = selectors;
192 } 242 }
193 243
194 HasSelector.prototype = { 244 HasSelector.prototype = {
195 requiresHiding: true, 245 requiresHiding: true,
196 246
197 get dependsOnStyles() 247 get dependsOnStyles()
(...skipping 10 matching lines...) Expand all
208 /** 258 /**
209 * Generator function returning selected elements. 259 * Generator function returning selected elements.
210 * @param {string} prefix the prefix for the selector. 260 * @param {string} prefix the prefix for the selector.
211 * @param {Node} subtree the subtree we work on. 261 * @param {Node} subtree the subtree we work on.
212 * @param {StringifiedStyle[]} styles the stringified style objects. 262 * @param {StringifiedStyle[]} styles the stringified style objects.
213 */ 263 */
214 *getElements(prefix, subtree, styles) 264 *getElements(prefix, subtree, styles)
215 { 265 {
216 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 266 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
217 prefix + "*" : prefix; 267 prefix + "*" : prefix;
218 let elements = subtree.querySelectorAll(actualPrefix); 268 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
219 for (let element of elements) 269 if (elements)
220 { 270 {
221 let iter = evaluate(this._innerSelectors, 0, "", element, styles); 271 for (let element of elements)
222 for (let selector of iter) 272 {
223 { 273 let iter = evaluate(this._innerSelectors, 0, "", element, styles);
224 if (selector == null) 274 for (let selector of iter)
225 { 275 {
226 yield null; 276 if (selector == null)
227 continue; 277 yield null;
228 } 278 else if (scopedQuerySelector(element, selector))
229 if (relativeSelectorRegexp.test(selector))
230 selector = ":scope" + selector;
231 try
232 {
233 if (element.querySelector(selector))
234 yield element; 279 yield element;
235 } 280 }
236 catch (e) 281 yield null;
237 {
238 // :scope isn't supported on Edge, ignore error caused by it.
239 }
240 } 282 }
241 yield null;
242 } 283 }
243 } 284 }
244 }; 285 };
245 286
246 function ContainsSelector(textContent) 287 function ContainsSelector(textContent)
247 { 288 {
248 this._regexp = makeRegExpParameter(textContent); 289 this._regexp = makeRegExpParameter(textContent);
249 } 290 }
250 291
251 ContainsSelector.prototype = { 292 ContainsSelector.prototype = {
252 requiresHiding: true, 293 requiresHiding: true,
253 294
254 *getSelectors(prefix, subtree, stylesheet) 295 *getSelectors(prefix, subtree, stylesheet)
255 { 296 {
256 for (let element of this.getElements(prefix, subtree, stylesheet)) 297 for (let element of this.getElements(prefix, subtree, stylesheet))
257 yield [makeSelector(element, ""), subtree]; 298 yield [makeSelector(element, ""), subtree];
258 }, 299 },
259 300
260 *getElements(prefix, subtree, stylesheet) 301 *getElements(prefix, subtree, stylesheet)
261 { 302 {
262 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 303 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
263 prefix + "*" : prefix; 304 prefix + "*" : prefix;
264 let elements = subtree.querySelectorAll(actualPrefix); 305
265 306 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
266 for (let element of elements) 307 if (elements)
267 { 308 {
268 if (this._regexp && this._regexp.test(element.textContent)) 309 for (let element of elements)
269 yield element; 310 {
270 else 311 if (this._regexp && this._regexp.test(element.textContent))
271 yield null; 312 yield element;
313 else
314 yield null;
315 }
272 } 316 }
273 } 317 }
274 }; 318 };
275 319
276 function PropsSelector(propertyExpression) 320 function PropsSelector(propertyExpression)
277 { 321 {
278 this._regexp = makeRegExpParameter(propertyExpression, "i"); 322 let regexpString;
323 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" &&
324 propertyExpression[propertyExpression.length - 1] == "/")
325 {
326 regexpString = propertyExpression.slice(1, -1)
327 .replace("\\7B ", "{").replace("\\7D ", "}");
328 }
329 else
330 regexpString = filterToRegExp(propertyExpression);
331
332 this._regexp = new RegExp(regexpString, "i");
279 } 333 }
280 334
281 PropsSelector.prototype = { 335 PropsSelector.prototype = {
282 preferHideWithSelector: true, 336 preferHideWithSelector: true,
283 dependsOnStyles: true, 337 dependsOnStyles: true,
284 338
285 *findPropsSelectors(styles, prefix, regexp) 339 *findPropsSelectors(styles, prefix, regexp)
286 { 340 {
287 for (let style of styles) 341 for (let style of styles)
288 if (regexp.test(style.style)) 342 if (regexp.test(style.style))
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 characterData: true, 669 characterData: true,
616 subtree: true 670 subtree: true
617 } 671 }
618 ); 672 );
619 this.document.addEventListener("load", this.onLoad.bind(this), true); 673 this.document.addEventListener("load", this.onLoad.bind(this), true);
620 } 674 }
621 } 675 }
622 }; 676 };
623 677
624 exports.ElemHideEmulation = ElemHideEmulation; 678 exports.ElemHideEmulation = ElemHideEmulation;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld