Debian Codegraph @

src/tests/phoenix_heex.rs

raw
1 // https://github.com/phoenixframework/tree-sitter-heex/tree/6603380caf806b3e6c7f0bf61627bb47023d79f1/test/corpus
2
3 #![cfg(feature = "phoenix_heex")]
4
5 use super::*;
6
7 // ============================================================================
8 // text.txt
9 // ============================================================================
10
11 #[test]
12 fn plain_text() {
13 html_opts!(
14 [extension.phoenix_heex],
15 "Hello World!\n",
16 "<p>Hello World!</p>\n",
17 );
18 }
19
20 // ============================================================================
21 // comments.txt
22 // ============================================================================
23
24 #[test]
25 fn comments() {
26 html_opts!(
27 [extension.phoenix_heex],
28 concat!(
29 "I am some text!\n",
30 "<%# Look a comment! %>\n",
31 "I am more some text!\n",
32 ),
33 concat!(
34 "<p>I am some text!\n",
35 "<%# Look a comment! %>\n",
36 "I am more some text!</p>\n",
37 ),
38 );
39 }
40
41 #[test]
42 fn empty_comments() {
43 html_opts!(
44 [extension.phoenix_heex],
45 concat!("I am some text!\n", "<%# %>\n", "I am more some text!\n",),
46 concat!(
47 "<p>I am some text!\n",
48 "<%# %>\n",
49 "I am more some text!</p>\n",
50 ),
51 );
52 }
53
54 #[test]
55 fn multi_line_comments() {
56 html_opts!(
57 [extension.phoenix_heex],
58 concat!(
59 "<%# I\n",
60 " AM\n",
61 " A\n",
62 " COMMENT\n",
63 "%>\n",
64 "and some text!\n",
65 ),
66 concat!(
67 "<%# I\n",
68 " AM\n",
69 " A\n",
70 " COMMENT\n",
71 "%>\n",
72 "<p>and some text!</p>\n",
73 ),
74 );
75 }
76
77 #[test]
78 fn elixir_comments() {
79 html_opts!(
80 [extension.phoenix_heex],
81 concat!("<%= foo\n", " # |> bar()\n", " |> baz() %>\n",),
82 concat!("<%= foo\n", " # |> bar()\n", " |> baz() %>\n",),
83 );
84 }
85
86 #[test]
87 fn html_comments() {
88 html_opts_i(
89 concat!(
90 "<p>I am some text!</p>\n",
91 "<!-- Look a comment! -->\n",
92 "<p>I am more some text!</p>\n",
93 ),
94 concat!(
95 "<p>I am some text!</p>\n",
96 "<!-- Look a comment! -->\n",
97 "<p>I am more some text!</p>\n",
98 ),
99 true,
100 |opts| {
101 opts.extension.phoenix_heex = true;
102 opts.render.r#unsafe = true;
103 },
104 );
105 }
106
107 #[test]
108 fn empty_html_comments() {
109 html_opts_i(
110 concat!(
111 "<p>I am some text!</p>\n",
112 "<!-- -->\n",
113 "<p>I am more some text!</p>\n",
114 ),
115 concat!(
116 "<p>I am some text!</p>\n",
117 "<!-- -->\n",
118 "<p>I am more some text!</p>\n",
119 ),
120 true,
121 |opts| {
122 opts.extension.phoenix_heex = true;
123 opts.render.r#unsafe = true;
124 },
125 );
126 }
127
128 #[test]
129 fn new_style_multi_line_comments() {
130 html_opts!(
131 [extension.phoenix_heex],
132 concat!(
133 "<%!--\n",
134 " Where'd the directive go?\n",
135 " <%= @x %>\n",
136 "--%>\n",
137 ),
138 concat!(
139 "<%!--\n",
140 " Where'd the directive go?\n",
141 " <%= @x %>\n",
142 "--%>\n",
143 ),
144 );
145 }
146
147 #[test]
148 fn empty_new_style_multi_line_comments() {
149 html_opts!([extension.phoenix_heex], "<%!-- --%>\n", "<%!-- --%>\n",);
150 }
151
152 // ============================================================================
153 // expressions.txt
154 // ============================================================================
155
156 #[test]
157 fn simple_expression() {
158 html_opts_i(
159 "<div hello={@hello} {@world}/>\n",
160 "<div hello={@hello} {@world}/>\n",
161 true,
162 |opts| {
163 opts.extension.phoenix_heex = true;
164 opts.render.r#unsafe = true;
165 },
166 );
167 }
168
169 #[test]
170 fn single_empty_tuple() {
171 html_opts_i(
172 "<div hello={{}} />\n",
173 "<div hello={{}} />\n",
174 true,
175 |opts| {
176 opts.extension.phoenix_heex = true;
177 opts.render.r#unsafe = true;
178 },
179 );
180 }
181
182 #[test]
183 fn nested_empty_tuples() {
184 html_opts_i(
185 "<div hello={{{{{}}}}} />\n",
186 "<div hello={{{{{}}}}} />\n",
187 true,
188 |opts| {
189 opts.extension.phoenix_heex = true;
190 opts.render.r#unsafe = true;
191 },
192 );
193 }
194
195 #[test]
196 fn interpolation() {
197 html_opts_i(
198 "<div id={ \"##{@id}\" } />\n",
199 "<div id={ \"##{@id}\" } />\n",
200 true,
201 |opts| {
202 opts.extension.phoenix_heex = true;
203 opts.render.r#unsafe = true;
204 },
205 );
206 }
207
208 #[test]
209 fn many_openings_and_closings() {
210 html_opts_i(
211 "<div hello={{{1}, {2}}} />\n",
212 "<div hello={{{1}, {2}}} />\n",
213 true,
214 |opts| {
215 opts.extension.phoenix_heex = true;
216 opts.render.r#unsafe = true;
217 },
218 );
219 }
220
221 #[test]
222 fn interpolation_inside_body() {
223 html_opts_i(
224 concat!(
225 "<div>\n",
226 " { @message }\n",
227 " {@message}\n",
228 " {\"#{1}\"}\n",
229 "</div>\n",
230 ),
231 concat!(
232 "<div>\n",
233 " { @message }\n",
234 " {@message}\n",
235 " {\"#{1}\"}\n",
236 "</div>\n",
237 ),
238 true,
239 |opts| {
240 opts.extension.phoenix_heex = true;
241 opts.render.r#unsafe = true;
242 },
243 );
244 }
245
246 // ============================================================================
247 // directives.txt
248 // ============================================================================
249
250 #[test]
251 fn if_expression_spread_between_multiple_directives() {
252 html_opts!(
253 [extension.phoenix_heex],
254 concat!("<%= if true do %>\n", " <%= @x %>\n", "<% end %>\n",),
255 concat!("<%= if true do %>\n", " <%= @x %>\n", "<% end %>\n",),
256 );
257 }
258
259 #[test]
260 fn case_expression_spread_between_multiple_directives() {
261 html_opts!(
262 [extension.phoenix_heex],
263 concat!(
264 "<%= case @x do %>\n",
265 " <%= ^x -> %>X, <%= x %>\n",
266 " <%= _ -> %>Not X\n",
267 "<% end %>\n",
268 ),
269 concat!(
270 "<%= case @x do %>\n",
271 " <%= ^x -> %>X, <%= x %>\n",
272 " <%= _ -> %>Not X\n",
273 "<% end %>\n",
274 ),
275 );
276 }
277
278 // ============================================================================
279 // components.txt
280 // ============================================================================
281
282 #[test]
283 fn component() {
284 html_opts!(
285 [extension.phoenix_heex],
286 concat!("<Root.render>\n", "</Root.render>\n",),
287 concat!("<Root.render>\n", "</Root.render>\n",),
288 );
289 }
290
291 #[test]
292 fn self_closing_component() {
293 html_opts!(
294 [extension.phoenix_heex],
295 "<MyApp.Components.Root.render/>\n",
296 "<MyApp.Components.Root.render/>\n",
297 );
298 }
299
300 #[test]
301 fn nested_components() {
302 html_opts!(
303 [extension.phoenix_heex],
304 concat!(
305 "<Root.render>\n",
306 " <Grid>\n",
307 " <Card />\n",
308 " </Card>\n",
309 "</Root.render>\n",
310 ),
311 concat!(
312 "<Root.render>\n",
313 " <Grid>\n",
314 " <Card />\n",
315 " </Card>\n",
316 "</Root.render>\n",
317 ),
318 );
319 }
320
321 #[test]
322 fn function_with_module_remote_component() {
323 html_opts!(
324 [extension.phoenix_heex],
325 "<MyComponent.btn text=\"Save\" />\n",
326 "<MyComponent.btn text=\"Save\" />\n",
327 );
328 }
329
330 #[test]
331 fn function_without_remote_component() {
332 html_opts!(
333 [extension.phoenix_heex],
334 "<.btn text=\"Save\" />\n",
335 "<.btn text=\"Save\" />\n",
336 );
337 }
338
339 #[test]
340 fn simple_example() {
341 html_opts_i(
342 concat!(
343 "<div class={@class} title=\"My div\">\n",
344 " <SomeModule.some_func_component attr1=\"some string\" attr2={@some_expression} {@other_dynamic_attrs} />\n",
345 " <.some_local_func_component attr1=\"some string\" />\n",
346 "</div>\n",
347 ),
348 concat!(
349 "<div class={@class} title=\"My div\">\n",
350 " <SomeModule.some_func_component attr1=\"some string\" attr2={@some_expression} {@other_dynamic_attrs} />\n",
351 " <.some_local_func_component attr1=\"some string\" />\n",
352 "</div>\n",
353 ),
354 true,
355 |opts| {
356 opts.extension.phoenix_heex = true;
357 opts.render.r#unsafe = true;
358 },
359 );
360 }
361
362 // ============================================================================
363 // slots.txt
364 // ============================================================================
365
366 #[test]
367 fn named_slots() {
368 html_opts!(
369 [extension.phoenix_heex],
370 concat!(
371 "<.modal>\n",
372 " <:header>\n",
373 " This is the top of the modal.\n",
374 " </:header>\n",
375 " This is the content of the modal.\n",
376 "</.modal>\n",
377 ),
378 concat!(
379 "<.modal>\n",
380 " <:header>\n",
381 " This is the top of the modal.\n",
382 " </:header>\n",
383 " This is the content of the modal.\n",
384 "</.modal>\n",
385 ),
386 );
387 }
388
389 #[test]
390 fn named_slots_with_attributes() {
391 html_opts!(
392 [extension.phoenix_heex],
393 concat!(
394 "<.modal>\n",
395 " <:header key={@value}>\n",
396 " </:header>\n",
397 "</.modal>\n",
398 ),
399 concat!(
400 "<.modal>\n",
401 " <:header key={@value}>\n",
402 " </:header>\n",
403 "</.modal>\n",
404 ),
405 );
406 }
407
408 #[test]
409 fn self_closing_slot_does_not_error() {
410 html_opts!(
411 [extension.phoenix_heex],
412 concat!("<.modal>\n", " <:header />\n", "</.modal>\n",),
413 concat!("<.modal>\n", " <:header />\n", "</.modal>\n",),
414 );
415 }
416
417 // ============================================================================
418 // special_attributes.txt
419 // ============================================================================
420
421 #[test]
422 fn component_special_attribute_let() {
423 html_opts!(
424 [extension.phoenix_heex],
425 concat!(
426 "<.form :let={f}>\n",
427 " <%= text_input f, :text %>\n",
428 " <%= submit \"Submit\" %>\n",
429 "</.form>\n",
430 ),
431 concat!(
432 "<.form :let={f}>\n",
433 " <%= text_input f, :text %>\n",
434 " <%= submit \"Submit\" %>\n",
435 "</.form>\n",
436 ),
437 );
438 }
439
440 #[test]
441 fn slot_special_attribute_let() {
442 html_opts!(
443 [extension.phoenix_heex],
444 concat!(
445 "<.func>\n",
446 " <:slot :let={foo}>\n",
447 " <%= foo %>\n",
448 " </:slot>\n",
449 "</.func>\n",
450 ),
451 concat!(
452 "<.func>\n",
453 " <:slot :let={foo}>\n",
454 " <%= foo %>\n",
455 " </:slot>\n",
456 "</.func>\n",
457 ),
458 );
459 }
460
461 #[test]
462 fn tag_special_attribute_for() {
463 html_opts_i(
464 concat!(
465 "<div :for={item <- @items}>\n",
466 " <%= item %>\n",
467 "</div>\n",
468 ),
469 concat!(
470 "<div :for={item <- @items}>\n",
471 " <%= item %>\n",
472 "</div>\n",
473 ),
474 true,
475 |opts| {
476 opts.extension.phoenix_heex = true;
477 opts.render.r#unsafe = true;
478 },
479 );
480 }
481
482 #[test]
483 fn self_closing_tag_special_attribute_for() {
484 html_opts_i(
485 "<div :for={item <- @items} />\n",
486 "<div :for={item <- @items} />\n",
487 true,
488 |opts| {
489 opts.extension.phoenix_heex = true;
490 opts.render.r#unsafe = true;
491 },
492 );
493 }
494
495 #[test]
496 fn self_closing_tag_special_attribute_for_no_space() {
497 html_opts_i(
498 "<div :for={item <- @items}/>\n",
499 "<div :for={item <- @items}/>\n",
500 true,
501 |opts| {
502 opts.extension.phoenix_heex = true;
503 opts.render.r#unsafe = true;
504 },
505 );
506 }
507
508 #[test]
509 fn component_special_attribute_for() {
510 html_opts!(
511 [extension.phoenix_heex],
512 concat!(
513 "<.component :for={item <- @items}>\n",
514 " <%= item %>\n",
515 "</.component>\n",
516 ),
517 concat!(
518 "<.component :for={item <- @items}>\n",
519 " <%= item %>\n",
520 "</.component>\n",
521 ),
522 );
523 }
524
525 #[test]
526 fn self_closing_component_special_attribute_for() {
527 html_opts!(
528 [extension.phoenix_heex],
529 "<.component :for={item <- @items}/>\n",
530 "<.component :for={item <- @items}/>\n",
531 );
532 }
533
534 #[test]
535 fn slot_special_attribute_for() {
536 html_opts!(
537 [extension.phoenix_heex],
538 concat!(
539 "<.component>\n",
540 " <:slot :for={item <- @items}>\n",
541 " <%= item %>\n",
542 " </:slot>\n",
543 "</.component>\n",
544 ),
545 concat!(
546 "<.component>\n",
547 " <:slot :for={item <- @items}>\n",
548 " <%= item %>\n",
549 " </:slot>\n",
550 "</.component>\n",
551 ),
552 );
553 }
554
555 #[test]
556 fn self_closing_slot_special_attribute_for() {
557 html_opts!(
558 [extension.phoenix_heex],
559 concat!(
560 "<.component>\n",
561 " <:slot :for={item <- @items} />\n",
562 "</.component>\n",
563 ),
564 concat!(
565 "<.component>\n",
566 " <:slot :for={item <- @items} />\n",
567 "</.component>\n",
568 ),
569 );
570 }
571
572 #[test]
573 fn tag_special_attribute_stream() {
574 html_opts_i(
575 concat!(
576 "<div :stream={item <- @items}>\n",
577 " <%= item %>\n",
578 "</div>\n",
579 ),
580 concat!(
581 "<div :stream={item <- @items}>\n",
582 " <%= item %>\n",
583 "</div>\n",
584 ),
585 true,
586 |opts| {
587 opts.extension.phoenix_heex = true;
588 opts.render.r#unsafe = true;
589 },
590 );
591 }
592
593 #[test]
594 fn tag_special_attribute_if() {
595 html_opts_i(
596 concat!("<div :if={@item}>\n", " <%= @item %>\n", "</div>\n",),
597 concat!("<div :if={@item}>\n", " <%= @item %>\n", "</div>\n",),
598 true,
599 |opts| {
600 opts.extension.phoenix_heex = true;
601 opts.render.r#unsafe = true;
602 },
603 );
604 }
605
606 #[test]
607 fn self_closing_tag_special_attribute_if() {
608 html_opts_i(
609 "<div :if={@item} />\n",
610 "<div :if={@item} />\n",
611 true,
612 |opts| {
613 opts.extension.phoenix_heex = true;
614 opts.render.r#unsafe = true;
615 },
616 );
617 }
618
619 #[test]
620 fn component_special_attribute_if() {
621 html_opts!(
622 [extension.phoenix_heex],
623 concat!(
624 "<.component :if={@expression}>\n",
625 " <%= @expression %>\n",
626 "</.component>\n",
627 ),
628 concat!(
629 "<.component :if={@expression}>\n",
630 " <%= @expression %>\n",
631 "</.component>\n",
632 ),
633 );
634 }
635
636 #[test]
637 fn self_closing_component_special_attribute_if() {
638 html_opts!(
639 [extension.phoenix_heex],
640 "<.component :if={@expression} />\n",
641 "<.component :if={@expression} />\n",
642 );
643 }
644
645 #[test]
646 fn slot_special_attribute_if() {
647 html_opts!(
648 [extension.phoenix_heex],
649 concat!(
650 "<.component>\n",
651 " <:slot :if={@expression}>\n",
652 " <%= @expression %>\n",
653 " </:slot>\n",
654 "</.component>\n",
655 ),
656 concat!(
657 "<.component>\n",
658 " <:slot :if={@expression}>\n",
659 " <%= @expression %>\n",
660 " </:slot>\n",
661 "</.component>\n",
662 ),
663 );
664 }
665
666 #[test]
667 fn self_closing_slot_special_attribute_if() {
668 html_opts!(
669 [extension.phoenix_heex],
670 concat!(
671 "<.component>\n",
672 " <:slot :if={@expression} />\n",
673 "</.component>\n",
674 ),
675 concat!(
676 "<.component>\n",
677 " <:slot :if={@expression} />\n",
678 "</.component>\n",
679 ),
680 );
681 }
682
683 // ============================================================================
684 // tags.txt
685 // ============================================================================
686
687 #[test]
688 fn html_tag() {
689 html_opts_i(
690 concat!("<div>\n", "</div>\n",),
691 concat!("<div>\n", "</div>\n",),
692 true,
693 |opts| {
694 opts.extension.phoenix_heex = true;
695 opts.render.r#unsafe = true;
696 },
697 );
698 }
699
700 #[test]
701 fn html_self_closing_tag() {
702 html_opts_i("<div/>\n", "<div/>\n", true, |opts| {
703 opts.extension.phoenix_heex = true;
704 opts.render.r#unsafe = true;
705 });
706 }
707
708 #[test]
709 fn html_nested_tags() {
710 html_opts_i(
711 concat!(
712 "<div>\n",
713 " <div>\n",
714 " <div />\n",
715 " </div>\n",
716 "</div>\n",
717 ),
718 concat!(
719 "<div>\n",
720 " <div>\n",
721 " <div />\n",
722 " </div>\n",
723 "</div>\n",
724 ),
725 true,
726 |opts| {
727 opts.extension.phoenix_heex = true;
728 opts.render.r#unsafe = true;
729 },
730 );
731 }
732
733 #[test]
734 fn doctype() {
735 html_opts_i(
736 concat!(
737 "<!DOCTYPE html>\n",
738 "\n",
739 "<html lang=\"en\">\n",
740 " <body>\n",
741 " <%= @inner_content %>\n",
742 " </body> \n",
743 "</html>\n",
744 ),
745 concat!(
746 "<!DOCTYPE html>\n",
747 "<html lang=\"en\">\n",
748 " <body>\n",
749 " <%= @inner_content %>\n",
750 " </body> \n",
751 "</html>\n",
752 ),
753 true,
754 |opts| {
755 opts.extension.phoenix_heex = true;
756 opts.render.r#unsafe = true;
757 },
758 );
759 }
760
761 #[test]
762 fn tag_name_with_hyphen() {
763 html_opts_i(
764 concat!("<box-icon name='like'>\n", "</box-icon>\n",),
765 concat!("<box-icon name='like'>\n", "</box-icon>\n",),
766 true,
767 |opts| {
768 opts.extension.phoenix_heex = true;
769 opts.render.r#unsafe = true;
770 },
771 );
772 }
773
774 // ============================================================================
775 // attributes.txt
776 // ============================================================================
777
778 #[test]
779 fn unquoted_attribute() {
780 html_opts!(
781 [extension.phoenix_heex],
782 "<Root.render key=value />\n",
783 "<Root.render key=value />\n",
784 );
785 }
786
787 #[test]
788 fn boolean_attribute() {
789 html_opts!(
790 [extension.phoenix_heex],
791 "<Root.render hidden />\n",
792 "<Root.render hidden />\n",
793 );
794 }
795
796 #[test]
797 fn single_quoted_attribute() {
798 html_opts!(
799 [extension.phoenix_heex],
800 "<Root.render key='value' />\n",
801 "<Root.render key='value' />\n",
802 );
803 }
804
805 #[test]
806 fn double_quoted_attribute() {
807 html_opts!(
808 [extension.phoenix_heex],
809 "<Root.render key=\"value\" />\n",
810 "<Root.render key=\"value\" />\n",
811 );
812 }
813
814 #[test]
815 fn expression_attribute() {
816 html_opts!(
817 [extension.phoenix_heex],
818 "<Root.render key={value} />\n",
819 "<Root.render key={value} />\n",
820 );
821 }
822
823 #[test]
824 fn single_character_attribute() {
825 html_opts!(
826 [extension.phoenix_heex],
827 "<Root.render a=\"a\" />\n",
828 "<Root.render a=\"a\" />\n",
829 );
830 }
831
832 #[test]
833 fn alpine_directive() {
834 html_opts_i(
835 concat!(
836 "<div x-data=\"{ open: false }\">\n",
837 " <button @click=\"open = ! open\">Toggle</button>\n",
838 " <div x-show=\"open\" @click.outside=\"open = false\">Contents...</div>\n",
839 "</div>\n",
840 ),
841 concat!(
842 "<div x-data=\"{ open: false }\">\n",
843 " <button @click=\"open = ! open\">Toggle</button>\n",
844 " <div x-show=\"open\" @click.outside=\"open = false\">Contents...</div>\n",
845 "</div>\n",
846 ),
847 true,
848 |opts| {
849 opts.extension.phoenix_heex = true;
850 opts.render.r#unsafe = true;
851 },
852 );
853 }
854
855 #[test]
856 fn alpine_attribute() {
857 html_opts_i(
858 concat!(
859 "<button x-on:click=\"open = ! open\">\n",
860 " Toggle\n",
861 "</button>\n",
862 ),
863 concat!(
864 "<button x-on:click=\"open = ! open\">\n",
865 " Toggle\n",
866 "</button>\n",
867 ),
868 true,
869 |opts| {
870 opts.extension.phoenix_heex = true;
871 opts.render.r#unsafe = true;
872 },
873 );
874 }
875
876 // ============================================================================
877 // markdown mixed
878 // ============================================================================
879
880 #[test]
881 fn markdown_mixed_heading_and_component() {
882 html_opts!(
883 [extension.phoenix_heex],
884 concat!("# Contact Form\n", "\n", "<.form>\n", "</.form>\n",),
885 concat!("<h1>Contact Form</h1>\n", "<.form>\n", "</.form>\n",),
886 );
887 }
888
889 #[test]
890 fn markdown_mixed_complex_form() {
891 html_opts!(
892 [extension.phoenix_heex],
893 concat!(
894 "# Contact Form\n",
895 "\n",
896 "<.form\n",
897 " for={@form}\n",
898 " phx-change=\"change_name\"\n",
899 ">\n",
900 " <.input field={@form[:email]} />\n",
901 "</.form>\n",
902 "\n",
903 "## Footer\n",
904 "\n",
905 "Socials\n",
906 ),
907 concat!(
908 "<h1>Contact Form</h1>\n",
909 "<.form\n",
910 " for={@form}\n",
911 " phx-change=\"change_name\"\n",
912 ">\n",
913 " <.input field={@form[:email]} />\n",
914 "</.form>\n",
915 "<h2>Footer</h2>\n",
916 "<p>Socials</p>\n",
917 ),
918 );
919 }
920
921 #[test]
922 fn markdown_mixed_inline_expression_and_component() {
923 html_opts!(
924 [extension.phoenix_heex],
925 concat!(
926 "# Posts from {Date.utc_today().year}\n",
927 "\n",
928 "<.button>Submit</.button>\n",
929 ),
930 concat!(
931 "<h1>Posts from {Date.utc_today().year}</h1>\n",
932 "<.button>Submit</.button>\n",
933 ),
934 );
935 }
936
937 // ============================================================================
938 // inline components
939 // ============================================================================
940
941 #[test]
942 fn inline_function_component() {
943 html_opts!(
944 [extension.phoenix_heex],
945 "Click <.button>here</.button> to continue\n",
946 "<p>Click <.button>here</.button> to continue</p>\n",
947 );
948 }
949
950 #[test]
951 fn markdown_after_inline_component() {
952 html_opts!(
953 [extension.phoenix_heex],
954 "<.component>first</.component> **second**\n",
955 "<p><.component>first</.component> <strong>second</strong></p>\n",
956 );
957 }
958
959 #[test]
960 fn inline_nested_components() {
961 html_opts!(
962 [extension.phoenix_heex],
963 "<.outer><.inner>text</.inner></.outer>\n",
964 "<.outer><.inner>text</.inner></.outer>\n",
965 );
966 }
967
968 #[test]
969 fn markdown_after_inline_nested_component() {
970 html_opts!(
971 [extension.phoenix_heex],
972 "<.outer><.inner>text</.inner></.outer> **after**\n",
973 "<p><.outer><.inner>text</.inner></.outer> <strong>after</strong></p>\n",
974 );
975 }
976
977 #[test]
978 fn markdown_after_multiple_inline_components() {
979 html_opts!(
980 [extension.phoenix_heex],
981 "<.a>x</.a><.b>y</.b> **after**\n",
982 "<p><.a>x</.a><.b>y</.b> <strong>after</strong></p>\n",
983 );
984 }
985
986 #[test]
987 fn markdown_after_self_closing_component() {
988 html_opts!(
989 [extension.phoenix_heex],
990 "<.icon name=\"star\" /> **label**\n",
991 "<p><.icon name=\"star\" /> <strong>label</strong></p>\n",
992 );
993 }
994
995 #[test]
996 fn markdown_after_component_with_attributes() {
997 html_opts!(
998 [extension.phoenix_heex],
999 "<.link href=\"/\">click</.link> **bold**\n",
1000 "<p><.link href=\"/\">click</.link> <strong>bold</strong></p>\n",
1001 );
1002 }
1003
1004 #[test]
1005 fn markdown_after_empty_component() {
1006 html_opts!(
1007 [extension.phoenix_heex],
1008 "<.spacer></.spacer> **after**\n",
1009 "<p><.spacer></.spacer> <strong>after</strong></p>\n",
1010 );
1011 }
1012
1013 #[test]
1014 fn code_blocks_mixed_with_components() {
1015 html_opts!(
1016 [extension.phoenix_heex],
1017 concat!(
1018 "# Hello World\n",
1019 "\n",
1020 "<.link href=\"/\">Regular anchor link</.link>\n",
1021 "\n",
1022 "```elixir\n",
1023 "IO.puts(\"Hello\")\n",
1024 "```\n",
1025 "\n",
1026 "<.link navigate=\"/?sort=asc\" replace={false}>\n",
1027 " Sort By Price\n",
1028 "</.link>\n",
1029 "\n",
1030 "```rust\n",
1031 "let result = ammonia::clean(\"<b><img src='' onerror=alert('attack')>I'm not trying to XSS you</b>\");\n",
1032 "```\n",
1033 ),
1034 concat!(
1035 "<h1>Hello World</h1>\n",
1036 "<.link href=\"/\">Regular anchor link</.link>\n",
1037 "<pre><code class=\"language-elixir\">IO.puts(&quot;Hello&quot;)\n",
1038 "</code></pre>\n",
1039 "<.link navigate=\"/?sort=asc\" replace={false}>\n",
1040 " Sort By Price\n",
1041 "</.link>\n",
1042 "<pre><code class=\"language-rust\">let result = ammonia::clean(&quot;&lt;b&gt;&lt;img src='' onerror=alert('attack')&gt;I'm not trying to XSS you&lt;/b&gt;&quot;);\n",
1043 "</code></pre>\n",
1044 ),
1045 );
1046 }
1047
1048 #[test]
1049 fn inline_slot() {
1050 // Slots are invalid inline (they only appear inside components)
1051 // so they should be HTML-escaped
1052 html_opts!(
1053 [extension.phoenix_heex],
1054 "Header: <:title>My Title</:title>\n",
1055 "<p>Header: &lt;:title&gt;My Title&lt;/:title&gt;</p>\n",
1056 );
1057 }
1058
1059 // ============================================================================
1060 // inline expressions
1061 // ============================================================================
1062
1063 #[test]
1064 fn inline_expression_simple() {
1065 html_opts!(
1066 [extension.phoenix_heex],
1067 "The year is {Date.utc_today().year}\n",
1068 "<p>The year is {Date.utc_today().year}</p>\n",
1069 );
1070 }
1071
1072 #[test]
1073 fn inline_expression_in_heading() {
1074 html_opts!(
1075 [extension.phoenix_heex],
1076 "# Posts from {Date.utc_today().year}\n",
1077 "<h1>Posts from {Date.utc_today().year}</h1>\n",
1078 );
1079 }
1080
1081 #[test]
1082 fn inline_expression_nested_braces() {
1083 html_opts!(
1084 [extension.phoenix_heex],
1085 "Value: {%{key: \"value\"}}\n",
1086 "<p>Value: {%{key: \"value\"}}</p>\n",
1087 );
1088 }
1089
1090 #[test]
1091 fn inline_expression_with_function_call() {
1092 html_opts!(
1093 [extension.phoenix_heex],
1094 "Count: {length(items)}\n",
1095 "<p>Count: {length(items)}</p>\n",
1096 );
1097 }
1098
1099 #[test]
1100 fn inline_expression_multiple() {
1101 html_opts!(
1102 [extension.phoenix_heex],
1103 "User {user.name} has {user.age} items\n",
1104 "<p>User {user.name} has {user.age} items</p>\n",
1105 );
1106 }
1107
1108 #[test]
1109 fn inline_expression_with_string_literal() {
1110 html_opts!(
1111 [extension.phoenix_heex],
1112 "Text: {\"hello {world}\"}\n",
1113 "<p>Text: {\"hello {world}\"}</p>\n",
1114 );
1115 }
1116
1117 // ============================================================================
1118 // inline directives
1119 // ============================================================================
1120
1121 #[test]
1122 fn directive_simple() {
1123 html_opts!(
1124 [extension.phoenix_heex],
1125 "The year is <% Date.utc_today().year %>\n",
1126 "<p>The year is <% Date.utc_today().year %></p>\n",
1127 );
1128 }
1129
1130 #[test]
1131 fn directive_output() {
1132 html_opts!(
1133 [extension.phoenix_heex],
1134 "The year is <%= Date.utc_today().year %>\n",
1135 "<p>The year is <%= Date.utc_today().year %></p>\n",
1136 );
1137 }
1138
1139 #[test]
1140 fn directive_escaped() {
1141 html_opts!(
1142 [extension.phoenix_heex],
1143 "Code: <%% IO.inspect(user) %>\n",
1144 "<p>Code: <%% IO.inspect(user) %></p>\n",
1145 );
1146 }
1147
1148 #[test]
1149 fn directive_escaped_output() {
1150 html_opts!(
1151 [extension.phoenix_heex],
1152 "Code: <%%= IO.inspect(user) %>\n",
1153 "<p>Code: <%%= IO.inspect(user) %></p>\n",
1154 );
1155 }
1156
1157 #[test]
1158 fn directive_with_string() {
1159 html_opts!(
1160 [extension.phoenix_heex],
1161 "Text: <%= \"hello %> world\" %>\n",
1162 "<p>Text: <%= \"hello %> world\" %></p>\n",
1163 );
1164 }
1165
1166 #[test]
1167 fn directive_in_heading() {
1168 html_opts!(
1169 [extension.phoenix_heex],
1170 "# Posts from <%= @year %>\n",
1171 "<h1>Posts from <%= @year %></h1>\n",
1172 );
1173 }
1174
1175 #[test]
1176 fn directive_multiple() {
1177 html_opts!(
1178 [extension.phoenix_heex],
1179 "User <%= @user.name %> has <%= @user.age %> years\n",
1180 "<p>User <%= @user.name %> has <%= @user.age %> years</p>\n",
1181 );
1182 }
1183
1184 #[test]
1185 fn directive_multiline() {
1186 html_opts!(
1187 [extension.phoenix_heex],
1188 concat!(
1189 "Result: <%=\n",
1190 " user.name\n",
1191 " |> String.upcase()\n",
1192 "%>\n",
1193 ),
1194 concat!(
1195 "<p>Result: <%=\n",
1196 " user.name\n",
1197 " |> String.upcase()\n",
1198 "%></p>\n",
1199 ),
1200 );
1201 }
1202
1203 #[test]
1204 fn directive_mixed_with_expressions() {
1205 html_opts!(
1206 [extension.phoenix_heex],
1207 "Value: {user.name} or <%= @user.name %>\n",
1208 "<p>Value: {user.name} or <%= @user.name %></p>\n",
1209 );
1210 }
1211
1212 // ============================================================================
1213 // special attributes combinations
1214 // ============================================================================
1215
1216 #[test]
1217 fn special_attributes_for_with_html_tag() {
1218 html_opts_i(
1219 concat!(
1220 "<div :for={item <- @items}>\n",
1221 " {item.name}\n",
1222 "</div>\n",
1223 ),
1224 concat!(
1225 "<div :for={item <- @items}>\n",
1226 " {item.name}\n",
1227 "</div>\n",
1228 ),
1229 true,
1230 |opts| {
1231 opts.extension.phoenix_heex = true;
1232 opts.render.r#unsafe = true;
1233 },
1234 );
1235 }
1236
1237 #[test]
1238 fn special_attributes_if_with_html_tag() {
1239 html_opts_i(
1240 concat!(
1241 "<div :if={@show_message}>\n",
1242 " Message here\n",
1243 "</div>\n",
1244 ),
1245 concat!(
1246 "<div :if={@show_message}>\n",
1247 " Message here\n",
1248 "</div>\n",
1249 ),
1250 true,
1251 |opts| {
1252 opts.extension.phoenix_heex = true;
1253 opts.render.r#unsafe = true;
1254 },
1255 );
1256 }
1257
1258 #[test]
1259 fn special_attributes_let_with_component() {
1260 html_opts!(
1261 [extension.phoenix_heex],
1262 concat!(
1263 "<.form :let={f} for={@changeset}>\n",
1264 " <.input field={f[:email]} />\n",
1265 "</.form>\n",
1266 ),
1267 concat!(
1268 "<.form :let={f} for={@changeset}>\n",
1269 " <.input field={f[:email]} />\n",
1270 "</.form>\n",
1271 ),
1272 );
1273 }
1274
1275 #[test]
1276 fn special_attributes_stream_with_html_tag() {
1277 html_opts_i(
1278 concat!(
1279 "<div :stream={@messages}>\n",
1280 " {message.text}\n",
1281 "</div>\n",
1282 ),
1283 concat!(
1284 "<div :stream={@messages}>\n",
1285 " {message.text}\n",
1286 "</div>\n",
1287 ),
1288 true,
1289 |opts| {
1290 opts.extension.phoenix_heex = true;
1291 opts.render.r#unsafe = true;
1292 },
1293 );
1294 }
1295
1296 #[test]
1297 fn special_attributes_slot_with_let() {
1298 html_opts!(
1299 [extension.phoenix_heex],
1300 concat!(
1301 "<.table rows={@users}>\n",
1302 " <:col :let={user} label=\"Name\">\n",
1303 " {user.name}\n",
1304 " </:col>\n",
1305 " <:col :let={user} label=\"Email\">\n",
1306 " {user.email}\n",
1307 " </:col>\n",
1308 "</.table>\n",
1309 ),
1310 concat!(
1311 "<.table rows={@users}>\n",
1312 " <:col :let={user} label=\"Name\">\n",
1313 " {user.name}\n",
1314 " </:col>\n",
1315 " <:col :let={user} label=\"Email\">\n",
1316 " {user.email}\n",
1317 " </:col>\n",
1318 "</.table>\n",
1319 ),
1320 );
1321 }
1322
1323 #[test]
1324 fn special_attributes_for_and_if_combined() {
1325 html_opts_i(
1326 concat!(
1327 "<ul>\n",
1328 " <li :for={user <- @users} :if={user.active}>\n",
1329 " {user.name}\n",
1330 " </li>\n",
1331 "</ul>\n",
1332 ),
1333 concat!(
1334 "<ul>\n",
1335 " <li :for={user <- @users} :if={user.active}>\n",
1336 " {user.name}\n",
1337 " </li>\n",
1338 "</ul>\n",
1339 ),
1340 true,
1341 |opts| {
1342 opts.extension.phoenix_heex = true;
1343 opts.render.r#unsafe = true;
1344 },
1345 );
1346 }
1347
1348 #[test]
1349 fn special_attributes_nested() {
1350 html_opts_i(
1351 concat!(
1352 "<div :if={@show_list}>\n",
1353 " <div :for={item <- @items}>\n",
1354 " {item.name}\n",
1355 " </div>\n",
1356 "</div>\n",
1357 ),
1358 concat!(
1359 "<div :if={@show_list}>\n",
1360 " <div :for={item <- @items}>\n",
1361 " {item.name}\n",
1362 " </div>\n",
1363 "</div>\n",
1364 ),
1365 true,
1366 |opts| {
1367 opts.extension.phoenix_heex = true;
1368 opts.render.r#unsafe = true;
1369 },
1370 );
1371 }
1372
1373 // ============================================================================
1374 // root attributes
1375 // ============================================================================
1376
1377 #[test]
1378 fn root_attributes_spread() {
1379 html_opts_i(
1380 concat!("<div class=\"base\" {@attrs}>\n", " Content\n", "</div>\n",),
1381 concat!("<div class=\"base\" {@attrs}>\n", " Content\n", "</div>\n",),
1382 true,
1383 |opts| {
1384 opts.extension.phoenix_heex = true;
1385 opts.render.r#unsafe = true;
1386 },
1387 );
1388 }
1389
1390 #[test]
1391 fn root_attributes_multiple() {
1392 html_opts_i(
1393 "<.component class={@class} {@attrs1} hidden {@attrs2} />\n",
1394 "<.component class={@class} {@attrs1} hidden {@attrs2} />\n",
1395 true,
1396 |opts| {
1397 opts.extension.phoenix_heex = true;
1398 opts.render.r#unsafe = true;
1399 },
1400 );
1401 }
1402
1403 // ============================================================================
1404 // escaping
1405 // ============================================================================
1406
1407 #[test]
1408 fn escaped_braces_in_string() {
1409 html_opts!(
1410 [extension.phoenix_heex],
1411 "Value: {\"\\{escaped}\"}\n",
1412 "<p>Value: {\"\\{escaped}\"}</p>\n",
1413 );
1414 }
1415
1416 // ============================================================================
1417 // attributes
1418 // ============================================================================
1419
1420 #[test]
1421 fn boolean_attributes_phoenix_component() {
1422 html_opts!(
1423 [extension.phoenix_heex],
1424 "<.input field={@form[:email]} required />\n",
1425 "<.input field={@form[:email]} required />\n",
1426 );
1427 }
1428
1429 #[test]
1430 fn attributes_with_expressions_and_spaces() {
1431 html_opts!(
1432 [extension.phoenix_heex],
1433 concat!(
1434 "<.component\n",
1435 " class = {get_class()}\n",
1436 " id= {@id}\n",
1437 " data ={@data}\n",
1438 "/>\n",
1439 ),
1440 concat!(
1441 "<.component\n",
1442 " class = {get_class()}\n",
1443 " id= {@id}\n",
1444 " data ={@data}\n",
1445 "/>\n",
1446 ),
1447 );
1448 }
1449
1450 #[test]
1451 fn complex_expressions_in_attributes() {
1452 html_opts!(
1453 [extension.phoenix_heex],
1454 concat!(
1455 "<.component\n",
1456 " value={user.name}\n",
1457 " count={length(items)}\n",
1458 " data={%{key: \"value\"}}\n",
1459 "/>\n",
1460 ),
1461 concat!(
1462 "<.component\n",
1463 " value={user.name}\n",
1464 " count={length(items)}\n",
1465 " data={%{key: \"value\"}}\n",
1466 "/>\n",
1467 ),
1468 );
1469 }
1470
1471 // ============================================================================
1472 // components
1473 // ============================================================================
1474
1475 #[test]
1476 fn function_component_with_attributes() {
1477 html_opts!(
1478 [extension.phoenix_heex],
1479 concat!(
1480 "<.form\n",
1481 " for={@form}\n",
1482 " phx-change=\"change_name\"\n",
1483 ">\n",
1484 "</.form>\n",
1485 ),
1486 concat!(
1487 "<.form\n",
1488 " for={@form}\n",
1489 " phx-change=\"change_name\"\n",
1490 ">\n",
1491 "</.form>\n",
1492 ),
1493 );
1494 }
1495
1496 #[test]
1497 fn function_component_nested() {
1498 html_opts!(
1499 [extension.phoenix_heex],
1500 concat!(
1501 "<.form\n",
1502 " for={@form}\n",
1503 " phx-change=\"change_name\"\n",
1504 ">\n",
1505 " <.input field={@form[:email]} />\n",
1506 "</.form>\n",
1507 ),
1508 concat!(
1509 "<.form\n",
1510 " for={@form}\n",
1511 " phx-change=\"change_name\"\n",
1512 ">\n",
1513 " <.input field={@form[:email]} />\n",
1514 "</.form>\n",
1515 ),
1516 );
1517 }
1518
1519 // ============================================================================
1520 // slots
1521 // ============================================================================
1522
1523 #[test]
1524 fn slot_tags() {
1525 html_opts!(
1526 [extension.phoenix_heex],
1527 concat!(
1528 "<Component>\n",
1529 " <:subtitle>\n",
1530 " Subtitle content\n",
1531 " </:subtitle>\n",
1532 " <:actions>\n",
1533 " Action buttons\n",
1534 " </:actions>\n",
1535 "</Component>\n",
1536 ),
1537 concat!(
1538 "<Component>\n",
1539 " <:subtitle>\n",
1540 " Subtitle content\n",
1541 " </:subtitle>\n",
1542 " <:actions>\n",
1543 " Action buttons\n",
1544 " </:actions>\n",
1545 "</Component>\n",
1546 ),
1547 );
1548 }
1549
1550 // ============================================================================
1551 // sourcepos
1552 // ============================================================================
1553
1554 #[test]
1555 fn sourcepos_with_content() {
1556 assert_ast_match!(
1557 [extension.phoenix_heex],
1558 "<.form>\n"
1559 " <.input />\n"
1560 "</.form>\n",
1561 (document (1:1-3:8) [
1562 (heex_block (1:1-3:8) "<.form>\n <.input />\n</.form>\n")
1563 ]),
1564 );
1565 }
1566
1567 #[test]
1568 fn block_with_content_inside_blockquote_sourcepos() {
1569 assert_ast_match!(
1570 [extension.phoenix_heex],
1571 "> <.form>\n"
1572 "> <.input />\n"
1573 "> </.form>\n",
1574 (document (1:1-3:10) [
1575 (block_quote (1:1-3:10) [
1576 (heex_block (1:3-3:10) "<.form>\n <.input />\n</.form>\n")
1577 ])
1578 ]),
1579 );
1580 }
1581
1582 #[test]
1583 fn block_with_code_fence() {
1584 assert_ast_match!(
1585 [extension.phoenix_heex],
1586 "<.header>\n"
1587 " ```elixir\n"
1588 "</.header>\n",
1589 (document (1:1-3:10) [
1590 (heex_block (1:1-3:10) "<.header>\n ```elixir\n</.header>\n")
1591 ]),
1592 );
1593 }
1594
1595 #[test]
1596 fn block_with_code_fence_inside_blockquote_sourcepos() {
1597 assert_ast_match!(
1598 [extension.phoenix_heex],
1599 "> <.header>\n"
1600 "> ```elixir\n"
1601 "> </.header>\n",
1602 (document (1:1-3:12) [
1603 (block_quote (1:1-3:12) [
1604 (heex_block (1:3-3:12) "<.header>\n ```elixir\n</.header>\n")
1605 ])
1606 ]),
1607 );
1608 }
1609
1610 #[test]
1611 fn block_with_text_and_paragraph() {
1612 assert_ast_match!(
1613 [extension.phoenix_heex],
1614 "<.header>\n"
1615 "hello\n"
1616 "</.header>\n"
1617 "\n"
1618 "hello world\n",
1619 (document (1:1-5:11) [
1620 (heex_block (1:1-3:10) "<.header>\nhello\n</.header>\n")
1621 (paragraph (5:1-5:11) [
1622 (text (5:1-5:11) "hello world")
1623 ])
1624 ]),
1625 );
1626 }
1627
1628 #[test]
1629 fn block_with_text_and_paragraph_inside_blockquote_sourcepos() {
1630 assert_ast_match!(
1631 [extension.phoenix_heex],
1632 "> <.header>\n"
1633 "> hello\n"
1634 "> </.header>\n"
1635 ">\n"
1636 "> hello world\n",
1637 (document (1:1-5:13) [
1638 (block_quote (1:1-5:13) [
1639 (heex_block (1:3-3:12) "<.header>\nhello\n</.header>\n")
1640 (paragraph (5:3-5:13) [
1641 (text (5:3-5:13) "hello world")
1642 ])
1643 ])
1644 ]),
1645 );
1646 }
1647
1648 #[test]
1649 fn inline_expression_sourcepos() {
1650 assert_ast_match!(
1651 [extension.phoenix_heex],
1652 "Value: {user.name}\n",
1653 (document (1:1-1:18) [
1654 (paragraph (1:1-1:18) [
1655 (text (1:1-1:7) "Value: ")
1656 (heex_inline (1:8-1:18) "{user.name}")
1657 ])
1658 ]),
1659 );
1660 }
1661
1662 #[test]
1663 fn inline_expression_inside_blockquote_sourcepos() {
1664 assert_ast_match!(
1665 [extension.phoenix_heex],
1666 "> Here is a value: {user.name}\n",
1667 (document (1:1-1:30) [
1668 (block_quote (1:1-1:30) [
1669 (paragraph (1:3-1:30) [
1670 (text (1:3-1:19) "Here is a value: ")
1671 (heex_inline (1:20-1:30) "{user.name}")
1672 ])
1673 ])
1674 ]),
1675 );
1676 }
1677
1678 #[test]
1679 fn directive_sourcepos() {
1680 assert_ast_match!(
1681 [extension.phoenix_heex],
1682 "Value: <%= @user %>\n",
1683 (document (1:1-1:19) [
1684 (paragraph (1:1-1:19) [
1685 (text (1:1-1:7) "Value: ")
1686 (heex_inline (1:8-1:19) "<%= @user %>")
1687 ])
1688 ]),
1689 );
1690 }
1691
1692 #[test]
1693 fn directive_inside_blockquote_sourcepos() {
1694 assert_ast_match!(
1695 [extension.phoenix_heex],
1696 "> Here is a value: <%= @user %>\n",
1697 (document (1:1-1:31) [
1698 (block_quote (1:1-1:31) [
1699 (paragraph (1:3-1:31) [
1700 (text (1:3-1:19) "Here is a value: ")
1701 (heex_inline (1:20-1:31) "<%= @user %>")
1702 ])
1703 ])
1704 ]),
1705 );
1706 }
1707
1708 // ============================================================================
1709 // tag-aware blocks with blank lines
1710 // ============================================================================
1711
1712 #[test]
1713 fn block_with_blank_lines_inside() {
1714 html_opts!(
1715 [extension.phoenix_heex],
1716 concat!("<.foo>\n", "\n", "\n", "\n", "</.foo>\n",),
1717 concat!("<.foo>\n", "\n", "\n", "\n", "</.foo>\n",),
1718 );
1719 }
1720
1721 #[test]
1722 fn module_component_with_blank_lines() {
1723 html_opts!(
1724 [extension.phoenix_heex],
1725 concat!(
1726 "<MyComponent>\n",
1727 "\n",
1728 " Content here\n",
1729 "\n",
1730 "</MyComponent>\n",
1731 ),
1732 concat!(
1733 "<MyComponent>\n",
1734 "\n",
1735 " Content here\n",
1736 "\n",
1737 "</MyComponent>\n",
1738 ),
1739 );
1740 }
1741
1742 #[test]
1743 fn nested_components_with_blank_lines() {
1744 html_opts!(
1745 [extension.phoenix_heex],
1746 concat!(
1747 "<.outer>\n",
1748 "\n",
1749 " <.inner>\n",
1750 "\n",
1751 " </.inner>\n",
1752 "\n",
1753 "</.outer>\n",
1754 ),
1755 concat!(
1756 "<.outer>\n",
1757 "\n",
1758 " <.inner>\n",
1759 "\n",
1760 " </.inner>\n",
1761 "\n",
1762 "</.outer>\n",
1763 ),
1764 );
1765 }
1766
1767 #[test]
1768 fn block_ends_on_closing_tag() {
1769 html_opts!(
1770 [extension.phoenix_heex],
1771 concat!("<.form>\n", "Content\n", "</.form>\n", "\n", "After\n",),
1772 concat!("<.form>\n", "Content\n", "</.form>\n", "<p>After</p>\n",),
1773 );
1774 }
1775
1776 #[test]
1777 fn block_with_multiple_consecutive_empty_lines() {
1778 html_opts!(
1779 [extension.phoenix_heex],
1780 concat!("<Foo>\n", "\n", "\n", "</Foo>\n",),
1781 concat!("<Foo>\n", "\n", "\n", "</Foo>\n",),
1782 );
1783 }
1784
1785 #[test]
1786 fn self_closing_followed_by_content() {
1787 html_opts!(
1788 [extension.phoenix_heex],
1789 concat!("<.btn />\n", "\n", "After\n"),
1790 concat!("<.btn />\n", "<p>After</p>\n"),
1791 );
1792 }
1793
1794 #[test]
1795 fn self_closing_component_followed_by_content() {
1796 html_opts!(
1797 [extension.phoenix_heex],
1798 concat!("<Component.render />\n", "\n", "After\n"),
1799 concat!("<Component.render />\n", "<p>After</p>\n"),
1800 );
1801 }
1802
1803 // ============================================================================
1804 // output formats
1805 // ============================================================================
1806
1807 #[test]
1808 fn xml_output() {
1809 xml_opts(
1810 concat!("<.form>\n", "</.form>\n",),
1811 concat!(
1812 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
1813 "<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n",
1814 "<document xmlns=\"http://commonmark.org/xml/1.0\">\n",
1815 " <heex_block xml:space=\"preserve\">&lt;.form&gt;\n",
1816 "&lt;/.form&gt;\n",
1817 "</heex_block>\n",
1818 "</document>\n",
1819 ),
1820 |opts| {
1821 opts.extension.phoenix_heex = true;
1822 },
1823 );
1824 }
1825
1826 #[test]
1827 fn commonmark_output() {
1828 let input = concat!("<.form>\n", "</.form>\n",);
1829
1830 let arena = Arena::new();
1831 let mut options = Options::default();
1832 options.extension.phoenix_heex = true;
1833
1834 let root = parse_document(&arena, input, &options);
1835 let mut output = String::new();
1836 cm::format_document(root, &options, &mut output).unwrap();
1837
1838 compare_strs(
1839 &output,
1840 concat!("<.form>\n", "</.form>\n",),
1841 "commonmark",
1842 input,
1843 );
1844 }
1845
1846 // ============================================================================
1847 // edge cases
1848 // ============================================================================
1849
1850 #[test]
1851 fn malformed_closing_tag_incomplete() {
1852 html_opts!(
1853 [extension.phoenix_heex],
1854 concat!("<.form>\n", "content\n", "</\n", "</.form>\n",),
1855 concat!("<.form>\n", "content\n", "</\n", "</.form>\n",),
1856 );
1857 }
1858
1859 #[test]
1860 fn malformed_closing_tag_no_name() {
1861 html_opts!(
1862 [extension.phoenix_heex],
1863 concat!("<.form>\n", "</> \n", "</.form>\n",),
1864 concat!("<.form>\n", "</> \n", "</.form>\n",),
1865 );
1866 }
1867
1868 #[test]
1869 fn empty_component_tag_name() {
1870 html_opts!([extension.phoenix_heex], "<. />\n", "<p>&lt;. /&gt;</p>\n",);
1871 }
1872
1873 #[test]
1874 fn very_long_tag_name() {
1875 let long_name = "Component".repeat(50);
1876 let input = format!("<{}>\n</{}>\n", long_name, long_name);
1877 let expected = format!("<{}>\n</{}>\n", long_name, long_name);
1878
1879 html_opts!([extension.phoenix_heex], &input, &expected,);
1880 }
1881
1882 // ============================================================================
1883 // edge cases - EOF handling
1884 // ============================================================================
1885
1886 #[test]
1887 fn unclosed_component_at_eof() {
1888 html_opts!(
1889 [extension.phoenix_heex],
1890 "<.form>\n content\n",
1891 "<.form>\n content\n",
1892 );
1893 }
1894
1895 #[test]
1896 fn unclosed_directive_at_eof() {
1897 html_opts!(
1898 [extension.phoenix_heex],
1899 "<%= foo\n bar\n",
1900 "<%= foo\n bar\n",
1901 );
1902 }
1903
1904 #[test]
1905 fn unclosed_comment_at_eof() {
1906 html_opts!(
1907 [extension.phoenix_heex],
1908 "<%# this is a comment\n that never closes\n",
1909 "<%# this is a comment\n that never closes\n",
1910 );
1911 }
1912
1913 // ============================================================================
1914 // edge cases - tag matching
1915 // ============================================================================
1916
1917 #[test]
1918 fn mismatched_component_tags() {
1919 html_opts!(
1920 [extension.phoenix_heex],
1921 concat!("<.form>\n", " content\n", "</.button>\n",),
1922 concat!("<.form>\n", " content\n", "</.button>\n",),
1923 );
1924 }
1925
1926 #[test]
1927 fn mismatched_module_component_tags() {
1928 html_opts!(
1929 [extension.phoenix_heex],
1930 concat!("<MyApp.Form>\n", " content\n", "</MyApp.Button>\n",),
1931 concat!("<MyApp.Form>\n", " content\n", "</MyApp.Button>\n",),
1932 );
1933 }
1934
1935 #[test]
1936 fn case_sensitive_matching() {
1937 html_opts!(
1938 [extension.phoenix_heex],
1939 concat!("<.form>\n", " content\n", "</.Form>\n",),
1940 concat!("<.form>\n", " content\n", "</.Form>\n",),
1941 );
1942 }
1943
1944 #[test]
1945 fn closing_tag_with_whitespace() {
1946 html_opts!(
1947 [extension.phoenix_heex],
1948 concat!("<.form>\n", " content\n", "</ .form>\n",),
1949 concat!("<.form>\n", " content\n", "</ .form>\n",),
1950 );
1951 }
1952
1953 // ============================================================================
1954 // edge cases - nesting
1955 // ============================================================================
1956
1957 #[test]
1958 fn nested_same_name_module_component() {
1959 // https://github.com/leandrocp/mdex/issues/298
1960 html_opts!(
1961 [extension.phoenix_heex],
1962 concat!(
1963 "<MyApp.List.list>\n",
1964 " <MyApp.List.li>\n",
1965 " content\n",
1966 " <MyApp.List.list>\n",
1967 " <MyApp.List.li>nested</MyApp.List.li>\n",
1968 " </MyApp.List.list>\n",
1969 " </MyApp.List.li>\n",
1970 "</MyApp.List.list>\n",
1971 ),
1972 concat!(
1973 "<MyApp.List.list>\n",
1974 " <MyApp.List.li>\n",
1975 " content\n",
1976 " <MyApp.List.list>\n",
1977 " <MyApp.List.li>nested</MyApp.List.li>\n",
1978 " </MyApp.List.list>\n",
1979 " </MyApp.List.li>\n",
1980 "</MyApp.List.list>\n",
1981 ),
1982 );
1983 }
1984
1985 #[test]
1986 fn nested_same_name_function_component() {
1987 html_opts!(
1988 [extension.phoenix_heex],
1989 concat!(
1990 "<.list>\n",
1991 " <.li>\n",
1992 " content\n",
1993 " <.list>\n",
1994 " <.li>nested</.li>\n",
1995 " </.list>\n",
1996 " </.li>\n",
1997 "</.list>\n",
1998 ),
1999 concat!(
2000 "<.list>\n",
2001 " <.li>\n",
2002 " content\n",
2003 " <.list>\n",
2004 " <.li>nested</.li>\n",
2005 " </.list>\n",
2006 " </.li>\n",
2007 "</.list>\n",
2008 ),
2009 );
2010 }
2011
2012 #[test]
2013 fn self_closing_same_name_inside_block() {
2014 html_opts!(
2015 [extension.phoenix_heex],
2016 concat!(
2017 "<MyApp.List.list>\n",
2018 " <MyApp.List.list />\n",
2019 "</MyApp.List.list>\n",
2020 ),
2021 concat!(
2022 "<MyApp.List.list>\n",
2023 " <MyApp.List.list />\n",
2024 "</MyApp.List.list>\n",
2025 ),
2026 );
2027 }
2028
2029 #[test]
2030 fn inline_open_close_same_name_inside_block() {
2031 html_opts!(
2032 [extension.phoenix_heex],
2033 concat!("<.list>\n", " <.list>inner</.list>\n", "</.list>\n"),
2034 concat!("<.list>\n", " <.list>inner</.list>\n", "</.list>\n")
2035 );
2036 }
2037
2038 #[test]
2039 fn nested_directives_in_component() {
2040 html_opts!(
2041 [extension.phoenix_heex],
2042 concat!(
2043 "<.form>\n",
2044 " <%= if true do %>\n",
2045 " <%= @value %>\n",
2046 " <% end %>\n",
2047 "</.form>\n",
2048 ),
2049 concat!(
2050 "<.form>\n",
2051 " <%= if true do %>\n",
2052 " <%= @value %>\n",
2053 " <% end %>\n",
2054 "</.form>\n",
2055 ),
2056 );
2057 }
2058
2059 // ============================================================================
2060 // edge cases - directive finalization
2061 // ============================================================================
2062
2063 #[test]
2064 fn multiple_directives_on_same_line() {
2065 html_opts!(
2066 [extension.phoenix_heex],
2067 "<%= foo %> text <%= bar %>\n",
2068 "<%= foo %> text <%= bar %>\n",
2069 );
2070 }
2071
2072 #[test]
2073 fn directive_with_text_after() {
2074 html_opts!(
2075 [extension.phoenix_heex],
2076 "<%= foo %>text after\n",
2077 "<%= foo %>text after\n",
2078 );
2079 }
2080
2081 #[test]
2082 fn back_to_back_directives() {
2083 html_opts!(
2084 [extension.phoenix_heex],
2085 "<% foo %><% bar %><% baz %>\n",
2086 "<% foo %><% bar %><% baz %>\n",
2087 );
2088 }
2089
2090 #[test]
2091 fn directive_incomplete_on_line() {
2092 html_opts!(
2093 [extension.phoenix_heex],
2094 concat!("text <%= foo\n", " bar %> after\n",),
2095 concat!("<p>text <%= foo\n", " bar %> after</p>\n",),
2096 );
2097 }
2098
2099 // ============================================================================
2100 // edge cases - empty and whitespace
2101 // ============================================================================
2102
2103 #[test]
2104 fn empty_component_block() {
2105 html_opts!(
2106 [extension.phoenix_heex],
2107 concat!("<.form>\n", "</.form>\n",),
2108 concat!("<.form>\n", "</.form>\n",),
2109 );
2110 }
2111
2112 #[test]
2113 fn component_with_only_whitespace() {
2114 html_opts!(
2115 [extension.phoenix_heex],
2116 concat!("<.form>\n", " \n", " \n", "</.form>\n",),
2117 concat!("<.form>\n", " \n", " \n", "</.form>\n",),
2118 );
2119 }
2120
2121 #[test]
2122 fn empty_directive() {
2123 html_opts!([extension.phoenix_heex], "<%%>\n", "<%%>\n",);
2124 }
2125
2126 // ============================================================================
2127 // edge cases - whitespace preservation in different contexts
2128 // ============================================================================
2129
2130 #[test]
2131 fn component_in_list() {
2132 html_opts!(
2133 [extension.phoenix_heex],
2134 concat!("- <.button>\n", " text\n", " </.button>\n",),
2135 concat!(
2136 "<ul>\n",
2137 "<li>\n",
2138 "<.button>\n",
2139 " text\n",
2140 "</.button>\n",
2141 "</li>\n",
2142 "</ul>\n",
2143 ),
2144 );
2145 }
2146
2147 // ============================================================================
2148 // edge cases - interaction with other extensions
2149 // ============================================================================
2150
2151 #[test]
2152 fn component_with_table() {
2153 html_opts_i(
2154 concat!(
2155 "<.wrapper>\n",
2156 "\n",
2157 "| a | b |\n",
2158 "|---|---|\n",
2159 "| c | d |\n",
2160 "\n",
2161 "</.wrapper>\n",
2162 ),
2163 concat!(
2164 "<.wrapper>\n",
2165 "\n",
2166 "| a | b |\n",
2167 "|---|---|\n",
2168 "| c | d |\n",
2169 "\n",
2170 "</.wrapper>\n",
2171 ),
2172 true,
2173 |opts| {
2174 opts.extension.phoenix_heex = true;
2175 opts.extension.table = true;
2176 opts.render.r#unsafe = true;
2177 },
2178 );
2179 }
2180
2181 #[test]
2182 fn directive_with_footnote() {
2183 html_opts_i(
2184 concat!("Text <%= @foo %>[^1]\n", "\n", "[^1]: note\n",),
2185 concat!(
2186 "<p>Text <%= @foo %><sup class=\"footnote-ref\"><a href=\"#fn-1\" id=\"fnref-1\" data-footnote-ref>1</a></sup></p>\n",
2187 "<section class=\"footnotes\" data-footnotes>\n",
2188 "<ol>\n",
2189 "<li id=\"fn-1\">\n",
2190 "<p>note <a href=\"#fnref-1\" class=\"footnote-backref\" data-footnote-backref data-footnote-backref-idx=\"1\" aria-label=\"Back to reference 1\">↩</a></p>\n",
2191 "</li>\n",
2192 "</ol>\n",
2193 "</section>\n",
2194 ),
2195 true,
2196 |opts| {
2197 opts.extension.phoenix_heex = true;
2198 opts.extension.footnotes = true;
2199 opts.render.r#unsafe = true;
2200 },
2201 );
2202 }
2203
2204 // ============================================================================
2205 // edge cases - special characters and escaping
2206 // ============================================================================
2207
2208 #[test]
2209 fn directive_with_escaped_percent() {
2210 html_opts!(
2211 [extension.phoenix_heex],
2212 "<%= \"100%% complete\" %>\n",
2213 "<%= \"100%% complete\" %>\n",
2214 );
2215 }
2216
2217 #[test]
2218 fn directive_with_angle_brackets_in_string() {
2219 html_opts!(
2220 [extension.phoenix_heex],
2221 "<%= \"<tag>\" %>\n",
2222 "<%= \"<tag>\" %>\n",
2223 );
2224 }
2225
2226 #[test]
2227 fn component_with_special_chars_in_attributes() {
2228 html_opts!(
2229 [extension.phoenix_heex],
2230 concat!("<.button data-key=\"<>&\\\"\">\n", "</.button>\n",),
2231 concat!("<.button data-key=\"<>&\\\"\">\n", "</.button>\n",),
2232 );
2233 }
2234
2235 // ============================================================================
2236 // edge cases - performance and limits
2237 // ============================================================================
2238
2239 #[test]
2240 fn large_component_content() {
2241 let content = "line of content\n".repeat(1000);
2242 let input = format!("<.wrapper>\n{}</.wrapper>\n", content);
2243 let expected = format!("<.wrapper>\n{}</.wrapper>\n", content);
2244
2245 html_opts!([extension.phoenix_heex], &input, &expected,);
2246 }
2247
2248 // ============================================================================
2249 // block-level expressions
2250 // ============================================================================
2251
2252 #[test]
2253 fn block_level_expression_with_string_concatenation() {
2254 html_opts!(
2255 [extension.phoenix_heex],
2256 concat!("{\n", " \"a\"\n", " <>\n", " \"b\"\n", "}\n",),
2257 concat!("{\n", " \"a\"\n", " <>\n", " \"b\"\n", "}\n",),
2258 );
2259 }