vize_atelier_dom 0.185.0

Atelier DOM - The DOM compiler workshop for Vize
Documentation
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
//! Vue compiler for DOM platform.
//!
//! This module provides DOM-specific compilation including:
//! - DOM element and attribute validation
//! - v-model transforms for form elements
//! - v-on event modifiers
//! - v-show transform
//! - Style and class binding handling

#![allow(clippy::collapsible_match)]
#![cfg_attr(
    test,
    allow(clippy::disallowed_macros, clippy::field_reassign_with_default)
)]

pub mod options;
pub mod transforms;

pub use options::{DomCompilerOptions, element_checks, event_modifiers};
pub use transforms::{
    EventModifiers, EventOptions, MouseModifiers, PropagationModifiers, SystemModifiers, V_SHOW,
    V_TEXT, VModelModifiers, generate_html_prop, generate_html_warning, generate_key_guard,
    generate_model_props, generate_modifier_guard, generate_show_directive, generate_show_style,
    generate_text_children, generate_text_content, get_model_event, get_model_helper,
    get_model_prop, is_v_html, is_v_show, is_v_text, resolve_key_alias,
};

// Re-export core types
pub use vize_atelier_core::{
    Allocator, CompilerError, Namespace, RootNode, TemplateChildNode, ast, codegen, errors, parser,
    runtime_helpers, tokenizer, transform,
};

use vize_atelier_core::codegen::CodegenResult;
use vize_atelier_core::{
    codegen::generate,
    options::{CodegenOptions, ParserOptions, TemplateSyntaxMode, TransformOptions},
    parser::parse_with_options_and_template_syntax,
    transform::{
        transform as do_transform, transform_with_hoisted_scope_id,
        transform_with_template_syntax_quirks,
        transform_with_template_syntax_quirks_and_hoisted_scope_id,
    },
};
use vize_carton::{Bump, String, profile};
use vize_croquis::Croquis;

/// Compile a Vue template for DOM with default options
pub fn compile_template<'a>(
    allocator: &'a Bump,
    source: &'a str,
) -> (RootNode<'a>, Vec<CompilerError>, CodegenResult) {
    compile_template_with_options(allocator, source, DomCompilerOptions::default())
}

/// Compile a Vue template for DOM with custom options
pub fn compile_template_with_options<'a>(
    allocator: &'a Bump,
    source: &'a str,
    options: DomCompilerOptions,
) -> (RootNode<'a>, Vec<CompilerError>, CodegenResult) {
    compile_template_inner(
        allocator,
        source,
        options,
        TemplateSyntaxMode::Standard,
        None,
    )
}

/// Compile a Vue template for DOM with Vue parser quirk compatibility.
#[deprecated(note = "use compile_template_with_template_syntax instead")]
pub fn compile_template_with_vue_parser_quirks<'a>(
    allocator: &'a Bump,
    source: &'a str,
    options: DomCompilerOptions,
) -> (RootNode<'a>, Vec<CompilerError>, CodegenResult) {
    compile_template_inner(allocator, source, options, TemplateSyntaxMode::Quirks, None)
}

/// Compile a Vue template for DOM with an explicit template syntax mode.
#[doc(hidden)]
pub fn compile_template_with_template_syntax<'a>(
    allocator: &'a Bump,
    source: &'a str,
    options: DomCompilerOptions,
    template_syntax: TemplateSyntaxMode,
) -> (RootNode<'a>, Vec<CompilerError>, CodegenResult) {
    compile_template_inner(allocator, source, options, template_syntax, None)
}

/// Compile a Vue template for DOM with an explicit scope ID for hoisted static VNodes.
#[doc(hidden)]
pub fn compile_template_with_options_and_hoisted_scope_id<'a>(
    allocator: &'a Bump,
    source: &'a str,
    options: DomCompilerOptions,
    hoisted_scope_id: Option<String>,
) -> (RootNode<'a>, Vec<CompilerError>, CodegenResult) {
    compile_template_inner(
        allocator,
        source,
        options,
        TemplateSyntaxMode::Standard,
        hoisted_scope_id,
    )
}

/// Compile a Vue template for DOM with Vue parser quirks and an explicit hoisted scope ID.
#[doc(hidden)]
#[deprecated(note = "use compile_template_with_template_syntax_and_hoisted_scope_id instead")]
pub fn compile_template_with_vue_parser_quirks_and_hoisted_scope_id<'a>(
    allocator: &'a Bump,
    source: &'a str,
    options: DomCompilerOptions,
    hoisted_scope_id: Option<String>,
) -> (RootNode<'a>, Vec<CompilerError>, CodegenResult) {
    compile_template_inner(
        allocator,
        source,
        options,
        TemplateSyntaxMode::Quirks,
        hoisted_scope_id,
    )
}

/// Compile a Vue template for DOM with template syntax mode and hoisted scope ID.
#[doc(hidden)]
pub fn compile_template_with_template_syntax_and_hoisted_scope_id<'a>(
    allocator: &'a Bump,
    source: &'a str,
    options: DomCompilerOptions,
    template_syntax: TemplateSyntaxMode,
    hoisted_scope_id: Option<String>,
) -> (RootNode<'a>, Vec<CompilerError>, CodegenResult) {
    compile_template_inner(
        allocator,
        source,
        options,
        template_syntax,
        hoisted_scope_id,
    )
}

fn compile_template_inner<'a>(
    allocator: &'a Bump,
    source: &'a str,
    options: DomCompilerOptions,
    template_syntax: TemplateSyntaxMode,
    hoisted_scope_id: Option<String>,
) -> (RootNode<'a>, Vec<CompilerError>, CodegenResult) {
    // Create parser options with DOM-specific settings
    let parser_opts = ParserOptions {
        is_void_tag: vize_carton::is_void_tag,
        is_native_tag: Some(vize_carton::is_native_tag),
        custom_renderer: options.custom_renderer,
        is_pre_tag: |tag| tag == "pre",
        get_namespace,
        comments: options.comments,
        ..ParserOptions::default()
    };

    // Parse
    let (mut root, errors) = profile!(
        "atelier.dom.template.parse",
        parse_with_options_and_template_syntax(allocator, source, parser_opts, template_syntax)
    );

    // Parser-level diagnostics that are recoverable (e.g. duplicate
    // attribute — Vue keeps the first and continues) must NOT gate
    // codegen, or downstream callers see a 0-byte module reported as a
    // success. (#958) The recoverable diagnostics still ride along in
    // the returned errors vec so the caller can surface them as
    // warnings or test for parity.
    let fatal_count = errors.iter().filter(|e| !e.is_recoverable()).count();
    if fatal_count > 0 {
        let codegen_result = CodegenResult {
            code: String::default(),
            preamble: String::default(),
            map: None,
        };
        return (root, errors.to_vec(), codegen_result);
    }

    // Transform with DOM-specific transforms
    // BindingMetadata is passed directly (no string conversion needed)
    let transform_opts = TransformOptions {
        prefix_identifiers: options.prefix_identifiers,
        hoist_static: options.hoist_static,
        cache_handlers: options.cache_handlers,
        scope_id: options.scope_id.clone(),
        ssr: options.ssr,
        is_ts: options.is_ts,
        inline: options.inline,
        custom_renderer: options.custom_renderer,
        binding_metadata: options.binding_metadata.clone(),
        ..Default::default()
    };
    let template_syntax_quirks = template_syntax.is_quirks();
    // Allocate Croquis in the arena so it shares the allocator lifetime
    let analysis: Option<&Croquis> = options.croquis.map(|c| &*allocator.alloc(*c));
    profile!(
        "atelier.dom.template.transform",
        if template_syntax_quirks {
            if hoisted_scope_id.is_some() {
                transform_with_template_syntax_quirks_and_hoisted_scope_id(
                    allocator,
                    &mut root,
                    transform_opts,
                    analysis,
                    hoisted_scope_id,
                )
            } else {
                transform_with_template_syntax_quirks(
                    allocator,
                    &mut root,
                    transform_opts,
                    analysis,
                )
            }
        } else if hoisted_scope_id.is_some() {
            transform_with_hoisted_scope_id(
                allocator,
                &mut root,
                transform_opts,
                analysis,
                hoisted_scope_id,
            )
        } else {
            do_transform(allocator, &mut root, transform_opts, analysis)
        }
    );

    // Codegen
    let codegen_opts = CodegenOptions {
        mode: options.mode,
        source_map: options.source_map,
        component_name: options.component_name,
        scope_id: options.scope_id.clone(),
        ssr: options.ssr,
        is_ts: options.is_ts,
        inline: options.inline,
        cache_handlers: options.cache_handlers,
        binding_metadata: options.binding_metadata,
        ..Default::default()
    };
    let codegen_result = profile!(
        "atelier.dom.template.codegen",
        generate(&root, codegen_opts)
    );

    (root, errors.to_vec(), codegen_result)
}

/// Get the namespace for an element based on its parent.
///
/// Mirrors the HTML tree-construction namespace rules used by `@vue/compiler-dom`: a tag
/// that names a foreign root (`<svg>`/`<math>`) always (re)enters that namespace, otherwise
/// the element inherits its parent's namespace — except across the HTML integration points
/// where SVG/MathML hand their descendants back to the HTML namespace.
fn get_namespace(tag: &str, parent: Option<&str>) -> Namespace {
    if vize_carton::is_svg_tag(tag) {
        return Namespace::Svg;
    }
    if vize_carton::is_math_ml_tag(tag) {
        return Namespace::MathMl;
    }

    // Inherit namespace from the parent, honouring the integration-point boundaries.
    if let Some(parent_tag) = parent {
        // Inside SVG, <foreignObject>/<desc>/<title> switch their descendants back to HTML
        // (e.g. a <div> inside <foreignObject> must NOT be in the SVG namespace).
        let svg_to_html = matches!(parent_tag, "foreignObject" | "desc" | "title");
        if vize_carton::is_svg_tag(parent_tag) && !svg_to_html {
            return Namespace::Svg;
        }
        // Inside MathML, <annotation-xml> and the text containers (<mi>/<mo>/<mn>/<ms>/
        // <mtext>) are HTML integration points; their descendants are HTML.
        let mathml_to_html = matches!(
            parent_tag,
            "annotation-xml" | "mi" | "mo" | "mn" | "ms" | "mtext"
        );
        if vize_carton::is_math_ml_tag(parent_tag) && !mathml_to_html {
            return Namespace::MathMl;
        }
    }

    Namespace::Html
}

#[cfg(test)]
mod tests {
    use super::{
        DomCompilerOptions, Namespace, TemplateChildNode, compile_template,
        compile_template_with_options, compile_template_with_template_syntax,
    };
    use vize_atelier_core::options::{CodegenMode, TemplateSyntaxMode};
    use vize_carton::Bump;

    fn full_output(preamble: &str, code: &str) -> vize_carton::String {
        let mut full = vize_carton::String::with_capacity(preamble.len() + code.len() + 1);
        full.push_str(preamble);
        full.push('\n');
        full.push_str(code);
        full
    }

    #[test]
    fn test_compile_simple_element() {
        let allocator = Bump::new();
        let (root, errors, result) = compile_template(&allocator, "<div>hello</div>");

        assert!(errors.is_empty());
        assert_eq!(root.children.len(), 1);
        let full = full_output(&result.preamble, &result.code);
        insta::assert_snapshot!(full.as_str());
    }

    #[test]
    fn test_compile_svg() {
        let allocator = Bump::new();
        let (root, errors, _) = compile_template(&allocator, "<svg><circle /></svg>");

        assert!(errors.is_empty());
        if let TemplateChildNode::Element(el) = &root.children[0] {
            assert_eq!(el.ns, Namespace::Svg);
        }
    }

    /// A dynamic component whose `:is` is written as `v-bind:is` must not flush an empty
    /// `{}` segment into `mergeProps`: `:is` is consumed as the component tag, so the first
    /// real merge argument is the `v-bind="obj"` spread — matching @vue/compiler-dom's
    /// `_mergeProps(obj, { ... })` rather than `_mergeProps({}, obj, { ... })`.
    #[test]
    fn test_dynamic_component_vbind_is_no_empty_merge_object() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template(
            &allocator,
            r#"<component :is="popup.component" v-bind="popup.props" :key="popup.id" @closed="onClose"/>"#,
        );
        assert!(errors.is_empty());
        let code = result.code.as_str();
        assert!(
            code.contains("_mergeProps(popup.props, {"),
            "merge should start with the spread, not an empty object:\n{code}"
        );
        assert!(
            !code.contains("_mergeProps({ }") && !code.contains("_mergeProps({  }"),
            "no empty object literal should be flushed into mergeProps:\n{code}"
        );
    }

    #[test]
    fn test_dynamic_component_v_if_does_not_emit_is_prop() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template(
            &allocator,
            r#"<Component :is="current" v-if="ok" :foo="foo" />"#,
        );
        assert!(errors.is_empty());
        let code = result.code.as_str();
        assert!(
            code.contains("_resolveDynamicComponent(current)"),
            "dynamic component should be resolved from the :is binding:\n{code}"
        );
        assert!(
            !code.contains("is:"),
            "v-if dynamic component branch must not pass consumed :is as a prop:\n{code}"
        );
        assert!(
            !code.contains(r#""is""#),
            "v-if dynamic component branch must not track consumed :is as a dynamic prop:\n{code}"
        );
    }

    #[test]
    fn test_template_ref_in_v_for_emits_ref_for() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template(
            &allocator,
            r#"<span v-for="item in items" ref="itemEls"></span>"#,
        );
        assert!(errors.is_empty());
        let code = result.code.as_str();
        assert!(
            code.contains("ref_for: true"),
            "template refs inside v-for must be marked as ref_for so Vue stores an array:\n{code}"
        );
    }

    #[test]
    fn test_static_ref_matching_prop_name_stays_string_ref() {
        use vize_atelier_core::options::{BindingMetadata, BindingType};
        use vize_carton::FxHashMap;

        let allocator = Bump::new();
        let mut bindings = FxHashMap::default();
        bindings.insert("buttons".into(), BindingType::Props);

        let options = DomCompilerOptions {
            mode: CodegenMode::Module,
            prefix_identifiers: true,
            inline: true,
            binding_metadata: Some(BindingMetadata {
                bindings,
                props_aliases: FxHashMap::default(),
                is_script_setup: true,
            }),
            ..Default::default()
        };

        let (_, errors, result) = compile_template_with_options(
            &allocator,
            r#"<button v-for="button in buttons" ref="buttons" :key="button"></button>"#,
            options,
        );

        assert!(errors.is_empty(), "Errors: {:?}", errors);
        let code = result.code.as_str();
        assert!(code.contains("ref_for: true"), "{code}");
        assert!(code.contains(r#"ref: "buttons""#), "{code}");
        assert!(
            !code.contains("ref: buttons"),
            "props bindings must not be emitted as runtime ref identifiers:\n{code}"
        );
    }

    /// Recursively find the first element with the given tag, descending through `v-if`
    /// branches and `v-for` bodies so the search works on the transformed tree as well.
    fn find_element<'a, 'b>(
        children: &'b [TemplateChildNode<'a>],
        tag: &str,
    ) -> Option<&'b super::ast::ElementNode<'a>> {
        for child in children {
            match child {
                TemplateChildNode::Element(el) => {
                    if el.tag.as_str() == tag {
                        return Some(el);
                    }
                    if let Some(found) = find_element(&el.children, tag) {
                        return Some(found);
                    }
                }
                TemplateChildNode::If(node) => {
                    for branch in &node.branches {
                        if let Some(found) = find_element(&branch.children, tag) {
                            return Some(found);
                        }
                    }
                }
                TemplateChildNode::IfBranch(branch) => {
                    if let Some(found) = find_element(&branch.children, tag) {
                        return Some(found);
                    }
                }
                TemplateChildNode::For(node) => {
                    if let Some(found) = find_element(&node.children, tag) {
                        return Some(found);
                    }
                }
                _ => {}
            }
        }
        None
    }

    /// #992: the SVG namespace must propagate from `<svg>` into every descendant so the
    /// runtime mounts them with `setAttributeNS`/the SVG namespace URI. This locks the
    /// parser-side propagation that codegen relies on (vize, like @vue/compiler-sfc, emits
    /// no namespace argument and depends on the runtime inferring it from a contiguous tree).
    #[test]
    fn test_svg_namespace_propagates_to_descendants() {
        let allocator = Bump::new();
        let (root, errors, _) = compile_template(
            &allocator,
            "<svg><g><path d=\"M0 0\"/></g><rect x=\"0\" y=\"0\"/></svg>",
        );
        assert!(errors.is_empty());

        let svg = find_element(&root.children, "svg").expect("svg present");
        assert_eq!(svg.ns, Namespace::Svg);
        assert_eq!(
            find_element(&root.children, "g").unwrap().ns,
            Namespace::Svg,
            "direct child <g> inherits svg namespace"
        );
        assert_eq!(
            find_element(&root.children, "path").unwrap().ns,
            Namespace::Svg,
            "nested <path> keeps svg namespace"
        );
        assert_eq!(
            find_element(&root.children, "rect").unwrap().ns,
            Namespace::Svg,
            "sibling <rect> inherits svg namespace"
        );
    }

    /// #992: `<foreignObject>` is the one boundary where the SVG namespace must NOT
    /// propagate further — its HTML descendants go back to the HTML namespace, while a
    /// `<rect>` sibling after the `<foreignObject>` stays in the SVG namespace.
    #[test]
    fn test_svg_foreign_object_resets_namespace() {
        let allocator = Bump::new();
        let (root, errors, _) = compile_template(
            &allocator,
            "<svg><foreignObject><div>hi</div></foreignObject><rect x=\"1\" y=\"1\"/></svg>",
        );
        assert!(errors.is_empty());

        assert_eq!(
            find_element(&root.children, "foreignObject").unwrap().ns,
            Namespace::Svg,
            "<foreignObject> itself is in the svg namespace"
        );
        assert_eq!(
            find_element(&root.children, "div").unwrap().ns,
            Namespace::Html,
            "<div> inside <foreignObject> returns to the HTML namespace"
        );
        assert_eq!(
            find_element(&root.children, "rect").unwrap().ns,
            Namespace::Svg,
            "<rect> after <foreignObject> is still in the svg namespace"
        );
    }

    /// #992: namespace propagation must survive the codegen shapes that could otherwise
    /// detach a child from its `<svg>` ancestor in the vnode tree — a `v-if` branch
    /// (re-entered via `createElementBlock`) keeps the `<rect>` nested inside the `<svg>`
    /// element call, so the runtime still threads the svg namespace into it.
    #[test]
    fn test_svg_namespace_with_v_if_branch() {
        let allocator = Bump::new();
        let (root, errors, _) = compile_template(
            &allocator,
            "<svg><rect v-if=\"show\" x=\"0\" y=\"0\"/></svg>",
        );
        assert!(errors.is_empty());
        assert_eq!(
            find_element(&root.children, "rect").unwrap().ns,
            Namespace::Svg,
            "v-if <rect> still carries the svg namespace"
        );
    }

    /// #992: lock the emitted shape for an SVG tree with a `<foreignObject>` exit. The SVG
    /// children must stay nested inside the `<svg>` `createElementVNode`/`createElementBlock`
    /// call so the runtime threads the SVG namespace into them at patch time (vize, like
    /// @vue/compiler-sfc, emits no explicit namespace argument).
    #[test]
    fn test_svg_codegen_shape_keeps_children_nested() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template(
            &allocator,
            "<svg><foreignObject><div>hi</div></foreignObject><rect x=\"1\" y=\"1\"/></svg>",
        );
        assert!(errors.is_empty());
        let full = full_output(&result.preamble, &result.code);
        insta::assert_snapshot!(full.as_str());
    }

    #[test]
    fn test_inline_svg_dynamic_subtree_uses_own_block() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template(
            &allocator,
            "<div><svg :width=\"w\"><g v-if=\"ok\"><rect :x=\"x\"/></g></svg></div>",
        );
        assert!(errors.is_empty());

        let code = result.code.as_str();
        assert!(
            code.contains(r#"_createElementBlock("svg""#),
            "inline <svg> must be a block so dynamic descendants patch with SVG namespace:\n{code}"
        );
        assert!(
            code.contains(r#"_createElementBlock("g""#),
            "dynamic SVG branch should keep its own block under the SVG namespace:\n{code}"
        );
    }

    #[test]
    fn test_inline_svg_descendants_inside_same_namespace_stay_vnodes() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template(
            &allocator,
            r#"<div><svg><defs><pattern :x="0"><line :x1="w"/></pattern></defs></svg></div>"#,
        );
        assert!(errors.is_empty());

        let code = result.code.as_str();
        assert!(
            code.contains(r#"_createElementBlock("svg""#),
            "inline <svg> must still enter the SVG namespace with a block:\n{code}"
        );
        for tag in ["defs", "pattern", "line"] {
            assert!(
                code.contains(&format!(r#"_createElementVNode("{tag}""#)),
                "SVG descendants inside the same namespace should be VNodes:\n{code}"
            );
            assert!(
                !code.contains(&format!(r#"_createElementBlock("{tag}""#)),
                "SVG descendants inside the same namespace should not be blocks:\n{code}"
            );
        }
    }

    #[test]
    fn test_svg_foreign_object_namespace_exit_uses_boundary_block() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template(
            &allocator,
            r#"<svg><foreignObject><div :id="id">hi</div></foreignObject></svg>"#,
        );
        assert!(errors.is_empty());

        let code = result.code.as_str();
        assert!(
            code.contains(r#"_createElementBlock("foreignObject""#),
            "<foreignObject> must keep its own block when descendants leave SVG namespace:\n{code}"
        );
        assert!(
            code.contains(r#"_createElementVNode("div""#),
            "HTML descendants after the namespace exit should remain VNodes:\n{code}"
        );
    }

    #[test]
    fn test_nested_svg_with_v_bind_uses_own_block() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template(
            &allocator,
            r#"<div><svg xmlns="http://www.w3.org/2000/svg" :width="w" /></div>"#,
        );
        assert!(errors.is_empty());

        let code = result.code.as_str();
        assert!(
            code.contains(r#"_createElementBlock("svg""#),
            "nested SVG elements with dynamic props must render as blocks:\n{code}"
        );
        assert!(
            !code.contains(r#"_createElementVNode("svg""#),
            "nested SVG elements with dynamic props must not render as plain VNodes:\n{code}"
        );
    }

    #[test]
    fn test_svg_constant_bound_children_are_cached_vnodes() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template(
            &allocator,
            r#"<svg xmlns="http://www.w3.org/2000/svg"><rect :x="1" /><rect x="1" /></svg>"#,
        );
        assert!(errors.is_empty());

        let code = result.code.as_str();
        assert!(
            code.contains("[...(_cache[0]"),
            "static SVG children should be cached together:\n{}\n{code}",
            result.preamble
        );
        assert!(
            code.contains(r#"_createElementVNode("rect", { x: 1 }, null, -1 /* CACHED */)"#),
            "constant v-bind SVG child should compile as a cached VNode:\n{}\n{code}",
            result.preamble
        );
        assert!(
            !code.contains(r#"_createElementBlock("rect""#),
            "constant SVG children must not become block roots:\n{}\n{code}",
            result.preamble
        );
    }

    #[test]
    fn test_compile_with_options() {
        let allocator = Bump::new();
        let opts = DomCompilerOptions {
            mode: CodegenMode::Module,
            ..Default::default()
        };
        let (_, errors, result) = compile_template_with_options(&allocator, "<div></div>", opts);

        assert!(errors.is_empty());
        // Empty div generates minimal code
        assert!(!result.code.is_empty());
    }

    #[test]
    fn test_compile_v_for_template_syntax_quirks_accepts_unmatched_alias_paren() {
        let allocator = Bump::new();
        let opts = DomCompilerOptions::default();
        let (_, errors, result) = compile_template_with_template_syntax(
            &allocator,
            r#"<div v-for="item) in items">{{ item }}</div>"#,
            opts,
            TemplateSyntaxMode::Quirks,
        );

        assert!(errors.is_empty(), "Errors: {:?}", errors);
        assert!(result.code.contains("_renderList(items, (item) =>"));
    }

    #[test]
    fn test_compile_template_syntax_quirks_accepts_invalid_html_self_closing() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template_with_template_syntax(
            &allocator,
            "<div /><span></span>",
            DomCompilerOptions::default(),
            TemplateSyntaxMode::Quirks,
        );

        assert!(errors.is_empty(), "Errors: {:?}", errors);
        assert!(!result.code.is_empty());
        assert!(result.code.contains(r#"_createElementVNode("div""#));
        assert!(result.code.contains(r#"_createElementVNode("span""#));
    }

    #[test]
    fn test_compile_standard_warns_and_rewrites_invalid_html_self_closing() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template_with_options(
            &allocator,
            "<div /><span></span>",
            DomCompilerOptions::default(),
        );

        assert!(errors.iter().any(|error| error.is_recoverable()));
        assert!(!result.code.is_empty());
        assert!(result.code.contains(r#"_createElementVNode("div""#));
        assert!(result.code.contains(r#"_createElementVNode("span""#));
    }

    #[test]
    fn test_compile_strict_rejects_invalid_html_self_closing() {
        let allocator = Bump::new();
        let (_, errors, result) = compile_template_with_template_syntax(
            &allocator,
            "<div /><span></span>",
            DomCompilerOptions::default(),
            TemplateSyntaxMode::Strict,
        );

        assert!(errors.iter().any(|error| !error.is_recoverable()));
        assert!(result.code.is_empty());
    }

    #[test]
    fn test_event_handler_setup_ref_value() {
        use vize_atelier_core::options::BindingType;
        use vize_carton::FxHashMap;

        let allocator = Bump::new();
        let mut bindings_map = FxHashMap::default();
        bindings_map.insert("quoteId".into(), BindingType::SetupRef);
        bindings_map.insert("renoteTargetNote".into(), BindingType::SetupRef);
        let binding_metadata = vize_atelier_core::options::BindingMetadata {
            bindings: bindings_map,
            props_aliases: FxHashMap::default(),
            is_script_setup: true,
        };

        let opts = DomCompilerOptions {
            mode: CodegenMode::Module,
            prefix_identifiers: true,
            inline: true,
            cache_handlers: true,
            binding_metadata: Some(binding_metadata),
            ..Default::default()
        };
        let template = r#"<button @click="quoteId = null; renoteTargetNote = null;">x</button>"#;
        let (_, errors, result) = compile_template_with_options(&allocator, template, opts);

        eprintln!(
            "=== Template Output ===\npreamble:\n{}\ncode:\n{}",
            result.preamble, result.code
        );
        assert!(errors.is_empty(), "Errors: {:?}", errors);
        let full = full_output(&result.preamble, &result.code);
        insta::assert_snapshot!(full.as_str());
    }

    #[test]
    fn test_inline_ref_class_binding_keeps_class_patch_flag() {
        use vize_atelier_core::options::{BindingMetadata, BindingType};
        use vize_carton::FxHashMap;

        let allocator = Bump::new();
        let mut bindings = FxHashMap::default();
        bindings.insert("currentTab".into(), BindingType::SetupRef);

        let options = DomCompilerOptions {
            mode: CodegenMode::Module,
            prefix_identifiers: true,
            inline: true,
            cache_handlers: true,
            binding_metadata: Some(BindingMetadata {
                bindings,
                props_aliases: FxHashMap::default(),
                is_script_setup: true,
            }),
            ..Default::default()
        };

        let (_, errors, result) = compile_template_with_options(
            &allocator,
            r#"<button :class="['tab', { active: currentTab === 'a' }]" @click="currentTab = 'b'">A</button>"#,
            options,
        );

        assert!(errors.is_empty(), "Errors: {:?}", errors);
        let full = full_output(&result.preamble, &result.code);
        insta::assert_snapshot!(full.as_str());
    }

    #[test]
    fn test_ref_scroll_keeps_need_patch_with_need_hydration() {
        use vize_atelier_core::options::{BindingMetadata, BindingType};
        use vize_carton::FxHashMap;

        let allocator = Bump::new();
        let mut bindings = FxHashMap::default();
        bindings.insert("onScroll".into(), BindingType::SetupConst);

        let options = DomCompilerOptions {
            mode: CodegenMode::Module,
            prefix_identifiers: true,
            inline: true,
            cache_handlers: true,
            binding_metadata: Some(BindingMetadata {
                bindings,
                props_aliases: FxHashMap::default(),
                is_script_setup: true,
            }),
            ..Default::default()
        };

        let (_, errors, result) = compile_template_with_options(
            &allocator,
            r#"<div ref="container" @scroll="onScroll"></div>"#,
            options,
        );

        assert!(errors.is_empty(), "Errors: {:?}", errors);
        let full = full_output(&result.preamble, &result.code);
        assert!(
            full.contains("544 /* NEED_HYDRATION, NEED_PATCH */"),
            "{full}"
        );
    }

    #[test]
    fn test_ref_text_keeps_need_patch_with_text_flag() {
        use vize_atelier_core::options::{BindingMetadata, BindingType};
        use vize_carton::FxHashMap;

        let allocator = Bump::new();
        let mut bindings = FxHashMap::default();
        bindings.insert("message".into(), BindingType::SetupRef);

        let options = DomCompilerOptions {
            mode: CodegenMode::Module,
            prefix_identifiers: true,
            inline: true,
            cache_handlers: true,
            binding_metadata: Some(BindingMetadata {
                bindings,
                props_aliases: FxHashMap::default(),
                is_script_setup: true,
            }),
            ..Default::default()
        };

        let (_, errors, result) = compile_template_with_options(
            &allocator,
            r#"<div ref="container">{{ message }}</div>"#,
            options,
        );

        assert!(errors.is_empty(), "Errors: {:?}", errors);
        let full = full_output(&result.preamble, &result.code);
        assert!(full.contains("513 /* TEXT, NEED_PATCH */"), "{full}");
    }

    #[test]
    fn test_inline_hoisted_bare_static_attrs_are_empty_strings() {
        let allocator = Bump::new();
        let options = DomCompilerOptions {
            mode: CodegenMode::Module,
            prefix_identifiers: true,
            inline: true,
            ..Default::default()
        };

        let (_, errors, result) = compile_template_with_options(
            &allocator,
            r#"<section><h2 sr-only font-bold flex="~ gap-1"><span block></span></h2></section>"#,
            options,
        );

        assert!(errors.is_empty(), "Errors: {:?}", errors);
        let full = full_output(&result.preamble, &result.code);
        assert!(full.contains(r#""sr-only": """#), "{full}");
        assert!(full.contains(r#""font-bold": """#), "{full}");
        assert!(full.contains(r#"block: """#), "{full}");
        assert!(!full.contains(r#""sr-only": "true""#), "{full}");
        assert!(!full.contains(r#""font-bold": "true""#), "{full}");
        assert!(!full.contains(r#"block: "true""#), "{full}");
    }

    #[test]
    fn test_inline_component_dynamic_prop_keeps_props_patch_flag() {
        use vize_atelier_core::options::{BindingMetadata, BindingType};
        use vize_carton::FxHashMap;

        let allocator = Bump::new();
        let mut bindings = FxHashMap::default();
        bindings.insert("message".into(), BindingType::SetupRef);
        bindings.insert("activeClass".into(), BindingType::SetupRef);

        let options = DomCompilerOptions {
            mode: CodegenMode::Module,
            prefix_identifiers: true,
            inline: true,
            cache_handlers: true,
            binding_metadata: Some(BindingMetadata {
                bindings,
                props_aliases: FxHashMap::default(),
                is_script_setup: true,
            }),
            ..Default::default()
        };

        let (_, errors, result) = compile_template_with_options(
            &allocator,
            r#"<div><MyComponent :msg="message" :class="activeClass" :full="true" /></div>"#,
            options,
        );

        assert!(errors.is_empty(), "Errors: {:?}", errors);
        let full = full_output(&result.preamble, &result.code);
        insta::assert_snapshot!(full.as_str());
    }

    #[test]
    fn test_v_if_branch_component_dynamic_prop_keeps_props_patch_flag() {
        use vize_atelier_core::options::{BindingMetadata, BindingType};
        use vize_carton::FxHashMap;

        let allocator = Bump::new();
        let mut bindings = FxHashMap::default();
        bindings.insert("show".into(), BindingType::SetupRef);
        bindings.insert("message".into(), BindingType::SetupRef);

        let options = DomCompilerOptions {
            mode: CodegenMode::Module,
            prefix_identifiers: true,
            inline: true,
            cache_handlers: true,
            binding_metadata: Some(BindingMetadata {
                bindings,
                props_aliases: FxHashMap::default(),
                is_script_setup: true,
            }),
            ..Default::default()
        };

        let (_, errors, result) = compile_template_with_options(
            &allocator,
            r#"<div><MyComponent v-if="show" :msg="message" /></div>"#,
            options,
        );

        assert!(errors.is_empty(), "Errors: {:?}", errors);
        let full = full_output(&result.preamble, &result.code);
        insta::assert_snapshot!(full.as_str());
    }
}