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

Side by Side Diff: lib/content/elemHideEmulation.js

Issue 29613805: Issue 6034 - :-abp-contains() accept a regular expression (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Address the regexp "escaping" Created Feb. 20, 2018, 5:49 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
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 {escapeRegExp, 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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 } 164 }
165 return all ? subtree.querySelectorAll(selector) : 165 return all ? subtree.querySelectorAll(selector) :
166 subtree.querySelector(selector); 166 subtree.querySelector(selector);
167 } 167 }
168 168
169 function scopedQuerySelectorAll(subtree, selector) 169 function scopedQuerySelectorAll(subtree, selector)
170 { 170 {
171 return scopedQuerySelector(subtree, selector, true); 171 return scopedQuerySelector(subtree, selector, true);
172 } 172 }
173 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.
179 * @param {string} text the text argument.
180 * it overrides flags passed in the text argument.
Manish Jethani 2018/02/21 14:41:59 This line got left out.
hub 2018/02/21 21:38:01 Done.
181 * @return {?RegExp} a RegExp object or null in case of error.
182 */
183 function makeRegExpParameter(text)
184 {
185 let flags;
186 let regexpString = null;
187 let match = regexpRegexp.exec(text);
188 if (match)
Manish Jethani 2018/02/21 14:41:59 So we're having a discussion on #eyeo-js about mod
Manish Jethani 2018/02/21 14:46:38 That spills over the 80 character line length, but
hub 2018/02/21 21:38:00 Done.
189 {
190 regexpString = match[1];
191 flags = match[2];
192 }
193 else
194 regexpString = escapeRegExp(text);
195
196 try
197 {
198 return new RegExp(regexpString, flags);
199 }
200 catch (e)
201 {
202 }
203 return null;
204 }
205
174 function* evaluate(chain, index, prefix, subtree, styles) 206 function* evaluate(chain, index, prefix, subtree, styles)
175 { 207 {
176 if (index >= chain.length) 208 if (index >= chain.length)
177 { 209 {
178 yield prefix; 210 yield prefix;
179 return; 211 return;
180 } 212 }
181 for (let [selector, element] of 213 for (let [selector, element] of
182 chain[index].getSelectors(prefix, subtree, styles)) 214 chain[index].getSelectors(prefix, subtree, styles))
183 { 215 {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 yield element; 288 yield element;
257 } 289 }
258 yield null; 290 yield null;
259 } 291 }
260 } 292 }
261 } 293 }
262 }; 294 };
263 295
264 function ContainsSelector(textContent) 296 function ContainsSelector(textContent)
265 { 297 {
266 this._text = textContent; 298 this._regexp = makeRegExpParameter(textContent);
267 } 299 }
268 300
269 ContainsSelector.prototype = { 301 ContainsSelector.prototype = {
270 requiresHiding: true, 302 requiresHiding: true,
271 303
272 *getSelectors(prefix, subtree, stylesheet) 304 *getSelectors(prefix, subtree, stylesheet)
273 { 305 {
274 for (let element of this.getElements(prefix, subtree, stylesheet)) 306 for (let element of this.getElements(prefix, subtree, stylesheet))
275 yield [makeSelector(element, ""), subtree]; 307 yield [makeSelector(element, ""), subtree];
276 }, 308 },
277 309
278 *getElements(prefix, subtree, stylesheet) 310 *getElements(prefix, subtree, stylesheet)
279 { 311 {
280 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 312 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
281 prefix + "*" : prefix; 313 prefix + "*" : prefix;
282 314
283 let elements = scopedQuerySelectorAll(subtree, actualPrefix); 315 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
284 if (elements) 316 if (elements)
285 { 317 {
286 for (let element of elements) 318 for (let element of elements)
287 { 319 {
288 if (element.textContent.includes(this._text)) 320 if (this._regexp && this._regexp.test(element.textContent))
289 yield element; 321 yield element;
290 else 322 else
291 yield null; 323 yield null;
292 } 324 }
293 } 325 }
294 } 326 }
295 }; 327 };
296 328
297 function PropsSelector(propertyExpression) 329 function PropsSelector(propertyExpression)
298 { 330 {
(...skipping 10 matching lines...) Expand all
309 this._regexp = new RegExp(regexpString, "i"); 341 this._regexp = new RegExp(regexpString, "i");
310 } 342 }
311 343
312 PropsSelector.prototype = { 344 PropsSelector.prototype = {
313 preferHideWithSelector: true, 345 preferHideWithSelector: true,
314 dependsOnStyles: true, 346 dependsOnStyles: true,
315 347
316 *findPropsSelectors(styles, prefix, regexp) 348 *findPropsSelectors(styles, prefix, regexp)
317 { 349 {
318 for (let style of styles) 350 for (let style of styles)
319 if (regexp.test(style.style)) 351 if (regexp && regexp.test(style.style))
Manish Jethani 2018/02/21 14:41:59 This check is no longer necessary? (i.e. this._reg
hub 2018/02/21 21:38:00 Done.
320 for (let subSelector of style.subSelectors) 352 for (let subSelector of style.subSelectors)
321 { 353 {
322 if (subSelector.startsWith("*") && 354 if (subSelector.startsWith("*") &&
323 !incompletePrefixRegexp.test(prefix)) 355 !incompletePrefixRegexp.test(prefix))
324 { 356 {
325 subSelector = subSelector.substr(1); 357 subSelector = subSelector.substr(1);
326 } 358 }
327 let idx = subSelector.lastIndexOf("::"); 359 let idx = subSelector.lastIndexOf("::");
328 if (idx != -1) 360 if (idx != -1)
329 subSelector = subSelector.substr(0, idx); 361 subSelector = subSelector.substr(0, idx);
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 characterData: true, 678 characterData: true,
647 subtree: true 679 subtree: true
648 } 680 }
649 ); 681 );
650 this.document.addEventListener("load", this.onLoad.bind(this), true); 682 this.document.addEventListener("load", this.onLoad.bind(this), true);
651 } 683 }
652 } 684 }
653 }; 685 };
654 686
655 exports.ElemHideEmulation = ElemHideEmulation; 687 exports.ElemHideEmulation = ElemHideEmulation;
OLDNEW
« lib/common.js ('K') | « lib/common.js ('k') | test/browser/elemHideEmulation.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld