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: Check regexp isn't null Created Jan. 10, 2018, 6:53 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 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 = /^\/(.*)\/([im]*)$/;
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} flags the regexp flags passed to the RegExp constructor:
132 * it overrides flags passed in the text argument.
133 * @return {?RegExp} a RegExp object or null in case of error.
134 */
135 function makeRegExpParameter(text, flags)
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 (!flags)
143 flags = match[2];
144 }
145 else
146 regexpString = filterToRegExp(text);
147
148 try
149 {
150 return new RegExp(regexpString, flags);
151 }
152 catch (e)
153 {
154 }
155 return null;
156 }
157
125 function* evaluate(chain, index, prefix, subtree, styles) 158 function* evaluate(chain, index, prefix, subtree, styles)
126 { 159 {
127 if (index >= chain.length) 160 if (index >= chain.length)
128 { 161 {
129 yield prefix; 162 yield prefix;
130 return; 163 return;
131 } 164 }
132 for (let [selector, element] of 165 for (let [selector, element] of
133 chain[index].getSelectors(prefix, subtree, styles)) 166 chain[index].getSelectors(prefix, subtree, styles))
134 { 167 {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 // :scope isn't supported on Edge, ignore error caused by it. 250 // :scope isn't supported on Edge, ignore error caused by it.
218 } 251 }
219 } 252 }
220 yield null; 253 yield null;
221 } 254 }
222 } 255 }
223 }; 256 };
224 257
225 function ContainsSelector(textContent) 258 function ContainsSelector(textContent)
226 { 259 {
227 this._text = textContent; 260 this._regexp = makeRegExpParameter(textContent);
228 } 261 }
229 262
230 ContainsSelector.prototype = { 263 ContainsSelector.prototype = {
231 requiresHiding: true, 264 requiresHiding: true,
232 265
233 *getSelectors(prefix, subtree, stylesheet) 266 *getSelectors(prefix, subtree, stylesheet)
234 { 267 {
235 for (let element of this.getElements(prefix, subtree, stylesheet)) 268 for (let element of this.getElements(prefix, subtree, stylesheet))
236 yield [makeSelector(element, ""), subtree]; 269 yield [makeSelector(element, ""), subtree];
237 }, 270 },
238 271
239 *getElements(prefix, subtree, stylesheet) 272 *getElements(prefix, subtree, stylesheet)
240 { 273 {
241 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 274 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
242 prefix + "*" : prefix; 275 prefix + "*" : prefix;
243 let elements = subtree.querySelectorAll(actualPrefix); 276 let elements = subtree.querySelectorAll(actualPrefix);
244 277
245 for (let element of elements) 278 for (let element of elements)
246 { 279 {
247 if (element.textContent.includes(this._text)) 280 if (this._regexp && this._regexp.test(element.textContent))
248 yield element; 281 yield element;
249 else 282 else
250 yield null; 283 yield null;
251 } 284 }
252 } 285 }
253 }; 286 };
254 287
255 function PropsSelector(propertyExpression) 288 function PropsSelector(propertyExpression)
256 { 289 {
257 let regexpString; 290 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 } 291 }
269 292
270 PropsSelector.prototype = { 293 PropsSelector.prototype = {
271 preferHideWithSelector: true, 294 preferHideWithSelector: true,
272 dependsOnStyles: true, 295 dependsOnStyles: true,
273 296
274 *findPropsSelectors(styles, prefix, regexp) 297 *findPropsSelectors(styles, prefix, regexp)
275 { 298 {
276 for (let style of styles) 299 for (let style of styles)
277 if (regexp.test(style.style)) 300 if (regexp && regexp.test(style.style))
278 for (let subSelector of style.subSelectors) 301 for (let subSelector of style.subSelectors)
279 { 302 {
280 if (subSelector.startsWith("*") && 303 if (subSelector.startsWith("*") &&
281 !incompletePrefixRegexp.test(prefix)) 304 !incompletePrefixRegexp.test(prefix))
282 { 305 {
283 subSelector = subSelector.substr(1); 306 subSelector = subSelector.substr(1);
284 } 307 }
285 let idx = subSelector.lastIndexOf("::"); 308 let idx = subSelector.lastIndexOf("::");
286 if (idx != -1) 309 if (idx != -1)
287 subSelector = subSelector.substr(0, idx); 310 subSelector = subSelector.substr(0, idx);
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 characterData: true, 627 characterData: true,
605 subtree: true 628 subtree: true
606 } 629 }
607 ); 630 );
608 this.document.addEventListener("load", this.onLoad.bind(this), true); 631 this.document.addEventListener("load", this.onLoad.bind(this), true);
609 } 632 }
610 } 633 }
611 }; 634 };
612 635
613 exports.ElemHideEmulation = ElemHideEmulation; 636 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