extract-properties.js
5.67 KB
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
184
185
186
187
188
189
190
191
192
193
var split = require('../utils/split');
var COMMA = ',';
var FORWARD_SLASH = '/';
var AT_RULE = 'at-rule';
var IMPORTANT_WORD = 'important';
var IMPORTANT_TOKEN = '!'+IMPORTANT_WORD;
var IMPORTANT_WORD_MATCH = new RegExp('^'+IMPORTANT_WORD+'$', 'i');
var IMPORTANT_TOKEN_MATCH = new RegExp('^'+IMPORTANT_TOKEN+'$', 'i');
function selectorName(value) {
return value[0];
}
function noop() {}
function withoutComments(string, into, heading, context) {
var matcher = heading ? /^__ESCAPED_COMMENT_/ : /__ESCAPED_COMMENT_/;
var track = heading ? context.track : noop; // don't track when comment not in a heading as we do it later in `trackComments`
while (matcher.test(string)) {
var startOfComment = string.indexOf('__');
var endOfComment = string.indexOf('__', startOfComment + 1) + 2;
var comment = string.substring(startOfComment, endOfComment);
string = string.substring(0, startOfComment) + string.substring(endOfComment);
track(comment);
into.push(comment);
}
return string;
}
function withoutHeadingComments(string, into, context) {
return withoutComments(string, into, true, context);
}
function withoutInnerComments(string, into, context) {
return withoutComments(string, into, false, context);
}
function trackComments(comments, into, context) {
for (var i = 0, l = comments.length; i < l; i++) {
context.track(comments[i]);
into.push(comments[i]);
}
}
function extractProperties(string, selectors, context) {
var list = [];
var innerComments = [];
var valueSeparator = /[\s,\/]/;
if (typeof string != 'string')
return [];
if (string.indexOf(')') > -1)
string = string.replace(/\)([^\s_;:,\)])/g, context.sourceMap ? ') __ESCAPED_COMMENT_CLEAN_CSS(0,-1)__ $1' : ') $1');
if (string.indexOf('ESCAPED_URL_CLEAN_CSS') > -1)
string = string.replace(/(ESCAPED_URL_CLEAN_CSS[^_]+?__)/g, context.sourceMap ? '$1 __ESCAPED_COMMENT_CLEAN_CSS(0,-1)__ ' : '$1 ');
var candidates = split(string, ';', false, '{', '}');
for (var i = 0, l = candidates.length; i < l; i++) {
var candidate = candidates[i];
var firstColonAt = candidate.indexOf(':');
var atRule = candidate.trim()[0] == '@';
if (atRule) {
context.track(candidate);
list.push([AT_RULE, candidate.trim()]);
continue;
}
if (firstColonAt == -1) {
context.track(candidate);
if (candidate.indexOf('__ESCAPED_COMMENT_SPECIAL') > -1)
list.push(candidate.trim());
continue;
}
if (candidate.indexOf('{') > 0 && candidate.indexOf('{') < firstColonAt) {
context.track(candidate);
continue;
}
var body = [];
var name = candidate.substring(0, firstColonAt);
innerComments = [];
if (name.indexOf('__ESCAPED_COMMENT') > -1)
name = withoutHeadingComments(name, list, context);
if (name.indexOf('__ESCAPED_COMMENT') > -1)
name = withoutInnerComments(name, innerComments, context);
body.push([name.trim()].concat(context.track(name, true)));
context.track(':');
trackComments(innerComments, list, context);
var firstBraceAt = candidate.indexOf('{');
var isVariable = name.trim().indexOf('--') === 0;
if (isVariable && firstBraceAt > 0) {
var blockPrefix = candidate.substring(firstColonAt + 1, firstBraceAt + 1);
var blockSuffix = candidate.substring(candidate.indexOf('}'));
var blockContent = candidate.substring(firstBraceAt + 1, candidate.length - blockSuffix.length);
context.track(blockPrefix);
body.push(extractProperties(blockContent, selectors, context));
list.push(body);
context.track(blockSuffix);
context.track(i < l - 1 ? ';' : '');
continue;
}
var values = split(candidate.substring(firstColonAt + 1), valueSeparator, true);
if (values.length == 1 && values[0] === '') {
context.warnings.push('Empty property \'' + name + '\' inside \'' + selectors.filter(selectorName).join(',') + '\' selector. Ignoring.');
continue;
}
for (var j = 0, m = values.length; j < m; j++) {
var value = values[j];
var trimmed = value.trim();
if (trimmed.length === 0)
continue;
var lastCharacter = trimmed[trimmed.length - 1];
var endsWithNonSpaceSeparator = trimmed.length > 1 && (lastCharacter == COMMA || lastCharacter == FORWARD_SLASH);
if (endsWithNonSpaceSeparator)
trimmed = trimmed.substring(0, trimmed.length - 1);
if (trimmed.indexOf('__ESCAPED_COMMENT_CLEAN_CSS(0,-') > -1) {
context.track(trimmed);
continue;
}
innerComments = [];
if (trimmed.indexOf('__ESCAPED_COMMENT') > -1)
trimmed = withoutHeadingComments(trimmed, list, context);
if (trimmed.indexOf('__ESCAPED_COMMENT') > -1)
trimmed = withoutInnerComments(trimmed, innerComments, context);
if (trimmed.length === 0) {
trackComments(innerComments, list, context);
continue;
}
var pos = body.length - 1;
if (IMPORTANT_WORD_MATCH.test(trimmed) && body[pos][0] == '!') {
context.track(trimmed);
body[pos - 1][0] += IMPORTANT_TOKEN;
body.pop();
continue;
}
if (IMPORTANT_TOKEN_MATCH.test(trimmed) || (IMPORTANT_WORD_MATCH.test(trimmed) && body[pos][0][body[pos][0].length - 1] == '!')) {
context.track(trimmed);
body[pos][0] += trimmed;
continue;
}
body.push([trimmed].concat(context.track(value, true)));
trackComments(innerComments, list, context);
if (endsWithNonSpaceSeparator) {
body.push([lastCharacter]);
context.track(lastCharacter);
}
}
if (i < l - 1)
context.track(';');
list.push(body);
}
return list;
}
module.exports = extractProperties;