azul-layout 0.0.7

Layout solver + font and image loader the Azul GUI 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
/// Unit tests for CSS 2.2 Section 17.2.1 - Anonymous Box Generation
///
/// Tests cover all three stages:
/// - Stage 1: Whitespace removal (remove irrelevant whitespace-only text nodes)
/// - Stage 2: Child wrapping (wrap non-cells in cells, non-rows in rows)
/// - Stage 3: Missing parent generation (wrap cells without rows, rows without tables)
use azul_css::props::layout::LayoutDisplay;

// Helper to check if a node display type matches expected
fn check_display(display: LayoutDisplay, expected: LayoutDisplay) -> bool {
    display == expected
}

#[test]
fn test_whitespace_detection_empty_string() {
    // CSS 2.2 Section 17.2.1, Stage 1:
    // Empty strings are whitespace-only
    let text = "";
    assert!(text.chars().all(|c| c.is_whitespace()));
}

#[test]
fn test_whitespace_detection_spaces() {
    // CSS 2.2 Section 17.2.1, Stage 1:
    // Spaces, tabs, newlines are whitespace
    let text = "   \t\n  ";
    assert!(text.chars().all(|c| c.is_whitespace()));
}

#[test]
fn test_whitespace_detection_with_content() {
    // CSS 2.2 Section 17.2.1, Stage 1:
    // Text with any non-whitespace character is NOT whitespace-only
    let text = "  hello  ";
    assert!(!text.chars().all(|c| c.is_whitespace()));
}

#[test]
fn test_whitespace_detection_unicode_spaces() {
    // CSS 2.2 Section 17.2.1, Stage 1:
    // Unicode whitespace characters should be detected
    let text = "\u{00A0}\u{2003}\u{2009}"; // NBSP, EM SPACE, THIN SPACE
    assert!(text.chars().all(|c| c.is_whitespace()));
}

#[test]
fn test_display_type_table() {
    // Verify table display type identification
    let display = LayoutDisplay::Table;
    assert!(check_display(display, LayoutDisplay::Table));
    assert!(!check_display(display, LayoutDisplay::Block));
}

#[test]
fn test_display_type_table_row() {
    // Verify table-row display type identification
    let display = LayoutDisplay::TableRow;
    assert!(check_display(display, LayoutDisplay::TableRow));
    assert!(!check_display(display, LayoutDisplay::Table));
}

#[test]
fn test_display_type_table_cell() {
    // Verify table-cell display type identification
    let display = LayoutDisplay::TableCell;
    assert!(check_display(display, LayoutDisplay::TableCell));
    assert!(!check_display(display, LayoutDisplay::TableRow));
}

#[test]
fn test_display_type_row_groups() {
    // Verify row group display types (thead, tbody, tfoot)
    let thead = LayoutDisplay::TableHeaderGroup;
    let tbody = LayoutDisplay::TableRowGroup;
    let tfoot = LayoutDisplay::TableFooterGroup;

    assert!(check_display(thead, LayoutDisplay::TableHeaderGroup));
    assert!(check_display(tbody, LayoutDisplay::TableRowGroup));
    assert!(check_display(tfoot, LayoutDisplay::TableFooterGroup));
}

#[test]
fn test_table_structural_elements() {
    // CSS 2.2 Section 17.2.1:
    // Table structural elements where whitespace should be skipped
    let structural = vec![
        LayoutDisplay::Table,
        LayoutDisplay::TableRowGroup,
        LayoutDisplay::TableHeaderGroup,
        LayoutDisplay::TableFooterGroup,
        LayoutDisplay::TableRow,
    ];

    for display in structural {
        assert!(matches!(
            display,
            LayoutDisplay::Table
                | LayoutDisplay::TableRowGroup
                | LayoutDisplay::TableHeaderGroup
                | LayoutDisplay::TableFooterGroup
                | LayoutDisplay::TableRow
        ));
    }
}

#[test]
fn test_non_structural_elements() {
    // Elements that should NOT trigger whitespace skipping
    let non_structural = vec![
        LayoutDisplay::Block,
        LayoutDisplay::Inline,
        LayoutDisplay::InlineBlock,
        LayoutDisplay::TableCell,
        LayoutDisplay::None,
    ];

    for display in non_structural {
        assert!(!matches!(
            display,
            LayoutDisplay::Table
                | LayoutDisplay::TableRowGroup
                | LayoutDisplay::TableHeaderGroup
                | LayoutDisplay::TableFooterGroup
                | LayoutDisplay::TableRow
        ));
    }
}

#[test]
fn test_stage2_cell_wrapping_needed() {
    // CSS 2.2 Section 17.2.1, Stage 2:
    // "If a child C of a table-row parent P is not a table-cell,
    // then generate an anonymous table-cell box around C"

    let parent = LayoutDisplay::TableRow;
    let child = LayoutDisplay::Block;

    // Block child in table-row needs anonymous cell wrapper
    assert!(parent == LayoutDisplay::TableRow);
    assert!(child != LayoutDisplay::TableCell);
}

#[test]
fn test_stage2_cell_wrapping_not_needed() {
    // CSS 2.2 Section 17.2.1, Stage 2:
    // If child is already a table-cell, no wrapping needed

    let parent = LayoutDisplay::TableRow;
    let child = LayoutDisplay::TableCell;

    assert!(parent == LayoutDisplay::TableRow);
    assert!(child == LayoutDisplay::TableCell);
}

#[test]
fn test_stage2_row_wrapping_needed() {
    // CSS 2.2 Section 17.2.1, Stage 2:
    // Table-cells that are not properly parented need anonymous row wrapper

    let parent = LayoutDisplay::Table;
    let child = LayoutDisplay::TableCell;

    // Cell directly in table (not in row) needs anonymous row
    assert!(parent == LayoutDisplay::Table);
    assert!(child == LayoutDisplay::TableCell);
}

#[test]
fn test_stage2_row_wrapping_not_needed() {
    // CSS 2.2 Section 17.2.1, Stage 2:
    // Table-rows in table don't need wrapping

    let parent = LayoutDisplay::Table;
    let child = LayoutDisplay::TableRow;

    assert!(parent == LayoutDisplay::Table);
    assert!(child == LayoutDisplay::TableRow);
}

#[test]
fn test_stage3_cell_needs_row_parent() {
    // CSS 2.2 Section 17.2.1, Stage 3:
    // "For each table-cell box C in a sequence of consecutive table-cell boxes
    // (that are not part of a table-row), an anonymous table-row box is generated"

    let parent = LayoutDisplay::Block;
    let child = LayoutDisplay::TableCell;

    // Table-cell in non-table parent needs anonymous row wrapper
    assert!(child == LayoutDisplay::TableCell);
    assert!(parent != LayoutDisplay::TableRow);
    assert!(parent != LayoutDisplay::TableRowGroup);
}

#[test]
fn test_stage3_row_needs_table_parent() {
    // CSS 2.2 Section 17.2.1, Stage 3:
    // Table-row without proper table parent needs anonymous table wrapper

    let parent = LayoutDisplay::Block;
    let child = LayoutDisplay::TableRow;

    // Table-row in non-table parent needs anonymous table wrapper
    assert!(child == LayoutDisplay::TableRow);
    assert!(parent != LayoutDisplay::Table);
    assert!(parent != LayoutDisplay::TableRowGroup);
}

#[test]
fn test_stage3_rowgroup_needs_table_parent() {
    // CSS 2.2 Section 17.2.1, Stage 3:
    // Row groups without table parent need anonymous table wrapper

    let parent = LayoutDisplay::Block;
    let child = LayoutDisplay::TableRowGroup;

    // Row group in non-table parent needs anonymous table wrapper
    assert!(matches!(
        child,
        LayoutDisplay::TableRowGroup
            | LayoutDisplay::TableHeaderGroup
            | LayoutDisplay::TableFooterGroup
    ));
    assert!(parent != LayoutDisplay::Table);
}

#[test]
fn test_stage3_cell_in_row_no_wrapper() {
    // CSS 2.2 Section 17.2.1, Stage 3:
    // Table-cell in table-row doesn't need wrapper

    let parent = LayoutDisplay::TableRow;
    let child = LayoutDisplay::TableCell;

    assert!(child == LayoutDisplay::TableCell);
    assert!(parent == LayoutDisplay::TableRow);
}

#[test]
fn test_stage3_cell_in_rowgroup_no_wrapper() {
    // CSS 2.2 Section 17.2.1, Stage 3:
    // Table-cell in row-group can be handled (will be wrapped in row by Stage 2)

    let parent = LayoutDisplay::TableRowGroup;
    let child = LayoutDisplay::TableCell;

    assert!(child == LayoutDisplay::TableCell);
    assert!(matches!(
        parent,
        LayoutDisplay::TableRowGroup
            | LayoutDisplay::TableHeaderGroup
            | LayoutDisplay::TableFooterGroup
    ));
}

#[test]
fn test_stage3_row_in_table_no_wrapper() {
    // CSS 2.2 Section 17.2.1, Stage 3:
    // Table-row in table doesn't need wrapper

    let parent = LayoutDisplay::Table;
    let child = LayoutDisplay::TableRow;

    assert!(child == LayoutDisplay::TableRow);
    assert!(parent == LayoutDisplay::Table);
}

#[test]
fn test_stage3_row_in_rowgroup_no_wrapper() {
    // CSS 2.2 Section 17.2.1, Stage 3:
    // Table-row in row-group doesn't need wrapper

    let parent = LayoutDisplay::TableRowGroup;
    let child = LayoutDisplay::TableRow;

    assert!(child == LayoutDisplay::TableRow);
    assert!(matches!(
        parent,
        LayoutDisplay::TableRowGroup
            | LayoutDisplay::TableHeaderGroup
            | LayoutDisplay::TableFooterGroup
    ));
}

#[test]
fn test_stage3_rowgroup_in_table_no_wrapper() {
    // CSS 2.2 Section 17.2.1, Stage 3:
    // Row group in table doesn't need wrapper

    let parent = LayoutDisplay::Table;
    let child = LayoutDisplay::TableRowGroup;

    assert!(matches!(
        child,
        LayoutDisplay::TableRowGroup
            | LayoutDisplay::TableHeaderGroup
            | LayoutDisplay::TableFooterGroup
    ));
    assert!(parent == LayoutDisplay::Table);
}

#[test]
fn test_complex_scenario_cell_in_block() {
    // CSS 2.2 Section 17.2.1:
    // Complex scenario - table-cell appearing in block context
    // Needs: anonymous row wrapper (Stage 3), then anonymous table wrapper (Stage 3)

    let parent = LayoutDisplay::Block;
    let child = LayoutDisplay::TableCell;

    // First, cell needs row wrapper
    assert!(child == LayoutDisplay::TableCell);
    assert!(parent != LayoutDisplay::TableRow);

    // Then, the anonymous row would need table wrapper
    let anonymous_row = LayoutDisplay::TableRow;
    assert!(anonymous_row == LayoutDisplay::TableRow);
    assert!(parent != LayoutDisplay::Table);
}

#[test]
fn test_complex_scenario_mixed_children() {
    // CSS 2.2 Section 17.2.1:
    // Table with mix of rows and cells needs to:
    // 1. Wrap consecutive cells in anonymous rows (Stage 2)
    // 2. Leave proper rows as-is

    let parent = LayoutDisplay::Table;
    let child1 = LayoutDisplay::TableRow;
    let child2 = LayoutDisplay::TableCell;
    let child3 = LayoutDisplay::TableCell;
    let child4 = LayoutDisplay::TableRow;

    // Row is fine
    assert!(child1 == LayoutDisplay::TableRow);

    // Consecutive cells (child2, child3) need anonymous row wrapper
    assert!(child2 == LayoutDisplay::TableCell);
    assert!(child3 == LayoutDisplay::TableCell);

    // Another row is fine
    assert!(child4 == LayoutDisplay::TableRow);
}

#[test]
fn test_edge_case_nested_tables() {
    // CSS 2.2 Section 17.2.1:
    // Table inside table-cell is valid, no anonymous boxes needed

    let outer_table = LayoutDisplay::Table;
    let row = LayoutDisplay::TableRow;
    let cell = LayoutDisplay::TableCell;
    let inner_table = LayoutDisplay::Table;

    assert!(outer_table == LayoutDisplay::Table);
    assert!(row == LayoutDisplay::TableRow);
    assert!(cell == LayoutDisplay::TableCell);
    assert!(inner_table == LayoutDisplay::Table);
}

#[test]
fn test_edge_case_empty_table() {
    // CSS 2.2 Section 17.2.1:
    // Empty table with no children is valid

    let display = LayoutDisplay::Table;
    assert!(display == LayoutDisplay::Table);
}

#[test]
fn test_edge_case_table_with_only_whitespace() {
    // CSS 2.2 Section 17.2.1, Stage 1:
    // Table containing only whitespace text nodes should skip all children

    let _parent = LayoutDisplay::Table;
    let whitespace1 = "   ";
    let whitespace2 = "\n\t";

    assert!(whitespace1.chars().all(|c| c.is_whitespace()));
    assert!(whitespace2.chars().all(|c| c.is_whitespace()));
}

#[test]
fn test_all_stages_integration() {
    // CSS 2.2 Section 17.2.1:
    // Complete integration test simulating all three stages

    // Original structure: div > "  " (whitespace) > table-cell > "content"
    let parent = LayoutDisplay::Block;
    let whitespace = "  ";
    let child = LayoutDisplay::TableCell;

    // Stage 1: Whitespace would be skipped (but only if parent is table structural)
    // In this case, parent is Block, so whitespace is kept but irrelevant for table
    assert!(whitespace.chars().all(|c| c.is_whitespace()));

    // Stage 2: Not applicable (no table structural parent yet)

    // Stage 3: Table-cell in block needs anonymous row wrapper
    assert!(child == LayoutDisplay::TableCell);
    assert!(parent != LayoutDisplay::TableRow);

    // After Stage 3, we'd have: div > anonymous-row > table-cell
    // Then anonymous-row in block would need anonymous table wrapper
    let anon_row = LayoutDisplay::TableRow;
    assert!(anon_row == LayoutDisplay::TableRow);
    assert!(parent != LayoutDisplay::Table);
}

#[test]
fn test_caption_not_wrapped() {
    // CSS 2.2 Section 17.2.1:
    // Table-caption should NOT be wrapped in anonymous boxes

    let parent = LayoutDisplay::Table;
    let caption = LayoutDisplay::TableCaption;

    assert!(parent == LayoutDisplay::Table);
    assert!(caption == LayoutDisplay::TableCaption);
    // Caption is a valid direct child of table, no wrapping needed
}

#[test]
fn test_column_group_not_wrapped() {
    // CSS 2.2 Section 17.2.1:
    // Table-column-group should NOT be wrapped

    let parent = LayoutDisplay::Table;
    let colgroup = LayoutDisplay::TableColumnGroup;

    assert!(parent == LayoutDisplay::Table);
    assert!(colgroup == LayoutDisplay::TableColumnGroup);
    // Column group is valid direct child of table
}

#[test]
fn test_column_not_wrapped() {
    // CSS 2.2 Section 17.2.1:
    // Table-column should NOT be wrapped

    let parent = LayoutDisplay::TableColumnGroup;
    let col = LayoutDisplay::TableColumn;

    assert!(parent == LayoutDisplay::TableColumnGroup);
    assert!(col == LayoutDisplay::TableColumn);
    // Column is valid child of column-group
}