@tbela99/css-parser 0.1.0 → 0.3.0
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 +267 -2
- package/dist/config.json.js +611 -4
- package/dist/index-umd-web.js +2898 -1223
- package/dist/index.cjs +2898 -1223
- package/dist/lib/ast/expand.js +11 -11
- package/dist/lib/ast/features/calc.js +33 -224
- package/dist/lib/ast/features/index.js +3 -3
- package/dist/lib/ast/features/inlinecssvariables.js +46 -31
- package/dist/lib/ast/features/shorthand.js +7 -7
- package/dist/lib/ast/features/utils/math.js +95 -0
- package/dist/lib/ast/math/expression.js +185 -0
- package/dist/lib/ast/math/math.js +95 -0
- package/dist/lib/ast/minify.js +34 -29
- package/dist/lib/ast/types.js +108 -78
- package/dist/lib/ast/walk.js +42 -9
- package/dist/lib/fs/resolve.js +4 -3
- package/dist/lib/iterable/set.js +48 -0
- package/dist/lib/iterable/weakmap.js +53 -0
- package/dist/lib/iterable/weakset.js +48 -0
- package/dist/lib/parser/declaration/list.js +7 -3
- package/dist/lib/parser/declaration/map.js +86 -7
- package/dist/lib/parser/declaration/set.js +43 -23
- package/dist/lib/parser/parse.js +561 -387
- package/dist/lib/parser/tokenize.js +42 -13
- package/dist/lib/parser/utils/declaration.js +67 -0
- package/dist/lib/parser/utils/syntax.js +32 -2
- package/dist/lib/parser/utils/type.js +7 -2
- package/dist/lib/renderer/render.js +163 -47
- package/dist/lib/renderer/utils/calccolor.js +238 -0
- package/dist/lib/renderer/utils/color.js +36 -164
- package/dist/lib/renderer/utils/hex.js +124 -0
- package/dist/lib/renderer/utils/hsl.js +49 -0
- package/dist/lib/renderer/utils/hsv.js +15 -0
- package/dist/lib/renderer/utils/hwb.js +50 -0
- package/dist/lib/renderer/utils/rgb.js +66 -0
- package/dist/node/index.js +8 -12
- package/dist/web/index.js +8 -12
- package/package.json +9 -7
- package/dist/index.d.ts +0 -1056
- /package/dist/lib/ast/{utiles → utils}/minifyfeature.js +0 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { EnumToken } from '../types.js';
|
|
2
|
+
import { compute } from './math.js';
|
|
3
|
+
import { reduceNumber } from '../../renderer/render.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* evaluate an array of tokens
|
|
7
|
+
* @param tokens
|
|
8
|
+
*/
|
|
9
|
+
function evaluate(tokens) {
|
|
10
|
+
const nodes = inlineExpression(evaluateExpression(buildExpression(tokens)));
|
|
11
|
+
if (nodes.length <= 1) {
|
|
12
|
+
return nodes;
|
|
13
|
+
}
|
|
14
|
+
const map = new Map;
|
|
15
|
+
let token;
|
|
16
|
+
let i;
|
|
17
|
+
for (i = 0; i < nodes.length; i++) {
|
|
18
|
+
token = nodes[i];
|
|
19
|
+
if (token.typ == EnumToken.Add) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (token.typ == EnumToken.Sub) {
|
|
23
|
+
if (!isScalarToken(nodes[i + 1])) {
|
|
24
|
+
token = { typ: EnumToken.ListToken, chi: [nodes[i], nodes[i + 1]] };
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
token = doEvaluate(nodes[i + 1], { typ: EnumToken.NumberTokenType, val: '-1' }, EnumToken.Mul);
|
|
28
|
+
}
|
|
29
|
+
i++;
|
|
30
|
+
}
|
|
31
|
+
if (!map.has(token.typ)) {
|
|
32
|
+
map.set(token.typ, [token]);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
map.get(token.typ).push(token);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return [...map].reduce((acc, curr) => {
|
|
39
|
+
const token = curr[1].reduce((acc, curr) => doEvaluate(acc, curr, EnumToken.Add));
|
|
40
|
+
if (token.typ != EnumToken.BinaryExpressionTokenType) {
|
|
41
|
+
if ('val' in token && +token.val < 0) {
|
|
42
|
+
acc.push({ typ: EnumToken.Sub }, { ...token, val: String(-token.val) });
|
|
43
|
+
return acc;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (acc.length > 0 && curr[0] != EnumToken.ListToken) {
|
|
47
|
+
acc.push({ typ: EnumToken.Add });
|
|
48
|
+
}
|
|
49
|
+
acc.push(token);
|
|
50
|
+
return acc;
|
|
51
|
+
}, []);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* evaluate arithmetic operation
|
|
55
|
+
* @param l
|
|
56
|
+
* @param r
|
|
57
|
+
* @param op
|
|
58
|
+
*/
|
|
59
|
+
function doEvaluate(l, r, op) {
|
|
60
|
+
const defaultReturn = {
|
|
61
|
+
typ: EnumToken.BinaryExpressionTokenType,
|
|
62
|
+
op,
|
|
63
|
+
l,
|
|
64
|
+
r
|
|
65
|
+
};
|
|
66
|
+
if (!isScalarToken(l) || !isScalarToken(r)) {
|
|
67
|
+
return defaultReturn;
|
|
68
|
+
}
|
|
69
|
+
if ((op == EnumToken.Add || op == EnumToken.Sub)) {
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
if (l.typ != r.typ) {
|
|
72
|
+
return defaultReturn;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const typ = l.typ == EnumToken.NumberTokenType ? r.typ : l.typ;
|
|
76
|
+
// @ts-ignore
|
|
77
|
+
const val = compute(typeof l.val == 'string' ? +l.val : l.val, typeof r.val == 'string' ? +r.val : r.val, op);
|
|
78
|
+
return { ...(l.typ == EnumToken.NumberTokenType ? r : l), typ, val: typeof val == 'number' ? reduceNumber(val) : val };
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* convert BinaryExpression into an array
|
|
82
|
+
* @param token
|
|
83
|
+
*/
|
|
84
|
+
function inlineExpression(token) {
|
|
85
|
+
const result = [];
|
|
86
|
+
if (token.typ == EnumToken.ParensTokenType && token.chi.length == 1) {
|
|
87
|
+
result.push(token.chi[0]);
|
|
88
|
+
}
|
|
89
|
+
else if (token.typ == EnumToken.BinaryExpressionTokenType) {
|
|
90
|
+
if ([EnumToken.Mul, EnumToken.Div].includes(token.op)) {
|
|
91
|
+
result.push(token);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
result.push(...inlineExpression(token.l), { typ: token.op }, ...inlineExpression(token.r));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
result.push(token);
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* evaluate expression
|
|
104
|
+
* @param token
|
|
105
|
+
*/
|
|
106
|
+
function evaluateExpression(token) {
|
|
107
|
+
if (token.typ != EnumToken.BinaryExpressionTokenType) {
|
|
108
|
+
return token;
|
|
109
|
+
}
|
|
110
|
+
if (token.r.typ == EnumToken.BinaryExpressionTokenType) {
|
|
111
|
+
token.r = evaluateExpression(token.r);
|
|
112
|
+
}
|
|
113
|
+
if (token.l.typ == EnumToken.BinaryExpressionTokenType) {
|
|
114
|
+
token.l = evaluateExpression(token.l);
|
|
115
|
+
}
|
|
116
|
+
return doEvaluate(token.l, token.r, token.op);
|
|
117
|
+
}
|
|
118
|
+
function isScalarToken(token) {
|
|
119
|
+
return 'unit' in token || [EnumToken.NumberTokenType, EnumToken.FractionTokenType, EnumToken.PercentageTokenType].includes(token.typ);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
*
|
|
123
|
+
* generate binary expression tree
|
|
124
|
+
* @param tokens
|
|
125
|
+
*/
|
|
126
|
+
function buildExpression(tokens) {
|
|
127
|
+
return factor(factor(tokens.filter(t => t.typ != EnumToken.WhitespaceTokenType), ['/', '*']), ['+', '-'])[0];
|
|
128
|
+
}
|
|
129
|
+
function getArithmeticOperation(op) {
|
|
130
|
+
if (op == '+') {
|
|
131
|
+
return EnumToken.Add;
|
|
132
|
+
}
|
|
133
|
+
if (op == '-') {
|
|
134
|
+
return EnumToken.Sub;
|
|
135
|
+
}
|
|
136
|
+
if (op == '/') {
|
|
137
|
+
return EnumToken.Div;
|
|
138
|
+
}
|
|
139
|
+
return EnumToken.Mul;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
*
|
|
143
|
+
* generate binary expression tree
|
|
144
|
+
* @param token
|
|
145
|
+
*/
|
|
146
|
+
function factorToken(token) {
|
|
147
|
+
if (token.typ == EnumToken.ParensTokenType || (token.typ == EnumToken.FunctionTokenType && token.val == 'calc')) {
|
|
148
|
+
if (token.typ == EnumToken.FunctionTokenType && token.val == 'calc') {
|
|
149
|
+
token = { ...token, typ: EnumToken.ParensTokenType };
|
|
150
|
+
// @ts-ignore
|
|
151
|
+
delete token.val;
|
|
152
|
+
}
|
|
153
|
+
return buildExpression(token.chi);
|
|
154
|
+
}
|
|
155
|
+
return token;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* generate binary expression tree
|
|
159
|
+
* @param tokens
|
|
160
|
+
* @param ops
|
|
161
|
+
*/
|
|
162
|
+
function factor(tokens, ops) {
|
|
163
|
+
let isOp;
|
|
164
|
+
const opList = ops.map(x => getArithmeticOperation(x));
|
|
165
|
+
if (tokens.length == 1) {
|
|
166
|
+
return [factorToken(tokens[0])];
|
|
167
|
+
}
|
|
168
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
169
|
+
isOp = opList.includes(tokens[i].typ);
|
|
170
|
+
if (isOp ||
|
|
171
|
+
// @ts-ignore
|
|
172
|
+
(tokens[i].typ == EnumToken.LiteralTokenType && ops.includes(tokens[i].val))) {
|
|
173
|
+
tokens.splice(i - 1, 3, {
|
|
174
|
+
typ: EnumToken.BinaryExpressionTokenType,
|
|
175
|
+
op: isOp ? tokens[i].typ : getArithmeticOperation(tokens[i].val),
|
|
176
|
+
l: factorToken(tokens[i - 1]),
|
|
177
|
+
r: factorToken(tokens[i + 1])
|
|
178
|
+
});
|
|
179
|
+
i--;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return tokens;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export { evaluate };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { EnumToken } from '../types.js';
|
|
2
|
+
import { reduceNumber } from '../../renderer/render.js';
|
|
3
|
+
|
|
4
|
+
const gcd = (x, y) => {
|
|
5
|
+
x = Math.abs(x);
|
|
6
|
+
y = Math.abs(y);
|
|
7
|
+
let t;
|
|
8
|
+
if (x == 0 || y == 0) {
|
|
9
|
+
return 1;
|
|
10
|
+
}
|
|
11
|
+
while (y) {
|
|
12
|
+
t = y;
|
|
13
|
+
y = x % y;
|
|
14
|
+
x = t;
|
|
15
|
+
}
|
|
16
|
+
return x;
|
|
17
|
+
};
|
|
18
|
+
function compute(a, b, op) {
|
|
19
|
+
if (typeof a == 'number' && typeof b == 'number') {
|
|
20
|
+
switch (op) {
|
|
21
|
+
case EnumToken.Add:
|
|
22
|
+
return a + b;
|
|
23
|
+
case EnumToken.Sub:
|
|
24
|
+
return a - b;
|
|
25
|
+
case EnumToken.Mul:
|
|
26
|
+
return a * b;
|
|
27
|
+
case EnumToken.Div:
|
|
28
|
+
const r = simplify(a, b);
|
|
29
|
+
if (r[1] == 1) {
|
|
30
|
+
return r[0];
|
|
31
|
+
}
|
|
32
|
+
const result = a / b;
|
|
33
|
+
const r2 = reduceNumber(r[0]) + '/' + reduceNumber(r[1]);
|
|
34
|
+
return reduceNumber(result).length <= r2.length ? result : {
|
|
35
|
+
typ: EnumToken.FractionTokenType,
|
|
36
|
+
l: { typ: EnumToken.NumberTokenType, val: reduceNumber(r[0]) },
|
|
37
|
+
r: { typ: EnumToken.NumberTokenType, val: reduceNumber(r[1]) }
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
let l1 = typeof a == 'number' ? {
|
|
42
|
+
typ: EnumToken.FractionTokenType,
|
|
43
|
+
l: { typ: EnumToken.NumberTokenType, val: reduceNumber(a) },
|
|
44
|
+
r: { typ: EnumToken.NumberTokenType, val: '1' }
|
|
45
|
+
} : a;
|
|
46
|
+
let r1 = typeof b == 'number' ? {
|
|
47
|
+
typ: EnumToken.FractionTokenType,
|
|
48
|
+
l: { typ: EnumToken.NumberTokenType, val: reduceNumber(b) },
|
|
49
|
+
r: { typ: EnumToken.NumberTokenType, val: '1' }
|
|
50
|
+
} : b;
|
|
51
|
+
let l2;
|
|
52
|
+
let r2;
|
|
53
|
+
switch (op) {
|
|
54
|
+
case EnumToken.Add:
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
l2 = l1.l.val * r1.r.val + l1.r.val * r1.l.val;
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
r2 = l1.r.val * r1.r.val;
|
|
59
|
+
break;
|
|
60
|
+
case EnumToken.Sub:
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
l2 = l1.l.val * r1.r.val - l1.r.val * r1.l.val;
|
|
63
|
+
// @ts-ignore
|
|
64
|
+
r2 = l1.r.val * r1.r.val;
|
|
65
|
+
break;
|
|
66
|
+
case EnumToken.Mul:
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
l2 = l1.l.val * r1.l.val;
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
r2 = l1.r.val * r1.r.val;
|
|
71
|
+
break;
|
|
72
|
+
case EnumToken.Div:
|
|
73
|
+
// @ts-ignore
|
|
74
|
+
l2 = l1.l.val * r1.r.val;
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
r2 = l1.r.val * r1.l.val;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
const a2 = simplify(l2, r2);
|
|
80
|
+
if (a2[1] == 1) {
|
|
81
|
+
return a2[0];
|
|
82
|
+
}
|
|
83
|
+
const result = a2[0] / a2[1];
|
|
84
|
+
return reduceNumber(result).length <= reduceNumber(a2[0]).length + 1 + reduceNumber(a2[1]).length ? result : {
|
|
85
|
+
typ: EnumToken.FractionTokenType,
|
|
86
|
+
l: { typ: EnumToken.NumberTokenType, val: reduceNumber(a2[0]) },
|
|
87
|
+
r: { typ: EnumToken.NumberTokenType, val: reduceNumber(a2[1]) }
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function simplify(a, b) {
|
|
91
|
+
const g = gcd(a, b);
|
|
92
|
+
return g > 1 ? [a / g, b / g] : [a, b];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { compute, gcd, simplify };
|
package/dist/lib/ast/minify.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseString } from '../parser/parse.js';
|
|
2
|
-
import {
|
|
2
|
+
import { isWhiteSpace, isIdent, isFunction, isIdentStart } from '../parser/utils/syntax.js';
|
|
3
3
|
import { EnumToken } from './types.js';
|
|
4
4
|
import { walkValues } from './walk.js';
|
|
5
5
|
import { replaceCompound } from './expand.js';
|
|
@@ -7,7 +7,7 @@ import { eq } from '../parser/utils/eq.js';
|
|
|
7
7
|
import { renderToken, doRender } from '../renderer/render.js';
|
|
8
8
|
import * as index from './features/index.js';
|
|
9
9
|
|
|
10
|
-
const combinators = ['+', '>', '~'];
|
|
10
|
+
const combinators = ['+', '>', '~', '||'];
|
|
11
11
|
const notEndingWith = ['(', '['].concat(combinators);
|
|
12
12
|
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
|
|
13
13
|
// @ts-ignore
|
|
@@ -23,7 +23,12 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
23
23
|
context.nodes.add(ast);
|
|
24
24
|
if (!('features' in options)) {
|
|
25
25
|
// @ts-ignore
|
|
26
|
-
options = {
|
|
26
|
+
options = {
|
|
27
|
+
removeDuplicateDeclarations: true,
|
|
28
|
+
computeShorthand: true,
|
|
29
|
+
computeCalcExpression: true,
|
|
30
|
+
features: [], ...options
|
|
31
|
+
};
|
|
27
32
|
// @ts-ignore
|
|
28
33
|
for (const feature of features) {
|
|
29
34
|
feature.register(options);
|
|
@@ -42,7 +47,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
42
47
|
curr.splice(0, 1);
|
|
43
48
|
}
|
|
44
49
|
}
|
|
45
|
-
else if (ast.typ ==
|
|
50
|
+
else if (ast.typ == EnumToken.RuleNodeType && (isIdent(curr[0]) || isFunction(curr[0]))) {
|
|
46
51
|
curr.unshift('&', ' ');
|
|
47
52
|
}
|
|
48
53
|
acc.push(curr.join(''));
|
|
@@ -51,7 +56,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
51
56
|
// @ts-ignore
|
|
52
57
|
if ('chi' in ast && ast.chi.length > 0) {
|
|
53
58
|
if (!nestingContent) {
|
|
54
|
-
nestingContent = options.nestingRules && ast.typ ==
|
|
59
|
+
nestingContent = options.nestingRules && ast.typ == EnumToken.RuleNodeType;
|
|
55
60
|
}
|
|
56
61
|
let i = 0;
|
|
57
62
|
let previous;
|
|
@@ -60,7 +65,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
60
65
|
// @ts-ignore
|
|
61
66
|
for (; i < ast.chi.length; i++) {
|
|
62
67
|
// @ts-ignore
|
|
63
|
-
if (ast.chi[i].typ ==
|
|
68
|
+
if (ast.chi[i].typ == EnumToken.CommentNodeType) {
|
|
64
69
|
continue;
|
|
65
70
|
}
|
|
66
71
|
// @ts-ignore
|
|
@@ -72,10 +77,10 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
72
77
|
i--;
|
|
73
78
|
continue;
|
|
74
79
|
}
|
|
75
|
-
if (node.typ ==
|
|
80
|
+
if (node.typ == EnumToken.AtRuleNodeType && node.nam == 'font-face') {
|
|
76
81
|
continue;
|
|
77
82
|
}
|
|
78
|
-
if (node.typ ==
|
|
83
|
+
if (node.typ == EnumToken.AtRuleNodeType) {
|
|
79
84
|
if (node.nam == 'media' && node.val == 'all') {
|
|
80
85
|
// @ts-ignore
|
|
81
86
|
ast.chi?.splice(i, 1, ...node.chi);
|
|
@@ -83,7 +88,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
83
88
|
continue;
|
|
84
89
|
}
|
|
85
90
|
// @ts-ignore
|
|
86
|
-
if (previous?.typ ==
|
|
91
|
+
if (previous?.typ == EnumToken.AtRuleNodeType &&
|
|
87
92
|
previous.nam == node.nam &&
|
|
88
93
|
previous.val == node.val) {
|
|
89
94
|
if ('chi' in node) {
|
|
@@ -102,14 +107,14 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
102
107
|
continue;
|
|
103
108
|
}
|
|
104
109
|
// @ts-ignore
|
|
105
|
-
if (node.typ ==
|
|
110
|
+
if (node.typ == EnumToken.RuleNodeType) {
|
|
106
111
|
reduceRuleSelector(node);
|
|
107
112
|
let wrapper;
|
|
108
113
|
let match;
|
|
109
114
|
// @ts-ignore
|
|
110
115
|
if (options.nestingRules) {
|
|
111
116
|
// @ts-ignore
|
|
112
|
-
if (previous?.typ ==
|
|
117
|
+
if (previous?.typ == EnumToken.RuleNodeType) {
|
|
113
118
|
// @ts-ignore
|
|
114
119
|
reduceRuleSelector(previous);
|
|
115
120
|
// @ts-ignore
|
|
@@ -130,7 +135,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
130
135
|
// @ts-ignore
|
|
131
136
|
const nextNode = ast.chi[i];
|
|
132
137
|
// @ts-ignore
|
|
133
|
-
if (nextNode.typ !=
|
|
138
|
+
if (nextNode.typ != EnumToken.RuleNodeType) {
|
|
134
139
|
break;
|
|
135
140
|
}
|
|
136
141
|
reduceRuleSelector(nextNode);
|
|
@@ -184,7 +189,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
184
189
|
curr.splice(0, 2);
|
|
185
190
|
}
|
|
186
191
|
else {
|
|
187
|
-
if (ast.typ !=
|
|
192
|
+
if (ast.typ != EnumToken.RuleNodeType && combinators.includes(curr[1])) {
|
|
188
193
|
wrap = false;
|
|
189
194
|
}
|
|
190
195
|
else {
|
|
@@ -232,18 +237,18 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
232
237
|
let k = previous.chi.length;
|
|
233
238
|
while (k-- > 0) {
|
|
234
239
|
// @ts-ignore
|
|
235
|
-
if (previous.chi[k].typ ==
|
|
240
|
+
if (previous.chi[k].typ == EnumToken.CommentNodeType) {
|
|
236
241
|
continue;
|
|
237
242
|
}
|
|
238
243
|
// @ts-ignore
|
|
239
|
-
shouldMerge = previous.chi[k].typ ==
|
|
244
|
+
shouldMerge = previous.chi[k].typ == EnumToken.DeclarationNodeType;
|
|
240
245
|
break;
|
|
241
246
|
}
|
|
242
247
|
if (shouldMerge) {
|
|
243
248
|
// @ts-ignore
|
|
244
|
-
if ((node.typ ==
|
|
249
|
+
if ((node.typ == EnumToken.RuleNodeType && node.sel == previous.sel) ||
|
|
245
250
|
// @ts-ignore
|
|
246
|
-
(node.typ ==
|
|
251
|
+
(node.typ == EnumToken.AtRuleNodeType) && node.val != 'font-face' && node.val == previous.val) {
|
|
247
252
|
// @ts-ignore
|
|
248
253
|
node.chi.unshift(...previous.chi);
|
|
249
254
|
// @ts-ignore
|
|
@@ -260,7 +265,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
260
265
|
nodeIndex = i;
|
|
261
266
|
continue;
|
|
262
267
|
}
|
|
263
|
-
else if (node.typ ==
|
|
268
|
+
else if (node.typ == EnumToken.RuleNodeType && previous?.typ == EnumToken.RuleNodeType) {
|
|
264
269
|
const intersect = diff(previous, node, reducer, options);
|
|
265
270
|
if (intersect != null) {
|
|
266
271
|
if (intersect.node1.chi.length == 0) {
|
|
@@ -317,7 +322,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
317
322
|
// @ts-ignore
|
|
318
323
|
previous != null &&
|
|
319
324
|
// previous.optimized != null &&
|
|
320
|
-
previous.typ ==
|
|
325
|
+
previous.typ == EnumToken.RuleNodeType &&
|
|
321
326
|
previous.sel.includes('&')) {
|
|
322
327
|
fixSelector(previous);
|
|
323
328
|
}
|
|
@@ -327,9 +332,9 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
327
332
|
// @ts-ignore
|
|
328
333
|
if (recursive && node != null && ('chi' in node)) {
|
|
329
334
|
// @ts-ignore
|
|
330
|
-
if (!node.chi.some(n => n.typ ==
|
|
335
|
+
if (!node.chi.some(n => n.typ == EnumToken.DeclarationNodeType)) {
|
|
331
336
|
// @ts-ignore
|
|
332
|
-
if (!(node.typ ==
|
|
337
|
+
if (!(node.typ == EnumToken.AtRuleNodeType && node.nam != 'font-face')) {
|
|
333
338
|
minify(node, options, recursive, errors, nestingContent, context);
|
|
334
339
|
}
|
|
335
340
|
}
|
|
@@ -338,12 +343,12 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
338
343
|
// @ts-ignore
|
|
339
344
|
node != null &&
|
|
340
345
|
// previous.optimized != null &&
|
|
341
|
-
node.typ ==
|
|
346
|
+
node.typ == EnumToken.RuleNodeType &&
|
|
342
347
|
node.sel.includes('&')) {
|
|
343
348
|
fixSelector(node);
|
|
344
349
|
}
|
|
345
350
|
}
|
|
346
|
-
if (ast.typ ==
|
|
351
|
+
if (ast.typ == EnumToken.StyleSheetNodeType) {
|
|
347
352
|
let parent;
|
|
348
353
|
let parents = [ast];
|
|
349
354
|
while (parents.length > 0) {
|
|
@@ -352,7 +357,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
352
357
|
for (let k = 0; k < parent.chi.length; k++) {
|
|
353
358
|
// @ts-ignore
|
|
354
359
|
const node = parent.chi[k];
|
|
355
|
-
if (!('chi' in node) || node.typ ==
|
|
360
|
+
if (!('chi' in node) || node.typ == EnumToken.StyleSheetNodeType || (node.typ == EnumToken.AtRuleNodeType && node.nam == 'font-face')) {
|
|
356
361
|
continue;
|
|
357
362
|
}
|
|
358
363
|
// @ts-ignore
|
|
@@ -463,11 +468,11 @@ function hasDeclaration(node) {
|
|
|
463
468
|
// @ts-ignore
|
|
464
469
|
for (let i = 0; i < node.chi?.length; i++) {
|
|
465
470
|
// @ts-ignore
|
|
466
|
-
if (node.chi[i].typ ==
|
|
471
|
+
if (node.chi[i].typ == EnumToken.CommentNodeType) {
|
|
467
472
|
continue;
|
|
468
473
|
}
|
|
469
474
|
// @ts-ignore
|
|
470
|
-
return node.chi[i].typ ==
|
|
475
|
+
return node.chi[i].typ == EnumToken.DeclarationNodeType;
|
|
471
476
|
}
|
|
472
477
|
return true;
|
|
473
478
|
}
|
|
@@ -613,7 +618,7 @@ function matchSelectors(selector1, selector2, parentType, errors) {
|
|
|
613
618
|
if (matchFunction != 0 || inAttr != 0) {
|
|
614
619
|
return null;
|
|
615
620
|
}
|
|
616
|
-
if (parentType !=
|
|
621
|
+
if (parentType != EnumToken.RuleNodeType) {
|
|
617
622
|
for (const part of match) {
|
|
618
623
|
if (part.length > 0 && combinators.includes(part[0].charAt(0))) {
|
|
619
624
|
return null;
|
|
@@ -819,7 +824,7 @@ function diff(n1, n2, reducer, options = {}) {
|
|
|
819
824
|
}
|
|
820
825
|
const intersect = [];
|
|
821
826
|
while (i--) {
|
|
822
|
-
if (node1.chi[i].typ ==
|
|
827
|
+
if (node1.chi[i].typ == EnumToken.CommentNodeType) {
|
|
823
828
|
continue;
|
|
824
829
|
}
|
|
825
830
|
j = node2.chi.length;
|
|
@@ -827,7 +832,7 @@ function diff(n1, n2, reducer, options = {}) {
|
|
|
827
832
|
break;
|
|
828
833
|
}
|
|
829
834
|
while (j--) {
|
|
830
|
-
if (node2.chi[j].typ ==
|
|
835
|
+
if (node2.chi[j].typ == EnumToken.CommentNodeType) {
|
|
831
836
|
continue;
|
|
832
837
|
}
|
|
833
838
|
if (node1.chi[i].nam == node2.chi[j].nam) {
|