This repository was archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
287 lines (245 loc) · 7.41 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'use strict';
import stringScore from './lib/score';
import cssSnippets from './lib/snippets';
const globalKeywords = ['auto', 'inherit', 'unset'];
const unitlessProperties = [
'z-index', 'line-height', 'opacity', 'font-weight', 'zoom',
'flex', 'flex-grow', 'flex-shrink'
];
const defaultOptions = {
intUnit: 'px',
floatUnit: 'em',
unitAliases: {
e :'em',
p: '%',
x: 'ex',
r: 'rem'
},
fuzzySearchMinScore: 0
};
/**
* For every node in given `tree`, finds matching snippet from `registry` and
* updates node with snippet data.
*
* This resolver uses fuzzy matching for searching matched snippets and their
* keyword values.
*/
export default function(tree, registry, options) {
options = Object.assign({}, defaultOptions, options);
options.unitAliases = Object.assign({}, defaultOptions.unitAliases, options && options.unitAliases);
const snippets = convertToCSSSnippets(registry);
tree.walk(node => resolveNode(node, snippets, options));
return tree;
}
export function convertToCSSSnippets(registry) {
return cssSnippets(registry.all({type: 'string'}));
}
export { stringScore, cssSnippets };
/**
* Resolves given node: finds matched CSS snippets using fuzzy match and resolves
* keyword aliases from node value
* @param {Node} node
* @param {CSSSnippet[]} snippets
* @param {Object} options
* @return {Node}
*/
function resolveNode(node, snippets, options) {
if (options.property) {
// Resolve as value of given CSS property
return resolveAsPropertyValue(node, snippets.find(snippet => snippet.property === options.property), options);
}
const snippet = findBestMatch(node.name, snippets, 'key', options.fuzzySearchMinScore);
if (!snippet) {
// Edge case: `!important` snippet
return node.name === '!' ? setNodeAsText(node, '!important') : node;
}
return snippet.property
? resolveAsProperty(node, snippet, options)
: resolveAsSnippet(node, snippet);
}
/**
* Resolves given parsed abbreviation node as CSS property
* @param {Node} node
* @param {CSSSnippet} snippet
* @param {Object} formatOptions
* @return {Node}
*/
function resolveAsProperty(node, snippet, formatOptions) {
const abbr = node.name;
node.name = snippet.property;
if (node.value && typeof node.value === 'object') {
// resolve keyword shortcuts
const keywords = snippet.keywords();
if (!node.value.size) {
// no value defined, try to resolve unmatched part as a keyword alias
let kw = findBestMatch(getUnmatchedPart(abbr, snippet.key), keywords);
if (!kw) {
// no matching value, try to get default one
kw = snippet.defaultValue;
if (kw && kw.indexOf('${') === -1) {
// Quick and dirty test for existing field. If not, wrap
// default value in a field
kw = `\${1:${kw}}`;
}
}
if (kw) {
node.value.add(kw);
}
} else {
// replace keyword aliases in current node value
for (let i = 0, token; i < node.value.value.length; i++) {
token = node.value.value[i];
if (token === '!') {
token = `${!i ? '${1} ' : ''}!important`;
} else if (isKeyword(token)) {
token = findBestMatch(token.value, keywords)
|| findBestMatch(token.value, globalKeywords)
|| token;
} else if (isNumericValue(token)) {
token = resolveNumericValue(node.name, token, formatOptions);
}
node.value.value[i] = token;
}
}
}
return node;
}
/**
* Resolves given parsed abbreviation node as a snippet: a plain code chunk
* @param {Node} node
* @param {CSSSnippet} snippet
* @return {Node}
*/
function resolveAsSnippet(node, snippet) {
return setNodeAsText(node, snippet.value);
}
/**
* Resolves given parsed abbreviation node as property value of given `snippet`:
* tries to find best matching keyword from CSS snippet
* @param {Node} node
* @param {CSSSnippet} snippet
* @param {Object} options
* @return {Node}
*/
function resolveAsPropertyValue(node, snippet, options) {
// Possible resolved result for CSS property:
// * matched snippet keyword
// * color (starts with #)
// Everything else should result the same as input abbreviation
let keywords = globalKeywords.slice();
if (snippet) {
keywords = keywords.concat(snippet.keywords());
}
const values = [node.name].concat(node.value.value)
.filter(Boolean)
.map(value => {
if (typeof value === 'string' || value.type === 'keyword') {
value = String(value);
return findBestMatch(value, keywords, null, options.fuzzySearchMinScore) || value;
}
return value;
});
node.name = null;
node.value.value = values;
return node;
}
/**
* Sets given parsed abbreviation node as a text snippet
* @param {Node} node
* @param {String} text
* @return {Node}
*/
function setNodeAsText(node, text) {
node.name = null;
node.value = text;
return node;
}
/**
* Finds best matching item from `items` array
* @param {String} abbr Abbreviation to match
* @param {Array} items List of items for match
* @param {String} [key] If `items` is a list of objects, use `key` as object
* property to test against
* @param {Number} fuzzySearchMinScore The minimum score the best matched item should have to be a valid match.
* @return {*}
*/
function findBestMatch(abbr, items, key, fuzzySearchMinScore) {
if (!abbr) {
return null;
}
let matchedItem = null;
let maxScore = 0;
fuzzySearchMinScore = fuzzySearchMinScore || 0;
for (let i = 0, item; i < items.length; i++) {
item = items[i];
const score = stringScore(abbr, getScoringPart(item, key));
if (score === 1) {
// direct hit, no need to look further
return item;
}
if (score && score >= maxScore) {
maxScore = score;
matchedItem = item;
}
}
return maxScore >= fuzzySearchMinScore ? matchedItem : null;
}
function getScoringPart(item, key) {
const value = item && typeof item === 'object' ? item[key] : item;
const m = (value || '').match(/^[\w-@]+/);
return m ? m[0] : value;
}
/**
* Returns a part of `abbr` that wasn’t directly matched agains `string`.
* For example, if abbreviation `poas` is matched against `position`, the unmatched part will be `as`
* since `a` wasn’t found in string stream
* @param {String} abbr
* @param {String} string
* @return {String}
*/
function getUnmatchedPart(abbr, string) {
for (let i = 0, lastPos = 0; i < abbr.length; i++) {
lastPos = string.indexOf(abbr[i], lastPos);
if (lastPos === -1) {
return abbr.slice(i);
}
lastPos++;
}
return '';
}
/**
* Check if given CSS value token is a keyword
* @param {*} token
* @return {Boolean}
*/
function isKeyword(token) {
return tokenTypeOf(token, 'keyword');
}
/**
* Check if given CSS value token is a numeric value
* @param {*} token
* @return {Boolean}
*/
function isNumericValue(token) {
return tokenTypeOf(token, 'numeric');
}
function tokenTypeOf(token, type) {
return token && typeof token === 'object' && token.type === type;
}
/**
* Resolves numeric value for given CSS property
* @param {String} property CSS property name
* @param {NumericValue} token CSS numeric value token
* @param {Object} formatOptions Formatting options for units
* @return {NumericValue}
*/
function resolveNumericValue(property, token, formatOptions) {
if (token.unit) {
token.unit = formatOptions.unitAliases[token.unit] || token.unit;
} else if (token.value !== 0 && unitlessProperties.indexOf(property) === -1) {
// use `px` for integers, `em` for floats
// NB: num|0 is a quick alternative to Math.round(0)
token.unit = token.value === (token.value|0) ? formatOptions.intUnit : formatOptions.floatUnit;
}
return token;
}