columns.js
4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _postcss = require('postcss');
var _postcssValueParser = require('postcss-value-parser');
var _stylehacks = require('stylehacks');
var _canMerge = require('../canMerge');
var _canMerge2 = _interopRequireDefault(_canMerge);
var _getDecls = require('../getDecls');
var _getDecls2 = _interopRequireDefault(_getDecls);
var _getValue = require('../getValue');
var _getValue2 = _interopRequireDefault(_getValue);
var _mergeRules = require('../mergeRules');
var _mergeRules2 = _interopRequireDefault(_mergeRules);
var _insertCloned = require('../insertCloned');
var _insertCloned2 = _interopRequireDefault(_insertCloned);
var _remove = require('../remove');
var _remove2 = _interopRequireDefault(_remove);
var _isCustomProp = require('../isCustomProp');
var _isCustomProp2 = _interopRequireDefault(_isCustomProp);
var _canExplode = require('../canExplode');
var _canExplode2 = _interopRequireDefault(_canExplode);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const properties = ['column-width', 'column-count'];
const auto = 'auto';
const inherit = 'inherit';
/**
* Normalize a columns shorthand definition. Both of the longhand
* properties' initial values are 'auto', and as per the spec,
* omitted values are set to their initial values. Thus, we can
* remove any 'auto' definition when there are two values.
*
* Specification link: https://www.w3.org/TR/css3-multicol/
*/
function normalize(values) {
if (values[0].toLowerCase() === auto) {
return values[1];
}
if (values[1].toLowerCase() === auto) {
return values[0];
}
if (values[0].toLowerCase() === inherit && values[1].toLowerCase() === inherit) {
return inherit;
}
return values.join(' ');
}
function explode(rule) {
rule.walkDecls(/^columns$/i, decl => {
if (!(0, _canExplode2.default)(decl)) {
return;
}
if ((0, _stylehacks.detect)(decl)) {
return;
}
let values = _postcss.list.space(decl.value);
if (values.length === 1) {
values.push(auto);
}
values.forEach((value, i) => {
let prop = properties[1];
if (value.toLowerCase() === auto) {
prop = properties[i];
} else if ((0, _postcssValueParser.unit)(value).unit) {
prop = properties[0];
}
(0, _insertCloned2.default)(decl.parent, decl, {
prop,
value
});
});
decl.remove();
});
}
function cleanup(rule) {
let decls = (0, _getDecls2.default)(rule, ['columns'].concat(properties));
while (decls.length) {
const lastNode = decls[decls.length - 1];
// remove properties of lower precedence
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);
lesser.forEach(_remove2.default);
decls = decls.filter(node => !~lesser.indexOf(node));
// get duplicate properties
let duplicates = decls.filter(node => !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && node.prop === lastNode.prop && !(!(0, _isCustomProp2.default)(node) && (0, _isCustomProp2.default)(lastNode)));
duplicates.forEach(_remove2.default);
decls = decls.filter(node => node !== lastNode && !~duplicates.indexOf(node));
}
}
function merge(rule) {
(0, _mergeRules2.default)(rule, properties, (rules, lastNode) => {
if ((0, _canMerge2.default)(rules) && !rules.some(_stylehacks.detect)) {
(0, _insertCloned2.default)(lastNode.parent, lastNode, {
prop: 'columns',
value: normalize(rules.map(_getValue2.default))
});
rules.forEach(_remove2.default);
return true;
}
});
cleanup(rule);
}
exports.default = {
explode,
merge
};
module.exports = exports['default'];