efront 4.0.55 → 4.0.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +1,258 @@
1
- var { STAMP, PROPERTY, SCOPED, VALUE, EXPRESS, QUOTED, createString } = require("./common");
1
+ var { STAMP, PROPERTY, SCOPED, VALUE, EXPRESS, QUOTED, SPACE, COMMENT, createString } = require("./common");
2
+ class Richarg extends Program {
3
+ straps = ["and"];
4
+ stamps = ',:;'.split("");
5
+ quotes = this.quotes.slice(0, 2).concat();
6
+ keepspace = true;
7
+ scopes = [["(", ")"], ["{", "}"]];
8
+ }
9
+
10
+ var rarg = new Richarg;
11
+ rarg.quotes.push(["url(", ")"]);
12
+ var killcalc = a => createString(a).replace(/((?:[\+\-]+)?(?:\d+(?:\.\d*)?|\.\d+))\s*(px|%|pt|cm|mm|r?em)?\s*([\/\*]\s*|[\+\-]\s+)((?:[\+\-]+)?(?:\d+(?:\.\d*)?|\.\d+))(px|%|pt|cm|mm|r?em)?/ig, function (_, d1, p1 = '', c, d2, p2 = '') {
13
+ d1 = eval(d1);
14
+ d2 = eval(d2);
15
+ if (!p2) {
16
+ if (c === '*') {
17
+ return d1 * d2 + p1;
18
+ }
19
+ if (c === '/') {
20
+ return d1 / d2 + p1;
21
+ }
22
+ }
23
+ else if (p1 === p2) {
24
+ if (c === "+") {
25
+ return (+d1 + +d2) + p1;
26
+ }
27
+ if (c === '-') {
28
+ return (d1 - d2) + p1;
29
+ }
30
+ }
31
+ return _;
32
+ });
33
+ var seprateFunc = function (express) {
34
+ var express = scanner2(express, rarg);
35
+ var sps = [];
36
+ var sp = [];
37
+ sp.autospace = false;
38
+ for (var cx = 0, dx = express.length; cx < dx; cx++) {
39
+ var o = express[cx];
40
+ if (o.type === SCOPED && o.entry === '(') {
41
+ if (!sp.length) continue;
42
+ var p = sp[sp.length - 1];
43
+ if (p.type & (EXPRESS | PROPERTY)) {
44
+ sp.pop();
45
+ if (sp.length) sps.push(killcalc(sp));
46
+ sp.splice(0, sp.length, p, o);
47
+ sps.push(createString(sp));
48
+ sp.splice(0, sp.length);
49
+ }
50
+ }
51
+ else if (o.type === QUOTED) {
52
+ if (sp.length) sps.push(killcalc(sp));
53
+ sp.splice(0, sp.length);
54
+ sps.push(createString([o]));
55
+ }
56
+ else sp.push(o);
57
+ }
58
+ if (sp.length) sps.push(killcalc(sp));
59
+ return sps;
60
+ }
61
+ var splitParams = function (params) {
62
+ if (!params) return [];
63
+ params = scanner2(params, rarg);
64
+ var o = params.first;
65
+ var params = [];
66
+ while (o) {
67
+ var p = [];
68
+ while (o) {
69
+ if (o.type === STAMP && o.text === ',') {
70
+ o = o.next;
71
+ break;
72
+ }
73
+ p.push(o);
74
+ o = o.next;
75
+ }
76
+ p.autospace = false;
77
+ params.push(createString(p));
78
+ }
79
+ return params;
80
+ }
81
+ var createArgMap = function (args, split = ',', equal = ':') {
82
+ if (args) args = scanner2(args, rarg);
83
+ else args = [];
84
+ var map = Object.create(null);
85
+ var o = args.first;
86
+ var args = [];
87
+ while (o) {
88
+ if (!(o.type & (PROPERTY | EXPRESS))) {
89
+ throw new Error("参数异常!");
90
+ }
91
+ var k = o.text;
92
+ args.push(k);
93
+ o = o.next;
94
+ if (o && o.type === STAMP && o.text === equal) {
95
+ var v = []
96
+ o = o.next;
97
+ while (o && (o.type !== STAMP || o.text !== split)) {
98
+ v.push(o);
99
+ o = o.next;
100
+ }
101
+ map[k] = createString(v);
102
+ }
103
+ if (o && o.type === STAMP && o.text === split) o = o.next;
104
+ }
105
+ args.defaults = map;
106
+ return args;
107
+ };
108
+
109
+ var macros = Object.create(null);
110
+ macros.range = function () {
111
+ if (arguments.length === 1) {
112
+ return ArrayFill(arguments[0], 0).map((a, i) => i + 1);
113
+ }
114
+ if (arguments.length === 3) {
115
+ var [start, end, step] = arguments;
116
+ var unit = /[^\d]+$/.exec(start);
117
+ if (!unit) unit = /[^\d]+$/.exec(end);
118
+ if (!unit) unit = /[^\d]+$/.exec(step);
119
+ if (unit) unit = unit[0];
120
+ else unit = '';
121
+ var fixed1 = start.length - start.lastIndexOf('.');
122
+ var fixed2 = end.length - end.lastIndexOf('.');
123
+ var fixed3 = step.length - step.lastIndexOf('.');
124
+ var fixed = Math.max(fixed1, fixed2, fixed3);
125
+ start = parseFloat(start);
126
+ end = parseFloat(end);
127
+ step = parseFloat(step) || 1;
128
+ var result = [];
129
+ for (var temp = start; temp < end; temp += step) {
130
+ result.push(temp.toFixed(fixed) + unit);
131
+ }
132
+ return result;
133
+ }
134
+ throw new Error("range参数错误:" + arguments);
135
+ };
136
+ macros.extract = function (list, index) {
137
+ if (typeof list === 'string') list = list.split(',');
138
+ else if (list instanceof Array) {
139
+ list = createArgMap(list.concat(list.rest).join(''), ';');
140
+ }
141
+ return list[index];
142
+ };
143
+ macros.length = function (list) {
144
+ if (typeof list === 'string') return list.split(',').length;
145
+ else if (list instanceof Array) {
146
+ return createArgMap(list.concat(list.rest).join(''), ';').length;
147
+ }
148
+ return 1;
149
+ };
150
+ macros.escape = function (a) {
151
+ return strings.decode(a).replace(/[^,\/\?@&\+'~\!\$\w\.]/g, function (a) {
152
+ return "%" + encodeUTF8(a).map(a => a < 16 ? "0" + a.toString(16) : a.toString(16));
153
+ });
154
+ };
155
+ macros.e = function (a) {
156
+ return strings.decode(a);
157
+ };
158
+ var wrapColor = function (f) {
159
+ return function (c) {
160
+ if (color.isColor(c)) return f(...arguments);
161
+ return f;
162
+ }
163
+ }
164
+ macros.saturate = wrapColor(color.strurate);
165
+ macros.desaturate = wrapColor(color.desaturate);
166
+ macros.lighten = wrapColor(color.lighten);
167
+ macros.darken = wrapColor(color.darken);
168
+ macros.fadein = wrapColor(color.fadein);
169
+ macros.fadeout = wrapColor(color.fadeout);
170
+ macros.fade = wrapColor(color.fade);
171
+ macros.spin = wrapColor(color.spin);
172
+ macros.mix = wrapColor(color.mix);
173
+ macros.tint = wrapColor(color.tint);
174
+ macros.shade = wrapColor(color.shade);
175
+ macros.grayscale = wrapColor(color.grayscale);
176
+ macros.grayluma = wrapColor(color.grayluma);
177
+ macros.fade = wrapColor(color.fade);
178
+ macros.each = function (list, body) {
179
+ var match = /^(?:\s*[#\.]?\(([\s\S]*?)\))?\s*\{([\s\S]*)\}$/.exec(body);
180
+ if (!match) throw new Error("each参数异常!");
181
+ var [_, args, content] = match;
182
+ if (!content) return;
183
+ content = richcss(content);
184
+ if (args) args = args.split(",").map(a => a.trim());
185
+ else args = [];
186
+ if (args.length < 1) args.push("@value");
187
+ if (args.length < 2) args.push("@key");
188
+ if (args.length < 3) args.push("@index");
189
+ if (typeof list === "string") list = splitParams(list);
190
+ else if (list instanceof Array) {
191
+ list = createArgMap(list.concat(list.rest).join(""), ';');
192
+ }
193
+ if (args.indexOf("@value") < 0) args.push("@value");
194
+ if (args.indexOf("@key") < 0) args.push("@key");
195
+ if (args.indexOf("@index") < 0) args.push("@index");
196
+ var reg = new RegExp(args.join("|") + /|@\{[^@\{]+\}/.source, 'g');
197
+ var defaults = list.defaults;
198
+ var argsMap = null;
199
+ var replace = a => {
200
+ if (/^@\{/.test(a)) {
201
+ var k = "@" + a.slice(2, -1).trim();
202
+ if (k in argsMap) return strings.decode(argsMap[k]);
203
+ return a;
204
+ }
205
+ if (a in argsMap) return argsMap[a];
206
+ return a;
207
+ };
208
+ if (defaults) list = list.map(function (a, i) {
209
+ argsMap = {
210
+ "@value": defaults[a],
211
+ "@key": a,
212
+ "@index": i + 1,
213
+ [args[0]]: defaults[a],
214
+ [args[1]]: a,
215
+ [args[2]]: i + 1,
216
+ };
217
+ return content.replace(reg, replace);
218
+ });
219
+ else list = list.map(function (a, i) {
220
+ argsMap = {
221
+ "@value": a,
222
+ "@key": i + 1,
223
+ "@index": i + 1,
224
+ };
225
+ return content.replace(reg, replace);
226
+ });
227
+ return list;
228
+ };
229
+
230
+
2
231
  class Richcss extends Program {
3
- straps = [];
232
+ straps = ["and"];
4
233
  stamps = `;:`.split("");
5
- quotes = this.quotes.slice(0, 2);
6
- scopes = [["{", "}"]]
234
+ quotes = rarg.quotes;
235
+ keepspace = true;
236
+ scopes = [["{", "}"], ["(", ")"]]
7
237
  }
238
+
8
239
  var presets = /^@(media|keyframes|layer|import|namespace|page|property|suppports|font-face|document|counter-style|charset|color-profile|container|font-feature-values|font-palette-values)(\s|\(|$)/i;
9
240
 
10
241
  Richcss.prototype.setType = function (o) {
11
242
  var p = o.prev;
12
243
  if (o.type !== SCOPED) {
13
- if (!p || p.type === STAMP && p.text === ";" || p.type === SCOPED) {
244
+ if (!p || p.type === STAMP && p.text === ";" || p.type === SCOPED && p.entry === '{') {
14
245
  o.type = PROPERTY;
15
246
  return;
16
247
  }
17
248
  }
18
249
  if (!p) return;
19
250
  var q = o.queue;
251
+ if (o.type & (PROPERTY | EXPRESS) && p && p === q[q.length - 1] && p.type & (PROPERTY | EXPRESS)) {
252
+ return false;
253
+ }
20
254
  if (o.type === SCOPED && o.entry === "{") {
21
- if (p.type & (PROPERTY | EXPRESS) && /@$/.test(p.text)) {
255
+ if (p && p.type & (PROPERTY | EXPRESS) && /@$/.test(p.text)) {
22
256
  return false;
23
257
  }
24
258
  var pps = [];
@@ -46,7 +280,7 @@ Richcss.prototype.setType = function (o) {
46
280
  return;
47
281
  }
48
282
  }
49
- if (o.type !== STAMP || o.text !== ";") return false;
283
+ if (o.type === STAMP && o.text !== ";") return false;
50
284
  }
51
285
  };
52
286
 
@@ -56,7 +290,7 @@ Richcss.prototype.createScoped = function (code) {
56
290
  for (var cx = s.length - 1; cx >= 0; cx--) {
57
291
  var { p: k, v } = s[cx];
58
292
  if (/^\-\-|^@[^\{]/.test(k) && !("used" in v) && v.length) {
59
- if (!vars) vars = {};
293
+ if (!vars) vars = Object.create(null);
60
294
  vars[k] = v.join(" ");
61
295
  s.splice(cx, 1);
62
296
  }
@@ -69,50 +303,72 @@ Richcss.prototype.createScoped = function (code) {
69
303
  s.used = used;
70
304
  s.vars = vars;
71
305
  };
72
- var run = function (o) {
306
+ var run = function (code) {
73
307
  var props = [];
74
- var values = null;
75
- loop: while (o) {
76
- switch (o.type) {
77
- case PROPERTY:
78
- var p = [];
79
- while (o && !(o.type & (STAMP | SCOPED))) {
80
- p.push(o.text);
81
- o = o.next;
308
+ var propmap = Object.create(null);
309
+ for (var cx = 0, dx = code.length; cx < dx; cx++) {
310
+ var o = code[cx];
311
+ if (o && (o.type & (SPACE | COMMENT) || o.type === STAMP && o.text === ';')) continue;
312
+ if (o.type !== PROPERTY) throw new Error("结构异常");
313
+ var p = [], v = [];
314
+ while (o && (o.type !== SCOPED || o.entry !== "{")) {
315
+ if (o.type === STAMP) break;
316
+ p.push(o);
317
+ o = code[++cx];
318
+ }
319
+ if (o && o.type === STAMP && o.text === ':') {
320
+ var n = code[++cx];
321
+ var tmp = [];
322
+ while (n && (n.type !== STAMP || n.text !== ";")) {
323
+ if (n.type === SCOPED && n.entry === '{') {
324
+ break;
82
325
  }
83
- var pj = p.join(' ');
84
- props.push({ p: pj, v: values = [] });
85
- if (!props[pj]) props[pj] = [];
86
- props[pj].push(values);
87
- if (!o) break loop;
88
- if (o.type === STAMP) break;
89
- continue;
90
- case STAMP:
91
- if (o.text === ';') break;
92
- case EXPRESS:
93
- case VALUE:
94
- case QUOTED:
95
- values.push(o.text);
96
- break;
97
- case SCOPED:
98
- var s = run(o.first);
99
- values.used = s.used;
100
- values.vars = s.vars;
101
-
326
+ tmp.push(n);
327
+ n = code[++cx];
328
+ }
329
+ if (n && n.type === SCOPED) {
330
+ o.unary = true;
331
+ p.push(o, ...tmp);
332
+ v = run(n);
333
+ }
334
+ else {
335
+ v = tmp;
336
+ }
337
+ o = n;
102
338
  }
103
- o = o.next;
339
+ else if (o) {
340
+ v = run(o);
341
+ }
342
+ p.autospace = false;
343
+ var pj = createString(p).trim();
344
+ if (!propmap[pj]) propmap[pj] = [];
345
+ var vs = [];
346
+ if (v.used === undefined) {
347
+ if (v.length) vs.push(createString(v).trim());
348
+ }
349
+ else vs.used = v.used, vs.vars = v.vars;
350
+ props.push({ p: pj, v: vs })
351
+ propmap[pj].push(vs);
104
352
  }
353
+ props.maps = propmap;
105
354
  setVarsUsed(props);
106
355
  return props;
107
356
  };
108
- return run(code.first);
357
+ return run(code);
109
358
  };
110
359
  Richcss.prototype.createString = createString;
111
- var getFromScopeList = function (name, varsList, value) {
360
+ var getFromScopeList = function (name, varsList, value = name) {
112
361
  name = name.replace(/^@\{\s*(\S*)\s*\}$/g, '@$1');
362
+ var queue = [];
113
363
  for (var cx = varsList.length - 1; cx >= 0; cx--) {
114
364
  var o = varsList[cx];
115
- if (name in o) return o[name];
365
+ if (name in o) queue = [];
366
+ while (name in o) {
367
+ name = o[name];
368
+ if (typeof name !== "string" || !/^\-\-|^@[^\{]/.test(name)) return name;
369
+ if (queue.indexOf(name) >= 0) throw `变量环形引用,无法初始化:${queue}`;
370
+ queue.push(name);
371
+ }
116
372
  }
117
373
  return value;
118
374
  }
@@ -136,28 +392,25 @@ var fixBase = function (b, a) {
136
392
  }).join(",");
137
393
  }
138
394
  function evalscoped(scoped, scopeNames, base = '') {
139
- var root = scoped[":root"], scope = scoped[":scope"], and = scoped["&"];
395
+ var smaps = scoped.maps;
396
+ var root = smaps[":root"], scope = smaps[":scope"], and = smaps["&"];
140
397
  var vars = extend(Object.create(null), scoped.vars);
141
398
  if (root) root.forEach(r => extend(vars, r.vars));
142
399
  if (scope) scope.forEach(s => extend(vars, s.vars));
143
400
  if (and) and.forEach(s => extend(vars, s.vars));
144
401
  scopeNames.forEach(s => {
145
- var ss = scoped[s];
402
+ var ss = smaps[s];
146
403
  if (ss) ss.forEach(s => {
147
404
  extend(vars, s.vars), s.rooted = true;
148
405
  })
149
406
  });
150
407
  var vlist = [vars];
151
- var mlist = [];
408
+ var mlist = [macros];
409
+ var clist = [smaps];
152
410
  var initvars = function (vars) {
153
- var queue = [];
154
411
  for (var k in vars) {
155
412
  var v = vars[k];
156
- while (/^\-\-|^@[^\{]/.test(v)) {
157
- if (queue.indexOf(v) >= 0) throw `变量环形引用,无法初始化:${queue}`;
158
- queue.push(v);
159
- v = getFromScopeList(v, vlist);
160
- }
413
+ v = getFromScopeList(v, vlist);
161
414
  vars[k] = v;
162
415
  }
163
416
  };
@@ -166,85 +419,119 @@ function evalscoped(scoped, scopeNames, base = '') {
166
419
  var eval2 = function (props) {
167
420
  var rest = [];
168
421
  var result = [];
169
- var methods = {};
422
+ var methods = Object.create(null);
170
423
  mlist.push(methods);
424
+ if (props.maps) clist.push(props.maps);
171
425
  var evalthis = function (p) {
172
- if (p.vars) vlist.push(p.vars);
173
- initvars(p.vars);
174
426
  var temp = base;
175
427
  base = p.base;
176
428
  var res = eval2(p.used);
177
429
  base = temp;
178
- if (p.vars) vlist.pop();
179
430
  return res;
180
431
  };
181
432
  var calcvars = function (v) {
182
- return v.replace(/(^|\s|[\]\)\(\[\-\+\*\/])(?:var\s*\(([\s\S]*?)\)|(--\S+|@[^\s\{\(\:\+\*\/]+|@\{[^\}@]*\}))/g, function (m, q, a, b) {
433
+ return v.replace(/(^|\s|[\]\)\(\[\-\+\*\/,;])(?:var\s*\(([\s\S]*?)\)|(--\S+|@[^\s\{\(\:\+\*\/,;\!\[\>\$\=\&\%\#\@\+'"`\?\.\/\|~]+|@\{[^\}@]*\}))/g, function (m, q, a, b) {
183
434
  return q + getFromScopeList(b || a.trim(), vlist, m.slice(q.length));
184
435
  });
185
436
  };
437
+ var evalproc = function (k, retnoparam) {
438
+ var match = (retnoparam !== false ? /^([^\(\)\s,;:]+)(?:\s*\(([\s\S]*)\))$/ : /^([^\(\)\s,;:]+)(?:\s*\(([\s\S]*)\))?$/).exec(k);
439
+ if (!match) return calcvars(k);
440
+ var [, name, params] = match;
441
+ params = splitParams(params);
442
+ var method = getFromScopeList(name, mlist);
443
+ if (!isFunction(method)) {
444
+ if (/^@/.test(name)) return calcvars(k);
445
+ var block = getFromScopeList(name, clist, null);
446
+ if (block) {
447
+ var res = [];
448
+ var rest = [];
449
+ block.map(eval2).forEach(e => {
450
+ res = res.concat(e);
451
+ rest = rest.concat(e.rest);
452
+ });
453
+ return res.concat(rest).join('');
454
+ }
455
+ else return k;
456
+ };
457
+ params = params.map(evalproc);
458
+ return method.apply(null, params);
459
+ };
460
+
186
461
  for (var { p: k, v: p } of props) {
187
462
  if (p.used) {
188
- var match = /^(@[^\s,]+)\s*\(\s*(@[^\s,]+\s*(?:,\s*@[^\s,]+\s*)*)?\)/.exec(k);
463
+ var match = /^([@\.#][^\s,]+)\s*\(([\s\S]*?)\)\s*$/.exec(k);
189
464
  if (!match) continue;
190
465
  if (presets.test(match[1])) continue;
191
- p.base = base;
192
466
  var [, name, args] = match;
193
- args = args.split(",").map(a => a.trim().split(':'));
194
- p.argDefaults = args.map(a => a[1]);
195
- args = p.args = args.map(a => a[0]);
196
- p.reg = new RegExp(args.join("|"), 'g');
467
+ p.base = base;
468
+ args = createArgMap(args);
469
+ p.args = args;
470
+ p.reg = new RegExp(args.join("|") + /|@\{[^\}@]+\}/.source, 'g');
197
471
  if (!methods[name]) methods[name] = [];
198
- methods[name].push(function () {
199
- var body = evalthis(this);
200
- var valueMap = {};
201
- var argDefaults = p.argDefaults;
472
+ var argDefaults = args.defaults;
473
+ Object.keys(argDefaults).forEach(k => {
474
+ argDefaults[k] = calcvars(argDefaults[k]);
475
+ });
476
+ methods[name] = function () {
477
+ var valueMap = Object.create(null);
478
+ vlist.push(valueMap);
479
+ var argDefaults = this.args.defaults;
202
480
  this.args.forEach((k, i) => {
203
481
  var a = arguments[i];
204
- if (a === undefined || a === null) a = argDefaults[i];
482
+ if (a === undefined || a === null) a = seprateFunc(calcvars(argDefaults[k])).map(evalproc).join('');
205
483
  valueMap[k] = a;
206
484
  });
207
485
  var replace = text => text.replace(this.reg, function (name) {
208
- if (name in valueMap) return valueMap[name];
486
+ if (/^\@\{/.test(name)) {
487
+ var key = "@" + name.slice(2, -1);
488
+ if (key in valueMap) return strings.decode(valueMap[key]);
489
+ }
490
+ else if (name in valueMap) return valueMap[name];
209
491
  return name;
210
492
  });
493
+ var vars = this.vars;
494
+ if (vars) Object.keys(vars).forEach(k => {
495
+ valueMap[k] = replace(calcvars(vars[k]));
496
+ });
497
+ var body = evalthis(this);
211
498
  var rest = body.rest.map(a => a.map(replace));
212
499
  var body = body.map(replace);
213
500
  body.rest = rest;
501
+ vlist.pop();
214
502
  return body;
215
- }.bind(p));
503
+ }.bind(p);
216
504
  p.isMethod = true;
217
505
  }
218
506
  }
219
507
  for (var { p: k, v: p } of props) {
220
508
  if (p.isMethod) continue;
221
- k = calcvars(k);
222
509
  if (p.used) {
510
+ k = calcvars(k);
223
511
  if (base && !p.rooted) p.base = fixBase(base, k);
224
512
  else p.base = presets.test(k) ? `@{${k}}` : k;
513
+ if (p.vars) vlist.push(p.vars);
225
514
  var value = evalthis(p);
515
+ if (p.vars) vlist.pop();
226
516
  if (value.rest.length) rest = rest.concat(value.rest);
227
517
  if (value.length) rest.push([p.base, '{', value.join(""), "}"]);
228
518
  }
229
519
  else if (p.length) {
230
- result.push(k, ":", calcvars(p.join(" ")), ';');
520
+ k = calcvars(k);
521
+ p = calcvars(p.join(" "));
522
+ p = seprateFunc(p).map(evalproc).join('');
523
+ result.push(k, ":", p, ';');
231
524
  }
232
525
  else {
233
- var match = /^(@\S+)\s*\(([\s\S]*)\)$/.exec(k);
234
- if (!match) continue;
235
- if (presets.test(match[1])) continue;
236
- var [, name, params] = match;
237
- params = params.split(",").map(a => a.trim());
238
- var method = getFromScopeList(name, mlist);
239
- if (!isArray(method)) throw `函数未定义:${name}`;
240
- method.forEach(m => {
241
- var res = m.apply(null, params);
242
- if (res.rest.length) rest = rest.concat(res.rest);
526
+ var res = evalproc(k, false);
527
+ if (res instanceof Array) {
528
+ if (res.rest && res.rest.length) rest = rest.concat(res.rest);
243
529
  if (res.length) result = result.concat(res);
244
- });
530
+ }
245
531
  }
246
532
  }
247
533
  mlist.pop();
534
+ if (props.maps) clist.pop();
248
535
  result.rest = rest;
249
536
  return result;
250
537
  }
@@ -271,36 +558,39 @@ function richcss(text, scopeName, compress) {
271
558
  })
272
559
  var { scoped } = code;
273
560
  var result = evalscoped(scoped, scopeNames, scopeName);
561
+ var queried = [];
562
+ var getquried = function () {
563
+ if (!queried.length) return "";
564
+ var ats = queried.key.split(';');
565
+ var b = /@keyframes\s/i.test(queried.key) ? queried.map(a => a.replace(new RegExp(/^\s*/.source + scopeName + /\s/.source), "")).join('') : queried.join('');
566
+ while (ats.length) {
567
+ b = ats.pop() + `{${b}}`;
568
+ }
569
+ queried = [];
570
+ return b;
571
+ }
274
572
  return result.rest.map(a => a.join("")).concat(result).map(a => {
275
573
  var ats = [];
276
574
  a = a.replace(/@\{(@[^\}]*)\}\s*/g, function (_, q) {
277
575
  ats.push(q);
278
576
  return ''
279
- })
280
- .replace(/((?:[\+\-]+\s*)?(?:\d+(?:\.\d*)?|\.\d+))\s*(px|%|pt|cm|mm|r?em)?\s*([\/\*\+\-])\s*((?:[\+\-]+\s*)?(?:\d+(?:\.\d*)?|\.\d+))(px|%|pt|cm|mm|r?em)?/ig, function (_, d1, p1 = '', c, d2, p2 = '') {
281
- d1 = eval(d1);
282
- d2 = eval(d2);
283
- if (!p2) {
284
- if (c === '*') {
285
- return d1 * d2 + p1;
286
- }
287
- if (c === '/') {
288
- return d1 / d2 + p1;
289
- }
290
- }
291
- else if (p1 === p2) {
292
- if (c === "+") {
293
- return (+d1 + +d2) + p1;
294
- }
295
- if (c === '-') {
296
- return (d1 - d2) + p1;
297
- }
298
- }
299
- return _;
300
- })
301
- while (ats.length) {
302
- a = ats.pop() + `{${a}}`;
577
+ }).replace(/~\s*(['"`])((?:\\[\s\S]|[^'"`\\])*?)\1/g, '$2');
578
+ if (!a) return '';
579
+ var atk = ats.join(';');
580
+ if (queried.key !== atk) {
581
+ var b = getquried();
582
+ if (!atk) return b + a;
583
+ queried = [a];
584
+ queried.key = atk;
585
+ return b;
586
+ }
587
+ else if (atk) {
588
+ queried.push(a);
589
+ return '';
590
+ }
591
+ else if (queried.length) {
592
+ return getquried() + a;
303
593
  }
304
594
  return a;
305
- }).join(compress ? "" : "\r\n");
595
+ }).filter(a => !!a).join(compress ? "" : "\r\n") + getquried();
306
596
  }
@@ -7,17 +7,17 @@ test(`:scope{--a:1}a{opacity:--a}`, `a{opacity:1;}`);
7
7
  test(`:scope{--b:--a;--a:1;}a{opacity:--b}`, `a{opacity:1;}`);
8
8
  test(`@a(@p,@b){@p{opacity:@b}}@a(a,1);`, `a{opacity:1;}`);
9
9
  test(`@a(a,1);@a(@p,@b){@p{opacity:@b}}`, `a{opacity:1;}`);
10
- test(`a{ b{a:1}}`,`a b{a:1;}`);
11
- test(`a{ >b{a:1}}`,`a>b{a:1;}`);
12
- test(`a{ &>b{a:1}}`,`a>b{a:1;}`);
13
- test(`a{ &b{a:1}}`,`ab{a:1;}`);
14
- test(`a{ &.b{a:1}}`,`a.b{a:1;}`);
15
- test(`a{ &[b]{a:1}}`,`a[b]{a:1;}`);
16
- test(`a{ &[b]:nth-child(1){a:1}}`,`a[b]:nth-child(1){a:1;}`);
17
- test(`a,b{c{a:1}}`,`a c,b c{a:1;}`);
18
- test(`@media(){div{a:1}}`,`@media(){div{a:1;}}`);
19
- test(`@keyframes a{%1{a:1}}`,`@keyframes a{%1{a:1;}}`);
20
- test(`@media screen and (max-width:200px){@keyframes a{%1{a:1}}}`,`@media screen and (max-width:200px){@keyframes a{%1{a:1;}}}`);
10
+ test(`a{ b{a:1}}`, `a b{a:1;}`);
11
+ test(`a{ >b{a:1}}`, `a>b{a:1;}`);
12
+ test(`a{ &>b{a:1}}`, `a>b{a:1;}`);
13
+ test(`a{ &b{a:1}}`, `ab{a:1;}`);
14
+ test(`a{ &.b{a:1}}`, `a.b{a:1;}`);
15
+ test(`a{ &[b]{a:1}}`, `a[b]{a:1;}`);
16
+ test(`a{ &[b]:nth-child(1){a:1}}`, `a[b]:nth-child(1){a:1;}`);
17
+ test(`a,b{c{a:1}}`, `a c,b c{a:1;}`);
18
+ test(`@media(){div{a:1}}`, `@media(){div{a:1;}}`);
19
+ test(`@keyframes a{%1{a:1}}`, `@keyframes a{%1{a:1;}}`);
20
+ test(`@media screen and (max-width: 200px){@keyframes a{%1{a:1}}}`, `@media screen and (max-width: 200px){@keyframes a{%1{a:1;}}}`);
21
21
  test(`@a:1`, ``);
22
22
  test(`@a:1;a{a:@a}`, `a{a:1;}`);
23
23
  test(`@a:1;a{@{a}:@a}`, `a{1:1;}`);
@@ -25,3 +25,16 @@ test(`@a:1;a{@a:2;@{a}:@a}`, `a{2:2;}`);
25
25
  test(`@a:1;@a{@a:2;@{a}:@a}`, `1{2:2;}`);
26
26
  test(`@b(@a:1){@a{a:b}}@b(2)`, `2{a:b;}`);
27
27
  test(`@a:1; a{a:@a/2}`, `a{a:0.5;}`);
28
+ test(`.a(){b{a:2}} .a();`, `b{a:2;}`);
29
+ test(`#a(){b{a:2}} #a();`, `b{a:2;}`);
30
+ test(`#a{a:1}`, `#a{a:1;}`);
31
+ test(`#a{}`, ``);
32
+ test(`@a{}`, ``);
33
+ test(`.a{}`, ``);
34
+ test(`a{}`, ``);
35
+ test(`@a: 1,2;each(@a,(){b{a:@value}})`, `b{a:1;}\r\nb{a:2;}`);
36
+ test(`@a(){a:A;b:B} each(@a(),(@v,@k,@i){@{key}@{i}{@{v}:@i}})`, `a1{A:1;}\r\nb2{B:2;}`);
37
+ test(`b{a:darken(#fff,10%)}`, `b{a:#f5f5f5;}`);
38
+ test(`b{a:darken(hsl(90, 80%, 50%), 20%)}`, `b{a:#6cd205;}`);
39
+ test(`b{a:darken(#6cd205, 20%)}`, `b{a:#58be00;}`);
40
+ test(`b{a:darken(#7ff,10%)}`, `b{a:#6df5f5;}`);