-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
183 lines (154 loc) · 4.27 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const css = require('css');
const indentString = require('indent-string');
const fromPairs = require('lodash/fromPairs');
const map = require('lodash/map');
const orderBy = require('lodash/orderBy');
const reject = require('lodash/reject');
const repeat = require('lodash/repeat');
const uniq = require('lodash/uniq');
const SPACE = ' ';
const COMMA_SPACE = ', ';
function isNested(selector) {
const parts = selector.split(COMMA_SPACE);
if (parts.some(part => part.indexOf(SPACE) > -1)) {
if (parts.length === 1) {
return true;
}
const parents = uniq(parts.map(part => part.split(SPACE)[0]));
if (parents.length === 1) {
return true;
}
}
return false;
}
function cleanup(styles) {
const cleaned = {};
for (let selector in styles) {
let value = styles[selector];
if (typeof value === 'object') {
selector = selector.replace(/\s&/g, '');
value = cleanup(value);
}
cleaned[selector] = value;
}
return cleaned;
}
function render(styles, count, options) {
let text = '';
let index = -1;
const omitBracketsAndSemicolons = options.omitBracketsAndSemicolons === true;
const indent = string =>
indentString(string, count, {
indent:
options.spaces !== false
? repeat(' ', Number.isInteger(options.spaces) ? options.spaces : 2)
: '\t'
});
for (const key in styles) {
index++;
const value = styles[key];
if (typeof value === 'object') {
if (index > 0) {
text += '\n';
}
text += indent(key);
if (!omitBracketsAndSemicolons) {
text += ' {';
}
text += `\n${render(value, count + 1, options)}`;
if (!omitBracketsAndSemicolons) {
text += indent('}\n');
}
continue;
}
text += indent(`${key}: ${value}`);
if (!omitBracketsAndSemicolons) {
text += ';';
}
text += '\n';
}
return text;
}
module.exports = function transformCss(code, options = {}) {
let parsed;
try {
parsed = css.parse(code);
} catch (error) {
return '';
}
// TODO: support comments
const rules = reject(parsed.stylesheet.rules, 'comment');
const styles = rules.reduce((obj, rule) => {
const declarations = fromPairs(
rule.declarations.map(declaration => [
declaration.property,
declaration.value
])
);
const selector = rule.selectors
.join(COMMA_SPACE)
.replace(/\s+/g, SPACE)
.replace(/(\w+)((#|\.|:)\w+)/g, '$1 &$2');
const existing = obj[selector] || {};
const merged = {
...existing,
...declarations
};
return {
...obj,
[selector]: merged
};
}, {});
const selectors = Object.keys(styles);
const nested = orderBy(
selectors.filter(isNested),
selector => selector.split(SPACE).length,
'desc'
);
nested.forEach(selector => {
if (selector.indexOf(COMMA_SPACE) > -1) {
// we're dealing with multiple nested selectors
const parts = selector.split(COMMA_SPACE).map(part => part.split(SPACE));
let offset = Math.min(...map(parts, 'length')) - 1;
while (offset > 0) {
const parents = parts.map(part => part.slice(0, offset).join(SPACE));
if (uniq(parents).length === 1) {
const parent = parents[0];
if (styles[parent]) {
const child = parts
.map(part => part.slice(offset).join(SPACE))
.join(COMMA_SPACE);
styles[parent] = {
...styles[parent],
[child]: styles[selector]
};
delete styles[selector];
break;
}
}
offset--;
}
return;
}
// try to find a home for a single nested selector
let lastIndex = selector.length;
while (lastIndex > -1) {
const index = selector.lastIndexOf(SPACE, lastIndex);
if (index > -1) {
const parent = selector.slice(0, index);
if (styles[parent]) {
const child = selector.slice(index + 1);
styles[parent] = {
...styles[parent],
[child]: styles[selector]
};
delete styles[selector];
break;
}
}
lastIndex = index - 1;
}
});
const cleaned = cleanup(styles);
return render(cleaned, 0, options).replace(/\s+$/, '');
};