| LEFT | RIGHT |
| 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 let knownExceptionDomains = new Set(); | 91 let knownExceptionDomains = new Set(); |
| 92 | 92 |
| 93 /** | 93 /** |
| 94 * Returns the suffix of the given domain that is known. If no suffix is known, | 94 * Returns the suffix of the given domain that is known. If no suffix is known, |
| 95 * an empty string is returned. | 95 * an empty string is returned. |
| 96 * @param {?string} domain | 96 * @param {?string} domain |
| 97 * @returns {string} | 97 * @returns {string} |
| 98 */ | 98 */ |
| 99 function getKnownSuffix(domain) | 99 function getKnownSuffix(domain) |
| 100 { | 100 { |
| 101 if (domain[domain.length - 1] == ".") | |
| 102 domain = domain.replace(/\.+$/, ""); | |
| 103 | |
| 104 domain = domain.toLowerCase(); | |
| 105 | |
| 106 while (domain && !filtersByDomain.has(domain) && | 101 while (domain && !filtersByDomain.has(domain) && |
| 107 !knownExceptionDomains.has(domain)) | 102 !knownExceptionDomains.has(domain)) |
| 108 { | 103 { |
| 109 let index = domain.indexOf("."); | 104 let index = domain.indexOf("."); |
| 110 domain = index == -1 ? "" : domain.substring(index + 1); | 105 domain = index == -1 ? "" : domain.substring(index + 1); |
| 111 } | 106 } |
| 112 | 107 |
| 113 return domain; | 108 return domain; |
| 114 } | 109 } |
| 115 | 110 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 140 function getUnconditionalSelectors() | 135 function getUnconditionalSelectors() |
| 141 { | 136 { |
| 142 if (!unconditionalSelectors) | 137 if (!unconditionalSelectors) |
| 143 unconditionalSelectors = [...filterBySelector.keys()]; | 138 unconditionalSelectors = [...filterBySelector.keys()]; |
| 144 | 139 |
| 145 return unconditionalSelectors; | 140 return unconditionalSelectors; |
| 146 } | 141 } |
| 147 | 142 |
| 148 /** | 143 /** |
| 149 * Returns the list of selectors that apply on a given domain from the subset | 144 * Returns the list of selectors that apply on a given domain from the subset |
| 150 * of filters that do not apply unconditionally on all domains. If no domain is | 145 * of filters that do not apply unconditionally on all domains. |
| 151 * given, includes only selectors from generic filters. | 146 * |
| 152 * | 147 * @param {string} domain The domain. |
| 153 * @param {string} [domain] The domain. | 148 * @param {boolean} specificOnly Whether selectors from generic filters should |
| 154 * @param {boolean} [specificOnly=false] Whether selectors from generic filters | 149 * be included. |
| 155 * should be included. | |
| 156 * | 150 * |
| 157 * @returns {Array.<string>} The list of selectors. | 151 * @returns {Array.<string>} The list of selectors. |
| 158 */ | 152 */ |
| 159 function getConditionalSelectors(domain = "", specificOnly = false) | 153 function getConditionalSelectors(domain, specificOnly) |
| 160 { | 154 { |
| 161 let selectors = []; | 155 let selectors = []; |
| 162 | 156 |
| 163 let excluded = new Set(); | 157 let excluded = new Set(); |
| 164 let currentDomain = domain; | 158 let currentDomain = domain; |
| 165 | 159 |
| 166 // This code is a performance hot-spot, which is why we've made certain | 160 // This code is a performance hot-spot, which is why we've made certain |
| 167 // micro-optimisations. Please be careful before making changes. | 161 // micro-optimisations. Please be careful before making changes. |
| 168 while (true) | 162 while (true) |
| 169 { | 163 { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 | 209 |
| 216 /** | 210 /** |
| 217 * Returns the common style sheet that applies on all unknown domains. | 211 * Returns the common style sheet that applies on all unknown domains. |
| 218 * @returns {string} | 212 * @returns {string} |
| 219 */ | 213 */ |
| 220 function getCommonStyleSheet() | 214 function getCommonStyleSheet() |
| 221 { | 215 { |
| 222 if (!commonStyleSheet) | 216 if (!commonStyleSheet) |
| 223 { | 217 { |
| 224 commonStyleSheet = getDefaultStyleSheet() + | 218 commonStyleSheet = getDefaultStyleSheet() + |
| 225 createStyleSheet(getConditionalSelectors()); | 219 createStyleSheet(getConditionalSelectors("", false)); |
| 226 } | 220 } |
| 227 | 221 |
| 228 return commonStyleSheet; | 222 return commonStyleSheet; |
| 229 } | 223 } |
| 230 | 224 |
| 231 ElemHideExceptions.on("added", ({domains, selector}) => | 225 ElemHideExceptions.on("added", ({domains, selector}) => |
| 232 { | 226 { |
| 233 commonStyleSheet = null; | 227 commonStyleSheet = null; |
| 234 | 228 |
| 235 if (domains) | 229 if (domains) |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 * | 357 * |
| 364 * @param {string} domain The domain. | 358 * @param {string} domain The domain. |
| 365 * @param {boolean} [specificOnly=false] Whether selectors from generic | 359 * @param {boolean} [specificOnly=false] Whether selectors from generic |
| 366 * filters should be included. | 360 * filters should be included. |
| 367 * | 361 * |
| 368 * @returns {ElemHideStyleSheet} An object containing the CSS code and the | 362 * @returns {ElemHideStyleSheet} An object containing the CSS code and the |
| 369 * list of selectors. | 363 * list of selectors. |
| 370 */ | 364 */ |
| 371 generateStyleSheetForDomain(domain, specificOnly = false) | 365 generateStyleSheetForDomain(domain, specificOnly = false) |
| 372 { | 366 { |
| 373 let knownSuffix = domain ? getKnownSuffix(domain) : ""; | 367 let code = null; |
| 374 | 368 let selectors = null; |
| 375 let selectors = getConditionalSelectors(knownSuffix, specificOnly); | 369 |
| 376 let code = specificOnly ? createStyleSheet(selectors) : | 370 if (domain[domain.length - 1] == ".") |
| 377 knownSuffix == "" ? getCommonStyleSheet() : | 371 domain = domain.replace(/\.+$/, ""); |
| 378 (getDefaultStyleSheet() + createStyleSheet(selectors)); | 372 |
| 379 | 373 domain = domain.toLowerCase(); |
| 380 if (!specificOnly) | 374 |
| 375 if (specificOnly) |
| 376 { |
| 377 selectors = getConditionalSelectors(domain, true); |
| 378 code = createStyleSheet(selectors); |
| 379 } |
| 380 else |
| 381 { |
| 382 let knownSuffix = getKnownSuffix(domain); |
| 383 |
| 384 selectors = getConditionalSelectors(knownSuffix, false); |
| 385 code = knownSuffix == "" ? getCommonStyleSheet() : |
| 386 (getDefaultStyleSheet() + createStyleSheet(selectors)); |
| 387 |
| 381 selectors = getUnconditionalSelectors().concat(selectors); | 388 selectors = getUnconditionalSelectors().concat(selectors); |
| 389 } |
| 382 | 390 |
| 383 return {code, selectors}; | 391 return {code, selectors}; |
| 384 } | 392 } |
| 385 }; | 393 }; |
| 386 | 394 |
| 387 /** | 395 /** |
| 388 * Splits a list of selectors into groups determined by the value of | 396 * Splits a list of selectors into groups determined by the value of |
| 389 * <code>{@link selectorGroupSize}</code>. | 397 * <code>{@link selectorGroupSize}</code>. |
| 390 * | 398 * |
| 391 * @param {Array.<string>} selectors | 399 * @param {Array.<string>} selectors |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 { | 443 { |
| 436 let styleSheet = ""; | 444 let styleSheet = ""; |
| 437 | 445 |
| 438 for (let selectorGroup of splitSelectors(selectors)) | 446 for (let selectorGroup of splitSelectors(selectors)) |
| 439 styleSheet += createRule(selectorGroup); | 447 styleSheet += createRule(selectorGroup); |
| 440 | 448 |
| 441 return styleSheet; | 449 return styleSheet; |
| 442 } | 450 } |
| 443 | 451 |
| 444 exports.createStyleSheet = createStyleSheet; | 452 exports.createStyleSheet = createStyleSheet; |
| LEFT | RIGHT |