@tbela99/css-parser 0.0.1-rc6 → 0.0.1
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/README.md +42 -7
- package/dist/index-umd-web.js +230 -194
- package/dist/index.cjs +230 -194
- package/dist/index.d.ts +4 -3
- package/dist/lib/ast/expand.js +4 -10
- 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/parser/utils/eq.js +1 -1
- package/dist/lib/parser/utils/syntax.js +1 -1
- 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':
|
|
@@ -827,7 +828,7 @@ function isColor(token) {
|
|
|
827
828
|
if (token.typ == 'Func' && token.chi.length > 0 && colorsFunc.includes(token.val)) {
|
|
828
829
|
// @ts-ignore
|
|
829
830
|
for (const v of token.chi) {
|
|
830
|
-
if (!['Number', 'Perc', 'Comma', 'Whitespace'].includes(v.typ)) {
|
|
831
|
+
if (!['Number', 'Angle', 'Perc', 'Comma', 'Whitespace', 'Literal'].includes(v.typ)) {
|
|
831
832
|
return false;
|
|
832
833
|
}
|
|
833
834
|
}
|
|
@@ -1947,7 +1948,7 @@ function eq(a, b) {
|
|
|
1947
1948
|
}
|
|
1948
1949
|
let key;
|
|
1949
1950
|
for (key of k1) {
|
|
1950
|
-
if (!eq(a[key], b[key])) {
|
|
1951
|
+
if (!(key in b) || !eq(a[key], b[key])) {
|
|
1951
1952
|
return false;
|
|
1952
1953
|
}
|
|
1953
1954
|
}
|
|
@@ -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,174 @@ 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
|
+
for (; i < ast.chi.length; i++) {
|
|
3899
|
+
if (ast.chi[i].typ == 'Rule') {
|
|
3900
|
+
const rule = ast.chi[i];
|
|
3901
|
+
if (!rule.sel.includes('&')) {
|
|
3902
|
+
const selRule = splitRule(rule.sel);
|
|
3903
|
+
selRule.forEach(arr => combinators.includes(arr[0].charAt(0)) ? arr.unshift(ast.sel) : arr.unshift(ast.sel, ' '));
|
|
3904
|
+
rule.sel = selRule.reduce((acc, curr) => {
|
|
3905
|
+
acc.push(curr.join(''));
|
|
3906
|
+
return acc;
|
|
3907
|
+
}, []).join(',');
|
|
3908
|
+
}
|
|
3909
|
+
else {
|
|
3910
|
+
rule.sel = replaceCompound(rule.sel, ast.sel);
|
|
3911
|
+
}
|
|
3912
|
+
ast.chi.splice(i--, 1);
|
|
3913
|
+
result.push(...expandRule(rule));
|
|
3914
|
+
}
|
|
3915
|
+
else if (ast.chi[i].typ == 'AtRule') {
|
|
3916
|
+
let astAtRule = ast.chi[i];
|
|
3917
|
+
const values = [];
|
|
3918
|
+
if (astAtRule.nam == 'scope') {
|
|
3919
|
+
if (astAtRule.val.includes('&')) {
|
|
3920
|
+
astAtRule.val = replaceCompound(astAtRule.val, ast.sel);
|
|
3921
|
+
}
|
|
3922
|
+
// @ts-ignore
|
|
3923
|
+
astAtRule = expand(astAtRule);
|
|
3924
|
+
}
|
|
3925
|
+
else {
|
|
3926
|
+
// @ts-ignore
|
|
3927
|
+
const clone = { ...ast, chi: astAtRule.chi.slice() };
|
|
3928
|
+
// @ts-ignore
|
|
3929
|
+
astAtRule.chi.length = 0;
|
|
3930
|
+
for (const r of expandRule(clone)) {
|
|
3931
|
+
if (r.typ == 'AtRule' && 'chi' in r) {
|
|
3932
|
+
if (astAtRule.val !== '' && r.val !== '') {
|
|
3933
|
+
if (astAtRule.nam == 'media' && r.nam == 'media') {
|
|
3934
|
+
r.val = astAtRule.val + ' and ' + r.val;
|
|
3935
|
+
}
|
|
3936
|
+
else if (astAtRule.nam == 'layer' && r.nam == 'layer') {
|
|
3937
|
+
r.val = astAtRule.val + '.' + r.val;
|
|
3938
|
+
}
|
|
3939
|
+
}
|
|
3940
|
+
// @ts-ignore
|
|
3941
|
+
values.push(r);
|
|
3942
|
+
}
|
|
3943
|
+
else if (r.typ == 'Rule') {
|
|
3944
|
+
// @ts-ignore
|
|
3945
|
+
astAtRule.chi.push(...expandRule(r));
|
|
3946
|
+
}
|
|
3947
|
+
else {
|
|
3948
|
+
// @ts-ignore
|
|
3949
|
+
astAtRule.chi.push(r);
|
|
3950
|
+
}
|
|
3951
|
+
}
|
|
3952
|
+
}
|
|
3953
|
+
// @ts-ignore
|
|
3954
|
+
result.push(...(astAtRule.chi.length > 0 ? [astAtRule].concat(values) : values));
|
|
3955
|
+
ast.chi.splice(i--, 1);
|
|
3956
|
+
}
|
|
3957
|
+
}
|
|
3958
|
+
}
|
|
3959
|
+
// @ts-ignore
|
|
3960
|
+
return ast.chi.length > 0 ? [ast].concat(result) : result;
|
|
3961
|
+
}
|
|
3962
|
+
function replaceCompound(input, replace) {
|
|
3963
|
+
const tokens = parseString(input);
|
|
3964
|
+
for (const t of walkValues(tokens)) {
|
|
3965
|
+
if (t.value.typ == 'Literal') {
|
|
3966
|
+
if (t.value.val == '&') {
|
|
3967
|
+
t.value.val = replace;
|
|
3968
|
+
}
|
|
3969
|
+
else if (t.value.val.length > 1 && t.value.val.charAt(0) == '&') {
|
|
3970
|
+
t.value.val = replaceCompoundLiteral(t.value.val, replace);
|
|
3971
|
+
}
|
|
3972
|
+
}
|
|
3973
|
+
}
|
|
3974
|
+
return tokens.reduce((acc, curr) => acc + renderToken(curr), '');
|
|
3975
|
+
}
|
|
3976
|
+
function replaceCompoundLiteral(selector, replace) {
|
|
3977
|
+
const tokens = [''];
|
|
3978
|
+
let i = 0;
|
|
3979
|
+
for (; i < selector.length; i++) {
|
|
3980
|
+
if (selector.charAt(i) == '&') {
|
|
3981
|
+
tokens.push('&');
|
|
3982
|
+
tokens.push('');
|
|
3983
|
+
}
|
|
3984
|
+
else {
|
|
3985
|
+
tokens[tokens.length - 1] += selector.charAt(i);
|
|
3986
|
+
}
|
|
3987
|
+
}
|
|
3988
|
+
return tokens.sort((a, b) => {
|
|
3989
|
+
if (a == '&') {
|
|
3990
|
+
return 1;
|
|
3991
|
+
}
|
|
3992
|
+
return b == '&' ? -1 : 0;
|
|
3993
|
+
}).reduce((acc, curr) => acc + (curr == '&' ? replace : curr), '');
|
|
3994
|
+
}
|
|
3995
|
+
|
|
3827
3996
|
const combinators = ['+', '>', '~'];
|
|
3828
3997
|
const notEndingWith = ['(', '['].concat(combinators);
|
|
3829
3998
|
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
|
|
3830
|
-
function minify(ast, options = {}, recursive = false, errors) {
|
|
3999
|
+
function minify(ast, options = {}, recursive = false, errors, nestingContent) {
|
|
3831
4000
|
function wrapNodes(previous, node, match, ast, i, nodeIndex) {
|
|
3832
4001
|
// @ts-ignore
|
|
3833
4002
|
let pSel = match.selector1.reduce(reducer, []).join(',');
|
|
@@ -4116,8 +4285,28 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4116
4285
|
selector2
|
|
4117
4286
|
};
|
|
4118
4287
|
}
|
|
4288
|
+
function fixSelector(node) {
|
|
4289
|
+
// @ts-ignore
|
|
4290
|
+
if (node.sel.includes('&')) {
|
|
4291
|
+
const attributes = parseString(node.sel);
|
|
4292
|
+
for (const attr of walkValues(attributes)) {
|
|
4293
|
+
if (attr.value.typ == 'Pseudo-class-func' && attr.value.val == ':is') {
|
|
4294
|
+
let i = attr.value.chi.length;
|
|
4295
|
+
while (i--) {
|
|
4296
|
+
if (attr.value.chi[i].typ == 'Literal' && attr.value.chi[i].val == '&') {
|
|
4297
|
+
attr.value.chi.splice(i, 1);
|
|
4298
|
+
}
|
|
4299
|
+
}
|
|
4300
|
+
}
|
|
4301
|
+
}
|
|
4302
|
+
node.sel = attributes.reduce((acc, curr) => acc + renderToken(curr), '');
|
|
4303
|
+
}
|
|
4304
|
+
}
|
|
4119
4305
|
// @ts-ignore
|
|
4120
4306
|
if (('chi' in ast) && ast.chi?.length > 0) {
|
|
4307
|
+
if (!nestingContent) {
|
|
4308
|
+
nestingContent = options.nestingRules && ast.typ == 'Rule';
|
|
4309
|
+
}
|
|
4121
4310
|
let i = 0;
|
|
4122
4311
|
let previous;
|
|
4123
4312
|
let node;
|
|
@@ -4132,7 +4321,6 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4132
4321
|
node = ast.chi[i];
|
|
4133
4322
|
// @ts-ignore
|
|
4134
4323
|
if (previous == node) {
|
|
4135
|
-
// console.error('idem!');
|
|
4136
4324
|
// @ts-ignore
|
|
4137
4325
|
ast.chi.splice(i, 1);
|
|
4138
4326
|
i--;
|
|
@@ -4148,7 +4336,6 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4148
4336
|
i--;
|
|
4149
4337
|
continue;
|
|
4150
4338
|
}
|
|
4151
|
-
// console.debug({previous, node});
|
|
4152
4339
|
// @ts-ignore
|
|
4153
4340
|
if (previous?.typ == 'AtRule' &&
|
|
4154
4341
|
previous.nam == node.nam &&
|
|
@@ -4168,7 +4355,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4168
4355
|
minifyRule(node);
|
|
4169
4356
|
}
|
|
4170
4357
|
else {
|
|
4171
|
-
minify(node, options, recursive, errors);
|
|
4358
|
+
minify(node, options, recursive, errors, nestingContent);
|
|
4172
4359
|
}
|
|
4173
4360
|
previous = node;
|
|
4174
4361
|
nodeIndex = i;
|
|
@@ -4222,7 +4409,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4222
4409
|
nodeIndex = --i;
|
|
4223
4410
|
// @ts-ignore
|
|
4224
4411
|
previous = ast.chi[nodeIndex];
|
|
4225
|
-
minify(wrapper, options, recursive, errors);
|
|
4412
|
+
minify(wrapper, options, recursive, errors, nestingContent);
|
|
4226
4413
|
continue;
|
|
4227
4414
|
}
|
|
4228
4415
|
// @ts-ignore
|
|
@@ -4255,7 +4442,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4255
4442
|
let wrap = true;
|
|
4256
4443
|
// @ts-ignore
|
|
4257
4444
|
const selector = node.optimized.selector.reduce((acc, curr) => {
|
|
4258
|
-
if (curr[0] == '&') {
|
|
4445
|
+
if (curr[0] == '&' && curr.length > 1) {
|
|
4259
4446
|
if (curr[1] == ' ') {
|
|
4260
4447
|
curr.splice(0, 2);
|
|
4261
4448
|
}
|
|
@@ -4279,7 +4466,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4279
4466
|
if (!wrap) {
|
|
4280
4467
|
wrap = selector.some(s => s[0] != '&');
|
|
4281
4468
|
}
|
|
4282
|
-
|
|
4469
|
+
let rule = selector.map(s => {
|
|
4283
4470
|
if (s[0] == '&') {
|
|
4284
4471
|
// @ts-ignore
|
|
4285
4472
|
s[0] = node.optimized.optimized[0];
|
|
@@ -4287,7 +4474,14 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4287
4474
|
return s.join('');
|
|
4288
4475
|
}).join(',');
|
|
4289
4476
|
// @ts-ignore
|
|
4290
|
-
|
|
4477
|
+
let sel = wrap ? node.optimized.optimized[0] + `:is(${rule})` : rule;
|
|
4478
|
+
if (rule.includes('&')) {
|
|
4479
|
+
// @ts-ignore
|
|
4480
|
+
rule = replaceCompound(rule, node.optimized.optimized[0]);
|
|
4481
|
+
}
|
|
4482
|
+
if (sel.length < node.sel.length) {
|
|
4483
|
+
node.sel = sel;
|
|
4484
|
+
}
|
|
4291
4485
|
}
|
|
4292
4486
|
}
|
|
4293
4487
|
// @ts-ignore
|
|
@@ -4323,7 +4517,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4323
4517
|
minifyRule(node);
|
|
4324
4518
|
}
|
|
4325
4519
|
else {
|
|
4326
|
-
minify(node, options, recursive, errors);
|
|
4520
|
+
minify(node, options, recursive, errors, nestingContent);
|
|
4327
4521
|
}
|
|
4328
4522
|
i--;
|
|
4329
4523
|
previous = node;
|
|
@@ -4368,7 +4562,7 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4368
4562
|
minifyRule(previous);
|
|
4369
4563
|
}
|
|
4370
4564
|
else {
|
|
4371
|
-
minify(previous, options, recursive, errors);
|
|
4565
|
+
minify(previous, options, recursive, errors, nestingContent);
|
|
4372
4566
|
}
|
|
4373
4567
|
}
|
|
4374
4568
|
}
|
|
@@ -4380,11 +4574,19 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4380
4574
|
minifyRule(previous);
|
|
4381
4575
|
}
|
|
4382
4576
|
else {
|
|
4383
|
-
minify(previous, options, recursive, errors);
|
|
4577
|
+
minify(previous, options, recursive, errors, nestingContent);
|
|
4384
4578
|
}
|
|
4385
4579
|
}
|
|
4386
4580
|
}
|
|
4387
4581
|
}
|
|
4582
|
+
if (!nestingContent &&
|
|
4583
|
+
// @ts-ignore
|
|
4584
|
+
previous != null &&
|
|
4585
|
+
// previous.optimized != null &&
|
|
4586
|
+
previous.typ == 'Rule' &&
|
|
4587
|
+
previous.sel.includes('&')) {
|
|
4588
|
+
fixSelector(previous);
|
|
4589
|
+
}
|
|
4388
4590
|
previous = node;
|
|
4389
4591
|
nodeIndex = i;
|
|
4390
4592
|
}
|
|
@@ -4397,10 +4599,18 @@ function minify(ast, options = {}, recursive = false, errors) {
|
|
|
4397
4599
|
else {
|
|
4398
4600
|
// @ts-ignore
|
|
4399
4601
|
if (!(node.typ == 'AtRule' && node.nam != 'font-face')) {
|
|
4400
|
-
minify(node, options, recursive, errors);
|
|
4602
|
+
minify(node, options, recursive, errors, nestingContent);
|
|
4401
4603
|
}
|
|
4402
4604
|
}
|
|
4403
4605
|
}
|
|
4606
|
+
if (!nestingContent &&
|
|
4607
|
+
// @ts-ignore
|
|
4608
|
+
node != null &&
|
|
4609
|
+
// previous.optimized != null &&
|
|
4610
|
+
node.typ == 'Rule' &&
|
|
4611
|
+
node.sel.includes('&')) {
|
|
4612
|
+
fixSelector(node);
|
|
4613
|
+
}
|
|
4404
4614
|
}
|
|
4405
4615
|
return ast;
|
|
4406
4616
|
}
|
|
@@ -4639,181 +4849,6 @@ function reduceRuleSelector(node) {
|
|
|
4639
4849
|
Object.defineProperty(node, 'raw', { ...definedPropertySettings, value: raw });
|
|
4640
4850
|
}
|
|
4641
4851
|
}
|
|
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
4852
|
}
|
|
4818
4853
|
|
|
4819
4854
|
async function transform$1(css, options = {}) {
|
|
@@ -4991,6 +5026,7 @@ exports.parseString = parseString;
|
|
|
4991
5026
|
exports.reduceSelector = reduceSelector;
|
|
4992
5027
|
exports.render = render;
|
|
4993
5028
|
exports.renderToken = renderToken;
|
|
5029
|
+
exports.replaceCompound = replaceCompound;
|
|
4994
5030
|
exports.resolve = resolve;
|
|
4995
5031
|
exports.splitRule = splitRule;
|
|
4996
5032
|
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
|
@@ -52,10 +52,6 @@ function expandRule(node) {
|
|
|
52
52
|
const result = [];
|
|
53
53
|
if (ast.typ == 'Rule') {
|
|
54
54
|
let i = 0;
|
|
55
|
-
// @ts-ignore
|
|
56
|
-
delete ast.raw;
|
|
57
|
-
// @ts-ignore
|
|
58
|
-
delete ast.optimized;
|
|
59
55
|
for (; i < ast.chi.length; i++) {
|
|
60
56
|
if (ast.chi[i].typ == 'Rule') {
|
|
61
57
|
const rule = ast.chi[i];
|
|
@@ -68,10 +64,8 @@ function expandRule(node) {
|
|
|
68
64
|
}, []).join(',');
|
|
69
65
|
}
|
|
70
66
|
else {
|
|
71
|
-
rule.sel =
|
|
67
|
+
rule.sel = replaceCompound(rule.sel, ast.sel);
|
|
72
68
|
}
|
|
73
|
-
delete rule.raw;
|
|
74
|
-
delete rule.optimized;
|
|
75
69
|
ast.chi.splice(i--, 1);
|
|
76
70
|
result.push(...expandRule(rule));
|
|
77
71
|
}
|
|
@@ -80,7 +74,7 @@ function expandRule(node) {
|
|
|
80
74
|
const values = [];
|
|
81
75
|
if (astAtRule.nam == 'scope') {
|
|
82
76
|
if (astAtRule.val.includes('&')) {
|
|
83
|
-
astAtRule.val =
|
|
77
|
+
astAtRule.val = replaceCompound(astAtRule.val, ast.sel);
|
|
84
78
|
}
|
|
85
79
|
// @ts-ignore
|
|
86
80
|
astAtRule = expand(astAtRule);
|
|
@@ -122,7 +116,7 @@ function expandRule(node) {
|
|
|
122
116
|
// @ts-ignore
|
|
123
117
|
return ast.chi.length > 0 ? [ast].concat(result) : result;
|
|
124
118
|
}
|
|
125
|
-
function
|
|
119
|
+
function replaceCompound(input, replace) {
|
|
126
120
|
const tokens = parseString(input);
|
|
127
121
|
for (const t of walkValues(tokens)) {
|
|
128
122
|
if (t.value.typ == 'Literal') {
|
|
@@ -156,4 +150,4 @@ function replaceCompoundLiteral(selector, replace) {
|
|
|
156
150
|
}).reduce((acc, curr) => acc + (curr == '&' ? replace : curr), '');
|
|
157
151
|
}
|
|
158
152
|
|
|
159
|
-
export { expand };
|
|
153
|
+
export { expand, replaceCompound };
|