modern-idoc 0.5.5 → 0.6.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/dist/index.cjs +715 -102
- package/dist/index.d.cts +230 -87
- package/dist/index.d.mts +230 -87
- package/dist/index.d.ts +230 -87
- package/dist/index.js +1 -1
- package/dist/index.mjs +711 -103
- package/package.json +11 -11
package/dist/index.cjs
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
const colord = require('colord');
|
|
4
4
|
|
|
5
5
|
function normalizeAudio(audio) {
|
|
6
|
-
if (
|
|
7
|
-
return void 0;
|
|
8
|
-
} else if (typeof audio === "string") {
|
|
6
|
+
if (typeof audio === "string") {
|
|
9
7
|
return { src: audio };
|
|
10
8
|
} else {
|
|
11
9
|
return audio;
|
|
@@ -26,10 +24,23 @@ function parseColor(color) {
|
|
|
26
24
|
}
|
|
27
25
|
return colord.colord(input);
|
|
28
26
|
}
|
|
27
|
+
function round(number, digits = 0, base = 10 ** digits) {
|
|
28
|
+
return Math.round(base * number) / base + 0;
|
|
29
|
+
}
|
|
30
|
+
function roundRgba(rgba) {
|
|
31
|
+
return {
|
|
32
|
+
r: round(rgba.r),
|
|
33
|
+
g: round(rgba.g),
|
|
34
|
+
b: round(rgba.b),
|
|
35
|
+
a: round(rgba.a, 3)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function format(number) {
|
|
39
|
+
const hex = number.toString(16);
|
|
40
|
+
return hex.length < 2 ? `0${hex}` : hex;
|
|
41
|
+
}
|
|
42
|
+
const defaultColor = "#000000FF";
|
|
29
43
|
function normalizeColor(color, orFail = false) {
|
|
30
|
-
if (color === void 0 || color === "none") {
|
|
31
|
-
return void 0;
|
|
32
|
-
}
|
|
33
44
|
const parsed = parseColor(color);
|
|
34
45
|
if (!parsed.isValid()) {
|
|
35
46
|
if (typeof color === "string") {
|
|
@@ -40,39 +51,645 @@ function normalizeColor(color, orFail = false) {
|
|
|
40
51
|
throw new Error(message);
|
|
41
52
|
} else {
|
|
42
53
|
console.warn(message);
|
|
43
|
-
return
|
|
54
|
+
return defaultColor;
|
|
44
55
|
}
|
|
45
56
|
}
|
|
46
|
-
const { r, g, b, a } = parsed.rgba;
|
|
47
|
-
return
|
|
57
|
+
const { r, g, b, a } = roundRgba(parsed.rgba);
|
|
58
|
+
return `#${format(r)}${format(g)}${format(b)}${format(round(a * 255))}`;
|
|
48
59
|
}
|
|
49
60
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
var GradientParser$1 = GradientParser$1 || {};
|
|
62
|
+
GradientParser$1.parse = /* @__PURE__ */ function() {
|
|
63
|
+
const tokens = {
|
|
64
|
+
linearGradient: /^(-(webkit|o|ms|moz)-)?(linear-gradient)/i,
|
|
65
|
+
repeatingLinearGradient: /^(-(webkit|o|ms|moz)-)?(repeating-linear-gradient)/i,
|
|
66
|
+
radialGradient: /^(-(webkit|o|ms|moz)-)?(radial-gradient)/i,
|
|
67
|
+
repeatingRadialGradient: /^(-(webkit|o|ms|moz)-)?(repeating-radial-gradient)/i,
|
|
68
|
+
sideOrCorner: /^to (left (top|bottom)|right (top|bottom)|top (left|right)|bottom (left|right)|left|right|top|bottom)/i,
|
|
69
|
+
extentKeywords: /^(closest-side|closest-corner|farthest-side|farthest-corner|contain|cover)/,
|
|
70
|
+
positionKeywords: /^(left|center|right|top|bottom)/i,
|
|
71
|
+
pixelValue: /^(-?((\d*\.\d+)|(\d+\.?)))px/,
|
|
72
|
+
percentageValue: /^(-?((\d*\.\d+)|(\d+\.?)))%/,
|
|
73
|
+
emValue: /^(-?((\d*\.\d+)|(\d+\.?)))em/,
|
|
74
|
+
angleValue: /^(-?((\d*\.\d+)|(\d+\.?)))deg/,
|
|
75
|
+
radianValue: /^(-?((\d*\.\d+)|(\d+\.?)))rad/,
|
|
76
|
+
startCall: /^\(/,
|
|
77
|
+
endCall: /^\)/,
|
|
78
|
+
comma: /^,/,
|
|
79
|
+
hexColor: /^#([0-9a-f]+)/i,
|
|
80
|
+
literalColor: /^([a-z]+)/i,
|
|
81
|
+
rgbColor: /^rgb/i,
|
|
82
|
+
rgbaColor: /^rgba/i,
|
|
83
|
+
varColor: /^var/i,
|
|
84
|
+
calcValue: /^calc/i,
|
|
85
|
+
variableName: /^(--[a-z0-9-,\s#]+)/i,
|
|
86
|
+
number: /^((\d*\.\d+)|(\d+\.?))/,
|
|
87
|
+
hslColor: /^hsl/i,
|
|
88
|
+
hslaColor: /^hsla/i
|
|
89
|
+
};
|
|
90
|
+
let input = "";
|
|
91
|
+
function error(msg) {
|
|
92
|
+
const err = new Error(`${input}: ${msg}`);
|
|
93
|
+
err.source = input;
|
|
94
|
+
throw err;
|
|
95
|
+
}
|
|
96
|
+
function getAST() {
|
|
97
|
+
const ast = matchListDefinitions();
|
|
98
|
+
if (input.length > 0) {
|
|
99
|
+
error("Invalid input not EOF");
|
|
100
|
+
}
|
|
101
|
+
return ast;
|
|
102
|
+
}
|
|
103
|
+
function matchListDefinitions() {
|
|
104
|
+
return matchListing(matchDefinition);
|
|
105
|
+
}
|
|
106
|
+
function matchDefinition() {
|
|
107
|
+
return matchGradient(
|
|
108
|
+
"linear-gradient",
|
|
109
|
+
tokens.linearGradient,
|
|
110
|
+
matchLinearOrientation
|
|
111
|
+
) || matchGradient(
|
|
112
|
+
"repeating-linear-gradient",
|
|
113
|
+
tokens.repeatingLinearGradient,
|
|
114
|
+
matchLinearOrientation
|
|
115
|
+
) || matchGradient(
|
|
116
|
+
"radial-gradient",
|
|
117
|
+
tokens.radialGradient,
|
|
118
|
+
matchListRadialOrientations
|
|
119
|
+
) || matchGradient(
|
|
120
|
+
"repeating-radial-gradient",
|
|
121
|
+
tokens.repeatingRadialGradient,
|
|
122
|
+
matchListRadialOrientations
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
function matchGradient(gradientType, pattern, orientationMatcher) {
|
|
126
|
+
return matchCall(pattern, (captures) => {
|
|
127
|
+
const orientation = orientationMatcher();
|
|
128
|
+
if (orientation) {
|
|
129
|
+
if (!scan(tokens.comma)) {
|
|
130
|
+
error("Missing comma before color stops");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
type: gradientType,
|
|
135
|
+
orientation,
|
|
136
|
+
colorStops: matchListing(matchColorStop)
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function matchCall(pattern, callback) {
|
|
141
|
+
const captures = scan(pattern);
|
|
142
|
+
if (captures) {
|
|
143
|
+
if (!scan(tokens.startCall)) {
|
|
144
|
+
error("Missing (");
|
|
145
|
+
}
|
|
146
|
+
const result = callback(captures);
|
|
147
|
+
if (!scan(tokens.endCall)) {
|
|
148
|
+
error("Missing )");
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function matchLinearOrientation() {
|
|
154
|
+
const sideOrCorner = matchSideOrCorner();
|
|
155
|
+
if (sideOrCorner) {
|
|
156
|
+
return sideOrCorner;
|
|
157
|
+
}
|
|
158
|
+
const legacyDirection = match("position-keyword", tokens.positionKeywords, 1);
|
|
159
|
+
if (legacyDirection) {
|
|
160
|
+
return {
|
|
161
|
+
type: "directional",
|
|
162
|
+
value: legacyDirection.value
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return matchAngle();
|
|
166
|
+
}
|
|
167
|
+
function matchSideOrCorner() {
|
|
168
|
+
return match("directional", tokens.sideOrCorner, 1);
|
|
169
|
+
}
|
|
170
|
+
function matchAngle() {
|
|
171
|
+
return match("angular", tokens.angleValue, 1) || match("angular", tokens.radianValue, 1);
|
|
172
|
+
}
|
|
173
|
+
function matchListRadialOrientations() {
|
|
174
|
+
let radialOrientations;
|
|
175
|
+
let radialOrientation = matchRadialOrientation();
|
|
176
|
+
let lookaheadCache;
|
|
177
|
+
if (radialOrientation) {
|
|
178
|
+
radialOrientations = [];
|
|
179
|
+
radialOrientations.push(radialOrientation);
|
|
180
|
+
lookaheadCache = input;
|
|
181
|
+
if (scan(tokens.comma)) {
|
|
182
|
+
radialOrientation = matchRadialOrientation();
|
|
183
|
+
if (radialOrientation) {
|
|
184
|
+
radialOrientations.push(radialOrientation);
|
|
185
|
+
} else {
|
|
186
|
+
input = lookaheadCache;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return radialOrientations;
|
|
191
|
+
}
|
|
192
|
+
function matchRadialOrientation() {
|
|
193
|
+
let radialType = matchCircle() || matchEllipse();
|
|
194
|
+
if (radialType) {
|
|
195
|
+
radialType.at = matchAtPosition();
|
|
196
|
+
} else {
|
|
197
|
+
const extent = matchExtentKeyword();
|
|
198
|
+
if (extent) {
|
|
199
|
+
radialType = extent;
|
|
200
|
+
const positionAt = matchAtPosition();
|
|
201
|
+
if (positionAt) {
|
|
202
|
+
radialType.at = positionAt;
|
|
203
|
+
}
|
|
204
|
+
} else {
|
|
205
|
+
const atPosition = matchAtPosition();
|
|
206
|
+
if (atPosition) {
|
|
207
|
+
radialType = {
|
|
208
|
+
type: "default-radial",
|
|
209
|
+
at: atPosition
|
|
210
|
+
};
|
|
211
|
+
} else {
|
|
212
|
+
const defaultPosition = matchPositioning();
|
|
213
|
+
if (defaultPosition) {
|
|
214
|
+
radialType = {
|
|
215
|
+
type: "default-radial",
|
|
216
|
+
at: defaultPosition
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return radialType;
|
|
223
|
+
}
|
|
224
|
+
function matchCircle() {
|
|
225
|
+
const circle = match("shape", /^(circle)/i, 0);
|
|
226
|
+
if (circle) {
|
|
227
|
+
circle.style = matchLength() || matchExtentKeyword();
|
|
228
|
+
}
|
|
229
|
+
return circle;
|
|
230
|
+
}
|
|
231
|
+
function matchEllipse() {
|
|
232
|
+
const ellipse = match("shape", /^(ellipse)/i, 0);
|
|
233
|
+
if (ellipse) {
|
|
234
|
+
ellipse.style = matchPositioning() || matchDistance() || matchExtentKeyword();
|
|
235
|
+
}
|
|
236
|
+
return ellipse;
|
|
237
|
+
}
|
|
238
|
+
function matchExtentKeyword() {
|
|
239
|
+
return match("extent-keyword", tokens.extentKeywords, 1);
|
|
240
|
+
}
|
|
241
|
+
function matchAtPosition() {
|
|
242
|
+
if (match("position", /^at/, 0)) {
|
|
243
|
+
const positioning = matchPositioning();
|
|
244
|
+
if (!positioning) {
|
|
245
|
+
error("Missing positioning value");
|
|
246
|
+
}
|
|
247
|
+
return positioning;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function matchPositioning() {
|
|
251
|
+
const location = matchCoordinates();
|
|
252
|
+
if (location.x || location.y) {
|
|
253
|
+
return {
|
|
254
|
+
type: "position",
|
|
255
|
+
value: location
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function matchCoordinates() {
|
|
56
260
|
return {
|
|
57
|
-
|
|
58
|
-
|
|
261
|
+
x: matchDistance(),
|
|
262
|
+
y: matchDistance()
|
|
59
263
|
};
|
|
60
264
|
}
|
|
61
|
-
|
|
265
|
+
function matchListing(matcher) {
|
|
266
|
+
let captures = matcher();
|
|
267
|
+
const result = [];
|
|
268
|
+
if (captures) {
|
|
269
|
+
result.push(captures);
|
|
270
|
+
while (scan(tokens.comma)) {
|
|
271
|
+
captures = matcher();
|
|
272
|
+
if (captures) {
|
|
273
|
+
result.push(captures);
|
|
274
|
+
} else {
|
|
275
|
+
error("One extra comma");
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
function matchColorStop() {
|
|
282
|
+
const color = matchColor();
|
|
283
|
+
if (!color) {
|
|
284
|
+
error("Expected color definition");
|
|
285
|
+
}
|
|
286
|
+
color.length = matchDistance();
|
|
287
|
+
return color;
|
|
288
|
+
}
|
|
289
|
+
function matchColor() {
|
|
290
|
+
return matchHexColor() || matchHSLAColor() || matchHSLColor() || matchRGBAColor() || matchRGBColor() || matchVarColor() || matchLiteralColor();
|
|
291
|
+
}
|
|
292
|
+
function matchLiteralColor() {
|
|
293
|
+
return match("literal", tokens.literalColor, 0);
|
|
294
|
+
}
|
|
295
|
+
function matchHexColor() {
|
|
296
|
+
return match("hex", tokens.hexColor, 1);
|
|
297
|
+
}
|
|
298
|
+
function matchRGBColor() {
|
|
299
|
+
return matchCall(tokens.rgbColor, () => {
|
|
300
|
+
return {
|
|
301
|
+
type: "rgb",
|
|
302
|
+
value: matchListing(matchNumber)
|
|
303
|
+
};
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
function matchRGBAColor() {
|
|
307
|
+
return matchCall(tokens.rgbaColor, () => {
|
|
308
|
+
return {
|
|
309
|
+
type: "rgba",
|
|
310
|
+
value: matchListing(matchNumber)
|
|
311
|
+
};
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
function matchVarColor() {
|
|
315
|
+
return matchCall(tokens.varColor, () => {
|
|
316
|
+
return {
|
|
317
|
+
type: "var",
|
|
318
|
+
value: matchVariableName()
|
|
319
|
+
};
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
function matchHSLColor() {
|
|
323
|
+
return matchCall(tokens.hslColor, () => {
|
|
324
|
+
const lookahead = scan(tokens.percentageValue);
|
|
325
|
+
if (lookahead) {
|
|
326
|
+
error("HSL hue value must be a number in degrees (0-360) or normalized (-360 to 360), not a percentage");
|
|
327
|
+
}
|
|
328
|
+
const hue = matchNumber();
|
|
329
|
+
scan(tokens.comma);
|
|
330
|
+
let captures = scan(tokens.percentageValue);
|
|
331
|
+
const sat = captures ? captures[1] : null;
|
|
332
|
+
scan(tokens.comma);
|
|
333
|
+
captures = scan(tokens.percentageValue);
|
|
334
|
+
const light = captures ? captures[1] : null;
|
|
335
|
+
if (!sat || !light) {
|
|
336
|
+
error("Expected percentage value for saturation and lightness in HSL");
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
type: "hsl",
|
|
340
|
+
value: [hue, sat, light]
|
|
341
|
+
};
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
function matchHSLAColor() {
|
|
345
|
+
return matchCall(tokens.hslaColor, () => {
|
|
346
|
+
const hue = matchNumber();
|
|
347
|
+
scan(tokens.comma);
|
|
348
|
+
let captures = scan(tokens.percentageValue);
|
|
349
|
+
const sat = captures ? captures[1] : null;
|
|
350
|
+
scan(tokens.comma);
|
|
351
|
+
captures = scan(tokens.percentageValue);
|
|
352
|
+
const light = captures ? captures[1] : null;
|
|
353
|
+
scan(tokens.comma);
|
|
354
|
+
const alpha = matchNumber();
|
|
355
|
+
if (!sat || !light) {
|
|
356
|
+
error("Expected percentage value for saturation and lightness in HSLA");
|
|
357
|
+
}
|
|
358
|
+
return {
|
|
359
|
+
type: "hsla",
|
|
360
|
+
value: [hue, sat, light, alpha]
|
|
361
|
+
};
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
function matchVariableName() {
|
|
365
|
+
return scan(tokens.variableName)[1];
|
|
366
|
+
}
|
|
367
|
+
function matchNumber() {
|
|
368
|
+
return scan(tokens.number)[1];
|
|
369
|
+
}
|
|
370
|
+
function matchDistance() {
|
|
371
|
+
return match("%", tokens.percentageValue, 1) || matchPositionKeyword() || matchCalc() || matchLength();
|
|
372
|
+
}
|
|
373
|
+
function matchPositionKeyword() {
|
|
374
|
+
return match("position-keyword", tokens.positionKeywords, 1);
|
|
375
|
+
}
|
|
376
|
+
function matchCalc() {
|
|
377
|
+
return matchCall(tokens.calcValue, () => {
|
|
378
|
+
let openParenCount = 1;
|
|
379
|
+
let i = 0;
|
|
380
|
+
while (openParenCount > 0 && i < input.length) {
|
|
381
|
+
const char = input.charAt(i);
|
|
382
|
+
if (char === "(") {
|
|
383
|
+
openParenCount++;
|
|
384
|
+
} else if (char === ")") {
|
|
385
|
+
openParenCount--;
|
|
386
|
+
}
|
|
387
|
+
i++;
|
|
388
|
+
}
|
|
389
|
+
if (openParenCount > 0) {
|
|
390
|
+
error("Missing closing parenthesis in calc() expression");
|
|
391
|
+
}
|
|
392
|
+
const calcContent = input.substring(0, i - 1);
|
|
393
|
+
consume(i - 1);
|
|
394
|
+
return {
|
|
395
|
+
type: "calc",
|
|
396
|
+
value: calcContent
|
|
397
|
+
};
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
function matchLength() {
|
|
401
|
+
return match("px", tokens.pixelValue, 1) || match("em", tokens.emValue, 1);
|
|
402
|
+
}
|
|
403
|
+
function match(type, pattern, captureIndex) {
|
|
404
|
+
const captures = scan(pattern);
|
|
405
|
+
if (captures) {
|
|
406
|
+
return {
|
|
407
|
+
type,
|
|
408
|
+
value: captures[captureIndex]
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
function scan(regexp) {
|
|
413
|
+
let captures, blankCaptures;
|
|
414
|
+
blankCaptures = /^\s+/.exec(input);
|
|
415
|
+
if (blankCaptures) {
|
|
416
|
+
consume(blankCaptures[0].length);
|
|
417
|
+
}
|
|
418
|
+
captures = regexp.exec(input);
|
|
419
|
+
if (captures) {
|
|
420
|
+
consume(captures[0].length);
|
|
421
|
+
}
|
|
422
|
+
return captures;
|
|
423
|
+
}
|
|
424
|
+
function consume(size) {
|
|
425
|
+
input = input.substr(size);
|
|
426
|
+
}
|
|
427
|
+
return function(code) {
|
|
428
|
+
input = code.toString().trim();
|
|
429
|
+
if (input.endsWith(";")) {
|
|
430
|
+
input = input.slice(0, -1);
|
|
431
|
+
}
|
|
432
|
+
return getAST();
|
|
433
|
+
};
|
|
434
|
+
}();
|
|
435
|
+
const parseGradient = GradientParser$1.parse.bind(GradientParser$1);
|
|
62
436
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
437
|
+
var GradientParser = GradientParser || {};
|
|
438
|
+
GradientParser.stringify = /* @__PURE__ */ function() {
|
|
439
|
+
var visitor = {
|
|
440
|
+
"visit_linear-gradient": function(node) {
|
|
441
|
+
return visitor.visit_gradient(node);
|
|
442
|
+
},
|
|
443
|
+
"visit_repeating-linear-gradient": function(node) {
|
|
444
|
+
return visitor.visit_gradient(node);
|
|
445
|
+
},
|
|
446
|
+
"visit_radial-gradient": function(node) {
|
|
447
|
+
return visitor.visit_gradient(node);
|
|
448
|
+
},
|
|
449
|
+
"visit_repeating-radial-gradient": function(node) {
|
|
450
|
+
return visitor.visit_gradient(node);
|
|
451
|
+
},
|
|
452
|
+
"visit_gradient": function(node) {
|
|
453
|
+
var orientation = visitor.visit(node.orientation);
|
|
454
|
+
if (orientation) {
|
|
455
|
+
orientation += ", ";
|
|
456
|
+
}
|
|
457
|
+
return node.type + "(" + orientation + visitor.visit(node.colorStops) + ")";
|
|
458
|
+
},
|
|
459
|
+
"visit_shape": function(node) {
|
|
460
|
+
var result = node.value, at = visitor.visit(node.at), style = visitor.visit(node.style);
|
|
461
|
+
if (style) {
|
|
462
|
+
result += " " + style;
|
|
463
|
+
}
|
|
464
|
+
if (at) {
|
|
465
|
+
result += " at " + at;
|
|
466
|
+
}
|
|
467
|
+
return result;
|
|
468
|
+
},
|
|
469
|
+
"visit_default-radial": function(node) {
|
|
470
|
+
var result = "", at = visitor.visit(node.at);
|
|
471
|
+
if (at) {
|
|
472
|
+
result += at;
|
|
473
|
+
}
|
|
474
|
+
return result;
|
|
475
|
+
},
|
|
476
|
+
"visit_extent-keyword": function(node) {
|
|
477
|
+
var result = node.value, at = visitor.visit(node.at);
|
|
478
|
+
if (at) {
|
|
479
|
+
result += " at " + at;
|
|
480
|
+
}
|
|
481
|
+
return result;
|
|
482
|
+
},
|
|
483
|
+
"visit_position-keyword": function(node) {
|
|
484
|
+
return node.value;
|
|
485
|
+
},
|
|
486
|
+
"visit_position": function(node) {
|
|
487
|
+
return visitor.visit(node.value.x) + " " + visitor.visit(node.value.y);
|
|
488
|
+
},
|
|
489
|
+
"visit_%": function(node) {
|
|
490
|
+
return node.value + "%";
|
|
491
|
+
},
|
|
492
|
+
"visit_em": function(node) {
|
|
493
|
+
return node.value + "em";
|
|
494
|
+
},
|
|
495
|
+
"visit_px": function(node) {
|
|
496
|
+
return node.value + "px";
|
|
497
|
+
},
|
|
498
|
+
"visit_calc": function(node) {
|
|
499
|
+
return "calc(" + node.value + ")";
|
|
500
|
+
},
|
|
501
|
+
"visit_literal": function(node) {
|
|
502
|
+
return visitor.visit_color(node.value, node);
|
|
503
|
+
},
|
|
504
|
+
"visit_hex": function(node) {
|
|
505
|
+
return visitor.visit_color("#" + node.value, node);
|
|
506
|
+
},
|
|
507
|
+
"visit_rgb": function(node) {
|
|
508
|
+
return visitor.visit_color("rgb(" + node.value.join(", ") + ")", node);
|
|
509
|
+
},
|
|
510
|
+
"visit_rgba": function(node) {
|
|
511
|
+
return visitor.visit_color("rgba(" + node.value.join(", ") + ")", node);
|
|
512
|
+
},
|
|
513
|
+
"visit_hsl": function(node) {
|
|
514
|
+
return visitor.visit_color("hsl(" + node.value[0] + ", " + node.value[1] + "%, " + node.value[2] + "%)", node);
|
|
515
|
+
},
|
|
516
|
+
"visit_hsla": function(node) {
|
|
517
|
+
return visitor.visit_color("hsla(" + node.value[0] + ", " + node.value[1] + "%, " + node.value[2] + "%, " + node.value[3] + ")", node);
|
|
518
|
+
},
|
|
519
|
+
"visit_var": function(node) {
|
|
520
|
+
return visitor.visit_color("var(" + node.value + ")", node);
|
|
521
|
+
},
|
|
522
|
+
"visit_color": function(resultColor, node) {
|
|
523
|
+
var result = resultColor, length = visitor.visit(node.length);
|
|
524
|
+
if (length) {
|
|
525
|
+
result += " " + length;
|
|
526
|
+
}
|
|
527
|
+
return result;
|
|
528
|
+
},
|
|
529
|
+
"visit_angular": function(node) {
|
|
530
|
+
return node.value + "deg";
|
|
531
|
+
},
|
|
532
|
+
"visit_directional": function(node) {
|
|
533
|
+
return "to " + node.value;
|
|
534
|
+
},
|
|
535
|
+
"visit_array": function(elements) {
|
|
536
|
+
var result = "", size = elements.length;
|
|
537
|
+
elements.forEach(function(element, i) {
|
|
538
|
+
result += visitor.visit(element);
|
|
539
|
+
if (i < size - 1) {
|
|
540
|
+
result += ", ";
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
return result;
|
|
544
|
+
},
|
|
545
|
+
"visit_object": function(obj) {
|
|
546
|
+
if (obj.width && obj.height) {
|
|
547
|
+
return visitor.visit(obj.width) + " " + visitor.visit(obj.height);
|
|
548
|
+
}
|
|
549
|
+
return "";
|
|
550
|
+
},
|
|
551
|
+
"visit": function(element) {
|
|
552
|
+
if (!element) {
|
|
553
|
+
return "";
|
|
554
|
+
}
|
|
555
|
+
if (element instanceof Array) {
|
|
556
|
+
return visitor.visit_array(element);
|
|
557
|
+
} else if (typeof element === "object" && !element.type) {
|
|
558
|
+
return visitor.visit_object(element);
|
|
559
|
+
} else if (element.type) {
|
|
560
|
+
var nodeVisitor = visitor["visit_" + element.type];
|
|
561
|
+
if (nodeVisitor) {
|
|
562
|
+
return nodeVisitor(element);
|
|
563
|
+
} else {
|
|
564
|
+
throw Error("Missing visitor visit_" + element.type);
|
|
565
|
+
}
|
|
566
|
+
} else {
|
|
567
|
+
throw Error("Invalid node.");
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
return function(root) {
|
|
572
|
+
return visitor.visit(root);
|
|
573
|
+
};
|
|
574
|
+
}();
|
|
575
|
+
const stringifyGradient = GradientParser.stringify.bind(GradientParser);
|
|
576
|
+
|
|
577
|
+
function parseLinearGradientNode(node) {
|
|
578
|
+
let angle = 0;
|
|
579
|
+
switch (node.orientation?.type) {
|
|
580
|
+
case "angular":
|
|
581
|
+
angle = Number(node.orientation.value);
|
|
582
|
+
break;
|
|
73
583
|
}
|
|
584
|
+
const stops = node.colorStops.map((stop) => {
|
|
585
|
+
const value = stop.value;
|
|
586
|
+
let offset = 0;
|
|
587
|
+
let color = "#00000000";
|
|
588
|
+
switch (stop.type) {
|
|
589
|
+
case "rgb":
|
|
590
|
+
color = normalizeColor({
|
|
591
|
+
r: Number(value[0] ?? 0),
|
|
592
|
+
g: Number(value[1] ?? 0),
|
|
593
|
+
b: Number(value[2] ?? 0)
|
|
594
|
+
});
|
|
595
|
+
break;
|
|
596
|
+
case "rgba":
|
|
597
|
+
color = normalizeColor({
|
|
598
|
+
r: Number(value[0] ?? 0),
|
|
599
|
+
g: Number(value[1] ?? 0),
|
|
600
|
+
b: Number(value[2] ?? 0),
|
|
601
|
+
a: Number(value[3] ?? 0)
|
|
602
|
+
});
|
|
603
|
+
break;
|
|
604
|
+
case "literal":
|
|
605
|
+
color = normalizeColor(stop.value);
|
|
606
|
+
break;
|
|
607
|
+
case "hex":
|
|
608
|
+
color = normalizeColor(stop.value);
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
switch (stop.length?.type) {
|
|
612
|
+
case "%":
|
|
613
|
+
offset = Number(stop.length.value) / 100;
|
|
614
|
+
break;
|
|
615
|
+
}
|
|
616
|
+
return { offset, color };
|
|
617
|
+
});
|
|
618
|
+
return {
|
|
619
|
+
type: "linear-gradient",
|
|
620
|
+
angle,
|
|
621
|
+
stops
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
function parseRadialGradientNode(ast) {
|
|
625
|
+
ast.orientation?.map((item) => {
|
|
626
|
+
switch (item?.type) {
|
|
627
|
+
case "shape":
|
|
628
|
+
case "default-radial":
|
|
629
|
+
case "extent-keyword":
|
|
630
|
+
default:
|
|
631
|
+
return null;
|
|
632
|
+
}
|
|
633
|
+
});
|
|
634
|
+
const stops = ast.colorStops.map((stop) => {
|
|
635
|
+
const value = stop.value;
|
|
636
|
+
let offset = 0;
|
|
637
|
+
let color = "#00000000";
|
|
638
|
+
switch (stop.type) {
|
|
639
|
+
case "rgb":
|
|
640
|
+
color = normalizeColor({
|
|
641
|
+
r: Number(value[0] ?? 0),
|
|
642
|
+
g: Number(value[1] ?? 0),
|
|
643
|
+
b: Number(value[2] ?? 0)
|
|
644
|
+
});
|
|
645
|
+
break;
|
|
646
|
+
case "rgba":
|
|
647
|
+
color = normalizeColor({
|
|
648
|
+
r: Number(value[0] ?? 0),
|
|
649
|
+
g: Number(value[1] ?? 0),
|
|
650
|
+
b: Number(value[2] ?? 0),
|
|
651
|
+
a: Number(value[3] ?? 0)
|
|
652
|
+
});
|
|
653
|
+
break;
|
|
654
|
+
case "literal":
|
|
655
|
+
color = normalizeColor(stop.value);
|
|
656
|
+
break;
|
|
657
|
+
case "hex":
|
|
658
|
+
color = normalizeColor(stop.value);
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
switch (stop.length?.type) {
|
|
662
|
+
case "%":
|
|
663
|
+
offset = Number(stop.length.value) / 100;
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
return { offset, color };
|
|
667
|
+
});
|
|
668
|
+
return {
|
|
669
|
+
type: "radial-gradient",
|
|
670
|
+
stops
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
function normalizeGradient(cssText) {
|
|
674
|
+
return parseGradient(cssText).map((node) => {
|
|
675
|
+
switch (node?.type) {
|
|
676
|
+
case "linear-gradient":
|
|
677
|
+
return parseLinearGradientNode(node);
|
|
678
|
+
case "repeating-linear-gradient":
|
|
679
|
+
return { ...parseLinearGradientNode(node), repeat: true };
|
|
680
|
+
case "radial-gradient":
|
|
681
|
+
return parseRadialGradientNode(node);
|
|
682
|
+
case "repeating-radial-gradient":
|
|
683
|
+
return { ...parseRadialGradientNode(node), repeat: true };
|
|
684
|
+
default:
|
|
685
|
+
return void 0;
|
|
686
|
+
}
|
|
687
|
+
}).filter(Boolean);
|
|
74
688
|
}
|
|
75
689
|
|
|
690
|
+
function isNone(value) {
|
|
691
|
+
return value === null || value === void 0 || value === "none";
|
|
692
|
+
}
|
|
76
693
|
function clearUndef(obj, deep = false) {
|
|
77
694
|
if (typeof obj !== "object" || !obj) {
|
|
78
695
|
return obj;
|
|
@@ -99,26 +716,44 @@ function clearUndef(obj, deep = false) {
|
|
|
99
716
|
return newObj;
|
|
100
717
|
}
|
|
101
718
|
|
|
719
|
+
function normalizeFill(fill) {
|
|
720
|
+
if (typeof fill === "string") {
|
|
721
|
+
return { color: normalizeColor(fill) };
|
|
722
|
+
} else {
|
|
723
|
+
return {
|
|
724
|
+
...fill,
|
|
725
|
+
color: isNone(fill.color) ? void 0 : normalizeColor(fill.color)
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function normalizeBackground(background) {
|
|
731
|
+
if (typeof background === "string") {
|
|
732
|
+
return { src: background };
|
|
733
|
+
} else {
|
|
734
|
+
return {
|
|
735
|
+
...background,
|
|
736
|
+
...normalizeFill(background)
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
102
741
|
function getDefaultInnerShadow() {
|
|
103
742
|
return {
|
|
104
|
-
color:
|
|
743
|
+
color: defaultColor,
|
|
105
744
|
offsetX: 0,
|
|
106
745
|
offsetY: 0,
|
|
107
746
|
blurRadius: 1
|
|
108
747
|
};
|
|
109
748
|
}
|
|
110
749
|
function normalizeInnerShadow(shadow) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
color: normalizeColor(shadow.color)
|
|
119
|
-
})
|
|
120
|
-
};
|
|
121
|
-
}
|
|
750
|
+
return {
|
|
751
|
+
...getDefaultInnerShadow(),
|
|
752
|
+
...clearUndef({
|
|
753
|
+
...shadow,
|
|
754
|
+
color: isNone(shadow.color) ? defaultColor : normalizeColor(shadow.color)
|
|
755
|
+
})
|
|
756
|
+
};
|
|
122
757
|
}
|
|
123
758
|
|
|
124
759
|
function getDefaultOuterShadow() {
|
|
@@ -129,41 +764,27 @@ function getDefaultOuterShadow() {
|
|
|
129
764
|
};
|
|
130
765
|
}
|
|
131
766
|
function normalizeOuterShadow(shadow) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
...getDefaultOuterShadow(),
|
|
137
|
-
...normalizeInnerShadow(shadow)
|
|
138
|
-
};
|
|
139
|
-
}
|
|
767
|
+
return {
|
|
768
|
+
...getDefaultOuterShadow(),
|
|
769
|
+
...normalizeInnerShadow(shadow)
|
|
770
|
+
};
|
|
140
771
|
}
|
|
141
772
|
|
|
142
773
|
function normalizeSoftEdge(softEdge) {
|
|
143
|
-
|
|
144
|
-
return void 0;
|
|
145
|
-
} else {
|
|
146
|
-
return softEdge;
|
|
147
|
-
}
|
|
774
|
+
return softEdge;
|
|
148
775
|
}
|
|
149
776
|
|
|
150
777
|
function normalizeEffect(effect) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
outerShadow: normalizeOuterShadow(effect.outerShadow),
|
|
158
|
-
innerShadow: normalizeInnerShadow(effect.innerShadow)
|
|
159
|
-
});
|
|
160
|
-
}
|
|
778
|
+
return clearUndef({
|
|
779
|
+
...effect,
|
|
780
|
+
softEdge: isNone(effect.softEdge) ? void 0 : normalizeSoftEdge(effect.softEdge),
|
|
781
|
+
outerShadow: isNone(effect.outerShadow) ? void 0 : normalizeOuterShadow(effect.outerShadow),
|
|
782
|
+
innerShadow: isNone(effect.innerShadow) ? void 0 : normalizeInnerShadow(effect.innerShadow)
|
|
783
|
+
});
|
|
161
784
|
}
|
|
162
785
|
|
|
163
786
|
function normalizeForeground(foreground) {
|
|
164
|
-
if (
|
|
165
|
-
return void 0;
|
|
166
|
-
} else if (typeof foreground === "string") {
|
|
787
|
+
if (typeof foreground === "string") {
|
|
167
788
|
return { src: foreground };
|
|
168
789
|
} else {
|
|
169
790
|
return {
|
|
@@ -174,9 +795,7 @@ function normalizeForeground(foreground) {
|
|
|
174
795
|
}
|
|
175
796
|
|
|
176
797
|
function normalizeGeometry(geometry) {
|
|
177
|
-
if (
|
|
178
|
-
return void 0;
|
|
179
|
-
} else if (typeof geometry === "string") {
|
|
798
|
+
if (typeof geometry === "string") {
|
|
180
799
|
return {
|
|
181
800
|
paths: [
|
|
182
801
|
{ data: geometry }
|
|
@@ -199,31 +818,27 @@ function normalizeGeometry(geometry) {
|
|
|
199
818
|
}
|
|
200
819
|
|
|
201
820
|
function normalizeOutline(outline) {
|
|
202
|
-
if (
|
|
203
|
-
return void 0;
|
|
204
|
-
} else if (typeof outline === "string") {
|
|
821
|
+
if (typeof outline === "string") {
|
|
205
822
|
return {
|
|
206
823
|
color: normalizeColor(outline)
|
|
207
824
|
};
|
|
208
825
|
} else {
|
|
209
826
|
return {
|
|
210
827
|
...outline,
|
|
211
|
-
color: normalizeColor(outline.color)
|
|
828
|
+
color: isNone(outline.color) ? void 0 : normalizeColor(outline.color)
|
|
212
829
|
};
|
|
213
830
|
}
|
|
214
831
|
}
|
|
215
832
|
|
|
216
833
|
function normalizeShadow(shadow) {
|
|
217
|
-
if (
|
|
218
|
-
return void 0;
|
|
219
|
-
} else if (typeof shadow === "string") {
|
|
834
|
+
if (typeof shadow === "string") {
|
|
220
835
|
return {
|
|
221
836
|
color: normalizeColor(shadow)
|
|
222
837
|
};
|
|
223
838
|
} else {
|
|
224
839
|
return {
|
|
225
840
|
...shadow,
|
|
226
|
-
color: normalizeColor(shadow.color)
|
|
841
|
+
color: isNone(shadow.color) ? defaultColor : normalizeColor(shadow.color)
|
|
227
842
|
};
|
|
228
843
|
}
|
|
229
844
|
}
|
|
@@ -393,16 +1008,13 @@ function getDefaultTextStyle() {
|
|
|
393
1008
|
}
|
|
394
1009
|
|
|
395
1010
|
function normalizeStyle(style) {
|
|
396
|
-
if (!style || style === "none") {
|
|
397
|
-
return void 0;
|
|
398
|
-
}
|
|
399
1011
|
return clearUndef({
|
|
400
1012
|
...style,
|
|
401
|
-
color: normalizeColor(style.color),
|
|
402
|
-
backgroundColor: normalizeColor(style.backgroundColor),
|
|
403
|
-
borderColor: normalizeColor(style.borderColor),
|
|
404
|
-
outlineColor: normalizeColor(style.outlineColor),
|
|
405
|
-
shadowColor: normalizeColor(style.shadowColor)
|
|
1013
|
+
color: isNone(style.color) ? void 0 : normalizeColor(style.color),
|
|
1014
|
+
backgroundColor: isNone(style.backgroundColor) ? void 0 : normalizeColor(style.backgroundColor),
|
|
1015
|
+
borderColor: isNone(style.borderColor) ? void 0 : normalizeColor(style.borderColor),
|
|
1016
|
+
outlineColor: isNone(style.outlineColor) ? void 0 : normalizeColor(style.outlineColor),
|
|
1017
|
+
shadowColor: isNone(style.shadowColor) ? void 0 : normalizeColor(style.shadowColor)
|
|
406
1018
|
});
|
|
407
1019
|
}
|
|
408
1020
|
function getDefaultStyle() {
|
|
@@ -452,9 +1064,7 @@ function normalizeTextContent(content = "") {
|
|
|
452
1064
|
});
|
|
453
1065
|
}
|
|
454
1066
|
function normalizeText(text) {
|
|
455
|
-
if (
|
|
456
|
-
return void 0;
|
|
457
|
-
} else if (typeof text === "string") {
|
|
1067
|
+
if (typeof text === "string") {
|
|
458
1068
|
return {
|
|
459
1069
|
content: [
|
|
460
1070
|
{
|
|
@@ -477,9 +1087,7 @@ function normalizeText(text) {
|
|
|
477
1087
|
}
|
|
478
1088
|
|
|
479
1089
|
function normalizeVideo(video) {
|
|
480
|
-
if (
|
|
481
|
-
return void 0;
|
|
482
|
-
} else if (typeof video === "string") {
|
|
1090
|
+
if (typeof video === "string") {
|
|
483
1091
|
return { src: video };
|
|
484
1092
|
} else {
|
|
485
1093
|
return video;
|
|
@@ -489,17 +1097,17 @@ function normalizeVideo(video) {
|
|
|
489
1097
|
function normalizeElement(element) {
|
|
490
1098
|
return clearUndef({
|
|
491
1099
|
...element,
|
|
492
|
-
style: normalizeStyle(element.style),
|
|
493
|
-
text: normalizeText(element.text),
|
|
494
|
-
background: normalizeBackground(element.background),
|
|
495
|
-
geometry: normalizeGeometry(element.geometry),
|
|
496
|
-
fill: normalizeFill(element.fill),
|
|
497
|
-
outline: normalizeOutline(element.outline),
|
|
498
|
-
foreground: normalizeForeground(element.foreground),
|
|
499
|
-
shadow: normalizeShadow(element.shadow),
|
|
500
|
-
video: normalizeVideo(element.video),
|
|
501
|
-
audio: normalizeAudio(element.audio),
|
|
502
|
-
effect: normalizeEffect(element.effect),
|
|
1100
|
+
style: isNone(element.style) ? void 0 : normalizeStyle(element.style),
|
|
1101
|
+
text: isNone(element.text) ? void 0 : normalizeText(element.text),
|
|
1102
|
+
background: isNone(element.background) ? void 0 : normalizeBackground(element.background),
|
|
1103
|
+
geometry: isNone(element.geometry) ? void 0 : normalizeGeometry(element.geometry),
|
|
1104
|
+
fill: isNone(element.fill) ? void 0 : normalizeFill(element.fill),
|
|
1105
|
+
outline: isNone(element.outline) ? void 0 : normalizeOutline(element.outline),
|
|
1106
|
+
foreground: isNone(element.foreground) ? void 0 : normalizeForeground(element.foreground),
|
|
1107
|
+
shadow: isNone(element.shadow) ? void 0 : normalizeShadow(element.shadow),
|
|
1108
|
+
video: isNone(element.video) ? void 0 : normalizeVideo(element.video),
|
|
1109
|
+
audio: isNone(element.audio) ? void 0 : normalizeAudio(element.audio),
|
|
1110
|
+
effect: isNone(element.effect) ? void 0 : normalizeEffect(element.effect),
|
|
503
1111
|
children: element.children?.map((child) => normalizeElement(child))
|
|
504
1112
|
});
|
|
505
1113
|
}
|
|
@@ -509,6 +1117,7 @@ function normalizeDocument(doc) {
|
|
|
509
1117
|
}
|
|
510
1118
|
|
|
511
1119
|
exports.clearUndef = clearUndef;
|
|
1120
|
+
exports.defaultColor = defaultColor;
|
|
512
1121
|
exports.getDefaultElementStyle = getDefaultElementStyle;
|
|
513
1122
|
exports.getDefaultHighlightStyle = getDefaultHighlightStyle;
|
|
514
1123
|
exports.getDefaultInnerShadow = getDefaultInnerShadow;
|
|
@@ -521,6 +1130,7 @@ exports.getDefaultTextInlineStyle = getDefaultTextInlineStyle;
|
|
|
521
1130
|
exports.getDefaultTextLineStyle = getDefaultTextLineStyle;
|
|
522
1131
|
exports.getDefaultTextStyle = getDefaultTextStyle;
|
|
523
1132
|
exports.getDefaultTransformStyle = getDefaultTransformStyle;
|
|
1133
|
+
exports.isNone = isNone;
|
|
524
1134
|
exports.normalizeAudio = normalizeAudio;
|
|
525
1135
|
exports.normalizeBackground = normalizeBackground;
|
|
526
1136
|
exports.normalizeColor = normalizeColor;
|
|
@@ -530,6 +1140,7 @@ exports.normalizeElement = normalizeElement;
|
|
|
530
1140
|
exports.normalizeFill = normalizeFill;
|
|
531
1141
|
exports.normalizeForeground = normalizeForeground;
|
|
532
1142
|
exports.normalizeGeometry = normalizeGeometry;
|
|
1143
|
+
exports.normalizeGradient = normalizeGradient;
|
|
533
1144
|
exports.normalizeInnerShadow = normalizeInnerShadow;
|
|
534
1145
|
exports.normalizeOuterShadow = normalizeOuterShadow;
|
|
535
1146
|
exports.normalizeOutline = normalizeOutline;
|
|
@@ -540,3 +1151,5 @@ exports.normalizeText = normalizeText;
|
|
|
540
1151
|
exports.normalizeTextContent = normalizeTextContent;
|
|
541
1152
|
exports.normalizeVideo = normalizeVideo;
|
|
542
1153
|
exports.parseColor = parseColor;
|
|
1154
|
+
exports.parseGradient = parseGradient;
|
|
1155
|
+
exports.stringifyGradient = stringifyGradient;
|