fd-core 0.1.18

FD (Fast Draft) — core data model, parser, emitter, and layout solver
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
//! HTML+CSS export: SceneGraph → standalone HTML page.
//!
//! Maps FD nodes to HTML elements:
//! - Rect/Frame → `<div>` with absolute positioning
//! - Ellipse → `<div>` with `border-radius: 50%`
//! - Text → `<p>` with font styles
//! - Path → inline `<svg>` element
//! - Group → wrapper `<div>` containing children
//!
//! Animations (hover/press triggers) become CSS transitions.

use crate::id::NodeId;
use crate::model::*;
use petgraph::graph::NodeIndex;
use std::collections::HashMap;
use std::fmt::Write;

/// Convert a SceneGraph (or a subset of selected nodes) to a standalone HTML page.
///
/// If `selected_ids` is empty, all top-level nodes are exported.
/// Returns a complete HTML document string.
pub fn export_html(
    graph: &SceneGraph,
    bounds: &HashMap<NodeIndex, ResolvedBounds>,
    selected_ids: &[String],
) -> String {
    let root_selection = collect_root_selection(graph, selected_ids);

    let mut elements = Vec::new();
    let mut css_rules = Vec::new();
    let mut id_counter: u64 = 1;

    for &idx in &root_selection {
        collect_html_elements(
            graph,
            idx,
            bounds,
            &mut elements,
            &mut css_rules,
            &mut id_counter,
        );
    }

    build_html_document(&elements, &css_rules)
}

/// An intermediate HTML element representation.
struct HtmlElement {
    tag: &'static str,
    css_class: String,
    inline_style: String,
    content: String,
    children: Vec<HtmlElement>,
    /// SVG content for path elements.
    svg_content: Option<String>,
}

/// Collect root-level node indices to export, filtering by selection.
fn collect_root_selection(graph: &SceneGraph, selected_ids: &[String]) -> Vec<NodeIndex> {
    if selected_ids.is_empty() {
        return graph.children(graph.root);
    }

    let mut roots = Vec::new();
    for id_str in selected_ids {
        let id = NodeId::intern(id_str);
        if let Some(idx) = graph.index_of(id) {
            let has_selected_ancestor = selected_ids
                .iter()
                .any(|other| other != id_str && graph.is_ancestor_of(NodeId::intern(other), id));
            if !has_selected_ancestor {
                roots.push(idx);
            }
        }
    }
    roots
}

/// Recursively collect HTML elements from the scene graph.
fn collect_html_elements(
    graph: &SceneGraph,
    idx: NodeIndex,
    bounds: &HashMap<NodeIndex, ResolvedBounds>,
    elements: &mut Vec<HtmlElement>,
    css_rules: &mut Vec<String>,
    id_counter: &mut u64,
) {
    let node = &graph.graph[idx];
    let b = match bounds.get(&idx) {
        Some(b) => b,
        None => return,
    };

    let style = graph.resolve_style(node, &[]);
    let class_name = format!("fd-{}", {
        let v = *id_counter;
        *id_counter += 1;
        v
    });

    let mut inline_styles = Vec::new();

    // Position and size
    inline_styles.push(format!("left: {}px", format_num(b.x)));
    inline_styles.push(format!("top: {}px", format_num(b.y)));
    inline_styles.push(format!("width: {}px", format_num(b.width)));
    inline_styles.push(format!("height: {}px", format_num(b.height)));
    inline_styles.push("position: absolute".to_string());

    // Fill
    if let Some(ref fill) = style.fill {
        inline_styles.push(format!("background: {}", paint_to_css(fill)));
    }

    // Stroke
    if let Some(ref stroke) = style.stroke {
        inline_styles.push(format!(
            "border: {}px solid {}",
            format_num(stroke.width),
            paint_to_css(&stroke.paint)
        ));
        inline_styles.push("box-sizing: border-box".to_string());
    }

    // Corner radius
    if let Some(r) = style.corner_radius
        && r > 0.0
    {
        inline_styles.push(format!("border-radius: {}px", format_num(r)));
    }

    // Opacity
    if let Some(op) = style.opacity
        && (op - 1.0).abs() > 0.001
    {
        inline_styles.push(format!("opacity: {}", format_num(op)));
    }

    // Shadow
    if let Some(ref shadow) = style.shadow {
        let color = color_to_css(&shadow.color);
        inline_styles.push(format!(
            "box-shadow: {}px {}px {}px {}",
            format_num(shadow.offset_x),
            format_num(shadow.offset_y),
            format_num(shadow.blur),
            color,
        ));
    }

    // Build animations as CSS transitions
    let has_animations = !node.animations.is_empty();
    if has_animations {
        inline_styles.push("transition: all 0.3s ease".to_string());
        build_animation_css(&class_name, &node.animations, &style, css_rules);
    }

    let mut element = match &node.kind {
        NodeKind::Rect { .. } | NodeKind::Frame { .. } | NodeKind::Image { .. } => {
            if matches!(&node.kind, NodeKind::Frame { clip: true, .. }) {
                inline_styles.push("overflow: hidden".to_string());
            }
            HtmlElement {
                tag: "div",
                css_class: class_name.clone(),
                inline_style: inline_styles.join("; "),
                content: String::new(),
                children: Vec::new(),
                svg_content: None,
            }
        }
        NodeKind::Ellipse { .. } => {
            inline_styles.push("border-radius: 50%".to_string());
            HtmlElement {
                tag: "div",
                css_class: class_name.clone(),
                inline_style: inline_styles.join("; "),
                content: String::new(),
                children: Vec::new(),
                svg_content: None,
            }
        }
        NodeKind::Text { content, .. } => {
            // Text styling
            if let Some(ref font) = style.font {
                inline_styles.push(format!("font-family: '{}', sans-serif", font.family));
                inline_styles.push(format!("font-size: {}px", format_num(font.size)));
                if font.weight != 400 {
                    inline_styles.push(format!("font-weight: {}", font.weight));
                }
            }

            // Text alignment
            let text_align = style.text_align.unwrap_or(TextAlign::Center);
            inline_styles.push(format!(
                "text-align: {}",
                match text_align {
                    TextAlign::Left => "left",
                    TextAlign::Center => "center",
                    TextAlign::Right => "right",
                }
            ));

            // Vertical alignment via flexbox
            let valign = style.text_valign.unwrap_or(TextVAlign::Middle);
            inline_styles.push("display: flex".to_string());
            inline_styles.push(format!(
                "align-items: {}",
                match valign {
                    TextVAlign::Top => "flex-start",
                    TextVAlign::Middle => "center",
                    TextVAlign::Bottom => "flex-end",
                }
            ));
            inline_styles.push(format!(
                "justify-content: {}",
                match text_align {
                    TextAlign::Left => "flex-start",
                    TextAlign::Center => "center",
                    TextAlign::Right => "flex-end",
                }
            ));

            // Text color from fill
            if let Some(ref fill) = style.fill {
                inline_styles.push(format!("color: {}", paint_to_css(fill)));
            }

            // Remove background for text (fill is used for text color)
            inline_styles.retain(|s| !s.starts_with("background:"));

            inline_styles.push("margin: 0".to_string());
            inline_styles.push("white-space: pre-wrap".to_string());
            inline_styles.push("word-break: break-word".to_string());

            // Convert FD newlines (backslash) to HTML line breaks
            let html_content = content.replace('\\', "<br>");

            HtmlElement {
                tag: "p",
                css_class: class_name.clone(),
                inline_style: inline_styles.join("; "),
                content: html_content,
                children: Vec::new(),
                svg_content: None,
            }
        }
        NodeKind::Path { commands } => {
            let svg = path_to_svg(commands, b);
            HtmlElement {
                tag: "div",
                css_class: class_name.clone(),
                inline_style: inline_styles.join("; "),
                content: String::new(),
                children: Vec::new(),
                svg_content: Some(svg),
            }
        }
        NodeKind::Group | NodeKind::Root | NodeKind::Generic => HtmlElement {
            tag: "div",
            css_class: class_name.clone(),
            inline_style: inline_styles.join("; "),
            content: String::new(),
            children: Vec::new(),
            svg_content: None,
        },
    };

    // Recurse into children
    for child_idx in graph.children(idx) {
        let mut child_elements = Vec::new();
        collect_html_elements(
            graph,
            child_idx,
            bounds,
            &mut child_elements,
            css_rules,
            id_counter,
        );
        element.children.extend(child_elements);
    }

    elements.push(element);
}

/// Build CSS animation rules for hover/press triggers.
fn build_animation_css(
    class_name: &str,
    animations: &[AnimKeyframe],
    _base_style: &Properties,
    css_rules: &mut Vec<String>,
) {
    for anim in animations {
        let pseudo_class = match anim.trigger {
            AnimTrigger::Hover => ":hover",
            AnimTrigger::Press => ":active",
            _ => continue, // Enter/Custom not supported as CSS yet
        };

        let duration_s = anim.duration_ms as f64 / 1000.0;
        let easing = match &anim.easing {
            Easing::Linear => "linear".to_string(),
            Easing::EaseIn => "ease-in".to_string(),
            Easing::EaseOut => "ease-out".to_string(),
            Easing::EaseInOut => "ease-in-out".to_string(),
            Easing::Spring => "cubic-bezier(0.175, 0.885, 0.32, 1.275)".to_string(),
            Easing::CubicBezier(a, b, c, d) => {
                format!("cubic-bezier({}, {}, {}, {})", a, b, c, d)
            }
        };

        let mut props = Vec::new();

        if let Some(ref fill) = anim.properties.fill {
            props.push(format!("background: {}", paint_to_css(fill)));
        }
        if let Some(op) = anim.properties.opacity {
            props.push(format!("opacity: {}", format_num(op)));
        }
        if let Some(scale) = anim.properties.scale {
            let mut transforms = vec![format!("scale({})", format_num(scale))];
            if let Some((tx, ty)) = anim.properties.translate {
                transforms.push(format!(
                    "translate({}px, {}px)",
                    format_num(tx),
                    format_num(ty)
                ));
            }
            if let Some(rot) = anim.properties.rotate {
                transforms.push(format!("rotate({}deg)", format_num(rot)));
            }
            props.push(format!("transform: {}", transforms.join(" ")));
        } else {
            let mut transforms = Vec::new();
            if let Some((tx, ty)) = anim.properties.translate {
                transforms.push(format!(
                    "translate({}px, {}px)",
                    format_num(tx),
                    format_num(ty)
                ));
            }
            if let Some(rot) = anim.properties.rotate {
                transforms.push(format!("rotate({}deg)", format_num(rot)));
            }
            if !transforms.is_empty() {
                props.push(format!("transform: {}", transforms.join(" ")));
            }
        }

        if !props.is_empty() {
            let mut rule = String::new();
            let _ = writeln!(rule, ".{}{} {{", class_name, pseudo_class);
            let _ = writeln!(
                rule,
                "  transition: all {}s {};",
                format_num_f64(duration_s),
                easing
            );
            for prop in &props {
                let _ = writeln!(rule, "  {};", prop);
            }
            rule.push('}');
            css_rules.push(rule);
        }
    }
}

/// Convert a Paint to a CSS color/gradient value.
fn paint_to_css(paint: &Paint) -> String {
    match paint {
        Paint::Solid(c) => color_to_css(c),
        Paint::LinearGradient { angle, stops } => {
            let mut parts = vec![format!("{}deg", angle)];
            for stop in stops {
                parts.push(format!(
                    "{} {}%",
                    color_to_css(&stop.color),
                    (stop.offset * 100.0) as u32
                ));
            }
            format!("linear-gradient({})", parts.join(", "))
        }
        Paint::RadialGradient { stops } => {
            let parts: Vec<String> = stops
                .iter()
                .map(|s| format!("{} {}%", color_to_css(&s.color), (s.offset * 100.0) as u32))
                .collect();
            format!("radial-gradient(circle, {})", parts.join(", "))
        }
    }
}

/// Convert a Color to a CSS rgba/hex string.
fn color_to_css(c: &Color) -> String {
    if (c.a - 1.0).abs() < 0.001 {
        c.to_hex()
    } else {
        format!(
            "rgba({}, {}, {}, {})",
            (c.r * 255.0).round() as u8,
            (c.g * 255.0).round() as u8,
            (c.b * 255.0).round() as u8,
            format_num(c.a),
        )
    }
}

/// Convert path commands to an inline SVG element.
fn path_to_svg(commands: &[PathCmd], b: &ResolvedBounds) -> String {
    let mut d = String::new();
    for cmd in commands {
        match cmd {
            PathCmd::MoveTo(x, y) => {
                let _ = write!(d, "M {} {} ", format_num(*x - b.x), format_num(*y - b.y));
            }
            PathCmd::LineTo(x, y) => {
                let _ = write!(d, "L {} {} ", format_num(*x - b.x), format_num(*y - b.y));
            }
            PathCmd::QuadTo(cx, cy, x, y) => {
                let _ = write!(
                    d,
                    "Q {} {} {} {} ",
                    format_num(*cx - b.x),
                    format_num(*cy - b.y),
                    format_num(*x - b.x),
                    format_num(*y - b.y)
                );
            }
            PathCmd::CubicTo(c1x, c1y, c2x, c2y, x, y) => {
                let _ = write!(
                    d,
                    "C {} {} {} {} {} {} ",
                    format_num(*c1x - b.x),
                    format_num(*c1y - b.y),
                    format_num(*c2x - b.x),
                    format_num(*c2y - b.y),
                    format_num(*x - b.x),
                    format_num(*y - b.y)
                );
            }
            PathCmd::Close => {
                d.push_str("Z ");
            }
        }
    }

    format!(
        r#"<svg width="{}" height="{}" viewBox="0 0 {} {}" xmlns="http://www.w3.org/2000/svg"><path d="{}" fill="none" stroke="currentColor" stroke-width="2"/></svg>"#,
        format_num(b.width),
        format_num(b.height),
        format_num(b.width),
        format_num(b.height),
        d.trim(),
    )
}

/// Build the complete HTML document from elements and CSS rules.
fn build_html_document(elements: &[HtmlElement], css_rules: &[String]) -> String {
    let mut html = String::with_capacity(4096);

    html.push_str("<!DOCTYPE html>\n");
    html.push_str("<html lang=\"en\">\n<head>\n");
    html.push_str("  <meta charset=\"UTF-8\">\n");
    html.push_str("  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
    html.push_str("  <title>Fast Draft Export</title>\n");
    html.push_str("  <link rel=\"preconnect\" href=\"https://fonts.googleapis.com\">\n");
    html.push_str("  <link rel=\"preconnect\" href=\"https://fonts.gstatic.com\" crossorigin>\n");

    // Collect unique font families
    let mut fonts = Vec::new();
    collect_fonts_from_elements(elements, &mut fonts);
    fonts.sort();
    fonts.dedup();

    if !fonts.is_empty() {
        let font_params: Vec<String> = fonts
            .iter()
            .map(|f| format!("family={}", f.replace(' ', "+")))
            .collect();
        let _ = writeln!(
            html,
            "  <link href=\"https://fonts.googleapis.com/css2?{}&display=swap\" rel=\"stylesheet\">",
            font_params.join("&")
        );
    }

    html.push_str("  <style>\n");
    html.push_str("    * { margin: 0; padding: 0; box-sizing: border-box; }\n");
    html.push_str(
        "    body { min-height: 100vh; position: relative; font-family: 'Inter', sans-serif; }\n",
    );
    html.push_str("    .fd-canvas { position: relative; width: 100%; min-height: 100vh; }\n");

    // Write animation CSS rules
    for rule in css_rules {
        let _ = writeln!(html, "    {}", rule);
    }

    html.push_str("  </style>\n");
    html.push_str("</head>\n<body>\n");
    html.push_str("  <div class=\"fd-canvas\">\n");

    for element in elements {
        write_html_element(&mut html, element, 2);
    }

    html.push_str("  </div>\n");
    html.push_str("</body>\n</html>\n");
    html
}

/// Collect unique font families from elements recursively.
fn collect_fonts_from_elements(elements: &[HtmlElement], fonts: &mut Vec<String>) {
    for el in elements {
        // Extract font-family from inline style
        if let Some(start) = el.inline_style.find("font-family: '") {
            let rest = &el.inline_style[start + 14..];
            if let Some(end) = rest.find('\'') {
                let family = &rest[..end];
                fonts.push(family.to_string());
            }
        }
        collect_fonts_from_elements(&el.children, fonts);
    }
}

/// Write a single HTML element with indentation.
fn write_html_element(out: &mut String, el: &HtmlElement, indent: usize) {
    let pad = "    ".repeat(indent);

    if let Some(ref svg) = el.svg_content {
        let _ = writeln!(
            out,
            "{}<div class=\"{}\" style=\"{}\">",
            pad, el.css_class, el.inline_style
        );
        let _ = writeln!(out, "{}  {}", pad, svg);
        let _ = writeln!(out, "{}</div>", pad);
        return;
    }

    let has_children = !el.children.is_empty();
    let has_content = !el.content.is_empty();

    if !has_children && !has_content {
        let _ = writeln!(
            out,
            "{}<{} class=\"{}\" style=\"{}\"></{}>",
            pad, el.tag, el.css_class, el.inline_style, el.tag
        );
    } else if has_content && !has_children {
        let _ = writeln!(
            out,
            "{}<{} class=\"{}\" style=\"{}\">{}</{}>",
            pad, el.tag, el.css_class, el.inline_style, el.content, el.tag
        );
    } else {
        let _ = writeln!(
            out,
            "{}<{} class=\"{}\" style=\"{}\">",
            pad, el.tag, el.css_class, el.inline_style
        );
        if has_content {
            let _ = writeln!(out, "{}  {}", pad, el.content);
        }
        for child in &el.children {
            write_html_element(out, child, indent + 1);
        }
        let _ = writeln!(out, "{}</{}>", pad, el.tag);
    }
}

/// Format a float: strip trailing zeros, integers without decimal.
fn format_num(v: f32) -> String {
    if v == v.floor() && v.abs() < 1e9 {
        format!("{}", v as i64)
    } else {
        let s = format!("{:.2}", v);
        s.trim_end_matches('0').trim_end_matches('.').to_string()
    }
}

/// Format a f64 similarly.
fn format_num_f64(v: f64) -> String {
    if v == v.floor() && v.abs() < 1e9 {
        format!("{}", v as i64)
    } else {
        let s = format!("{:.2}", v);
        s.trim_end_matches('0').trim_end_matches('.').to_string()
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::parse_document;

    fn parse_and_resolve(input: &str) -> (SceneGraph, HashMap<NodeIndex, ResolvedBounds>) {
        let graph = parse_document(input).expect("test parse failed");
        let viewport = crate::layout::Viewport {
            width: 800.0,
            height: 600.0,
        };
        let bounds = crate::layout::resolve_layout(&graph, viewport);
        (graph, bounds)
    }

    #[test]
    fn export_html_rect() {
        let (graph, bounds) = parse_and_resolve("rect @box { w: 100 h: 60 fill: #FF0000 }");
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("<div"));
        assert!(html.contains("width: 100px"));
        assert!(html.contains("height: 60px"));
        assert!(html.contains("background: #FF0000"));
    }

    #[test]
    fn export_html_ellipse() {
        let (graph, bounds) = parse_and_resolve("ellipse @circle { w: 80 h: 80 fill: #00FF00 }");
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("border-radius: 50%"));
        assert!(html.contains("background: #00FF00"));
    }

    #[test]
    fn export_html_text() {
        let (graph, bounds) = parse_and_resolve("text @label \"Hello World\" {}");
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("<p"));
        assert!(html.contains("Hello World"));
    }

    #[test]
    fn export_html_text_with_font() {
        let (graph, bounds) =
            parse_and_resolve("text @title \"Dashboard\" { font: \"Inter\" bold 24 }");
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("font-family: 'Inter'"));
        assert!(html.contains("font-size: 24px"));
        assert!(html.contains("font-weight: 700"));
        assert!(html.contains("Dashboard"));
    }

    #[test]
    fn export_html_selection() {
        let input = "rect @a { w: 100 h: 50 }\nrect @b { w: 200 h: 80 }";
        let (graph, bounds) = parse_and_resolve(input);
        let html = export_html(&graph, &bounds, &["a".to_string()]);

        // Should have only one div with width: 100px
        assert!(html.contains("width: 100px"));
        assert!(!html.contains("width: 200px"));
    }

    #[test]
    fn export_html_empty_graph() {
        let graph = SceneGraph::new();
        let bounds = HashMap::new();
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("<div class=\"fd-canvas\">"));
    }

    #[test]
    fn export_html_corner_radius() {
        let (graph, bounds) = parse_and_resolve("rect @r { w: 100 h: 50 corner: 12 }");
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("border-radius: 12px"));
    }

    #[test]
    fn export_html_opacity() {
        let (graph, bounds) = parse_and_resolve("rect @r { w: 100 h: 50 opacity: 0.5 }");
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("opacity: 0.5"));
    }

    #[test]
    fn export_html_shadow() {
        let (graph, bounds) =
            parse_and_resolve("rect @r { w: 100 h: 50 shadow: (4,4,8,#00000040) }");
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("box-shadow:"));
    }

    #[test]
    fn export_html_stroke() {
        let (graph, bounds) = parse_and_resolve("rect @r { w: 100 h: 50 stroke: #333 2 }");
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("border:"));
        assert!(html.contains("#333333"));
    }

    #[test]
    fn export_html_nested_frame() {
        let input = r#"
frame @container { w: 400 h: 300 fill: #F0F0F0
  rect @child { w: 100 h: 50 fill: #FF0000 }
}
"#;
        let (graph, bounds) = parse_and_resolve(input);
        let html = export_html(&graph, &bounds, &[]);

        // Parent div should contain child div
        assert!(html.contains("fd-canvas"));
        // Both elements should be present
        let div_count = html.matches("<div").count();
        assert!(
            div_count >= 3,
            "Expected at least 3 divs (canvas + frame + child), got {}",
            div_count
        );
    }

    #[test]
    fn export_html_hover_animation() {
        let input = r#"
rect @btn { w: 120 h: 40 fill: #6C5CE7
  when :hover {
    fill: #A29BFE
    ease: ease_out 200
  }
}
"#;
        let (graph, bounds) = parse_and_resolve(input);
        let html = export_html(&graph, &bounds, &[]);

        // Should have a :hover CSS rule
        assert!(html.contains(":hover"));
        assert!(html.contains("background:"));
        assert!(html.contains("transition:"));
    }

    #[test]
    fn export_html_google_fonts() {
        let (graph, bounds) = parse_and_resolve("text @t \"Hi\" { font: \"Roboto\" 16 }");
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("fonts.googleapis.com"));
        assert!(html.contains("Roboto"));
    }

    #[test]
    fn export_html_gradient() {
        let (graph, bounds) = parse_and_resolve(
            "rect @g { w: 200 h: 100 fill: linear(90deg, #FF0000 0, #0000FF 1) }",
        );
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.contains("linear-gradient"));
    }

    #[test]
    fn export_html_valid_structure() {
        let input = r#"
rect @card { w: 200 h: 150 fill: #3498DB corner: 12 }
ellipse @avatar { w: 60 h: 60 fill: #E74C3C }
text @title "Dashboard" { font: "Inter" bold 24 }
"#;
        let (graph, bounds) = parse_and_resolve(input);
        let html = export_html(&graph, &bounds, &[]);

        assert!(html.starts_with("<!DOCTYPE html>"));
        assert!(html.contains("</html>"));
        assert!(html.contains("<head>"));
        assert!(html.contains("</head>"));
        assert!(html.contains("<body>"));
        assert!(html.contains("</body>"));
    }
}