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