UNPKG

@helpscout/cyan

Version:

Cypress-like Testing for React + JSDOM

50 lines (41 loc) 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDocumentCSS = exports.getCSSRulesFromStyleSheet = exports.getCSSTextFromRule = void 0; var getCSSTextFromRule = function (rule) { var style = rule.style, selectorText = rule.selectorText; if (!style) return ''; var _importants = style._importants; var styleKeys = Object.keys(style).filter(function (prop) { return !isNaN(Number(prop)); }); var ruleKeys = styleKeys.map(function (key) { return style[key]; }); var cssText = ruleKeys.reduce(function (cssText, key) { var baseRule = key + ": " + style[key]; if (_importants[key]) { baseRule += ' !important'; } return cssText + " " + baseRule + ";"; }, ''); return selectorText + " { " + cssText.trim() + " }"; }; exports.getCSSTextFromRule = getCSSTextFromRule; var getCSSRulesFromStyleSheet = function (styleSheet) { var rules = styleSheet.cssRules || []; return rules.reduce(function (styles, rule) { return styles + " " + getCSSTextFromRule(rule); }, ''); }; exports.getCSSRulesFromStyleSheet = getCSSRulesFromStyleSheet; var getDocumentCSS = function () { var styleSheets = Object.values(document.styleSheets); var css = styleSheets.reduce(function (styles, styleSheet) { return styles + " " + getCSSRulesFromStyleSheet(styleSheet); }, ''); return css; }; exports.getDocumentCSS = getDocumentCSS;