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: Parse the regexp flag. Created Jan. 10, 2018, 6:01 a.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 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 const regexpRegexp = /^\/(.*)\/([gimuy]*)$/;
Manish Jethani 2018/01/10 11:22:15 We don't have to support all these flags, some of
hub 2018/01/10 18:27:22 let's restrict to "im" for the flags.
126
127 /**
128 * Make a regular expression from a text argument. If it can be parsed as a
129 * regular expression, parse it and the flags.
130 * @param {string} text the text argument.
131 * @param {?string} flag the regexp flag passed to the RegExp constructor:
132 * it overrides flags passed in the text argument.
133 * @return {RegExp} a RegExp object.
134 */
135 function makeRegExpParameter(text, flag)
Manish Jethani 2018/01/10 11:22:14 Shouldn't the parameter be called "flags" (plural)
hub 2018/01/10 18:27:21 Done.
136 {
137 let regexpString = null;
138 let match = regexpRegexp.exec(text);
139 if (match)
140 {
141 regexpString = match[1].replace("\\7B ", "{").replace("\\7D ", "}");
142 if (!flag)
Manish Jethani 2018/01/10 11:22:15 This changes the syntax of PropsSelector so that n
hub 2018/01/10 18:27:22 It is just more permissive to allow the regexp syn
143 flag = match[2];
144 }
145 else
146 regexpString = filterToRegExp(text);
147
148 return new RegExp(regexpString, flag);
Manish Jethani 2018/01/10 11:22:14 We still need to handle the error if any.
hub 2018/01/10 18:27:21 Done.
149 }
150
125 function* evaluate(chain, index, prefix, subtree, styles) 151 function* evaluate(chain, index, prefix, subtree, styles)
126 { 152 {
127 if (index >= chain.length) 153 if (index >= chain.length)
128 { 154 {
129 yield prefix; 155 yield prefix;
130 return; 156 return;
131 } 157 }
132 for (let [selector, element] of 158 for (let [selector, element] of
133 chain[index].getSelectors(prefix, subtree, styles)) 159 chain[index].getSelectors(prefix, subtree, styles))
134 { 160 {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 // :scope isn't supported on Edge, ignore error caused by it. 243 // :scope isn't supported on Edge, ignore error caused by it.
218 } 244 }
219 } 245 }
220 yield null; 246 yield null;
221 } 247 }
222 } 248 }
223 }; 249 };
224 250
225 function ContainsSelector(textContent) 251 function ContainsSelector(textContent)
226 { 252 {
227 this._text = textContent; 253 this._regexp = makeRegExpParameter(textContent);
228 } 254 }
229 255
230 ContainsSelector.prototype = { 256 ContainsSelector.prototype = {
231 requiresHiding: true, 257 requiresHiding: true,
232 258
233 *getSelectors(prefix, subtree, stylesheet) 259 *getSelectors(prefix, subtree, stylesheet)
234 { 260 {
235 for (let element of this.getElements(prefix, subtree, stylesheet)) 261 for (let element of this.getElements(prefix, subtree, stylesheet))
236 yield [makeSelector(element, ""), subtree]; 262 yield [makeSelector(element, ""), subtree];
237 }, 263 },
238 264
239 *getElements(prefix, subtree, stylesheet) 265 *getElements(prefix, subtree, stylesheet)
240 { 266 {
241 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 267 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
242 prefix + "*" : prefix; 268 prefix + "*" : prefix;
243 let elements = subtree.querySelectorAll(actualPrefix); 269 let elements = subtree.querySelectorAll(actualPrefix);
244 270
245 for (let element of elements) 271 for (let element of elements)
246 { 272 {
247 if (element.textContent.includes(this._text)) 273 if (this._regexp && this._regexp.test(element.textContent))
248 yield element; 274 yield element;
249 else 275 else
250 yield null; 276 yield null;
lainverse 2018/01/10 09:14:02 BTW, while we are at it why do we even want to yie
lainverse 2018/01/10 19:10:22 Checked. makeSelectors returns null if null is pas
251 } 277 }
252 } 278 }
253 }; 279 };
254 280
255 function PropsSelector(propertyExpression) 281 function PropsSelector(propertyExpression)
256 { 282 {
257 let regexpString; 283 this._regexp = makeRegExpParameter(propertyExpression, "i");
258 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" &&
259 propertyExpression[propertyExpression.length - 1] == "/")
260 {
261 regexpString = propertyExpression.slice(1, -1)
262 .replace("\\7B ", "{").replace("\\7D ", "}");
263 }
264 else
265 regexpString = filterToRegExp(propertyExpression);
266
267 this._regexp = new RegExp(regexpString, "i");
268 } 284 }
269 285
270 PropsSelector.prototype = { 286 PropsSelector.prototype = {
271 preferHideWithSelector: true, 287 preferHideWithSelector: true,
272 dependsOnStyles: true, 288 dependsOnStyles: true,
273 289
274 *findPropsSelectors(styles, prefix, regexp) 290 *findPropsSelectors(styles, prefix, regexp)
275 { 291 {
276 for (let style of styles) 292 for (let style of styles)
277 if (regexp.test(style.style)) 293 if (regexp.test(style.style))
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 characterData: true, 620 characterData: true,
605 subtree: true 621 subtree: true
606 } 622 }
607 ); 623 );
608 this.document.addEventListener("load", this.onLoad.bind(this), true); 624 this.document.addEventListener("load", this.onLoad.bind(this), true);
609 } 625 }
610 } 626 }
611 }; 627 };
612 628
613 exports.ElemHideEmulation = ElemHideEmulation; 629 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