UNPKG

3.28 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.transformCss = transformCss;
9exports.toRules = toRules;
10exports.isKeyframesSelector = isKeyframesSelector;
11exports.default = void 0;
12
13var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
14
15var _parseCss = require("./parseCss.js");
16
17/*
18 * The MIT License (MIT)
19 *
20 * Copyright (c) 2015 - present Instructure, Inc.
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to deal
24 * in the Software without restriction, including without limitation the rights
25 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 * SOFTWARE.
39 */
40function 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
50function isKeyframesSelector(rule) {
51 return rule.parent && rule.parent.type === _parseCss.ruleTypes.keyframes;
52}
53
54function 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
73function 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
90function 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
115var _default = transformCss;
116exports.default = _default;
\No newline at end of file