@tbela99/css-parser 0.0.1-rc6 → 0.0.1-rc7
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.
- package/dist/index-umd-web.js +234 -192
- package/dist/index.cjs +234 -192
- package/dist/index.d.ts +4 -3
- package/dist/lib/ast/expand.js +4 -4
- package/dist/lib/ast/minify.js +57 -15
- package/dist/lib/ast/walk.js +2 -6
- package/dist/lib/parser/parse.js +2 -2
- package/dist/lib/parser/tokenize.js +6 -2
- package/dist/lib/renderer/render.js +2 -1
- package/dist/node/index.js +1 -1
- package/dist/web/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -770,7 +770,8 @@ function renderToken(token, options = {}, reducer, errors) {
|
|
|
770
770
|
}
|
|
771
771
|
return val + unit;
|
|
772
772
|
case 'Perc':
|
|
773
|
-
|
|
773
|
+
const perc = reduceNumber(token.val);
|
|
774
|
+
return options.minify && perc == '0' ? '0' : perc + '%';
|
|
774
775
|
case 'Number':
|
|
775
776
|
return reduceNumber(token.val);
|
|
776
777
|
case 'Comment':
|
|
@@ -2242,12 +2243,16 @@ function* tokenize(iterator) {
|
|
|
2242
2243
|
}
|
|
2243
2244
|
function next(count = 1) {
|
|
2244
2245
|
let char = '';
|
|
2245
|
-
|
|
2246
|
+
let chr = '';
|
|
2247
|
+
if (count < 0) {
|
|
2248
|
+
return '';
|
|
2249
|
+
}
|
|
2250
|
+
while (count-- && (chr = iterator.charAt(ind + 1))) {
|
|
2251
|
+
char += chr;
|
|
2246
2252
|
const codepoint = iterator.charCodeAt(++ind);
|
|
2247
2253
|
if (isNaN(codepoint)) {
|
|
2248
2254
|
return char;
|
|
2249
2255
|
}
|
|
2250
|
-
char += iterator.charAt(ind);
|
|
2251
2256
|
if (isNewLine(codepoint)) {
|
|
2252
2257
|
lin++;
|
|
2253
2258
|
col = 0;
|
|
@@ -2814,7 +2819,7 @@ async function parse$1(iterator, opt = {}) {
|
|
|
2814
2819
|
if (delim.typ == 'Block-start') {
|
|
2815
2820
|
const position = map.get(tokens[0]);
|
|
2816
2821
|
const uniq = new Map;
|
|
2817
|
-
parseTokens(tokens, { minify:
|
|
2822
|
+
parseTokens(tokens, { minify: true }).reduce((acc, curr, index, array) => {
|
|
2818
2823
|
if (curr.typ == 'Whitespace') {
|
|
2819
2824
|
if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
|
|
2820
2825
|
trimWhiteSpace.includes(array[index + 1]?.typ) ||
|
|
@@ -2823,7 +2828,7 @@ async function parse$1(iterator, opt = {}) {
|
|
|
2823
2828
|
return acc;
|
|
2824
2829
|
}
|
|
2825
2830
|
}
|
|
2826
|
-
let t = renderToken(curr, { minify:
|
|
2831
|
+
let t = renderToken(curr, { minify: false });
|
|
2827
2832
|
if (t == ',') {
|
|
2828
2833
|
acc.push([]);
|
|
2829
2834
|
}
|
|
@@ -3824,10 +3829,180 @@ class PropertyList {
|
|
|
3824
3829
|
}
|
|
3825
3830
|
}
|
|
3826
3831
|
|
|
3832
|
+
function* walk(node, parent, root) {
|
|
3833
|
+
yield { node, parent, root };
|
|
3834
|
+
if ('chi' in node) {
|
|
3835
|
+
for (const child of node.chi) {
|
|
3836
|
+
yield* walk(child, node, (root ?? node));
|
|
3837
|
+
}
|
|
3838
|
+
}
|
|
3839
|
+
}
|
|
3840
|
+
function* walkValues(values, parent) {
|
|
3841
|
+
for (const value of values) {
|
|
3842
|
+
// @ts-ignore
|
|
3843
|
+
yield { value, parent };
|
|
3844
|
+
if ('chi' in value) {
|
|
3845
|
+
yield* walkValues(value.chi, value);
|
|
3846
|
+
}
|
|
3847
|
+
}
|
|
3848
|
+
}
|
|
3849
|
+
|
|
3850
|
+
function expand(ast) {
|
|
3851
|
+
//
|
|
3852
|
+
if (!['Rule', 'StyleSheet', 'AtRule'].includes(ast.typ)) {
|
|
3853
|
+
return ast;
|
|
3854
|
+
}
|
|
3855
|
+
if ('Rule' == ast.typ) {
|
|
3856
|
+
return {
|
|
3857
|
+
typ: 'StyleSheet',
|
|
3858
|
+
chi: expandRule(ast)
|
|
3859
|
+
};
|
|
3860
|
+
}
|
|
3861
|
+
if (!('chi' in ast)) {
|
|
3862
|
+
return { ...ast };
|
|
3863
|
+
}
|
|
3864
|
+
const result = { ...ast, chi: [] };
|
|
3865
|
+
// @ts-ignore
|
|
3866
|
+
for (let i = 0; i < ast.chi.length; i++) {
|
|
3867
|
+
// @ts-ignore
|
|
3868
|
+
const node = ast.chi[i];
|
|
3869
|
+
if (node.typ == 'Rule') {
|
|
3870
|
+
// @ts-ignore
|
|
3871
|
+
result.chi.push(...expandRule(node));
|
|
3872
|
+
// i += expanded.length - 1;
|
|
3873
|
+
}
|
|
3874
|
+
else if (node.typ == 'AtRule' && 'chi' in node) {
|
|
3875
|
+
let hasRule = false;
|
|
3876
|
+
let j = node.chi.length;
|
|
3877
|
+
while (j--) {
|
|
3878
|
+
if (node.chi[j].typ == 'Rule' || node.chi[j].typ == 'AtRule') {
|
|
3879
|
+
hasRule = true;
|
|
3880
|
+
break;
|
|
3881
|
+
}
|
|
3882
|
+
}
|
|
3883
|
+
// @ts-ignore
|
|
3884
|
+
result.chi.push({ ...(hasRule ? expand(node) : node) });
|
|
3885
|
+
}
|
|
3886
|
+
else {
|
|
3887
|
+
// @ts-ignore
|
|
3888
|
+
result.chi.push(node);
|
|
3889
|
+
}
|
|
3890
|
+
}
|
|
3891
|
+
return result;
|
|
3892
|
+
}
|
|
3893
|
+
function expandRule(node) {
|
|
3894
|
+
const ast = { ...node, chi: node.chi.slice() };
|
|
3895
|
+
const result = [];
|
|
3896
|
+
if (ast.typ == 'Rule') {
|
|
3897
|
+
let i = 0;
|
|
3898
|
+
// @ts-ignore
|
|
3899
|
+
delete ast.raw;
|
|
3900
|
+
// @ts-ignore
|
|
3901
|
+
delete ast.optimized;
|
|
3902
|
+
for (; i < ast.chi.length; i++) {
|
|
3903
|
+
if (ast.chi[i].typ == 'Rule') {
|
|
3904
|
+
const rule = ast.chi[i];
|
|
3905
|
+
if (!rule.sel.includes('&')) {
|
|
3906
|
+
const selRule = splitRule(rule.sel);
|
|
3907
|
+
selRule.forEach(arr => combinators.includes(arr[0].charAt(0)) ? arr.unshift(ast.sel) : arr.unshift(ast.sel, ' '));
|
|
3908
|
+
rule.sel = selRule.reduce((acc, curr) => {
|
|
3909
|
+
acc.push(curr.join(''));
|
|
3910
|
+
return acc;
|
|
3911
|
+
}, []).join(',');
|
|
3912
|
+
}
|
|
3913
|
+
else {
|
|
3914
|
+
rule.sel = replaceCompound(rule.sel, ast.sel);
|
|
3915
|
+
}
|
|
3916
|
+
delete rule.raw;
|
|
3917
|
+
delete rule.optimized;
|
|
3918
|
+
ast.chi.splice(i--, 1);
|
|
3919
|
+
result.push(...expandRule(rule));
|
|
3920
|
+
}
|
|
3921
|
+
else if (ast.chi[i].typ == 'AtRule') {
|
|
3922
|
+
let astAtRule = ast.chi[i];
|
|
3923
|
+
const values = [];
|
|
3924
|
+
if (astAtRule.nam == 'scope') {
|
|
3925
|
+
if (astAtRule.val.includes('&')) {
|
|
3926
|
+
astAtRule.val = replaceCompound(astAtRule.val, ast.sel);
|
|
3927
|
+
}
|
|
3928
|
+
// @ts-ignore
|
|
3929
|
+
astAtRule = expand(astAtRule);
|
|
3930
|
+
}
|
|
3931
|
+
else {
|
|
3932
|
+
// @ts-ignore
|
|
3933
|
+
const clone = { ...ast, chi: astAtRule.chi.slice() };
|
|
3934
|
+
// @ts-ignore
|
|
3935
|
+
astAtRule.chi.length = 0;
|
|
3936
|
+
for (const r of expandRule(clone)) {
|
|
3937
|
+
if (r.typ == 'AtRule' && 'chi' in r) {
|
|
3938
|
+
if (astAtRule.val !== '' && r.val !== '') {
|
|
3939
|
+
if (astAtRule.nam == 'media' && r.nam == 'media') {
|
|
3940
|
+
r.val = astAtRule.val + ' and ' + r.val;
|
|
3941
|
+
}
|
|
3942
|
+
else if (astAtRule.nam == 'layer' && r.nam == 'layer') {
|
|
3943
|
+
r.val = astAtRule.val + '.' + r.val;
|
|
3944
|
+
}
|
|
3945
|
+
}
|
|
3946
|
+
// @ts-ignore
|
|
3947
|
+
values.push(r);
|
|
3948
|
+
}
|
|
3949
|
+
else if (r.typ == 'Rule') {
|
|
3950
|
+
// @ts-ignore
|
|
3951
|
+
astAtRule.chi.push(...expandRule(r));
|
|
3952
|
+
}
|
|
3953
|
+
else {
|
|
3954
|
+
// @ts-ignore
|
|
3955
|
+
astAtRule.chi.push(r);
|
|
3956
|
+
}
|
|
3957
|
+
}
|
|
3958
|
+
}
|
|
3959
|
+
// @ts-ignore
|
|
3960
|
+
result.push(...(astAtRule.chi.length > 0 ? [astAtRule].concat(values) : values));
|
|
3961
|
+
ast.chi.splice(i--, 1);
|
|
3962
|
+
}
|
|
3963
|
+
}
|
|
3964
|
+
}
|
|
3965
|
+
// @ts-ignore
|
|
3966
|
+
return ast.chi.length > 0 ? [ast].concat(result) : result;
|
|
3967
|
+
}
|
|
3968
|
+
function replaceCompound(input, replace) {
|
|
3969
|
+
const tokens = parseString(input);
|
|
3970
|
+
for (const t of walkValues(tokens)) {
|
|
3971
|
+
if (t.value.typ == 'Literal') {
|
|
3972
|
+
if (t.value.val == '&') {
|
|
3973
|
+
t.value.val = replace;
|
|
3974
|
+
}
|
|
3975
|
+
else if (t.value.val.length > 1 && t.value.val.charAt(0) == '&') {
|
|
3976
|
+
t.value.val = replaceCompoundLiteral(t.value.val, replace);
|
|
3977
|
+
}
|
|
3978
|
+
}
|
|
3979
|
+
}
|
|
3980
|
+
return tokens.reduce((acc, curr) => acc + renderToken(curr), '');
|
|
3981
|
+
}
|
|
3982
|
+
function replaceCompoundLiteral(selector, replace) {
|
|
3983
|
+
const tokens = [''];
|
|
3984
|
+
let i = 0;
|
|
3985
|
+
for (; i < selector.length; i++) {
|
|
3986
|
+
if (selector.charAt(i) == '&') {
|
|
3987
|
+
tokens.push('&');
|
|
3988
|
+
tokens.push('');
|
|
3989
|
+
}
|
|
3990
|
+
else {
|
|
3991
|
+
tokens[tokens.length - 1] += selector.charAt(i);
|
|
3992
|
+
}
|
|
3993
|
+
}
|
|
3994
|
+
return tokens.sort((a, b) => {
|
|
3995
|
+
if (a == '&') {
|
|
3996
|
+
return 1;
|
|
3997
|
+
}
|
|
3998
|
+
return b == '&' ? -1 : 0;
|
|
3999
|
+
}).reduce((acc, curr) => acc + (curr == '&' ? replace : curr), '');
|
|
4000
|
+
}
|
|
4001
|
+
|
|
3827
4002
|
const combinators = ['+', '>', '~'];
|
|
3828
4003
|
const notEndingWith = ['(', '['].concat(combinators);
|
|
3829
4004
|
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
|
|
3830
|
-
function minify(ast, options = {}, recursive = false, errors) {
|
|
4005
|
+
function minify(ast, options = {}, recursive = false, errors, nestingContent) {
|
|
3831
4006
|
function wrapNodes(previous, node, match, ast, i, nodeIndex) {
|
|
3832
4007
|
// @ts-ignore
|
|
3833
4008
|
let pSel = match.selector1.reduce(reducer, []).join(',');
|
|
@@ -4116,8 +4291,28 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4116
4291
|
selector2
|
|
4117
4292
|
};
|
|
4118
4293
|
}
|
|
4294
|
+
function fixSelector(node) {
|
|
4295
|
+
// @ts-ignore
|
|
4296
|
+
if (node.sel.includes('&')) {
|
|
4297
|
+
const attributes = parseString(node.sel);
|
|
4298
|
+
for (const attr of walkValues(attributes)) {
|
|
4299
|
+
if (attr.value.typ == 'Pseudo-class-func' && attr.value.val == ':is') {
|
|
4300
|
+
let i = attr.value.chi.length;
|
|
4301
|
+
while (i--) {
|
|
4302
|
+
if (attr.value.chi[i].typ == 'Literal' && attr.value.chi[i].val == '&') {
|
|
4303
|
+
attr.value.chi.splice(i, 1);
|
|
4304
|
+
}
|
|
4305
|
+
}
|
|
4306
|
+
}
|
|
4307
|
+
}
|
|
4308
|
+
node.sel = attributes.reduce((acc, curr) => acc + renderToken(curr), '');
|
|
4309
|
+
}
|
|
4310
|
+
}
|
|
4119
4311
|
// @ts-ignore
|
|
4120
4312
|
if (('chi' in ast) && ast.chi?.length > 0) {
|
|
4313
|
+
if (!nestingContent) {
|
|
4314
|
+
nestingContent = options.nestingRules && ast.typ == 'Rule';
|
|
4315
|
+
}
|
|
4121
4316
|
let i = 0;
|
|
4122
4317
|
let previous;
|
|
4123
4318
|
let node;
|
|
@@ -4132,7 +4327,6 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4132
4327
|
node = ast.chi[i];
|
|
4133
4328
|
// @ts-ignore
|
|
4134
4329
|
if (previous == node) {
|
|
4135
|
-
// console.error('idem!');
|
|
4136
4330
|
// @ts-ignore
|
|
4137
4331
|
ast.chi.splice(i, 1);
|
|
4138
4332
|
i--;
|
|
@@ -4148,7 +4342,6 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4148
4342
|
i--;
|
|
4149
4343
|
continue;
|
|
4150
4344
|
}
|
|
4151
|
-
// console.debug({previous, node});
|
|
4152
4345
|
// @ts-ignore
|
|
4153
4346
|
if (previous?.typ == 'AtRule' &&
|
|
4154
4347
|
previous.nam == node.nam &&
|
|
@@ -4168,7 +4361,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4168
4361
|
minifyRule(node);
|
|
4169
4362
|
}
|
|
4170
4363
|
else {
|
|
4171
|
-
minify(node, options, recursive, errors);
|
|
4364
|
+
minify(node, options, recursive, errors, nestingContent);
|
|
4172
4365
|
}
|
|
4173
4366
|
previous = node;
|
|
4174
4367
|
nodeIndex = i;
|
|
@@ -4222,7 +4415,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4222
4415
|
nodeIndex = --i;
|
|
4223
4416
|
// @ts-ignore
|
|
4224
4417
|
previous = ast.chi[nodeIndex];
|
|
4225
|
-
minify(wrapper, options, recursive, errors);
|
|
4418
|
+
minify(wrapper, options, recursive, errors, nestingContent);
|
|
4226
4419
|
continue;
|
|
4227
4420
|
}
|
|
4228
4421
|
// @ts-ignore
|
|
@@ -4255,7 +4448,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4255
4448
|
let wrap = true;
|
|
4256
4449
|
// @ts-ignore
|
|
4257
4450
|
const selector = node.optimized.selector.reduce((acc, curr) => {
|
|
4258
|
-
if (curr[0] == '&') {
|
|
4451
|
+
if (curr[0] == '&' && curr.length > 1) {
|
|
4259
4452
|
if (curr[1] == ' ') {
|
|
4260
4453
|
curr.splice(0, 2);
|
|
4261
4454
|
}
|
|
@@ -4279,7 +4472,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4279
4472
|
if (!wrap) {
|
|
4280
4473
|
wrap = selector.some(s => s[0] != '&');
|
|
4281
4474
|
}
|
|
4282
|
-
|
|
4475
|
+
let rule = selector.map(s => {
|
|
4283
4476
|
if (s[0] == '&') {
|
|
4284
4477
|
// @ts-ignore
|
|
4285
4478
|
s[0] = node.optimized.optimized[0];
|
|
@@ -4287,7 +4480,14 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4287
4480
|
return s.join('');
|
|
4288
4481
|
}).join(',');
|
|
4289
4482
|
// @ts-ignore
|
|
4290
|
-
|
|
4483
|
+
let sel = wrap ? node.optimized.optimized[0] + `:is(${rule})` : rule;
|
|
4484
|
+
if (rule.includes('&')) {
|
|
4485
|
+
// @ts-ignore
|
|
4486
|
+
rule = replaceCompound(rule, node.optimized.optimized[0]);
|
|
4487
|
+
}
|
|
4488
|
+
if (sel.length < node.sel.length) {
|
|
4489
|
+
node.sel = sel;
|
|
4490
|
+
}
|
|
4291
4491
|
}
|
|
4292
4492
|
}
|
|
4293
4493
|
// @ts-ignore
|
|
@@ -4323,7 +4523,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4323
4523
|
minifyRule(node);
|
|
4324
4524
|
}
|
|
4325
4525
|
else {
|
|
4326
|
-
minify(node, options, recursive, errors);
|
|
4526
|
+
minify(node, options, recursive, errors, nestingContent);
|
|
4327
4527
|
}
|
|
4328
4528
|
i--;
|
|
4329
4529
|
previous = node;
|
|
@@ -4368,7 +4568,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4368
4568
|
minifyRule(previous);
|
|
4369
4569
|
}
|
|
4370
4570
|
else {
|
|
4371
|
-
minify(previous, options, recursive, errors);
|
|
4571
|
+
minify(previous, options, recursive, errors, nestingContent);
|
|
4372
4572
|
}
|
|
4373
4573
|
}
|
|
4374
4574
|
}
|
|
@@ -4380,11 +4580,19 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4380
4580
|
minifyRule(previous);
|
|
4381
4581
|
}
|
|
4382
4582
|
else {
|
|
4383
|
-
minify(previous, options, recursive, errors);
|
|
4583
|
+
minify(previous, options, recursive, errors, nestingContent);
|
|
4384
4584
|
}
|
|
4385
4585
|
}
|
|
4386
4586
|
}
|
|
4387
4587
|
}
|
|
4588
|
+
if (!nestingContent &&
|
|
4589
|
+
// @ts-ignore
|
|
4590
|
+
previous != null &&
|
|
4591
|
+
// previous.optimized != null &&
|
|
4592
|
+
previous.typ == 'Rule' &&
|
|
4593
|
+
previous.sel.includes('&')) {
|
|
4594
|
+
fixSelector(previous);
|
|
4595
|
+
}
|
|
4388
4596
|
previous = node;
|
|
4389
4597
|
nodeIndex = i;
|
|
4390
4598
|
}
|
|
@@ -4397,10 +4605,18 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4397
4605
|
else {
|
|
4398
4606
|
// @ts-ignore
|
|
4399
4607
|
if (!(node.typ == 'AtRule' && node.nam != 'font-face')) {
|
|
4400
|
-
minify(node, options, recursive, errors);
|
|
4608
|
+
minify(node, options, recursive, errors, nestingContent);
|
|
4401
4609
|
}
|
|
4402
4610
|
}
|
|
4403
4611
|
}
|
|
4612
|
+
if (!nestingContent &&
|
|
4613
|
+
// @ts-ignore
|
|
4614
|
+
node != null &&
|
|
4615
|
+
// previous.optimized != null &&
|
|
4616
|
+
node.typ == 'Rule' &&
|
|
4617
|
+
node.sel.includes('&')) {
|
|
4618
|
+
fixSelector(node);
|
|
4619
|
+
}
|
|
4404
4620
|
}
|
|
4405
4621
|
return ast;
|
|
4406
4622
|
}
|
|
@@ -4639,181 +4855,6 @@ function reduceRuleSelector(node) {
|
|
|
4639
4855
|
Object.defineProperty(node, 'raw', { ...definedPropertySettings, value: raw });
|
|
4640
4856
|
}
|
|
4641
4857
|
}
|
|
4642
|
-
// }
|
|
4643
|
-
}
|
|
4644
|
-
|
|
4645
|
-
function* walk(node) {
|
|
4646
|
-
// @ts-ignore
|
|
4647
|
-
yield* doWalk(node, null, null);
|
|
4648
|
-
}
|
|
4649
|
-
function* doWalk(node, parent, root) {
|
|
4650
|
-
yield { node, parent, root };
|
|
4651
|
-
if ('chi' in node) {
|
|
4652
|
-
for (const child of node.chi) {
|
|
4653
|
-
yield* doWalk(child, node, (root ?? node));
|
|
4654
|
-
}
|
|
4655
|
-
}
|
|
4656
|
-
}
|
|
4657
|
-
function* walkValues(values, parent) {
|
|
4658
|
-
for (const value of values) {
|
|
4659
|
-
// @ts-ignore
|
|
4660
|
-
yield { value, parent };
|
|
4661
|
-
if ('chi' in value) {
|
|
4662
|
-
yield* walkValues(value.chi, value);
|
|
4663
|
-
}
|
|
4664
|
-
}
|
|
4665
|
-
}
|
|
4666
|
-
|
|
4667
|
-
function expand(ast) {
|
|
4668
|
-
//
|
|
4669
|
-
if (!['Rule', 'StyleSheet', 'AtRule'].includes(ast.typ)) {
|
|
4670
|
-
return ast;
|
|
4671
|
-
}
|
|
4672
|
-
if ('Rule' == ast.typ) {
|
|
4673
|
-
return {
|
|
4674
|
-
typ: 'StyleSheet',
|
|
4675
|
-
chi: expandRule(ast)
|
|
4676
|
-
};
|
|
4677
|
-
}
|
|
4678
|
-
if (!('chi' in ast)) {
|
|
4679
|
-
return { ...ast };
|
|
4680
|
-
}
|
|
4681
|
-
const result = { ...ast, chi: [] };
|
|
4682
|
-
// @ts-ignore
|
|
4683
|
-
for (let i = 0; i < ast.chi.length; i++) {
|
|
4684
|
-
// @ts-ignore
|
|
4685
|
-
const node = ast.chi[i];
|
|
4686
|
-
if (node.typ == 'Rule') {
|
|
4687
|
-
// @ts-ignore
|
|
4688
|
-
result.chi.push(...expandRule(node));
|
|
4689
|
-
// i += expanded.length - 1;
|
|
4690
|
-
}
|
|
4691
|
-
else if (node.typ == 'AtRule' && 'chi' in node) {
|
|
4692
|
-
let hasRule = false;
|
|
4693
|
-
let j = node.chi.length;
|
|
4694
|
-
while (j--) {
|
|
4695
|
-
if (node.chi[j].typ == 'Rule' || node.chi[j].typ == 'AtRule') {
|
|
4696
|
-
hasRule = true;
|
|
4697
|
-
break;
|
|
4698
|
-
}
|
|
4699
|
-
}
|
|
4700
|
-
// @ts-ignore
|
|
4701
|
-
result.chi.push({ ...(hasRule ? expand(node) : node) });
|
|
4702
|
-
}
|
|
4703
|
-
else {
|
|
4704
|
-
// @ts-ignore
|
|
4705
|
-
result.chi.push(node);
|
|
4706
|
-
}
|
|
4707
|
-
}
|
|
4708
|
-
return result;
|
|
4709
|
-
}
|
|
4710
|
-
function expandRule(node) {
|
|
4711
|
-
const ast = { ...node, chi: node.chi.slice() };
|
|
4712
|
-
const result = [];
|
|
4713
|
-
if (ast.typ == 'Rule') {
|
|
4714
|
-
let i = 0;
|
|
4715
|
-
// @ts-ignore
|
|
4716
|
-
delete ast.raw;
|
|
4717
|
-
// @ts-ignore
|
|
4718
|
-
delete ast.optimized;
|
|
4719
|
-
for (; i < ast.chi.length; i++) {
|
|
4720
|
-
if (ast.chi[i].typ == 'Rule') {
|
|
4721
|
-
const rule = ast.chi[i];
|
|
4722
|
-
if (!rule.sel.includes('&')) {
|
|
4723
|
-
const selRule = splitRule(rule.sel);
|
|
4724
|
-
selRule.forEach(arr => combinators.includes(arr[0].charAt(0)) ? arr.unshift(ast.sel) : arr.unshift(ast.sel, ' '));
|
|
4725
|
-
rule.sel = selRule.reduce((acc, curr) => {
|
|
4726
|
-
acc.push(curr.join(''));
|
|
4727
|
-
return acc;
|
|
4728
|
-
}, []).join(',');
|
|
4729
|
-
}
|
|
4730
|
-
else {
|
|
4731
|
-
rule.sel = replaceCompount(rule.sel, ast.sel);
|
|
4732
|
-
}
|
|
4733
|
-
delete rule.raw;
|
|
4734
|
-
delete rule.optimized;
|
|
4735
|
-
ast.chi.splice(i--, 1);
|
|
4736
|
-
result.push(...expandRule(rule));
|
|
4737
|
-
}
|
|
4738
|
-
else if (ast.chi[i].typ == 'AtRule') {
|
|
4739
|
-
let astAtRule = ast.chi[i];
|
|
4740
|
-
const values = [];
|
|
4741
|
-
if (astAtRule.nam == 'scope') {
|
|
4742
|
-
if (astAtRule.val.includes('&')) {
|
|
4743
|
-
astAtRule.val = replaceCompount(astAtRule.val, ast.sel);
|
|
4744
|
-
}
|
|
4745
|
-
// @ts-ignore
|
|
4746
|
-
astAtRule = expand(astAtRule);
|
|
4747
|
-
}
|
|
4748
|
-
else {
|
|
4749
|
-
// @ts-ignore
|
|
4750
|
-
const clone = { ...ast, chi: astAtRule.chi.slice() };
|
|
4751
|
-
// @ts-ignore
|
|
4752
|
-
astAtRule.chi.length = 0;
|
|
4753
|
-
for (const r of expandRule(clone)) {
|
|
4754
|
-
if (r.typ == 'AtRule' && 'chi' in r) {
|
|
4755
|
-
if (astAtRule.val !== '' && r.val !== '') {
|
|
4756
|
-
if (astAtRule.nam == 'media' && r.nam == 'media') {
|
|
4757
|
-
r.val = astAtRule.val + ' and ' + r.val;
|
|
4758
|
-
}
|
|
4759
|
-
else if (astAtRule.nam == 'layer' && r.nam == 'layer') {
|
|
4760
|
-
r.val = astAtRule.val + '.' + r.val;
|
|
4761
|
-
}
|
|
4762
|
-
}
|
|
4763
|
-
// @ts-ignore
|
|
4764
|
-
values.push(r);
|
|
4765
|
-
}
|
|
4766
|
-
else if (r.typ == 'Rule') {
|
|
4767
|
-
// @ts-ignore
|
|
4768
|
-
astAtRule.chi.push(...expandRule(r));
|
|
4769
|
-
}
|
|
4770
|
-
else {
|
|
4771
|
-
// @ts-ignore
|
|
4772
|
-
astAtRule.chi.push(r);
|
|
4773
|
-
}
|
|
4774
|
-
}
|
|
4775
|
-
}
|
|
4776
|
-
// @ts-ignore
|
|
4777
|
-
result.push(...(astAtRule.chi.length > 0 ? [astAtRule].concat(values) : values));
|
|
4778
|
-
ast.chi.splice(i--, 1);
|
|
4779
|
-
}
|
|
4780
|
-
}
|
|
4781
|
-
}
|
|
4782
|
-
// @ts-ignore
|
|
4783
|
-
return ast.chi.length > 0 ? [ast].concat(result) : result;
|
|
4784
|
-
}
|
|
4785
|
-
function replaceCompount(input, replace) {
|
|
4786
|
-
const tokens = parseString(input);
|
|
4787
|
-
for (const t of walkValues(tokens)) {
|
|
4788
|
-
if (t.value.typ == 'Literal') {
|
|
4789
|
-
if (t.value.val == '&') {
|
|
4790
|
-
t.value.val = replace;
|
|
4791
|
-
}
|
|
4792
|
-
else if (t.value.val.length > 1 && t.value.val.charAt(0) == '&') {
|
|
4793
|
-
t.value.val = replaceCompoundLiteral(t.value.val, replace);
|
|
4794
|
-
}
|
|
4795
|
-
}
|
|
4796
|
-
}
|
|
4797
|
-
return tokens.reduce((acc, curr) => acc + renderToken(curr), '');
|
|
4798
|
-
}
|
|
4799
|
-
function replaceCompoundLiteral(selector, replace) {
|
|
4800
|
-
const tokens = [''];
|
|
4801
|
-
let i = 0;
|
|
4802
|
-
for (; i < selector.length; i++) {
|
|
4803
|
-
if (selector.charAt(i) == '&') {
|
|
4804
|
-
tokens.push('&');
|
|
4805
|
-
tokens.push('');
|
|
4806
|
-
}
|
|
4807
|
-
else {
|
|
4808
|
-
tokens[tokens.length - 1] += selector.charAt(i);
|
|
4809
|
-
}
|
|
4810
|
-
}
|
|
4811
|
-
return tokens.sort((a, b) => {
|
|
4812
|
-
if (a == '&') {
|
|
4813
|
-
return 1;
|
|
4814
|
-
}
|
|
4815
|
-
return b == '&' ? -1 : 0;
|
|
4816
|
-
}).reduce((acc, curr) => acc + (curr == '&' ? replace : curr), '');
|
|
4817
4858
|
}
|
|
4818
4859
|
|
|
4819
4860
|
async function transform$1(css, options = {}) {
|
|
@@ -4991,6 +5032,7 @@ exports.parseString = parseString;
|
|
|
4991
5032
|
exports.reduceSelector = reduceSelector;
|
|
4992
5033
|
exports.render = render;
|
|
4993
5034
|
exports.renderToken = renderToken;
|
|
5035
|
+
exports.replaceCompound = replaceCompound;
|
|
4994
5036
|
exports.resolve = resolve;
|
|
4995
5037
|
exports.splitRule = splitRule;
|
|
4996
5038
|
exports.tokenize = tokenize;
|
package/dist/index.d.ts
CHANGED
|
@@ -633,7 +633,7 @@ type AstNode =
|
|
|
633
633
|
| AstDeclaration;
|
|
634
634
|
|
|
635
635
|
declare const combinators: string[];
|
|
636
|
-
declare function minify(ast: AstNode, options?: ParserOptions, recursive?: boolean, errors?: ErrorDescription[]): AstNode;
|
|
636
|
+
declare function minify(ast: AstNode, options?: ParserOptions, recursive?: boolean, errors?: ErrorDescription[], nestingContent?: boolean): AstNode;
|
|
637
637
|
declare function reduceSelector(selector: string[][]): {
|
|
638
638
|
match: boolean;
|
|
639
639
|
optimized: string[];
|
|
@@ -644,7 +644,7 @@ declare function hasDeclaration(node: AstRule): boolean;
|
|
|
644
644
|
declare function minifyRule(ast: AstRule | AstAtRule): AstRule | AstAtRule;
|
|
645
645
|
declare function splitRule(buffer: string): string[][];
|
|
646
646
|
|
|
647
|
-
declare function walk(node: AstNode): Generator<{
|
|
647
|
+
declare function walk(node: AstNode, parent?: AstRuleList, root?: AstRuleList): Generator<{
|
|
648
648
|
node: AstNode;
|
|
649
649
|
parent?: AstRuleList;
|
|
650
650
|
root?: AstRuleList;
|
|
@@ -655,6 +655,7 @@ declare function walkValues(values: Token[], parent?: Token): Generator<{
|
|
|
655
655
|
}>;
|
|
656
656
|
|
|
657
657
|
declare function expand(ast: AstNode): AstNode;
|
|
658
|
+
declare function replaceCompound(input: string, replace: string): string;
|
|
658
659
|
|
|
659
660
|
declare const colorsFunc: string[];
|
|
660
661
|
declare function render(data: AstNode, opt?: RenderOptions): RenderResult;
|
|
@@ -707,4 +708,4 @@ declare function resolve(url: string, currentDirectory: string, cwd?: string): {
|
|
|
707
708
|
declare function parse(iterator: string, opt?: ParserOptions): Promise<ParseResult>;
|
|
708
709
|
declare function transform(css: string, options?: TransformOptions): Promise<TransformResult>;
|
|
709
710
|
|
|
710
|
-
export { colorsFunc, combinators, dirname, expand, funcList, getConfig, hasDeclaration, isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, load, matchType, matchUrl, minify, minifyRule, parse, parseDimension, parseString, reduceSelector, render, renderToken, resolve, splitRule, tokenize, transform, urlTokenMatcher, walk, walkValues };
|
|
711
|
+
export { colorsFunc, combinators, dirname, expand, funcList, getConfig, hasDeclaration, isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, load, matchType, matchUrl, minify, minifyRule, parse, parseDimension, parseString, reduceSelector, render, renderToken, replaceCompound, resolve, splitRule, tokenize, transform, urlTokenMatcher, walk, walkValues };
|
package/dist/lib/ast/expand.js
CHANGED
|
@@ -68,7 +68,7 @@ function expandRule(node) {
|
|
|
68
68
|
}, []).join(',');
|
|
69
69
|
}
|
|
70
70
|
else {
|
|
71
|
-
rule.sel =
|
|
71
|
+
rule.sel = replaceCompound(rule.sel, ast.sel);
|
|
72
72
|
}
|
|
73
73
|
delete rule.raw;
|
|
74
74
|
delete rule.optimized;
|
|
@@ -80,7 +80,7 @@ function expandRule(node) {
|
|
|
80
80
|
const values = [];
|
|
81
81
|
if (astAtRule.nam == 'scope') {
|
|
82
82
|
if (astAtRule.val.includes('&')) {
|
|
83
|
-
astAtRule.val =
|
|
83
|
+
astAtRule.val = replaceCompound(astAtRule.val, ast.sel);
|
|
84
84
|
}
|
|
85
85
|
// @ts-ignore
|
|
86
86
|
astAtRule = expand(astAtRule);
|
|
@@ -122,7 +122,7 @@ function expandRule(node) {
|
|
|
122
122
|
// @ts-ignore
|
|
123
123
|
return ast.chi.length > 0 ? [ast].concat(result) : result;
|
|
124
124
|
}
|
|
125
|
-
function
|
|
125
|
+
function replaceCompound(input, replace) {
|
|
126
126
|
const tokens = parseString(input);
|
|
127
127
|
for (const t of walkValues(tokens)) {
|
|
128
128
|
if (t.value.typ == 'Literal') {
|
|
@@ -156,4 +156,4 @@ function replaceCompoundLiteral(selector, replace) {
|
|
|
156
156
|
}).reduce((acc, curr) => acc + (curr == '&' ? replace : curr), '');
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
export { expand };
|
|
159
|
+
export { expand, replaceCompound };
|