-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathhtml.test.tsx
More file actions
3263 lines (3123 loc) · 102 KB
/
html.test.tsx
File metadata and controls
3263 lines (3123 loc) · 102 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { expect, test, describe } from "vitest";
import { $, css, renderTemplate, token, ws } from "@webstudio-is/template";
import type { WebstudioFragment } from "@webstudio-is/sdk";
import { generateFragmentFromHtml as _generateFragmentFromHtml } from "./html";
// Wrapper that strips skippedSelectors for tests that only compare fragment shape
const generateFragmentFromHtml = (html: string) => {
const { skippedSelectors: _skipped, ...fragment } =
_generateFragmentFromHtml(html);
return fragment;
};
test("generate instances from html", () => {
expect(
generateFragmentFromHtml(`
<main>
<section>
<h1>It works!</h1>
<p>Webstudio is great.</p>
<ul>
<li>one</li>
<li>two</li>
</ul>
</section>
</main>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="main">
<ws.element ws:tag="section">
<ws.element ws:tag="h1">It works!</ws.element>
<ws.element ws:tag="p">Webstudio is great.</ws.element>
<ws.element ws:tag="ul">
<ws.element ws:tag="li">one</ws.element>
<ws.element ws:tag="li">two</ws.element>
</ws.element>
</ws.element>
</ws.element>
)
);
});
test("generate multiple root instances from html", () => {
expect(
generateFragmentFromHtml(`
<section>
<h1>One</h1>
</section>
<section>
<h1>Two</h1>
</section>
`)
).toEqual(
renderTemplate(
<>
<ws.element ws:tag="section">
<ws.element ws:tag="h1">One</ws.element>
</ws.element>
<ws.element ws:tag="section">
<ws.element ws:tag="h1">Two</ws.element>
</ws.element>
</>
)
);
});
test("handle broken html", () => {
expect(
generateFragmentFromHtml(`
<section>
<h1>One</h1>
</section attribute="value">
`)
).toEqual(
renderTemplate(
<>
<ws.element ws:tag="section">
<ws.element ws:tag="h1">One</ws.element>
</ws.element>
</>
)
);
});
test("handle non-html", () => {
expect(generateFragmentFromHtml("")).toEqual(renderTemplate(<></>));
expect(generateFragmentFromHtml("It works!")).toEqual(renderTemplate(<></>));
});
test("ignore custom elements", () => {
expect(
generateFragmentFromHtml(`
<custom-element>
<div></div>
</custom-element>
<section></section>
`)
).toEqual(renderTemplate(<ws.element ws:tag="section"></ws.element>));
});
test("ignore not allowed tags", () => {
expect(
generateFragmentFromHtml(`<div><marquee>test</marquee></div>`)
).toEqual(renderTemplate(<ws.element ws:tag="div"></ws.element>));
});
test("generate props from html attributes", () => {
expect(
generateFragmentFromHtml(`
<form action="/my-action">
<button class="my-button">My Button</button>
</form>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="form" action="/my-action">
<ws.element ws:tag="button" class="my-button">
My Button
</ws.element>
</ws.element>
)
);
});
test("generate props from number and boolean html attributes", () => {
expect(
generateFragmentFromHtml(`
<button autofocus tabindex="-1">My Button</button>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="button" autofocus={true} tabindex={-1}>
My Button
</ws.element>
)
);
});
test("generate props from number and boolean aria attributes", () => {
expect(
generateFragmentFromHtml(`
<button aria-expanded aria-valuemin="100">My Button</button>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="button" aria-expanded={true} aria-valuemin={100}>
My Button
</ws.element>
)
);
});
test("wrap text with span when spotted outside of rich text", () => {
expect(
generateFragmentFromHtml(`
<div>div<article>article</article></div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div">
<ws.element ws:tag="span">div</ws.element>
<ws.element ws:tag="article">article</ws.element>
</ws.element>
)
);
expect(
generateFragmentFromHtml(`
<div>div<b><br></b></div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div">
<ws.element ws:tag="span">div</ws.element>
<ws.element ws:tag="b">
<ws.element ws:tag="br"></ws.element>
</ws.element>
</ws.element>
)
);
});
test("do not wrap text with span when spotted near link", () => {
expect(
generateFragmentFromHtml(`
<div>div<a>link</a></div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div">
div
<ws.element ws:tag="a">link</ws.element>
</ws.element>
)
);
});
test("collapse any spacing characters inside text", () => {
expect(
generateFragmentFromHtml(`
<div>
line
another line
</div>
`)
).toEqual(
renderTemplate(<ws.element ws:tag="div">{"line another line"}</ws.element>)
);
});
test("collapse any spacing characters inside rich text", () => {
expect(
generateFragmentFromHtml(`
<div>
<i> line </i>
<b> another line </b>
text
</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div">
<ws.element ws:tag="i">{"line "}</ws.element>
<ws.element ws:tag="b">another line</ws.element> text
</ws.element>
)
);
});
test("preserve leading space in inline element following text without trailing space", () => {
expect(generateFragmentFromHtml(`<h2>About<span> Us</span></h2>`)).toEqual(
renderTemplate(
<ws.element ws:tag="h2">
{"About"}
<ws.element ws:tag="span">{" Us"}</ws.element>
</ws.element>
)
);
});
test("generate style attribute as local styles", () => {
expect(
generateFragmentFromHtml(`
<div style="display: inline"></div>
`)
).toEqual(
renderTemplate(
<ws.element
ws:tag="div"
ws:style={css`
display: inline;
`}
></ws.element>
)
);
});
test("script as html embed", () => {
expect(generateFragmentFromHtml(`<script>a;</script>`)).toEqual(
renderTemplate(
<$.HtmlEmbed
ws:label="Script"
clientOnly={true}
code={`<script>a;</script>`}
/>
)
);
});
test("style as html embed", () => {
expect(generateFragmentFromHtml(`<style>a;</style>`)).toEqual(
renderTemplate(<$.HtmlEmbed code={`<style>a;</style>`} ws:label="Style" />)
);
});
test("generate textarea element", () => {
expect(
generateFragmentFromHtml(`
<div>
<textarea>
my text
</textarea>
</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div">
<ws.element ws:tag="textarea" value="my text" />
</ws.element>
)
);
});
test("generate select element", () => {
expect(
generateFragmentFromHtml(`
<div>
<select>
<option value="one">One</option>
<option value="two" selected>Two</option>
</select>
<select>
<option>One</option>
<option selected>Two</option>
</select>
</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div">
<ws.element ws:tag="select" value="two">
<ws.element ws:tag="option" value="one">
One
</ws.element>
<ws.element ws:tag="option" value="two">
Two
</ws.element>
</ws.element>
<ws.element ws:tag="select" value="Two">
<ws.element ws:tag="option">One</ws.element>
<ws.element ws:tag="option">Two</ws.element>
</ws.element>
</ws.element>
)
);
});
test("generate Image component instead of img element", () => {
expect(
generateFragmentFromHtml(`
<div>
<img src="./my-url">
</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div">
<$.Image src="./my-url" />
</ws.element>
)
);
});
test("strip unsupported attribute names", () => {
expect(
generateFragmentFromHtml(`
<button @click="open = true">Expand</button>
<button x-on:click="open = !open">
Toggle
</button>
`)
).toEqual(
renderTemplate(
<>
<ws.element ws:tag="button">Expand</ws.element>
<ws.element ws:tag="button" x-on:click="open = !open">
Toggle
</ws.element>
</>
)
);
});
describe("style tag to tokens", () => {
test("extract single class from style tag as token", () => {
const cardToken = token(
"card",
css`
display: flex;
`
);
expect(
generateFragmentFromHtml(`
<style>.card { display: flex; }</style>
<div class="card">Hello</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" ws:tokens={[cardToken]}>
Hello
</ws.element>
)
);
});
test("extract multiple classes from style tag as tokens", () => {
const cardToken = token(
"card",
css`
display: flex;
padding: 16px;
`
);
const titleToken = token(
"title",
css`
font-size: 24px;
font-weight: bold;
`
);
expect(
generateFragmentFromHtml(`
<style>
.card { display: flex; padding: 16px; }
.title { font-size: 24px; font-weight: bold; }
</style>
<div class="card">
<h1 class="title">Hello</h1>
</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" ws:tokens={[cardToken]}>
<ws.element ws:tag="h1" ws:tokens={[titleToken]}>
Hello
</ws.element>
</ws.element>
)
);
});
test("reuse same token when class appears on multiple elements", () => {
const cardToken = token(
"card",
css`
display: flex;
`
);
expect(
generateFragmentFromHtml(`
<style>.card { display: flex; }</style>
<div class="card">
<section class="card">Hello</section>
</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" ws:tokens={[cardToken]}>
<ws.element ws:tag="section" ws:tokens={[cardToken]}>
Hello
</ws.element>
</ws.element>
)
);
});
test("preserve unresolved class names as class prop", () => {
const cardToken = token(
"card",
css`
display: flex;
`
);
expect(
generateFragmentFromHtml(`
<style>.card { display: flex; }</style>
<div class="card external-lib">Hello</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" class="external-lib" ws:tokens={[cardToken]}>
Hello
</ws.element>
)
);
});
test("remove class prop entirely when all classes resolved to tokens", () => {
const fragment = generateFragmentFromHtml(`
<style>.card { display: flex; }</style>
<div class="card">Hello</div>
`);
expect(fragment.props.find((p) => p.name === "class")).toBeUndefined();
});
test("combine inline style attribute with class token", () => {
const fragment = generateFragmentFromHtml(`
<style>.card { display: flex; }</style>
<div class="card" style="color: red">Hello</div>
`);
const cardToken = fragment.styleSources.find(
(source) => source.type === "token" && source.name === "card"
);
expect(cardToken).toBeDefined();
expect(fragment.children).toEqual([{ type: "id", value: "0" }]);
expect(fragment.instances).toEqual([
{
type: "instance",
id: "0",
component: "ws:element",
tag: "div",
children: [{ type: "text", value: "Hello" }],
},
]);
expect(fragment.styleSourceSelections).toEqual([
{ instanceId: "0", values: [cardToken!.id, "0:ws:style"] },
]);
expect(fragment.styles).toEqual(
expect.arrayContaining([
{
styleSourceId: cardToken!.id,
breakpointId: "base",
property: "display",
value: { type: "keyword", value: "flex" },
},
{
styleSourceId: "0:ws:style",
breakpointId: "base",
property: "color",
value: { type: "keyword", value: "red" },
},
])
);
});
test("no html embed when all style rules are class rules", () => {
const fragment = generateFragmentFromHtml(`
<style>
.card { display: flex; }
.title { font-size: 24px; }
</style>
<div class="card">
<h1 class="title">Hello</h1>
</div>
`);
expect(fragment.instances.some((i) => i.component === "HtmlEmbed")).toBe(
false
);
});
test("keep style tag as html embed when it has no class rules", () => {
expect(
generateFragmentFromHtml(`
<style>#hero { color: red; }</style>
<div>Hello</div>
`)
).toEqual(
renderTemplate(
<>
<$.HtmlEmbed
ws:label="Style"
code={`<style>#hero { color: red; }</style>`}
/>
<ws.element ws:tag="div">Hello</ws.element>
</>
)
);
});
test("extract class rules and keep remaining rules as html embed", () => {
const fragment = generateFragmentFromHtml(`
<style>
.card { display: flex; }
#hero { color: red; }
</style>
<div class="card">Hello</div>
`);
// token for .card should be created
const tokenSource = fragment.styleSources.find(
(s) => s.type === "token" && s.name === "card"
);
expect(tokenSource).toBeDefined();
// remaining #hero rule should stay as HtmlEmbed
const htmlEmbed = fragment.instances.find(
(i) => i.component === "HtmlEmbed"
);
expect(htmlEmbed).toBeDefined();
const codeProp = fragment.props.find(
(p) => p.instanceId === htmlEmbed!.id && p.name === "code"
);
expect(codeProp).toBeDefined();
expect(codeProp!.type === "string" && codeProp!.value).toContain("#hero");
// class prop should not be set since .card was resolved
expect(fragment.props.find((p) => p.name === "class")).toBeUndefined();
});
test("extract classes from multiple style tags", () => {
const cardToken = token(
"card",
css`
display: flex;
`
);
const titleToken = token(
"title",
css`
font-size: 24px;
`
);
expect(
generateFragmentFromHtml(`
<style>.card { display: flex; }</style>
<style>.title { font-size: 24px; }</style>
<div class="card">
<h1 class="title">Hello</h1>
</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" ws:tokens={[cardToken]}>
<ws.element ws:tag="h1" ws:tokens={[titleToken]}>
Hello
</ws.element>
</ws.element>
)
);
});
test("resolve nested selectors - descendant", () => {
const cardInnerToken = token(
"card__inner",
css`
color: red;
`
);
expect(
generateFragmentFromHtml(`
<style>.card .inner { color: red; }</style>
<div class="card">
<span class="inner">Hello</span>
</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" class="card">
<ws.element ws:tag="span" ws:tokens={[cardInnerToken]}>
Hello
</ws.element>
</ws.element>
)
);
});
test("resolve nested selectors - child combinator", () => {
const parentChildToken = token(
"parent__child",
css`
margin: 0;
`
);
expect(
generateFragmentFromHtml(`
<style>.parent > .child { margin: 0; }</style>
<div class="parent"><span class="child">Hello</span></div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" class="parent">
<ws.element ws:tag="span" ws:tokens={[parentChildToken]}>
Hello
</ws.element>
</ws.element>
)
);
});
test("ignore element selectors in style tag", () => {
// element selectors like `div { }` should not become tokens
expect(
generateFragmentFromHtml(`
<style>div { color: red; }</style>
<div>Hello</div>
`)
).toEqual(
renderTemplate(
<>
<$.HtmlEmbed
ws:label="Style"
code={`<style>div { color: red; }</style>`}
/>
<ws.element ws:tag="div">Hello</ws.element>
</>
)
);
});
test("handle multiple classes on one element with multiple tokens", () => {
const cardToken = token(
"card",
css`
display: flex;
`
);
const largeToken = token(
"large",
css`
font-size: 32px;
`
);
expect(
generateFragmentFromHtml(`
<style>
.card { display: flex; }
.large { font-size: 32px; }
</style>
<div class="card large">Hello</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" ws:tokens={[cardToken, largeToken]}>
Hello
</ws.element>
)
);
});
test("handle class with multiple properties", () => {
const cardToken = token(
"card",
css`
display: flex;
gap: 16px;
padding: 24px;
border-radius: 8px;
`
);
expect(
generateFragmentFromHtml(`
<style>
.card {
display: flex;
gap: 16px;
padding: 24px;
border-radius: 8px;
}
</style>
<div class="card">Hello</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" ws:tokens={[cardToken]}>
Hello
</ws.element>
)
);
});
test("empty style tag produces no html embed", () => {
expect(
generateFragmentFromHtml(`
<style></style>
<div>Hello</div>
`)
).toEqual(renderTemplate(<ws.element ws:tag="div">Hello</ws.element>));
});
test("class attribute without any style tag stays as prop", () => {
// existing behavior: class is just a prop when no style tag is present
expect(
generateFragmentFromHtml(`
<div class="my-class">Hello</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" class="my-class">
Hello
</ws.element>
)
);
});
test("style tag with only whitespace produces no html embed", () => {
expect(
generateFragmentFromHtml(`
<style> </style>
<div>Hello</div>
`)
).toEqual(renderTemplate(<ws.element ws:tag="div">Hello</ws.element>));
});
test("extract token and apply to nested structure", () => {
const wrapperToken = token(
"wrapper",
css`
max-width: 1200px;
margin-left: auto;
margin-right: auto;
`
);
const gridToken = token(
"grid",
css`
display: grid;
gap: 16px;
`
);
const itemToken = token(
"item",
css`
padding: 8px;
`
);
expect(
generateFragmentFromHtml(`
<style>
.wrapper { max-width: 1200px; margin-left: auto; margin-right: auto; }
.grid { display: grid; gap: 16px; }
.item { padding: 8px; }
</style>
<div class="wrapper">
<div class="grid">
<div class="item">One</div>
<div class="item">Two</div>
<div class="item">Three</div>
</div>
</div>
`)
).toEqual(
renderTemplate(
<ws.element ws:tag="div" ws:tokens={[wrapperToken]}>
<ws.element ws:tag="div" ws:tokens={[gridToken]}>
<ws.element ws:tag="div" ws:tokens={[itemToken]}>
One
</ws.element>
<ws.element ws:tag="div" ws:tokens={[itemToken]}>
Two
</ws.element>
<ws.element ws:tag="div" ws:tokens={[itemToken]}>
Three
</ws.element>
</ws.element>
</ws.element>
)
);
});
test("element with both resolved and unresolved classes plus inline style", () => {
const fragment = generateFragmentFromHtml(`
<style>.card { display: flex; }</style>
<div class="card external" style="color: red">Hello</div>
`);
const cardToken = fragment.styleSources.find(
(source) => source.type === "token" && source.name === "card"
);
expect(cardToken).toBeDefined();
expect(fragment.children).toEqual([{ type: "id", value: "0" }]);
expect(fragment.instances).toEqual([
{
type: "instance",
id: "0",
component: "ws:element",
tag: "div",
children: [{ type: "text", value: "Hello" }],
},
]);
expect(fragment.props).toEqual([
{
id: "0:class",
instanceId: "0",
name: "class",
type: "string",
value: "external",
},
]);
expect(fragment.styleSourceSelections).toEqual([
{ instanceId: "0", values: [cardToken!.id, "0:ws:style"] },
]);
expect(fragment.styles).toEqual(
expect.arrayContaining([
{
styleSourceId: cardToken!.id,
breakpointId: "base",
property: "display",
value: { type: "keyword", value: "flex" },
},
{
styleSourceId: "0:ws:style",
breakpointId: "base",
property: "color",
value: { type: "keyword", value: "red" },
},
])
);
});
test("token names preserve original class names", () => {
const fragment = generateFragmentFromHtml(`
<style>
.my-component { display: flex; }
.text-lg { font-size: 18px; }
.bg_primary { background-color: blue; }
</style>
<div class="my-component">
<span class="text-lg bg_primary">Hello</span>
</div>
`);
const tokenNames = fragment.styleSources
.filter((s) => s.type === "token")
.map((s) => (s as { name: string }).name);
expect(tokenNames).toContain("my-component");
expect(tokenNames).toContain("text-lg");
expect(tokenNames).toContain("bg_primary");
});
test("class defined in style tag but not used by any element", () => {
const fragment = generateFragmentFromHtml(`
<style>
.used { display: flex; }
.unused { color: red; }
</style>
<div class="used">Hello</div>
`);
// all class rules become tokens, even if not referenced by elements
const tokenNames = fragment.styleSources
.filter((s) => s.type === "token")
.map((s) => (s as { name: string }).name);
expect(tokenNames).toContain("used");
expect(tokenNames).toContain("unused");
// only "used" is assigned to the instance
const divInstance = fragment.instances.find((i) => i.tag === "div");
const sel = fragment.styleSourceSelections.find(
(s) => s.instanceId === divInstance?.id
);
const usedToken = fragment.styleSources.find(
(s) => s.type === "token" && s.name === "used"
);
const unusedToken = fragment.styleSources.find(
(s) => s.type === "token" && s.name === "unused"
);
expect(sel?.values).toContain(usedToken?.id);
expect(sel?.values).not.toContain(unusedToken?.id);
// no HtmlEmbed since all rules are class rules
expect(fragment.instances.some((i) => i.component === "HtmlEmbed")).toBe(
false
);
});
test("duplicate class rules across multiple style tags are merged", () => {
const fragment = generateFragmentFromHtml(`
<style>.card { display: flex; }</style>
<style>.card { padding: 16px; }</style>
<div class="card">Hello</div>
`);
// should create a single token with both properties
const tokens = fragment.styleSources.filter((s) => s.type === "token");
expect(tokens).toHaveLength(1);
const tokenId = tokens[0].id;
const tokenStyles = fragment.styles.filter(
(s) => s.styleSourceId === tokenId
);
const properties = tokenStyles.map((s) => s.property);
expect(properties).toContain("display");
expect(properties).toContain("paddingTop");
});
test("script tag still becomes html embed alongside token extraction", () => {
const cardToken = token(
"card",
css`
display: flex;
`
);
expect(
generateFragmentFromHtml(`
<style>.card { display: flex; }</style>
<script>console.log("hi");</script>
<div class="card">Hello</div>
`)
).toEqual(
renderTemplate(
<>
<$.HtmlEmbed
ws:label="Script"
clientOnly={true}
code={`<script>console.log("hi");</script>`}
/>
<ws.element ws:tag="div" ws:tokens={[cardToken]}>
Hello
</ws.element>
</>
)
);
});
test("style tag with pseudo-class creates token with state", () => {
const fragment = generateFragmentFromHtml(`
<style>
.btn { background-color: blue; }
.btn:hover { background-color: darkblue; }
</style>
<button class="btn">Click me</button>
`);
const tokenSource = fragment.styleSources.find(
(s) => s.type === "token" && s.name === "btn"
);
expect(tokenSource).toBeDefined();
// base style
const baseStyle = fragment.styles.find(
(s) =>
s.styleSourceId === tokenSource!.id &&
s.property === "backgroundColor" &&
s.state === undefined
);
expect(baseStyle).toBeDefined();
// hover state style
const hoverStyle = fragment.styles.find(
(s) =>
s.styleSourceId === tokenSource!.id &&
s.property === "backgroundColor" &&
s.state === ":hover"
);