This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostcss-alter-property-value.js
147 lines (128 loc) · 4.89 KB
/
postcss-alter-property-value.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
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
/* postcss alter property value (papv) */
/* ES5 code */
var postcss = require('postcss');
module.exports = postcss.plugin('postcss-alter-property-value', function (options) {
var options = options || {},
declarations = options.declarations || {},
config = options.config || {};
return function (root, result) {
var props = Object.keys(declarations);
props.map(function (prop, index) {
root.walkDecls(prop, function (decl) {
var value = declarations[prop],
copyProp = decl.prop + '',
copyVal = decl.value + '';
if (typeof value === 'string') {
decl.value = value;
addInfoToValue(decl, ' /* papv - changeValue from [' + copyVal + '] */');
} else if (typeof value === 'object' || value === undefined) {
if (!value) {
decl.prop = prop + '__papv__disable';
} else if (!value.task) {
// no task to do if not set
} else {
if (value.whenRegex && typeof value.whenRegex === 'object') {
regexParser({value: value, decl: decl});
} else {
switch (value.task) {
case 'remove':
if (hasNoFields(value)) {
decl.remove();
} else if (value.whenValueEquals !== undefined && value.whenValueEquals === decl.value) {
decl.remove();
}
break;
case 'cloneBefore':
var node = decl.cloneBefore({prop: value.to});
break;
case 'cloneAfter':
var node = decl.cloneAfter({prop: value.to});
break;
case 'disable':
if (hasNoFields(value)) {
decl.prop = prop + '__papv_disable';
} else if (value.whenValueEquals !== undefined && value.whenValueEquals === decl.value) {
decl.prop = prop + '__papv_disable';
}
break;
case 'changeProperty':
if (hasNoFields(value)) {
decl.prop = value.to;
addInfoToValue(decl, ' /* papv - changeProp from [' + copyProp + '] */');
} else if (value.whenValueEquals !== undefined && value.whenValueEquals === decl.value) {
decl.prop = value.to;
addInfoToValue(decl, ' /* papv - changeProp from [' + copyProp + '] */');
}
break;
case 'changeValue':
if (hasNoFields(value)) {
decl.value = value.to;
addInfoToValue(decl, ' /* papv - changeValue from [' + copyVal + '] */');
} else if (value.whenValueEquals !== undefined && value.whenValueEquals === decl.value) {
decl.value = value.to;
addInfoToValue(decl, ' /* papv - changeValue from [' + copyVal + '] */');
}
break;
default:
result.warn('unknown task: [' + value.task + ']');
break;
}
}
}
}
});
});
};
/* Helper utils */
function regexParser(data) {
var value = data.value,
decl = data.decl,
whenRegex = value.whenRegex;
if (whenRegex.value === undefined) {
return; // nothing to do when value is not set
}
var copyProp = decl.prop + '',
copyVal = decl.value + '',
task = value.task;
var regex = new RegExp(whenRegex.value, whenRegex.flags || '');
switch (task) {
case 'disable':
if (regex.test(decl.value))
decl.prop = prop + '__papv_disable';
break;
case 'remove':
if (regex.test(decl.value))
decl.remove();
break;
case 'changeProperty':
if (regex.test(decl.value)) {
decl.prop = value.to;
addInfoToValue(decl, ' /* papv - changeProp from [' + copyProp + '] */');
}
break;
case 'changeValue':
switch (whenRegex.mode) {
case 'partial':
decl.value = decl.value.replace(regex, value.to);
addInfoToValue(decl, ' /* --papv - changeValue from [' + copyVal + '] */');
break;
case 'replace':
default:
if (regex.test(decl.value)) {
decl.value = value.to;
addInfoToValue(decl, ' /* papv - changeValue from [' + copyVal + '] */');
}
break;
}
break;
}
}
function hasNoFields(value) {
return value.whenValueEquals === undefined && value.whenRegex === undefined;
}
function addInfoToValue(decl, str) {
if (config.addInfo) {
decl.value += str;
}
}
});