-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
66 lines (52 loc) · 1.28 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
const postcss = require('postcss');
const DEFAULT_OPTIONS = {
display: 'block'
};
/**
* Clear: fix; rule handler
* @param {string} decl current decleration
*/
function clearFix(decl, opts) {
let origRule = decl.parent,
ruleSelectors = origRule.selectors,
newRule;
if (decl.value !== 'fix') {
return;
}
ruleSelectors = ruleSelectors
.map(ruleSelector => ruleSelector + ':after').join(',\n');
// Insert the :after rule before the original rule
newRule = origRule.cloneAfter({
selector: ruleSelectors
}).removeAll();
newRule.append({
prop: 'content',
value: '\'\'',
source: decl.source
}, {
prop: 'display',
value: opts.display,
source: decl.source
}, {
prop: 'clear',
value: 'both',
source: decl.source
});
// If the original rule only had clear:fix in it, remove the whole rule
if (decl.prev() === undefined && decl.next() === undefined) {
origRule.remove();
} else {
// Otherwise just remove the delcl
decl.remove();
}
}
module.exports = postcss.plugin('postcss-clearfix', function(opts) {
opts = opts || {};
opts = Object.assign({}, DEFAULT_OPTIONS, opts);
return function(css) {
css.walkDecls('clear', function(decl) {
clearFix(decl, opts);
});
};
});