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: Remove the escaping of curly braces for :-abp-contains() Created Feb. 8, 2018, 5: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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 { 170 {
171 return scopedQuerySelector(subtree, selector, true); 171 return scopedQuerySelector(subtree, selector, true);
172 } 172 }
173 173
174 const regexpRegexp = /^\/(.*)\/([im]*)$/; 174 const regexpRegexp = /^\/(.*)\/([im]*)$/;
175 175
176 /** 176 /**
177 * Make a regular expression from a text argument. If it can be parsed as a 177 * Make a regular expression from a text argument. If it can be parsed as a
178 * regular expression, parse it and the flags. 178 * regular expression, parse it and the flags.
179 * @param {string} text the text argument. 179 * @param {string} text the text argument.
180 * @param {boolean} escape indicate whether we escape curly braces.
Manish Jethani 2018/02/20 10:47:10 escape could be made optional ("?boolean"), then t
hub 2018/02/20 17:50:18 Good point, but this is moot in the current patch.
181 * @param {?string} flags the regexp flags passed to the RegExp constructor:
182 * it overrides flags passed in the text argument.
183 * @return {?RegExp} a RegExp object or null in case of error. 180 * @return {?RegExp} a RegExp object or null in case of error.
184 */ 181 */
185 function makeRegExpParameter(text, escape, flags) 182 function makeRegExpParameter(text)
186 { 183 {
187 let regexpString = null; 184 let [, pattern, flags] =
188 let match = regexpRegexp.exec(text); 185 regexpRegexp.exec(text) || [undefined, textToRegExp(text)];
189 if (match)
190 {
191 regexpString =
192 escape ? match[1].replace("\\7B ", "{").replace("\\7D ", "}") : match[1];
193 if (!flags)
194 flags = match[2];
195 }
196 else
197 regexpString = filterToRegExp(text);
Manish Jethani 2018/02/20 10:47:10 This doesn't make sense for the contains filter, d
hub 2018/02/20 17:50:18 You are totally right. The new patch fixes that.
198 186
199 try 187 try
200 { 188 {
201 return new RegExp(regexpString, flags); 189 return new RegExp(pattern, flags);
202 } 190 }
203 catch (e) 191 catch (e)
204 { 192 {
205 } 193 }
206 return null; 194 return null;
207 } 195 }
208 196
209 function* evaluate(chain, index, prefix, subtree, styles) 197 function* evaluate(chain, index, prefix, subtree, styles)
210 { 198 {
211 if (index >= chain.length) 199 if (index >= chain.length)
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 yield element; 279 yield element;
292 } 280 }
293 yield null; 281 yield null;
294 } 282 }
295 } 283 }
296 } 284 }
297 }; 285 };
298 286
299 function ContainsSelector(textContent) 287 function ContainsSelector(textContent)
300 { 288 {
301 this._regexp = makeRegExpParameter(textContent, false); 289 this._regexp = makeRegExpParameter(textContent);
302 } 290 }
303 291
304 ContainsSelector.prototype = { 292 ContainsSelector.prototype = {
305 requiresHiding: true, 293 requiresHiding: true,
306 294
307 *getSelectors(prefix, subtree, stylesheet) 295 *getSelectors(prefix, subtree, stylesheet)
308 { 296 {
309 for (let element of this.getElements(prefix, subtree, stylesheet)) 297 for (let element of this.getElements(prefix, subtree, stylesheet))
310 yield [makeSelector(element, ""), subtree]; 298 yield [makeSelector(element, ""), subtree];
311 }, 299 },
(...skipping 12 matching lines...) Expand all
324 yield element; 312 yield element;
325 else 313 else
326 yield null; 314 yield null;
327 } 315 }
328 } 316 }
329 } 317 }
330 }; 318 };
331 319
332 function PropsSelector(propertyExpression) 320 function PropsSelector(propertyExpression)
333 { 321 {
334 this._regexp = makeRegExpParameter(propertyExpression, true, "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");
335 } 333 }
336 334
337 PropsSelector.prototype = { 335 PropsSelector.prototype = {
338 preferHideWithSelector: true, 336 preferHideWithSelector: true,
339 dependsOnStyles: true, 337 dependsOnStyles: true,
340 338
341 *findPropsSelectors(styles, prefix, regexp) 339 *findPropsSelectors(styles, prefix, regexp)
342 { 340 {
343 for (let style of styles) 341 for (let style of styles)
344 if (regexp && regexp.test(style.style)) 342 if (regexp.test(style.style))
345 for (let subSelector of style.subSelectors) 343 for (let subSelector of style.subSelectors)
346 { 344 {
347 if (subSelector.startsWith("*") && 345 if (subSelector.startsWith("*") &&
348 !incompletePrefixRegexp.test(prefix)) 346 !incompletePrefixRegexp.test(prefix))
349 { 347 {
350 subSelector = subSelector.substr(1); 348 subSelector = subSelector.substr(1);
351 } 349 }
352 let idx = subSelector.lastIndexOf("::"); 350 let idx = subSelector.lastIndexOf("::");
353 if (idx != -1) 351 if (idx != -1)
354 subSelector = subSelector.substr(0, idx); 352 subSelector = subSelector.substr(0, idx);
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 characterData: true, 669 characterData: true,
672 subtree: true 670 subtree: true
673 } 671 }
674 ); 672 );
675 this.document.addEventListener("load", this.onLoad.bind(this), true); 673 this.document.addEventListener("load", this.onLoad.bind(this), true);
676 } 674 }
677 } 675 }
678 }; 676 };
679 677
680 exports.ElemHideEmulation = ElemHideEmulation; 678 exports.ElemHideEmulation = ElemHideEmulation;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld