1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.transformCss = transformCss;
|
9 | exports.toRules = toRules;
|
10 | exports.isKeyframesSelector = isKeyframesSelector;
|
11 | exports.default = void 0;
|
12 |
|
13 | var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
14 |
|
15 | var _parseCss = require("./parseCss.js");
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function transformCss(cssText, transform) {
|
41 | var node = (0, _parseCss.parseCss)(cssText);
|
42 |
|
43 | if (typeof transform === 'function') {
|
44 | node = transformNode(node, transform);
|
45 | }
|
46 |
|
47 | return toCssText(node);
|
48 | }
|
49 |
|
50 | function isKeyframesSelector(rule) {
|
51 | return rule.parent && rule.parent.type === _parseCss.ruleTypes.keyframes;
|
52 | }
|
53 |
|
54 | function toRules(cssText) {
|
55 | var node = (0, _parseCss.parseCss)(cssText);
|
56 | var rules = [];
|
57 |
|
58 | if (node.rules && node.rules.length > 0) {
|
59 | rules = node.rules.map(function (rule) {
|
60 | return toCssText(rule);
|
61 | });
|
62 | } else {
|
63 | var _cssText = toCssText(node);
|
64 |
|
65 | if (_cssText) {
|
66 | rules = [_cssText];
|
67 | }
|
68 | }
|
69 |
|
70 | return rules;
|
71 | }
|
72 |
|
73 | function transformNode(node, transform) {
|
74 | if (!node) {
|
75 | return;
|
76 | }
|
77 |
|
78 | if (node.type === _parseCss.ruleTypes.style) {
|
79 | return transform(node);
|
80 | }
|
81 |
|
82 | var rules = node.rules || [];
|
83 | var transformed = (0, _objectSpread2.default)({}, node);
|
84 | transformed.rules = rules.map(function (rule) {
|
85 | return transformNode(rule, transform);
|
86 | });
|
87 | return transformed;
|
88 | }
|
89 |
|
90 | function toCssText(node, text) {
|
91 | var cssText = '';
|
92 | var result = text || '';
|
93 |
|
94 | if (node.rules && node.rules.length > 0) {
|
95 | cssText = node.rules.map(function (rule) {
|
96 | return toCssText(rule, cssText);
|
97 | }).join('\n');
|
98 | } else {
|
99 | cssText = node.cssText.trim();
|
100 |
|
101 | if (cssText) {
|
102 | cssText = " ".concat(cssText, "\n");
|
103 | }
|
104 | }
|
105 |
|
106 | if (cssText) {
|
107 | var prefix = node.selector ? "".concat(node.selector, " {\n") : '';
|
108 | var suffix = node.selector ? '}\n' : '';
|
109 | result += "".concat(prefix).concat(cssText).concat(suffix);
|
110 | }
|
111 |
|
112 | return result;
|
113 | }
|
114 |
|
115 | var _default = transformCss;
|
116 | exports.default = _default; |
\ | No newline at end of file |