velox-dom 0.1.2

Virtual DOM and layout engine for the Velox UI framework
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
use velox_dom::{
    Props, VNode,
    diff::{Patch, diff},
    h, text,
};

// =============================================================================
// Keyed children diff tests
// =============================================================================

#[test]
fn diff_keyed_children_reorder() {
    // old: a, b, c
    // new: c, a, b
    let old = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "a")], vec![text("A")]),
            h("li", vec![("key", "b")], vec![text("B")]),
            h("li", vec![("key", "c")], vec![text("C")]),
        ],
    );
    let new = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "c")], vec![text("C")]),
            h("li", vec![("key", "a")], vec![text("A")]),
            h("li", vec![("key", "b")], vec![text("B")]),
        ],
    );

    let patches = diff(&old, &new);
    // Reordering [a,b,c] -> [c,a,b] with identical content is expressed as a
    // single move (node keyed "c" relocates from old index 2 to new index 0),
    // preserving each keyed node's identity. No Insert/Remove is emitted.
    assert_eq!(patches, vec![Patch::MoveChild(2, 0)]);
}

#[test]
fn diff_keyed_children_reorder_with_updates() {
    // old: a, b, c   new: c, a, b  (reorder) with changed text on every node
    let old = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "a")], vec![text("A")]),
            h("li", vec![("key", "b")], vec![text("B")]),
            h("li", vec![("key", "c")], vec![text("C")]),
        ],
    );
    let new = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "c")], vec![text("C2")]),
            h("li", vec![("key", "a")], vec![text("A2")]),
            h("li", vec![("key", "b")], vec![text("B2")]),
        ],
    );

    let patches = diff(&old, &new);

    // "c" moves to the front...
    assert!(patches.contains(&Patch::MoveChild(2, 0)));
    // ...and every keyed node is updated in place at its new index. The whole
    // diff is a pure reorder + in-place updates — no Insert/Remove — so keyed
    // identity is preserved across the reorder.
    assert!(
        patches
            .iter()
            .all(|p| matches!(p, Patch::MoveChild(_, _) | Patch::UpdateChild(_, _)))
    );
}

#[test]
fn diff_keyed_children_insert_in_middle() {
    let old = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "a")], vec![text("A")]),
            h("li", vec![("key", "c")], vec![text("C")]),
        ],
    );
    let new = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "a")], vec![text("A")]),
            h("li", vec![("key", "b")], vec![text("B")]),
            h("li", vec![("key", "c")], vec![text("C")]),
        ],
    );

    let patches = diff(&old, &new);
    // Insert in the middle: only the brand-new "b" node is inserted at index 1;
    // the keyed "a" and "c" nodes are matched by key (no-op content), so no
    // insert/remove is emitted for them.
    let b_node = h("li", vec![("key", "b")], vec![text("B")]);
    assert_eq!(patches, vec![Patch::InsertChild(1, b_node)]);
}

#[test]
fn diff_keyed_children_remove_from_middle() {
    let old = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "a")], vec![text("A")]),
            h("li", vec![("key", "b")], vec![text("B")]),
            h("li", vec![("key", "c")], vec![text("C")]),
        ],
    );
    let new = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "a")], vec![text("A")]),
            h("li", vec![("key", "c")], vec![text("C")]),
        ],
    );

    let patches = diff(&old, &new);
    // Removing "b" from the middle: "c" moves up (2 -> 1) and the leftover "b"
    // at live index 2 is removed.
    assert_eq!(patches, vec![Patch::MoveChild(2, 1), Patch::RemoveChild(2)]);
}

#[test]
fn diff_keyed_children_full_replace() {
    let old = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "a")], vec![text("A")]),
            h("li", vec![("key", "b")], vec![text("B")]),
        ],
    );
    let new = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "x")], vec![text("X")]),
            h("li", vec![("key", "y")], vec![text("Y")]),
        ],
    );

    let patches = diff(&old, &new);
    // All keys differ: every old node is removed and every new node inserted.
    // The two RemoveChild(2) refer to the two old nodes, which collapse onto
    // live index 2 as each is removed in sequence.
    let x = h("li", vec![("key", "x")], vec![text("X")]);
    let y = h("li", vec![("key", "y")], vec![text("Y")]);
    assert_eq!(
        patches,
        vec![
            Patch::InsertChild(0, x),
            Patch::InsertChild(1, y),
            Patch::RemoveChild(2),
            Patch::RemoveChild(2),
        ]
    );
}

#[test]
fn diff_keyed_children_with_nested_updates() {
    let old = h(
        "ul",
        (),
        vec![
            h(
                "li",
                vec![("key", "a")],
                vec![h("span", (), vec![text("old-a")])],
            ),
            h(
                "li",
                vec![("key", "b")],
                vec![h("span", (), vec![text("old-b")])],
            ),
        ],
    );
    let new = h(
        "ul",
        (),
        vec![
            h(
                "li",
                vec![("key", "a")],
                vec![h("span", (), vec![text("new-a")])],
            ),
            h(
                "li",
                vec![("key", "b")],
                vec![h("span", (), vec![text("new-b")])],
            ),
        ],
    );

    let patches = diff(&old, &new);
    // Should have UpdateChild patches for both
    let update_count = patches
        .iter()
        .filter(|p| matches!(p, Patch::UpdateChild(_, _)))
        .count();
    assert_eq!(update_count, 2);
}

#[test]
fn diff_keyed_children_mixed_keyed_and_unkeyed() {
    // When new children have keys but old don't, it falls back to unkeyed diff
    let old = h("ul", (), vec![text("a"), text("b"), text("c")]);
    let new = h(
        "ul",
        (),
        vec![
            h("li", vec![("key", "a")], vec![text("A")]),
            h("li", vec![("key", "b")], vec![text("B")]),
        ],
    );

    let patches = diff(&old, &new);
    assert!(!patches.is_empty());
}

// =============================================================================
// Props diff edge cases
// =============================================================================

#[test]
fn diff_no_prop_change() {
    let a = h("div", vec![("class", "x"), ("id", "y")], vec![]);
    let b = h("div", vec![("class", "x"), ("id", "y")], vec![]);
    let patches = diff(&a, &b);
    assert!(patches.is_empty());
}

#[test]
fn diff_add_prop() {
    let a = h("div", (), vec![]);
    let b = h("div", vec![("class", "x")], vec![]);
    let patches = diff(&a, &b);
    assert_eq!(patches, vec![Patch::SetAttr("class".into(), "x".into())]);
}

#[test]
fn diff_remove_all_props() {
    let a = h("div", vec![("class", "x"), ("id", "y")], vec![]);
    let b = h("div", (), vec![]);
    let patches = diff(&a, &b);
    assert!(patches.contains(&Patch::RemoveAttr("class".into())));
    assert!(patches.contains(&Patch::RemoveAttr("id".into())));
}

#[test]
fn diff_prop_value_change() {
    let a = h("div", vec![("class", "old")], vec![]);
    let b = h("div", vec![("class", "new")], vec![]);
    let patches = diff(&a, &b);
    assert_eq!(patches, vec![Patch::SetAttr("class".into(), "new".into())]);
}

#[test]
fn diff_many_props() {
    let a = h("div", vec![("a", "1"), ("b", "2"), ("c", "3")], vec![]);
    let b = h("div", vec![("a", "1"), ("b", "20"), ("d", "4")], vec![]);
    let patches = diff(&a, &b);
    assert!(patches.contains(&Patch::SetAttr("b".into(), "20".into())));
    assert!(patches.contains(&Patch::SetAttr("d".into(), "4".into())));
    assert!(patches.contains(&Patch::RemoveAttr("c".into())));
    // "a" should not be in patches (same value)
    assert!(
        !patches
            .iter()
            .any(|p| matches!(p, Patch::SetAttr(k, _) if k == "a"))
    );
}

// =============================================================================
// Children diff edge cases
// =============================================================================

#[test]
fn diff_empty_to_single_child() {
    let a = h("div", (), vec![]);
    let b = h("div", (), vec![text("hello")]);
    let patches = diff(&a, &b);
    assert_eq!(patches, vec![Patch::InsertChild(0, text("hello"))]);
}

#[test]
fn diff_single_to_empty() {
    let a = h("div", (), vec![text("hello")]);
    let b = h("div", (), vec![]);
    let patches = diff(&a, &b);
    assert!(patches.contains(&Patch::RemoveChild(0)));
}

#[test]
fn diff_replace_first_child() {
    let a = h("div", (), vec![text("a"), text("b"), text("c")]);
    let b = h("div", (), vec![text("x"), text("b"), text("c")]);
    let patches = diff(&a, &b);
    assert_eq!(patches.len(), 1);
    assert!(matches!(&patches[0], Patch::UpdateChild(0, _)));
}

#[test]
fn diff_replace_last_child() {
    let a = h("div", (), vec![text("a"), text("b"), text("c")]);
    let b = h("div", (), vec![text("a"), text("b"), text("z")]);
    let patches = diff(&a, &b);
    assert_eq!(patches.len(), 1);
    assert!(matches!(&patches[0], Patch::UpdateChild(2, _)));
}

#[test]
fn diff_add_multiple_children() {
    let a = h("div", (), vec![text("a")]);
    let b = h("div", (), vec![text("a"), text("b"), text("c")]);
    let patches = diff(&a, &b);
    let inserts: Vec<_> = patches
        .iter()
        .filter(|p| matches!(p, Patch::InsertChild(_, _)))
        .collect();
    assert_eq!(inserts.len(), 2);
}

#[test]
fn diff_remove_multiple_children() {
    let a = h("div", (), vec![text("a"), text("b"), text("c"), text("d")]);
    let b = h("div", (), vec![text("a")]);
    let patches = diff(&a, &b);
    let removals: Vec<_> = patches
        .iter()
        .filter(|p| matches!(p, Patch::RemoveChild(_)))
        .collect();
    assert_eq!(removals.len(), 3);
}

#[test]
fn diff_completely_different_children() {
    let a = h("div", (), vec![text("a"), text("b")]);
    let b = h("div", (), vec![h("span", (), vec![]), h("p", (), vec![])]);
    let patches = diff(&a, &b);
    assert_eq!(patches.len(), 2);
    assert!(matches!(&patches[0], Patch::UpdateChild(0, _)));
    assert!(matches!(&patches[1], Patch::UpdateChild(1, _)));
}

// =============================================================================
// VNode key method tests
// =============================================================================

#[test]
fn vnode_key_returns_none_for_text() {
    let node = text("hello");
    assert!(node.key().is_none());
}

#[test]
fn vnode_key_returns_none_for_element_without_key() {
    let node = h("div", vec![("class", "x")], vec![]);
    assert!(node.key().is_none());
}

#[test]
fn vnode_key_returns_some_for_element_with_key() {
    let node = h("div", vec![("key", "my-key")], vec![]);
    assert_eq!(node.key(), Some("my-key".to_string()));
}

// =============================================================================
// Large tree tests
// =============================================================================

#[test]
fn diff_large_flat_list() {
    let old_children: Vec<VNode> = (0..100)
        .map(|i| {
            let s = i.to_string();
            h(
                "li",
                vec![("key", s.as_str())],
                vec![text(format!("old-{}", i))],
            )
        })
        .collect();
    let new_children: Vec<VNode> = (0..100)
        .map(|i| {
            let s = i.to_string();
            h(
                "li",
                vec![("key", s.as_str())],
                vec![text(format!("new-{}", i))],
            )
        })
        .collect();

    let old = h("ul", (), old_children);
    let new = h("ul", (), new_children);

    let patches = diff(&old, &new);
    // Should have 100 UpdateChild patches (one per item with changed text)
    assert_eq!(patches.len(), 100);
}

#[test]
fn diff_large_tree_deep() {
    // Create a deeply nested tree (50 levels deep)
    fn make_deep_tree(depth: usize) -> VNode {
        if depth == 0 {
            text("leaf")
        } else {
            h("div", (), vec![make_deep_tree(depth - 1)])
        }
    }
    let a = make_deep_tree(50);
    let b = make_deep_tree(50);

    // Same tree, should have no patches
    let patches = diff(&a, &b);
    assert!(patches.is_empty());

    // Change the leaf
    let c = h("div", (), vec![make_deep_tree(49)]);
    let c = h("div", (), vec![c]);
    let patches2 = diff(&a, &c);
    assert!(!patches2.is_empty());
}

#[test]
fn diff_large_tree_wide() {
    fn make_wide_tree(width: usize) -> VNode {
        let children: Vec<VNode> = (0..width)
            .map(|i| h("span", (), vec![text(i.to_string())]))
            .collect();
        h("div", (), children)
    }

    let a = make_wide_tree(200);
    let b = make_wide_tree(200);

    let patches = diff(&a, &b);
    assert!(patches.is_empty());
}

#[test]
fn diff_large_tree_text_change() {
    fn make_wide_tree_with_prefix(width: usize, prefix: &str) -> VNode {
        let children: Vec<VNode> = (0..width)
            .map(|i| h("span", (), vec![text(format!("{}{}", prefix, i))]))
            .collect();
        h("div", (), children)
    }

    let a = make_wide_tree_with_prefix(100, "a-");
    let b = make_wide_tree_with_prefix(100, "b-");

    let patches = diff(&a, &b);
    // Every text node should be replaced
    assert_eq!(patches.len(), 100);
}

#[test]
fn diff_deeply_nested_element_change() {
    let old = h(
        "div",
        (),
        vec![h(
            "section",
            (),
            vec![h("article", (), vec![h("p", (), vec![text("old")])])],
        )],
    );
    let new = h(
        "div",
        (),
        vec![h(
            "section",
            (),
            vec![h("article", (), vec![h("p", (), vec![text("new")])])],
        )],
    );

    let patches = diff(&old, &new);
    // Should drill down to the text change
    assert_eq!(patches.len(), 1);
    assert!(
        matches!(&patches[0], Patch::UpdateChild(0, inner_patches) if inner_patches.len() == 1)
    );
}

// =============================================================================
// Props builder tests
// =============================================================================

#[test]
fn props_builder_chaining() {
    let props = Props::new()
        .set("class", "btn")
        .set("id", "submit")
        .set("data-x", "1");
    assert_eq!(props.attrs.get("class"), Some(&"btn".to_string()));
    assert_eq!(props.attrs.get("id"), Some(&"submit".to_string()));
    assert_eq!(props.attrs.get("data-x"), Some(&"1".to_string()));
}

#[test]
fn props_default_is_empty() {
    let props = Props::default();
    assert!(props.attrs.is_empty());
}

#[test]
fn props_new_is_empty() {
    let props = Props::new();
    assert!(props.attrs.is_empty());
}