-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
44 lines (40 loc) · 1.54 KB
/
index.js
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
var postcss = require('postcss');
DEFAULT_OPTIONS = {
fallback: 'column-count'
}
AVAILABLE_FALLBACKS = ['column-count', 'overflow', 'clearfix']
module.exports = postcss.plugin('postcss-display-flow-root', function(options) {
var options = Object.assign({}, DEFAULT_OPTIONS, options)
return function(root, result) {
if (AVAILABLE_FALLBACKS.indexOf(options.fallback) === -1) {
result.warn(
"Fallback option '" + options.fallback + "' isn`t available, choose from the followings: " +
AVAILABLE_FALLBACKS.join(', ') + "."
)
}
root.walkRules(function(rule) {
// NOTE: nothing to do, if element have overflow:hidden cuz it create own flow
var haveOverflowHidden = false;
rule.walkDecls('overflow', function(decl) {
if (decl.value == 'hidden') { haveOverflowHidden = true; }
});
rule.walkDecls('display', function(decl) {
if (decl.value == 'flow-root' && !haveOverflowHidden) {
switch (options.fallback) {
case 'column-count':
rule.append('column-count: 1;');
break;
case 'clearfix':
// TODO: case when rule.selector already pseudo-element
// NOTE: last clearfix version (IE8+) https://css-tricks.com/snippets/css/clear-fix/
root.append(rule.selector + '::after {content: "";display: table;clear: both;}');
break;
case 'overflow':
rule.append('overflow: hidden;');
break;
}
}
})
});
}
});