oxipdf-html 0.1.0

HTML+CSS → StyledTree adapter for the oxipdf PDF engine
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
//! HTML `<table>` → `ContentVariant::Table` conversion.
//!
//! Two-pass approach:
//! 1. Walk the HTML table structure to collect cells, determine column count,
//!    and build the row group topology.
//! 2. Predict NodeIds for cell content nodes, build `TableContent`, add the
//!    table node, then add cell content nodes as children.

use scraper::{Html, Node};

use oxipdf_ir::TextContent;
use oxipdf_ir::node::{
    BorderCollapse, ContentVariant, TableCell as IrTableCell, TableColumn, TableColumnWidth,
    TableContent, TableLayoutMode, TableRow as IrTableRow, TableRowGroup, TableRowGroupKind,
};
use oxipdf_ir::semantic::SemanticRole;
use oxipdf_ir::style::ResolvedStyle;
use oxipdf_ir::tree::StyledTreeBuilder;
use oxipdf_ir::units::Pt;

use crate::css::{apply_declarations, parse_declarations};
use crate::error::HtmlError;

use super::cascade::{
    apply_important_stylesheet_rules, apply_matching_rules, apply_normal_stylesheet_rules,
};

/// Intermediate cell data collected during HTML table traversal.
struct CollectedCell<'a> {
    /// DOM node reference for the <td>/<th> element.
    node_ref: ego_tree::NodeRef<'a, Node>,
    /// The element data.
    el: &'a scraper::node::Element,
    /// colspan attribute (default 1).
    colspan: u32,
    /// rowspan attribute (default 1).
    rowspan: u32,
    /// Whether this is a <th> header cell.
    is_header: bool,
    /// Which row group this cell belongs to.
    group_kind: TableRowGroupKind,
}

/// Convert an HTML `<table>` to a `ContentVariant::Table(TableContent)` node.
///
/// # NodeId invariant
///
/// `StyledTreeBuilder::add_child()` assigns sequential NodeIds: each call
/// increments the internal counter by exactly one. This allows us to predict
/// the NodeId that will be assigned to each cell content node before we
/// actually add them. The `debug_assert_eq!` on `table_id.raw()` and the
/// final `cell_idx == total_cells` check both validate this invariant at
/// runtime in debug builds.
#[allow(clippy::too_many_arguments)]
pub(crate) fn convert_table(
    document: &Html,
    table_ref: ego_tree::NodeRef<'_, Node>,
    parent_id: oxipdf_ir::node::NodeId,
    table_style: ResolvedStyle,
    rules: &[crate::css::CssRule],
    element_id: Option<String>,
    builder: &mut StyledTreeBuilder,
) -> Result<(), HtmlError> {
    // Pass 1: Collect all rows and cells from the HTML table structure.
    let mut collected_rows: Vec<Vec<CollectedCell<'_>>> = Vec::new();

    // Determine row group kind for rows at different levels.
    for child in table_ref.children() {
        if let Node::Element(el) = child.value() {
            let tag = el.name().to_lowercase();
            match tag.as_str() {
                "thead" => {
                    collect_rows_from_group(child, TableRowGroupKind::Header, &mut collected_rows)
                }
                "tbody" => {
                    collect_rows_from_group(child, TableRowGroupKind::Body, &mut collected_rows)
                }
                "tfoot" => {
                    collect_rows_from_group(child, TableRowGroupKind::Footer, &mut collected_rows)
                }
                "tr" => {
                    // Bare <tr> outside any group → treat as Body.
                    if let Some(row) = collect_row(child, TableRowGroupKind::Body) {
                        collected_rows.push(row);
                    }
                }
                _ => {} // Skip <caption>, <colgroup>, etc.
            }
        }
    }

    if collected_rows.is_empty() {
        // Empty table → render as empty container.
        builder.add_child(
            parent_id,
            ContentVariant::Container,
            table_style,
            Some(SemanticRole::Table),
            element_id,
        );
        return Ok(());
    }

    // Determine column count: max cells across all rows (accounting for colspan).
    let num_columns = collected_rows
        .iter()
        .map(|row| row.iter().map(|c| c.colspan).sum::<u32>())
        .max()
        .unwrap_or(1) as usize;

    // Pass 2: Build TableContent with predicted NodeIds.
    // The table node will be added at `builder.len()`, and cell content nodes
    // will follow immediately as children: first cell at `builder.len() + 1`.
    let table_node_idx = builder.len() as u32;
    let first_cell_idx = table_node_idx + 1;
    let total_cells: usize = collected_rows.iter().map(|r| r.len()).sum();

    // Build row groups and assign predicted NodeIds to cells.
    let mut row_groups: Vec<TableRowGroup> = Vec::new();
    let mut cell_idx = 0u32;

    // Group consecutive rows by their group_kind.
    let mut current_kind: Option<TableRowGroupKind> = None;
    let mut current_rows: Vec<IrTableRow> = Vec::new();

    for row in &collected_rows {
        let row_kind = row
            .first()
            .map(|c| c.group_kind)
            .unwrap_or(TableRowGroupKind::Body);

        if let Some(prev_kind) = current_kind {
            if prev_kind != row_kind && !current_rows.is_empty() {
                row_groups.push(TableRowGroup {
                    kind: prev_kind,
                    rows: std::mem::take(&mut current_rows),
                });
            }
        }
        current_kind = Some(row_kind);

        let mut ir_cells = Vec::new();
        for cell in row {
            let content_node = oxipdf_ir::node::NodeId::from_raw(first_cell_idx + cell_idx);
            ir_cells.push(IrTableCell {
                content_node,
                colspan: cell.colspan,
                rowspan: cell.rowspan,
            });
            cell_idx += 1;
        }
        current_rows.push(IrTableRow { cells: ir_cells });
    }
    // Flush last group.
    if !current_rows.is_empty() {
        if let Some(kind) = current_kind {
            row_groups.push(TableRowGroup {
                kind,
                rows: current_rows,
            });
        }
    }

    let columns: Vec<TableColumn> = (0..num_columns)
        .map(|_| TableColumn {
            width: TableColumnWidth::Auto,
        })
        .collect();

    let table_content = TableContent {
        columns,
        row_groups,
        border_collapse: BorderCollapse::Collapse,
        cell_spacing_h: Pt::new(0.0),
        cell_spacing_v: Pt::new(0.0),
        table_layout: TableLayoutMode::Auto,
    };

    // Add the table node.
    let table_id = builder.add_child(
        parent_id,
        ContentVariant::Table(table_content),
        table_style,
        Some(SemanticRole::Table),
        element_id,
    );

    // Verify our NodeId prediction was correct: add_child() assigns sequential
    // IDs, so the table node must have exactly the index we reserved above.
    debug_assert_eq!(table_id.raw(), table_node_idx);

    // Add cell content nodes as children of the table.
    for row in &collected_rows {
        for cell in row {
            let mut cell_style = ResolvedStyle::default();

            // Apply the full CSS cascade to the cell element:
            //   1. Normal stylesheet declarations
            //   2. Normal inline declarations
            //   3. !important stylesheet declarations
            //   4. !important inline declarations
            if let Some(inline_css) = cell.el.attr("style") {
                let decls = parse_declarations(inline_css);

                apply_normal_stylesheet_rules(document, cell.node_ref.id(), &mut cell_style, rules);

                let normal: Vec<_> = decls.iter().filter(|d| !d.important).cloned().collect();
                if !normal.is_empty() {
                    apply_declarations(&mut cell_style, &normal);
                }

                apply_important_stylesheet_rules(
                    document,
                    cell.node_ref.id(),
                    &mut cell_style,
                    rules,
                );

                let important: Vec<_> = decls.iter().filter(|d| d.important).cloned().collect();
                if !important.is_empty() {
                    apply_declarations(&mut cell_style, &important);
                }
            } else {
                apply_matching_rules(document, cell.node_ref.id(), &mut cell_style, rules);
            }

            // Header cells get bold text by default.
            if cell.is_header && cell_style.typography.font_weight < 700 {
                cell_style.typography.font_weight = 700;
            }

            // Determine cell content: collect text from children.
            let cell_text = collect_text_content(cell.node_ref);

            let role = if cell.is_header {
                Some(SemanticRole::TableHeader)
            } else {
                Some(SemanticRole::TableCell)
            };

            let cell_node_id = builder.add_child(
                table_id,
                ContentVariant::Text(TextContent::new(&cell_text)),
                cell_style,
                role,
                None,
            );

            // Verify sequential NodeId prediction: each add_child() call
            // increments the counter by one, so each cell node must have
            // exactly the index we reserved in Pass 2 above.
            let _ = cell_node_id;
        }
    }

    // Verify total cell count matches prediction.
    debug_assert_eq!(cell_idx as usize, total_cells);

    Ok(())
}

/// Collect rows from a `<thead>`, `<tbody>`, or `<tfoot>` group element.
fn collect_rows_from_group<'a>(
    group_ref: ego_tree::NodeRef<'a, Node>,
    kind: TableRowGroupKind,
    rows: &mut Vec<Vec<CollectedCell<'a>>>,
) {
    for child in group_ref.children() {
        if let Node::Element(el) = child.value() {
            if el.name().eq_ignore_ascii_case("tr") {
                if let Some(row) = collect_row(child, kind) {
                    rows.push(row);
                }
            }
        }
    }
}

/// Collect cells from a `<tr>` element.
fn collect_row(
    tr_ref: ego_tree::NodeRef<'_, Node>,
    group_kind: TableRowGroupKind,
) -> Option<Vec<CollectedCell<'_>>> {
    let mut cells = Vec::new();

    for child in tr_ref.children() {
        if let Node::Element(el) = child.value() {
            let tag = el.name().to_lowercase();
            if tag == "td" || tag == "th" {
                let colspan = el
                    .attr("colspan")
                    .and_then(|v| v.parse::<u32>().ok())
                    .unwrap_or(1)
                    .max(1);
                let rowspan = el
                    .attr("rowspan")
                    .and_then(|v| v.parse::<u32>().ok())
                    .unwrap_or(1)
                    .max(1);

                cells.push(CollectedCell {
                    node_ref: child,
                    el,
                    colspan,
                    rowspan,
                    is_header: tag == "th",
                    group_kind,
                });
            }
        }
    }

    if cells.is_empty() { None } else { Some(cells) }
}

/// Recursively collect text content from a DOM subtree.
fn collect_text_content(node_ref: ego_tree::NodeRef<'_, Node>) -> String {
    let mut text = String::new();
    for child in node_ref.children() {
        match child.value() {
            Node::Text(t) => text.push_str(&t.text),
            Node::Element(el) => {
                let tag = el.name().to_lowercase();
                if tag == "br" {
                    text.push('\n');
                } else {
                    text.push_str(&collect_text_content(child));
                }
            }
            _ => {}
        }
    }
    text
}

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

    #[test]
    fn simple_table_creates_table_node() {
        let html = r#"
            <table>
                <tr><td>A</td><td>B</td></tr>
                <tr><td>C</td><td>D</td></tr>
            </table>
        "#;
        let tree = html_to_tree(html).unwrap();
        let mut found_table = false;
        for node in tree.iter_nodes() {
            if let ContentVariant::Table(ref tc) = node.content {
                found_table = true;
                assert_eq!(tc.columns.len(), 2, "should have 2 columns");
                assert_eq!(tc.total_rows(), 2, "should have 2 rows");
                // All cells should be body rows.
                for rg in &tc.row_groups {
                    assert_eq!(rg.kind, TableRowGroupKind::Body);
                }
            }
        }
        assert!(found_table, "should have a table node");
    }

    #[test]
    fn table_with_thead_tbody() {
        let html = r#"
            <table>
                <thead><tr><th>Header 1</th><th>Header 2</th></tr></thead>
                <tbody>
                    <tr><td>Data 1</td><td>Data 2</td></tr>
                    <tr><td>Data 3</td><td>Data 4</td></tr>
                </tbody>
            </table>
        "#;
        let tree = html_to_tree(html).unwrap();
        let mut found = false;
        for node in tree.iter_nodes() {
            if let ContentVariant::Table(ref tc) = node.content {
                found = true;
                assert_eq!(tc.columns.len(), 2);
                assert_eq!(tc.total_rows(), 3);
                // Should have Header and Body groups.
                assert!(
                    tc.row_groups
                        .iter()
                        .any(|rg| rg.kind == TableRowGroupKind::Header),
                    "should have header group"
                );
                assert!(
                    tc.row_groups
                        .iter()
                        .any(|rg| rg.kind == TableRowGroupKind::Body),
                    "should have body group"
                );
                // Header group should have 1 row.
                let header_rows: usize = tc
                    .row_groups
                    .iter()
                    .filter(|rg| rg.kind == TableRowGroupKind::Header)
                    .map(|rg| rg.rows.len())
                    .sum();
                assert_eq!(header_rows, 1);
            }
        }
        assert!(found);
    }

    #[test]
    fn table_cell_content_is_text() {
        let html = r#"<table><tr><td>Hello</td></tr></table>"#;
        let tree = html_to_tree(html).unwrap();
        // The table's children should be text nodes with cell content.
        let mut found_hello = false;
        for node in tree.iter_nodes() {
            if let ContentVariant::Text(ref t) = node.content {
                if t.text.contains("Hello") {
                    found_hello = true;
                }
            }
        }
        assert!(found_hello, "cell text should be in tree");
    }

    #[test]
    fn table_th_cells_are_bold() {
        let html = r#"<table><tr><th>Bold</th><td>Normal</td></tr></table>"#;
        let tree = html_to_tree(html).unwrap();
        let mut bold_count = 0;
        for node in tree.iter_nodes() {
            if node.semantic_role == Some(SemanticRole::TableHeader)
                && node.style.typography.font_weight >= 700
            {
                bold_count += 1;
            }
        }
        assert!(bold_count >= 1, "th cells should be bold");
    }

    #[test]
    fn table_colspan() {
        let html = r#"
            <table>
                <tr><td colspan="2">Wide</td></tr>
                <tr><td>A</td><td>B</td></tr>
            </table>
        "#;
        let tree = html_to_tree(html).unwrap();
        for node in tree.iter_nodes() {
            if let ContentVariant::Table(ref tc) = node.content {
                assert_eq!(tc.columns.len(), 2);
                // First row should have 1 cell with colspan=2.
                let first_row = &tc.row_groups[0].rows[0];
                assert_eq!(first_row.cells.len(), 1);
                assert_eq!(first_row.cells[0].colspan, 2);
                // Second row should have 2 cells.
                let second_row = &tc.row_groups[0].rows[1];
                assert_eq!(second_row.cells.len(), 2);
            }
        }
    }

    #[test]
    fn table_rowspan() {
        let html = r#"
            <table>
                <tr><td rowspan="2">Tall</td><td>B</td></tr>
                <tr><td>D</td></tr>
            </table>
        "#;
        let tree = html_to_tree(html).unwrap();
        for node in tree.iter_nodes() {
            if let ContentVariant::Table(ref tc) = node.content {
                let first_row = &tc.row_groups[0].rows[0];
                assert_eq!(first_row.cells[0].rowspan, 2);
            }
        }
    }

    #[test]
    fn empty_table_becomes_container() {
        let html = r#"<table></table>"#;
        let tree = html_to_tree(html).unwrap();
        // Should not panic; empty table → container.
        assert!(tree.node_count() >= 2);
    }

    #[test]
    fn table_cell_nodeids_are_valid() {
        let html = r#"
            <table>
                <tr><td>A</td><td>B</td></tr>
                <tr><td>C</td><td>D</td></tr>
            </table>
        "#;
        let tree = html_to_tree(html).unwrap();
        for node in tree.iter_nodes() {
            if let ContentVariant::Table(ref tc) = node.content {
                // All cell content_node references should be valid NodeIds.
                for row in tc.all_rows() {
                    for cell in &row.cells {
                        let cell_node = tree.node(cell.content_node);
                        assert!(
                            matches!(cell_node.content, ContentVariant::Text(_)),
                            "cell content should be text"
                        );
                    }
                }
            }
        }
    }
}