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: Remove the escaping of curly braces for :-abp-contains() Created Feb. 8, 2018, 5:25 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
« no previous file with comments | « no previous file | test/browser/elemHideEmulation.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 153 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 * @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.
184 */
185 function makeRegExpParameter(text, escape, flags)
186 {
187 let regexpString = null;
188 let match = regexpRegexp.exec(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
199 try
200 {
201 return new RegExp(regexpString, flags);
202 }
203 catch (e)
204 {
205 }
206 return null;
207 }
208
174 function* evaluate(chain, index, prefix, subtree, styles) 209 function* evaluate(chain, index, prefix, subtree, styles)
175 { 210 {
176 if (index >= chain.length) 211 if (index >= chain.length)
177 { 212 {
178 yield prefix; 213 yield prefix;
179 return; 214 return;
180 } 215 }
181 for (let [selector, element] of 216 for (let [selector, element] of
182 chain[index].getSelectors(prefix, subtree, styles)) 217 chain[index].getSelectors(prefix, subtree, styles))
183 { 218 {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 yield element; 291 yield element;
257 } 292 }
258 yield null; 293 yield null;
259 } 294 }
260 } 295 }
261 } 296 }
262 }; 297 };
263 298
264 function ContainsSelector(textContent) 299 function ContainsSelector(textContent)
265 { 300 {
266 this._text = textContent; 301 this._regexp = makeRegExpParameter(textContent, false);
267 } 302 }
268 303
269 ContainsSelector.prototype = { 304 ContainsSelector.prototype = {
270 requiresHiding: true, 305 requiresHiding: true,
271 306
272 *getSelectors(prefix, subtree, stylesheet) 307 *getSelectors(prefix, subtree, stylesheet)
273 { 308 {
274 for (let element of this.getElements(prefix, subtree, stylesheet)) 309 for (let element of this.getElements(prefix, subtree, stylesheet))
275 yield [makeSelector(element, ""), subtree]; 310 yield [makeSelector(element, ""), subtree];
276 }, 311 },
277 312
278 *getElements(prefix, subtree, stylesheet) 313 *getElements(prefix, subtree, stylesheet)
279 { 314 {
280 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 315 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
281 prefix + "*" : prefix; 316 prefix + "*" : prefix;
282 317
283 let elements = scopedQuerySelectorAll(subtree, actualPrefix); 318 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
284 if (elements) 319 if (elements)
285 { 320 {
286 for (let element of elements) 321 for (let element of elements)
287 { 322 {
288 if (element.textContent.includes(this._text)) 323 if (this._regexp && this._regexp.test(element.textContent))
289 yield element; 324 yield element;
290 else 325 else
291 yield null; 326 yield null;
292 } 327 }
293 } 328 }
294 } 329 }
295 }; 330 };
296 331
297 function PropsSelector(propertyExpression) 332 function PropsSelector(propertyExpression)
298 { 333 {
299 let regexpString; 334 this._regexp = makeRegExpParameter(propertyExpression, true, "i");
300 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" &&
301 propertyExpression[propertyExpression.length - 1] == "/")
302 {
303 regexpString = propertyExpression.slice(1, -1)
304 .replace("\\7B ", "{").replace("\\7D ", "}");
305 }
306 else
307 regexpString = filterToRegExp(propertyExpression);
308
309 this._regexp = new RegExp(regexpString, "i");
310 } 335 }
311 336
312 PropsSelector.prototype = { 337 PropsSelector.prototype = {
313 preferHideWithSelector: true, 338 preferHideWithSelector: true,
314 dependsOnStyles: true, 339 dependsOnStyles: true,
315 340
316 *findPropsSelectors(styles, prefix, regexp) 341 *findPropsSelectors(styles, prefix, regexp)
317 { 342 {
318 for (let style of styles) 343 for (let style of styles)
319 if (regexp.test(style.style)) 344 if (regexp && regexp.test(style.style))
320 for (let subSelector of style.subSelectors) 345 for (let subSelector of style.subSelectors)
321 { 346 {
322 if (subSelector.startsWith("*") && 347 if (subSelector.startsWith("*") &&
323 !incompletePrefixRegexp.test(prefix)) 348 !incompletePrefixRegexp.test(prefix))
324 { 349 {
325 subSelector = subSelector.substr(1); 350 subSelector = subSelector.substr(1);
326 } 351 }
327 let idx = subSelector.lastIndexOf("::"); 352 let idx = subSelector.lastIndexOf("::");
328 if (idx != -1) 353 if (idx != -1)
329 subSelector = subSelector.substr(0, idx); 354 subSelector = subSelector.substr(0, idx);
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 characterData: true, 671 characterData: true,
647 subtree: true 672 subtree: true
648 } 673 }
649 ); 674 );
650 this.document.addEventListener("load", this.onLoad.bind(this), true); 675 this.document.addEventListener("load", this.onLoad.bind(this), true);
651 } 676 }
652 } 677 }
653 }; 678 };
654 679
655 exports.ElemHideEmulation = ElemHideEmulation; 680 exports.ElemHideEmulation = ElemHideEmulation;
OLDNEW
« no previous file with comments | « no previous file | test/browser/elemHideEmulation.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld