Ticket #6428: withExceptions.patch

File withExceptions.patch, 3.9 KB (added by mjethani, 13 months ago)

Apply on revision 107b12edc115

  • lib/elemHide.js

    diff --git a/lib/elemHide.js b/lib/elemHide.js
    a b  
    142142 
    143143/** 
    144144 * Returns the list of selectors that apply on a given domain from the subset 
    145145 * of filters that do not apply unconditionally on all domains. 
    146146 * 
    147147 * @param {string} domain The domain. 
    148148 * @param {boolean} specificOnly Whether selectors from generic filters should 
    149149 *   be included. 
     150 * @param {boolean} [withExceptions=false] Whether exceptions should be 
     151 *   included. 
    150152 * 
    151153 * @returns {Array.<string>} The list of selectors. 
    152154 */ 
    153 function getConditionalSelectors(domain, specificOnly) 
     155function getConditionalSelectors(domain, specificOnly, withExceptions = false) 
    154156{ 
    155157  let selectors = []; 
     158  let exceptions = []; 
    156159 
    157160  let excluded = new Set(); 
    158161  let currentDomain = domain; 
    159162 
    160163  // This code is a performance hot-spot, which is why we've made certain 
    161164  // micro-optimisations. Please be careful before making changes. 
    162165  while (true) 
    163166  { 
     
    171174      { 
    172175        if (!isIncluded) 
    173176        { 
    174177          excluded.add(filter); 
    175178        } 
    176179        else 
    177180        { 
    178181          let {selector} = filter; 
    179           if ((excluded.size == 0 || !excluded.has(filter)) && 
    180               !ElemHideExceptions.getException(selector, domain)) 
     182          if (excluded.size == 0 || !excluded.has(filter)) 
    181183          { 
    182             selectors.push(selector); 
     184            let exception = ElemHideExceptions.getException(selector, domain); 
     185            if (!exception) 
     186              selectors.push(selector); 
     187            else if (withExceptions) 
     188              exceptions.push(exception); 
    183189          } 
    184190        } 
    185191      } 
    186192    } 
    187193 
    188194    if (currentDomain == "") 
    189195      break; 
    190196 
    191197    let nextDot = currentDomain.indexOf("."); 
    192198    currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 
    193199  } 
    194200 
     201  if (withExceptions) 
     202    selectors.exceptions = exceptions; 
     203 
    195204  return selectors; 
    196205} 
    197206 
    198207/** 
    199208 * Returns the default style sheet that applies on all domains. 
    200209 * @returns {string} 
    201210 */ 
    202211function getDefaultStyleSheet() 
     
    355364   * Generates a style sheet for a given domain based on the current set of 
    356365   * filters. 
    357366   * 
    358367   * @param {string} domain The domain. 
    359368   * @param {boolean} [specificOnly=false] Whether selectors from generic 
    360369   *   filters should be included. 
    361370   * @param {boolean} [includeSelectors=false] Whether the return value should 
    362371   *   include a separate list of selectors. 
     372   * @param {boolean} [withExceptions=false] Whether the selectors should 
     373   *   include exceptions. Ignored if <code>includeSelectors</code> is 
     374   *   <code>false</code>. 
    363375   * 
    364376   * @returns {ElemHideStyleSheet} An object containing the CSS code and the 
    365377   *   list of selectors. 
    366378   */ 
    367379  generateStyleSheetForDomain(domain, specificOnly = false, 
    368                               includeSelectors = false) 
     380                              includeSelectors = false, withExceptions = false) 
    369381  { 
    370382    let code = null; 
    371383    let selectors = null; 
    372384 
    373385    if (domain[domain.length - 1] == ".") 
    374386      domain = domain.replace(/\.+$/, ""); 
    375387 
    376388    domain = domain.toLowerCase(); 
    377389 
    378390    if (specificOnly) 
    379391    { 
    380       selectors = getConditionalSelectors(domain, true); 
     392      selectors = getConditionalSelectors(domain, true, 
     393                                          includeSelectors && withExceptions); 
    381394      code = createStyleSheet(selectors); 
    382395    } 
    383396    else 
    384397    { 
    385398      let knownSuffix = getKnownSuffix(domain); 
    386399 
    387400      if (includeSelectors) 
    388401      { 
    389         selectors = getConditionalSelectors(knownSuffix, false); 
     402        selectors = getConditionalSelectors(knownSuffix, false, withExceptions); 
    390403        code = knownSuffix == "" ? getCommonStyleSheet() : 
    391404                 (getDefaultStyleSheet() + createStyleSheet(selectors)); 
    392405 
    393406        selectors = getUnconditionalSelectors().concat(selectors); 
    394407      } 
    395408      else 
    396409      { 
    397410        code = knownSuffix == "" ? getCommonStyleSheet() :