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: Use RegExp.test() Created Nov. 22, 2017, 2:34 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') | test/browser/elemHideEmulation.js » ('J')
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 104 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 /**
126 * Check if a text argument is a regexp.
127 * @param {string} text argument.
128 * @return {string} an unescaped RegExp string. null if it is not a
kzar 2018/01/08 15:29:35 Shouldn't this be `{?string}`?
hub 2018/01/08 16:29:51 The reference I use for JSDoc doesn't mention this
kzar 2018/01/08 16:56:06 Well the page you linked says this "@returns [{typ
hub 2018/01/08 17:53:13 Done
129 * regexp, ie not surrounded by '/'.
130 */
131 function checkRegExpParameter(text)
132 {
133 let regexpString = null;
134 if (text.length >= 2 && text[0] == "/" &&
135 text[text.length - 1] == "/")
136 {
137 regexpString = text.slice(1, -1)
138 .replace("\\7B ", "{").replace("\\7D ", "}");
139 }
140 return regexpString;
141 }
142
125 function* evaluate(chain, index, prefix, subtree, styles) 143 function* evaluate(chain, index, prefix, subtree, styles)
126 { 144 {
127 if (index >= chain.length) 145 if (index >= chain.length)
128 { 146 {
129 yield prefix; 147 yield prefix;
130 return; 148 return;
131 } 149 }
132 for (let [selector, element] of 150 for (let [selector, element] of
133 chain[index].getSelectors(prefix, subtree, styles)) 151 chain[index].getSelectors(prefix, subtree, styles))
134 { 152 {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 // :scope isn't supported on Edge, ignore error caused by it. 235 // :scope isn't supported on Edge, ignore error caused by it.
218 } 236 }
219 } 237 }
220 yield null; 238 yield null;
221 } 239 }
222 } 240 }
223 }; 241 };
224 242
225 function ContainsSelector(textContent) 243 function ContainsSelector(textContent)
226 { 244 {
227 this._text = textContent; 245 let regexpString = checkRegExpParameter(textContent);
246 if (regexpString)
247 this._regexp = new RegExp(regexpString);
kzar 2018/01/08 15:35:01 Probably a dumb question, but how come with the Pr
hub 2018/01/08 16:29:51 PropsSelector search for the CSS properties using
hub 2018/01/10 06:05:17 The newer revision of the patch changed this as re
248 else
249 this._text = textContent;
228 } 250 }
229 251
230 ContainsSelector.prototype = { 252 ContainsSelector.prototype = {
231 requiresHiding: true, 253 requiresHiding: true,
232 254
255 match(element)
256 {
257 if (this._regexp)
258 return this._regexp.test(element.textContent);
259
260 return element.textContent.includes(this._text);
261 },
262
233 *getSelectors(prefix, subtree, stylesheet) 263 *getSelectors(prefix, subtree, stylesheet)
234 { 264 {
235 for (let element of this.getElements(prefix, subtree, stylesheet)) 265 for (let element of this.getElements(prefix, subtree, stylesheet))
236 yield [makeSelector(element, ""), subtree]; 266 yield [makeSelector(element, ""), subtree];
237 }, 267 },
238 268
239 *getElements(prefix, subtree, stylesheet) 269 *getElements(prefix, subtree, stylesheet)
240 { 270 {
241 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 271 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
242 prefix + "*" : prefix; 272 prefix + "*" : prefix;
243 let elements = subtree.querySelectorAll(actualPrefix); 273 let elements = subtree.querySelectorAll(actualPrefix);
244 274
245 for (let element of elements) 275 for (let element of elements)
246 { 276 {
247 if (element.textContent.includes(this._text)) 277 if (this.match(element))
248 yield element; 278 yield element;
249 else 279 else
250 yield null; 280 yield null;
251 } 281 }
252 } 282 }
253 }; 283 };
254 284
255 function PropsSelector(propertyExpression) 285 function PropsSelector(propertyExpression)
256 { 286 {
257 let regexpString; 287 let regexpString = checkRegExpParameter(propertyExpression);
258 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && 288 if (!regexpString)
259 propertyExpression[propertyExpression.length - 1] == "/")
260 {
261 regexpString = propertyExpression.slice(1, -1)
262 .replace("\\7B ", "{").replace("\\7D ", "}");
263 }
264 else
265 regexpString = filterToRegExp(propertyExpression); 289 regexpString = filterToRegExp(propertyExpression);
266 290
267 this._regexp = new RegExp(regexpString, "i"); 291 this._regexp = new RegExp(regexpString, "i");
268 } 292 }
269 293
270 PropsSelector.prototype = { 294 PropsSelector.prototype = {
271 preferHideWithSelector: true, 295 preferHideWithSelector: true,
272 dependsOnStyles: true, 296 dependsOnStyles: true,
273 297
274 *findPropsSelectors(styles, prefix, regexp) 298 *findPropsSelectors(styles, prefix, regexp)
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 characterData: true, 628 characterData: true,
605 subtree: true 629 subtree: true
606 } 630 }
607 ); 631 );
608 this.document.addEventListener("load", this.onLoad.bind(this), true); 632 this.document.addEventListener("load", this.onLoad.bind(this), true);
609 } 633 }
610 } 634 }
611 }; 635 };
612 636
613 exports.ElemHideEmulation = ElemHideEmulation; 637 exports.ElemHideEmulation = ElemHideEmulation;
OLDNEW
« no previous file with comments | « no previous file | test/browser/elemHideEmulation.js » ('j') | test/browser/elemHideEmulation.js » ('J')

Powered by Google App Engine
This is Rietveld