aesthetic-utils
Advanced tools
Comparing version 2.0.0-alpha.6 to 2.0.0-rc.0
import toArray from './toArray'; | ||
export default function purgeStyles(styles) { | ||
var NON_GLOBAL_PREFIX = /^(#|\.|@)/; | ||
function containsNestedRules(rule) { | ||
return typeof CSSStyleSheet !== 'undefined' && rule instanceof CSSStyleSheet || typeof CSSMediaRule !== 'undefined' && rule instanceof CSSMediaRule || typeof CSSSupportsRule !== 'undefined' && rule instanceof CSSSupportsRule; | ||
} | ||
function deleteRule(parent, ruleToDelete) { | ||
Array.from(parent.cssRules).some(function (rule, index) { | ||
if (rule === ruleToDelete && parent.cssRules[index]) { | ||
if (typeof parent.deleteRule === 'function') { | ||
parent.deleteRule(index); | ||
} else { | ||
delete parent.cssRules[index]; | ||
} | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (process.env.NODE_ENV === 'test') { | ||
parent.cssRules = parent.cssRules.filter(Boolean); | ||
} | ||
if (parent.cssRules.length === 0 && parent instanceof CSSRule) { | ||
if (containsNestedRules(parent.parentRule)) { | ||
deleteRule(parent.parentRule, parent); | ||
} | ||
if (containsNestedRules(parent.parentStyleSheet)) { | ||
deleteRule(parent.parentStyleSheet, parent); | ||
} | ||
} | ||
} | ||
function purgeRules(parent, onlyGlobals) { | ||
if (onlyGlobals === void 0) { | ||
onlyGlobals = false; | ||
} | ||
if (!parent.cssRules) { | ||
return; | ||
} | ||
var rulesToDelete = []; | ||
Array.from(parent.cssRules).forEach(function (rule) { | ||
if (containsNestedRules(rule)) { | ||
purgeRules(rule); | ||
} | ||
if (onlyGlobals && NON_GLOBAL_PREFIX.test(rule.cssText)) { | ||
return; | ||
} | ||
rulesToDelete.push(rule); | ||
}); | ||
rulesToDelete.forEach(function (rule) { | ||
deleteRule(parent, rule); | ||
}); | ||
} | ||
export default function purgeStyles(styles, onlyGlobals) { | ||
if (onlyGlobals === void 0) { | ||
onlyGlobals = false; | ||
} | ||
toArray(styles).forEach(function (style) { | ||
style.textContent = ''; | ||
var sheet = style.sheet; | ||
if (sheet && sheet.cssRules) { | ||
Array.from(sheet.cssRules).forEach(function (rule, index) { | ||
if (sheet.cssRules[index]) { | ||
sheet.deleteRule(index); | ||
} | ||
}); | ||
if (containsNestedRules(style.sheet)) { | ||
purgeRules(style.sheet, onlyGlobals); | ||
} | ||
}); | ||
} |
@@ -1,2 +0,2 @@ | ||
export default function purgeStyles(styles: HTMLStyleElement | HTMLStyleElement[]): void; | ||
export default function purgeStyles(styles: HTMLStyleElement | HTMLStyleElement[], onlyGlobals?: boolean): void; | ||
//# sourceMappingURL=purgeStyles.d.ts.map |
@@ -10,15 +10,76 @@ "use strict"; | ||
function purgeStyles(styles) { | ||
var NON_GLOBAL_PREFIX = /^(#|\.|@)/; | ||
function containsNestedRules(rule) { | ||
return typeof CSSStyleSheet !== 'undefined' && rule instanceof CSSStyleSheet || typeof CSSMediaRule !== 'undefined' && rule instanceof CSSMediaRule || typeof CSSSupportsRule !== 'undefined' && rule instanceof CSSSupportsRule; | ||
} | ||
function deleteRule(parent, ruleToDelete) { | ||
Array.from(parent.cssRules).some(function (rule, index) { | ||
if (rule === ruleToDelete && parent.cssRules[index]) { | ||
if (typeof parent.deleteRule === 'function') { | ||
parent.deleteRule(index); | ||
} else { | ||
delete parent.cssRules[index]; | ||
} | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (process.env.NODE_ENV === 'test') { | ||
parent.cssRules = parent.cssRules.filter(Boolean); | ||
} | ||
if (parent.cssRules.length === 0 && parent instanceof CSSRule) { | ||
if (containsNestedRules(parent.parentRule)) { | ||
deleteRule(parent.parentRule, parent); | ||
} | ||
if (containsNestedRules(parent.parentStyleSheet)) { | ||
deleteRule(parent.parentStyleSheet, parent); | ||
} | ||
} | ||
} | ||
function purgeRules(parent, onlyGlobals) { | ||
if (onlyGlobals === void 0) { | ||
onlyGlobals = false; | ||
} | ||
if (!parent.cssRules) { | ||
return; | ||
} | ||
var rulesToDelete = []; | ||
Array.from(parent.cssRules).forEach(function (rule) { | ||
if (containsNestedRules(rule)) { | ||
purgeRules(rule); | ||
} | ||
if (onlyGlobals && NON_GLOBAL_PREFIX.test(rule.cssText)) { | ||
return; | ||
} | ||
rulesToDelete.push(rule); | ||
}); | ||
rulesToDelete.forEach(function (rule) { | ||
deleteRule(parent, rule); | ||
}); | ||
} | ||
function purgeStyles(styles, onlyGlobals) { | ||
if (onlyGlobals === void 0) { | ||
onlyGlobals = false; | ||
} | ||
(0, _toArray.default)(styles).forEach(function (style) { | ||
style.textContent = ''; | ||
var sheet = style.sheet; | ||
if (sheet && sheet.cssRules) { | ||
Array.from(sheet.cssRules).forEach(function (rule, index) { | ||
if (sheet.cssRules[index]) { | ||
sheet.deleteRule(index); | ||
} | ||
}); | ||
if (containsNestedRules(style.sheet)) { | ||
purgeRules(style.sheet, onlyGlobals); | ||
} | ||
}); | ||
} |
{ | ||
"name": "aesthetic-utils", | ||
"version": "2.0.0-alpha.6", | ||
"version": "2.0.0-rc.0", | ||
"description": "Utility functions for Aesthetic.", | ||
@@ -22,3 +22,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "8a8e92b75a056555209a0690098d0fa855fd1cf3" | ||
"gitHead": "fffc4bd0af4e34bc4efceb1d39f2a3dbf3b58545" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20735
399
5