Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: chrome/content/elemHideEmulation.js

Issue 29448560: Issue 5249 - Implement :-abp-contains() (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Use includes() and improve test. Created June 28, 2017, 4:11 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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 element, styles); 223 element, styles);
224 for (let selector of iter) 224 for (let selector of iter)
225 // we insert a space between the two. It becomes a no-op if selector 225 // we insert a space between the two. It becomes a no-op if selector
226 // doesn't have a combinator 226 // doesn't have a combinator
227 if (subtree.querySelector(selector)) 227 if (subtree.querySelector(selector))
228 yield element; 228 yield element;
229 } 229 }
230 } 230 }
231 }; 231 };
232 232
233 function ContainsSelector(textContent)
234 {
235 this._text = textContent;
236 }
237
238 ContainsSelector.prototype = {
239 requiresHiding: true,
240
241 *getSelectors(prefix, subtree, stylesheet)
242 {
243 for (let element of this.getElements(prefix, subtree, stylesheet))
244 yield [makeSelector(element, ""), subtree];
245 },
246
247 *getElements(prefix, subtree, stylesheet)
248 {
249 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
250 prefix + "*" : prefix;
251 let elements = subtree.querySelectorAll(actualPrefix);
252 for (let element of elements)
253 if (element.textContent.includes(this._text))
254 yield element;
255 }
256 };
257
233 function PropsSelector(propertyExpression) 258 function PropsSelector(propertyExpression)
234 { 259 {
235 let regexpString; 260 let regexpString;
236 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && 261 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" &&
237 propertyExpression[propertyExpression.length - 1] == "/") 262 propertyExpression[propertyExpression.length - 1] == "/")
238 { 263 {
239 regexpString = propertyExpression.slice(1, -1) 264 regexpString = propertyExpression.slice(1, -1)
240 .replace("\\x7B ", "{").replace("\\x7D ", "}"); 265 .replace("\\x7B ", "{").replace("\\x7D ", "}");
241 } 266 }
242 else 267 else
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 } 341 }
317 if (match[1] == "properties") 342 if (match[1] == "properties")
318 selectors.push(new PropsSelector(content.text)); 343 selectors.push(new PropsSelector(content.text));
319 else if (match[1] == "has") 344 else if (match[1] == "has")
320 { 345 {
321 let hasSelectors = this.parseSelector(content.text); 346 let hasSelectors = this.parseSelector(content.text);
322 if (hasSelectors == null) 347 if (hasSelectors == null)
323 return null; 348 return null;
324 selectors.push(new HasSelector(hasSelectors)); 349 selectors.push(new HasSelector(hasSelectors));
325 } 350 }
351 else if (match[1] == "contains")
352 selectors.push(new ContainsSelector(content.text));
326 else 353 else
327 { 354 {
328 // this is an error, can't parse selector. 355 // this is an error, can't parse selector.
329 this.window.console.error( 356 this.window.console.error(
330 new SyntaxError("Failed to parse Adblock Plus " + 357 new SyntaxError("Failed to parse Adblock Plus " +
331 `selector ${selector}, invalid ` + 358 `selector ${selector}, invalid ` +
332 `pseudo-class :-abp-${match[1]}().`)); 359 `pseudo-class :-abp-${match[1]}().`));
333 return null; 360 return null;
334 } 361 }
335 362
336 let suffix = this.parseSelector(selector.substr(content.end + 1)); 363 let suffix = this.parseSelector(selector.substr(content.end + 1));
337 if (suffix == null) 364 if (suffix == null)
338 return null; 365 return null;
339 366
340 selectors.push(...suffix); 367 selectors.push(...suffix);
341 368
369 if (selectors.length == 1 && selectors[0] instanceof ContainsSelector)
370 {
371 this.window.console.error(
372 new SyntaxError("Failed to parse Adblock Plus " +
373 `selector ${selector}, can't ` +
374 "have a lonely :-abp-contains()."));
375 return null;
376 }
342 return selectors; 377 return selectors;
343 }, 378 },
344 379
345 addSelectors(stylesheets) 380 addSelectors(stylesheets)
346 { 381 {
347 let selectors = []; 382 let selectors = [];
348 let selectorFilters = []; 383 let selectorFilters = [];
349 384
350 let elements = []; 385 let elements = [];
351 let elementFilters = []; 386 let elementFilters = [];
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 455
421 if (this.patterns.length > 0) 456 if (this.patterns.length > 0)
422 { 457 {
423 let {document} = this.window; 458 let {document} = this.window;
424 this.addSelectors(document.styleSheets); 459 this.addSelectors(document.styleSheets);
425 document.addEventListener("load", this.onLoad.bind(this), true); 460 document.addEventListener("load", this.onLoad.bind(this), true);
426 } 461 }
427 }); 462 });
428 } 463 }
429 }; 464 };
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