1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _postcss = require("postcss");
|
---|
9 |
|
---|
10 | var _postcssValueParser = require("postcss-value-parser");
|
---|
11 |
|
---|
12 | var _stylehacks = require("stylehacks");
|
---|
13 |
|
---|
14 | var _canMerge = _interopRequireDefault(require("../canMerge"));
|
---|
15 |
|
---|
16 | var _getDecls = _interopRequireDefault(require("../getDecls"));
|
---|
17 |
|
---|
18 | var _getValue = _interopRequireDefault(require("../getValue"));
|
---|
19 |
|
---|
20 | var _mergeRules = _interopRequireDefault(require("../mergeRules"));
|
---|
21 |
|
---|
22 | var _insertCloned = _interopRequireDefault(require("../insertCloned"));
|
---|
23 |
|
---|
24 | var _remove = _interopRequireDefault(require("../remove"));
|
---|
25 |
|
---|
26 | var _isCustomProp = _interopRequireDefault(require("../isCustomProp"));
|
---|
27 |
|
---|
28 | var _canExplode = _interopRequireDefault(require("../canExplode"));
|
---|
29 |
|
---|
30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
31 |
|
---|
32 | const properties = ['column-width', 'column-count'];
|
---|
33 | const auto = 'auto';
|
---|
34 | const inherit = 'inherit';
|
---|
35 | /**
|
---|
36 | * Normalize a columns shorthand definition. Both of the longhand
|
---|
37 | * properties' initial values are 'auto', and as per the spec,
|
---|
38 | * omitted values are set to their initial values. Thus, we can
|
---|
39 | * remove any 'auto' definition when there are two values.
|
---|
40 | *
|
---|
41 | * Specification link: https://www.w3.org/TR/css3-multicol/
|
---|
42 | */
|
---|
43 |
|
---|
44 | function normalize(values) {
|
---|
45 | if (values[0].toLowerCase() === auto) {
|
---|
46 | return values[1];
|
---|
47 | }
|
---|
48 |
|
---|
49 | if (values[1].toLowerCase() === auto) {
|
---|
50 | return values[0];
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (values[0].toLowerCase() === inherit && values[1].toLowerCase() === inherit) {
|
---|
54 | return inherit;
|
---|
55 | }
|
---|
56 |
|
---|
57 | return values.join(' ');
|
---|
58 | }
|
---|
59 |
|
---|
60 | function explode(rule) {
|
---|
61 | rule.walkDecls(/^columns$/i, decl => {
|
---|
62 | if (!(0, _canExplode.default)(decl)) {
|
---|
63 | return;
|
---|
64 | }
|
---|
65 |
|
---|
66 | if ((0, _stylehacks.detect)(decl)) {
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | let values = _postcss.list.space(decl.value);
|
---|
71 |
|
---|
72 | if (values.length === 1) {
|
---|
73 | values.push(auto);
|
---|
74 | }
|
---|
75 |
|
---|
76 | values.forEach((value, i) => {
|
---|
77 | let prop = properties[1];
|
---|
78 |
|
---|
79 | if (value.toLowerCase() === auto) {
|
---|
80 | prop = properties[i];
|
---|
81 | } else if ((0, _postcssValueParser.unit)(value).unit) {
|
---|
82 | prop = properties[0];
|
---|
83 | }
|
---|
84 |
|
---|
85 | (0, _insertCloned.default)(decl.parent, decl, {
|
---|
86 | prop,
|
---|
87 | value
|
---|
88 | });
|
---|
89 | });
|
---|
90 | decl.remove();
|
---|
91 | });
|
---|
92 | }
|
---|
93 |
|
---|
94 | function cleanup(rule) {
|
---|
95 | let decls = (0, _getDecls.default)(rule, ['columns'].concat(properties));
|
---|
96 |
|
---|
97 | while (decls.length) {
|
---|
98 | const lastNode = decls[decls.length - 1]; // remove properties of lower precedence
|
---|
99 |
|
---|
100 | const lesser = decls.filter(node => !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && lastNode.prop === 'columns' && node.prop !== lastNode.prop);
|
---|
101 | lesser.forEach(_remove.default);
|
---|
102 | decls = decls.filter(node => !~lesser.indexOf(node)); // get duplicate properties
|
---|
103 |
|
---|
104 | let duplicates = decls.filter(node => !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && node.prop === lastNode.prop && !(!(0, _isCustomProp.default)(node) && (0, _isCustomProp.default)(lastNode)));
|
---|
105 | duplicates.forEach(_remove.default);
|
---|
106 | decls = decls.filter(node => node !== lastNode && !~duplicates.indexOf(node));
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | function merge(rule) {
|
---|
111 | (0, _mergeRules.default)(rule, properties, (rules, lastNode) => {
|
---|
112 | if ((0, _canMerge.default)(rules) && !rules.some(_stylehacks.detect)) {
|
---|
113 | (0, _insertCloned.default)(lastNode.parent, lastNode, {
|
---|
114 | prop: 'columns',
|
---|
115 | value: normalize(rules.map(_getValue.default))
|
---|
116 | });
|
---|
117 | rules.forEach(_remove.default);
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 | });
|
---|
121 | cleanup(rule);
|
---|
122 | }
|
---|
123 |
|
---|
124 | var _default = {
|
---|
125 | explode,
|
---|
126 | merge
|
---|
127 | };
|
---|
128 | exports.default = _default;
|
---|
129 | module.exports = exports.default; |
---|