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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29886555: Issue 6957 - Add generateStyleSheetForDomain function to ElemHide module (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Sept. 20, 2018, 12:22 p.m.
Right Patch Set: Rebase, remove getSelectorsForDomain Created Sept. 27, 2018, 3:55 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/elemHide.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 filtersByDomain.delete(domain); 268 filtersByDomain.delete(domain);
269 } 269 }
270 } 270 }
271 } 271 }
272 272
273 knownFilters.delete(filter); 273 knownFilters.delete(filter);
274 filterNotifier.emit("elemhideupdate"); 274 filterNotifier.emit("elemhideupdate");
275 }, 275 },
276 276
277 /** 277 /**
278 * Determines from the current filter list which selectors should be applied
279 * on a particular host name.
280 * @param {string} domain
281 * @param {boolean} [specificOnly] true if generic filters should not apply.
282 * @returns {string[]} List of selectors.
283 */
284 getSelectorsForDomain(domain, specificOnly = false)
285 {
286 let selectors = getConditionalSelectorsForDomain(domain, specificOnly);
287
288 if (!specificOnly)
289 selectors = getUnconditionalSelectors().concat(selectors);
290
291 return selectors;
292 },
293
294 /**
295 * @typedef {object} ElemHideStyleSheet 278 * @typedef {object} ElemHideStyleSheet
296 * @property {string} code CSS code. 279 * @property {string} code CSS code.
297 * @property {Array.<string>} selectors List of selectors. 280 * @property {Array.<string>} selectors List of selectors.
298 */ 281 */
299 282
300 /** 283 /**
301 * Generates a style sheet for a given domain based on the current set of 284 * Generates a style sheet for a given domain based on the current set of
302 * filters. 285 * filters.
303 * 286 *
304 * @param {string} domain The domain. 287 * @param {string} domain The domain.
305 * @param {boolean} [specificOnly=false] Whether selectors from generic 288 * @param {boolean} [specificOnly=false] Whether selectors from generic
306 * filters should be included. 289 * filters should be included.
307 * 290 *
308 * @returns {ElemHideStyleSheet} An object containing the CSS code and the 291 * @returns {ElemHideStyleSheet} An object containing the CSS code and the
309 * list of selectors. 292 * list of selectors.
310 */ 293 */
311 generateStyleSheetForDomain(domain, specificOnly = false) 294 generateStyleSheetForDomain(domain, specificOnly = false)
Manish Jethani 2018/09/20 12:27:24 The idea is that calling this function will be fas
Jon Sonesen 2018/09/23 18:01:59 Acknowledged.
312 { 295 {
313 let selectors = getConditionalSelectorsForDomain(domain, specificOnly); 296 let selectors = getConditionalSelectorsForDomain(domain, specificOnly);
314 let code = (specificOnly ? "" : getDefaultStyleSheet()) + 297 let code = specificOnly ? createStyleSheet(selectors) :
315 createStyleSheet(selectors); 298 (getDefaultStyleSheet() + createStyleSheet(selectors));
Jon Sonesen 2018/09/23 18:01:59 Regarding the use of this ternary operation, I won
Manish Jethani 2018/09/24 11:51:14 This function is supposed to be very performance c
Jon Sonesen 2018/09/24 15:09:44 Ah yeah, that's a good point here. My bad, haha I
Manish Jethani 2018/09/27 15:58:29 So I tested this by the way, it seems the Patch Se
Manish Jethani 2018/09/27 15:58:29 Acknowledged.
316 299
317 if (!specificOnly) 300 if (!specificOnly)
318 selectors = getUnconditionalSelectors().concat(selectors); 301 selectors = getUnconditionalSelectors().concat(selectors);
319 302
320 return {code, selectors}; 303 return {code, selectors};
321 } 304 }
322 }; 305 };
323 306
324 /** 307 /**
325 * Splits a list of selectors into groups determined by the value of 308 * Splits a list of selectors into groups determined by the value of
(...skipping 13 matching lines...) Expand all
339 // calculate the sizes of the selectors and divide them up accordingly, but 322 // calculate the sizes of the selectors and divide them up accordingly, but
340 // this approach is more efficient and has worked well in practice. In theory 323 // this approach is more efficient and has worked well in practice. In theory
341 // this could still lead to some selectors not working on Chromium, but it is 324 // this could still lead to some selectors not working on Chromium, but it is
342 // highly unlikely. 325 // highly unlikely.
343 // See issue #6298 and https://crbug.com/804179 326 // See issue #6298 and https://crbug.com/804179
344 for (let i = 0; i < selectors.length; i += selectorGroupSize) 327 for (let i = 0; i < selectors.length; i += selectorGroupSize)
345 yield selectors.slice(i, i + selectorGroupSize); 328 yield selectors.slice(i, i + selectorGroupSize);
346 } 329 }
347 330
348 /** 331 /**
349 * Creates element hiding CSS rules for a given list of selectors. Each rule 332 * Creates an element hiding CSS rule for a given list of selectors.
350 * contains no more than the maximum number of selectors as determined by the
351 * value of <code>{@link selectorGroupSize}</code>.
352 * 333 *
353 * @param {Array.<string>} selectors 334 * @param {Array.<string>} selectors
354 * @yields {string} 335 * @returns {string}
355 */ 336 */
356 function* createRules(selectors) 337 function createRule(selectors)
357 { 338 {
358 for (let selectorGroup of splitSelectors(selectors)) 339 let rule = "";
359 yield selectorGroup.join(", ") + " {display: none !important;}"; 340
341 for (let i = 0; i < selectors.length - 1; i++)
342 rule += selectors[i] + ", ";
343
344 rule += selectors[selectors.length - 1] + " {display: none !important;}\n";
345
346 return rule;
360 } 347 }
361 348
362 /** 349 /**
363 * Creates an element hiding CSS style sheet from a given list of selectors. 350 * Creates an element hiding CSS style sheet from a given list of selectors.
364 * @param {Array.<string>} selectors 351 * @param {Array.<string>} selectors
365 * @returns {string} 352 * @returns {string}
366 */ 353 */
367 function createStyleSheet(selectors) 354 function createStyleSheet(selectors)
368 { 355 {
369 let styleSheet = ""; 356 let styleSheet = "";
370 357
371 for (let rule of createRules(selectors)) 358 for (let selectorGroup of splitSelectors(selectors))
372 styleSheet += rule + "\n"; 359 styleSheet += createRule(selectorGroup);
373 360
374 return styleSheet; 361 return styleSheet;
375 } 362 }
376 363
377 exports.createStyleSheet = createStyleSheet; 364 exports.createStyleSheet = createStyleSheet;
LEFTRIGHT
« no previous file | test/elemHide.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld