-
Notifications
You must be signed in to change notification settings - Fork 2
/
tidy-property.js
120 lines (106 loc) · 3.22 KB
/
tidy-property.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
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
const postcss = require('postcss');
const cleanClone = require('./lib/cleanClone');
const hasComment = require('./lib/hasComment');
/**
* Pattern to match the `tidy-offset-*` property.
*
* @type {RegExp}
*/
const OFFSET_REGEX = /tidy-offset-(left|right)/;
/**
* Replace `tidy-*` properties.
* - tidy-span
* - tidy-offset-left
* - tidy-offset-right
*
* @see https://github.com/goodguyry/postcss-tidy-columns#span
* @see https://github.com/goodguyry/postcss-tidy-columns#offset-left
* @see https://github.com/goodguyry/postcss-tidy-columns#offset-right
*
* @param {Object} declaration The current CSS declaration.
* @param {Object} tidy An instance of the Tidy class.
*/
function tidyProperty(declaration, tidy) {
const { fullWidthRule, columns, columns: { options } } = tidy;
// Replace `tidy-span` declaration with a `width` declaration.
if ('tidy-span' === declaration.prop) {
/**
* fluid: calc() function based on 100vw base.
* full: calc() function based on `siteMax` base.
*/
const { fluid, full } = columns.spanCalc(declaration.value);
const columnDecl = [];
// Save the original declaration in a comment for debugging.
if (
options.debug
&& undefined !== declaration.parent
&& !hasComment(declaration)
) {
declaration.cloneBefore(postcss.comment({ text: declaration.toString() }));
}
columnDecl.push(cleanClone(
declaration,
{
prop: 'width',
value: fluid,
},
));
// Conditionally insert a `max-width` declaration.
if (undefined !== full) {
columnDecl.push(cleanClone(
declaration,
{
prop: 'max-width',
value: full,
},
));
}
declaration.replaceWith(columnDecl);
}
// Replace`tidy-offset-left|right` declaration with `margin-left|right`.
if (OFFSET_REGEX.test(declaration.prop)) {
/**
* {undefined} The full property name.
* direction: The side upon which the offset will be applied.
*/
const [, direction] = declaration.prop.match(OFFSET_REGEX);
/**
* fluid: calc() function based on 100vw base.
* full: calc() function based on `siteMax` base.
*/
const { fluid, full } = columns.offsetCalc(declaration.value);
// Save the original declaration in a comment for debugging.
if (
options.debug
&& undefined !== declaration.parent
&& !hasComment(declaration)
) {
declaration.cloneBefore(postcss.comment({ text: declaration.toString() }));
}
// Clone the declaration with `fluid` declaration overrides.
const fluidDecl = cleanClone(
declaration,
{
prop: `margin-${direction}`,
value: fluid,
},
);
// Replace the declaration with the cloned fluid declaration.
declaration.replaceWith(fluidDecl);
// Conditionally add a `margin-[left|right]` declaration to the media query.
if (undefined !== full && 'rule' === fullWidthRule.type) {
// Clone the declaration with `full` declaration overrides.
fullWidthRule.append(cleanClone(
declaration,
{
prop: `margin-${direction}`,
value: full,
},
));
}
}
}
module.exports = {
tidyProperty,
OFFSET_REGEX,
};