-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
70 lines (61 loc) · 2.36 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
67
68
69
70
const postcss = require('postcss');
const Tidy = require('./Tidy');
const getGlobalOptions = require('./src/getGlobalOptions');
const { tidyShorthandProperty } = require('./tidy-shorthand-property');
const { tidyProperty } = require('./tidy-property');
const { tidyFunction } = require('./tidy-function');
const { tidyVar } = require('./tidy-var');
/**
* Parse rules and insert span and offset values.
*
* @param {Object} root The root CSS object.
*/
module.exports = postcss.plugin(
'postcss-tidy-columns',
(options = {}) => function postcssTidyColumns(root) {
// Collect the global options.
const globalOptions = Object.freeze(getGlobalOptions(root, options));
// Parse rules and declarations, replace `tidy-` properties.
root.walkRules((rule) => {
const tidy = new Tidy(rule, globalOptions);
// Replace shorthand declarations with their long-form equivalents.
rule.walkDecls(/^tidy-(column|offset)$/, (declaration) => {
tidyShorthandProperty(declaration, tidy);
});
// Set up rule-specific properties.
tidy.initRule();
rule.walkDecls((declaration) => {
// Replace `tidy-var()` functions.
tidyVar(declaration, tidy);
// Replace `tidy-*` properties.
tidyProperty(declaration, tidy);
// Replace `tidy-[span|offset]()` and `tidy-[span|offset]-full()` functions.
tidyFunction(declaration, tidy);
});
const { fullWidthRule } = tidy;
const { siteMax } = tidy.columns.options;
// Add the media query if a siteMax is declared and the `fullWidthRule` has children.
if (undefined !== siteMax && fullWidthRule.nodes.length > 0) {
/**
* The siteMax-width atRule.
* Contains full-width margin offset declarations.
*/
const fullWidthAtRule = postcss.atRule({
name: 'media',
params: `(min-width: ${siteMax})`,
nodes: [],
source: rule.source,
}).append(fullWidthRule);
// Insert the media query
if ('atrule' === rule.parent.type) {
// Insert after the parent at-rule.
root.insertAfter(rule.parent, fullWidthAtRule);
} else {
// Insert after the current rule.
root.insertAfter(rule, fullWidthAtRule);
}
}
});
},
);
// module.exports = postcss.plugin('postcss-tidy-columns', postcssTidyColumns);