-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathtest_serialization.py
More file actions
1145 lines (950 loc) · 38.1 KB
/
Copy pathtest_serialization.py
File metadata and controls
1145 lines (950 loc) · 38.1 KB
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Test serialization."""
import threading
from pathlib import Path
from unittest.mock import MagicMock, patch
from xml.etree import ElementTree as ET
import pytest
from docling_core.transforms.serializer.common import _DEFAULT_LABELS
from docling_core.transforms.serializer.html import (
HTMLDocSerializer,
HTMLMetaSerializer,
HTMLOutputStyle,
HTMLParams,
HTMLTableSerializer,
)
from docling_core.transforms.serializer.markdown import (
FurnitureMode,
MarkdownDocSerializer,
MarkdownParams,
MarkdownTableSerializer,
OrigListItemMarkerMode,
_cell_content_has_table,
)
from docling_core.transforms.serializer.webvtt import WebVTTDocSerializer, WebVTTParams
from docling_core.transforms.visualizer.layout_visualizer import LayoutVisualizer
from docling_core.types.doc import DoclingDocument
from docling_core.types.doc.base import ImageRefMode
from docling_core.types.doc.document import (
BaseMeta,
CharSpan,
ContentLayer,
DescriptionAnnotation,
EntitiesMetaField,
EntityMention,
LanguageMetaField,
PictureClassificationMetaField,
PictureClassificationPrediction,
PictureMeta,
RefItem,
RichTableCell,
SummaryMetaField,
TableCell,
TableData,
TextItem,
)
from docling_core.types.doc.labels import DocItemLabel
from .test_data_gen_flag import GEN_TEST_DATA
def verify(exp_file: Path, actual: str):
if GEN_TEST_DATA:
with open(exp_file, "w", encoding="utf-8") as f:
f.write(f"{actual}\n")
else:
with open(exp_file, encoding="utf-8") as f:
expected = f.read().rstrip()
# Normalize platform-dependent quote escaping for DocTags outputs
name = exp_file.name
if name.endswith((".dt", ".idt", ".idt.xml")):
def _normalize_quotes(s: str) -> str:
return s.replace(""", '"').replace(""", '"')
expected = _normalize_quotes(expected)
actual = _normalize_quotes(actual)
assert actual == expected
# ===============================
# Markdown tests
# ===============================
def test_md_cross_page_list_page_break():
src = Path("./test/data/doc/activities.json")
doc = DoclingDocument.load_from_json(src)
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
image_mode=ImageRefMode.PLACEHOLDER,
image_placeholder="<!-- image -->",
page_break_placeholder="<!-- page break -->",
labels=_DEFAULT_LABELS - {DocItemLabel.PICTURE},
),
)
actual = ser.serialize().text
verify(exp_file=src.with_suffix(".gt.md"), actual=actual)
def test_md_checkboxes():
src = Path("./test/data/doc/checkboxes.json")
doc = DoclingDocument.load_from_json(src)
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
image_mode=ImageRefMode.PLACEHOLDER,
image_placeholder="<!-- image -->",
page_break_placeholder="<!-- page break -->",
labels=_DEFAULT_LABELS - {DocItemLabel.PICTURE},
),
)
actual = ser.serialize().text
verify(exp_file=src.parent / f"{src.stem}.gt.md", actual=actual)
def test_md_cross_page_list_page_break_none():
src = Path("./test/data/doc/activities.json")
doc = DoclingDocument.load_from_json(src)
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
image_mode=ImageRefMode.PLACEHOLDER,
image_placeholder="<!-- image -->",
page_break_placeholder=None,
labels=_DEFAULT_LABELS - {DocItemLabel.PICTURE},
),
)
actual = ser.serialize().text
verify(exp_file=src.parent / f"{src.stem}_pb_none.gt.md", actual=actual)
def test_md_cross_page_list_page_break_empty():
src = Path("./test/data/doc/activities.json")
doc = DoclingDocument.load_from_json(src)
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
image_mode=ImageRefMode.PLACEHOLDER,
image_placeholder="<!-- image -->",
page_break_placeholder="",
labels=_DEFAULT_LABELS - {DocItemLabel.PICTURE},
),
)
actual = ser.serialize().text
verify(exp_file=src.parent / f"{src.stem}_pb_empty.gt.md", actual=actual)
def test_md_cross_page_list_page_break_non_empty():
src = Path("./test/data/doc/activities.json")
doc = DoclingDocument.load_from_json(src)
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
image_mode=ImageRefMode.PLACEHOLDER,
image_placeholder="<!-- image -->",
page_break_placeholder="<!-- page-break -->",
labels=_DEFAULT_LABELS - {DocItemLabel.PICTURE},
),
)
actual = ser.serialize().text
verify(exp_file=src.parent / f"{src.stem}_pb_non_empty.gt.md", actual=actual)
def test_md_cross_page_list_page_break_p2():
src = Path("./test/data/doc/activities.json")
doc = DoclingDocument.load_from_json(src)
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
image_mode=ImageRefMode.PLACEHOLDER,
image_placeholder="<!-- image -->",
page_break_placeholder=None,
pages={2},
),
)
actual = ser.serialize().text
verify(exp_file=src.parent / f"{src.stem}_p2.gt.md", actual=actual)
def test_md_charts():
src = Path("./test/data/doc/barchart.json")
doc = DoclingDocument.load_from_json(src)
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
image_mode=ImageRefMode.PLACEHOLDER,
),
)
actual = ser.serialize().text
verify(exp_file=src.with_suffix(".gt.md"), actual=actual)
def test_md_inline_and_formatting():
src = Path("./test/data/doc/inline_and_formatting.yaml")
doc = DoclingDocument.load_from_yaml(src)
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
image_mode=ImageRefMode.PLACEHOLDER,
),
)
actual = ser.serialize().text
verify(exp_file=src.with_suffix(".gt.md"), actual=actual)
def test_md_pb_placeholder_and_page_filter():
src = Path("./test/data/doc/2408.09869v3_enriched.json")
doc = DoclingDocument.load_from_json(src)
# NOTE ambiguous case
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
page_break_placeholder="<!-- page break -->",
pages={3, 4, 6},
),
)
actual = ser.serialize().text
verify(exp_file=src.with_suffix(".gt.md"), actual=actual)
def test_md_list_item_markers(sample_doc):
root_dir = Path("./test/data/doc")
for mode in OrigListItemMarkerMode:
for valid in [False, True]:
ser = MarkdownDocSerializer(
doc=sample_doc,
params=MarkdownParams(
orig_list_item_marker_mode=mode,
ensure_valid_list_item_marker=valid,
),
)
actual = ser.serialize().text
verify(
root_dir / f"constructed_mode_{str(mode.value).lower()}_valid_{str(valid).lower()}.gt.md",
actual=actual,
)
def test_md_mark_meta_true():
src = Path("./test/data/doc/2408.09869v3_enriched.json")
doc = DoclingDocument.load_from_json(src)
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
mark_meta=True,
pages={1, 5},
),
)
actual = ser.serialize().text
verify(
exp_file=src.parent / f"{src.stem}_p1_mark_meta_true.gt.md",
actual=actual,
)
def test_md_mark_meta_false():
src = Path("./test/data/doc/2408.09869v3_enriched.json")
doc = DoclingDocument.load_from_json(src)
ser = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
mark_meta=False,
pages={1, 5},
),
)
actual = ser.serialize().text
verify(
exp_file=src.parent / f"{src.stem}_p1_mark_meta_false.gt.md",
actual=actual,
)
def test_md_legacy_annotations_mark_true(sample_doc):
exp_file = Path("./test/data/doc/constructed_legacy_annot_mark_true.gt.md")
with pytest.warns(DeprecationWarning):
sample_doc.tables[0].annotations.append(
DescriptionAnnotation(text="This is a description of table 1.", provenance="foo")
)
ser = MarkdownDocSerializer(
doc=sample_doc,
params=MarkdownParams(
mark_annotations=True,
),
)
actual = ser.serialize().text
verify(
exp_file=exp_file,
actual=actual,
)
def test_md_legacy_annotations_mark_false(sample_doc):
exp_file = Path("./test/data/doc/constructed_legacy_annot_mark_false.gt.md")
with pytest.warns(DeprecationWarning):
sample_doc.tables[0].annotations.append(
DescriptionAnnotation(text="This is a description of table 1.", provenance="foo")
)
ser = MarkdownDocSerializer(
doc=sample_doc,
params=MarkdownParams(
mark_annotations=False,
),
)
actual = ser.serialize().text
verify(
exp_file=exp_file,
actual=actual,
)
def test_md_nested_lists():
src = Path("./test/data/doc/polymers.json")
doc = DoclingDocument.load_from_json(src)
ser = MarkdownDocSerializer(doc=doc)
actual = ser.serialize().text
verify(exp_file=src.with_suffix(".gt.md"), actual=actual)
def test_md_rich_table(rich_table_doc):
exp_file = Path("./test/data/doc/rich_table.gt.md")
ser = MarkdownDocSerializer(doc=rich_table_doc)
actual = ser.serialize().text
verify(exp_file=exp_file, actual=actual)
def test_md_single_row_table():
exp_file = Path("./test/data/doc/single_row_table.gt.md")
words = ["foo", "bar"]
doc = DoclingDocument(name="")
row_idx = 0
table = doc.add_table(data=TableData(num_rows=1, num_cols=len(words)))
for col_idx, word in enumerate(words):
doc.add_table_cell(
table_item=table,
cell=TableCell(
start_row_offset_idx=row_idx,
end_row_offset_idx=row_idx + 1,
start_col_offset_idx=col_idx,
end_col_offset_idx=col_idx + 1,
text=word,
),
)
ser = MarkdownDocSerializer(doc=doc)
actual = ser.serialize().text
verify(exp_file=exp_file, actual=actual)
def test_md_pipe_in_table():
doc = DoclingDocument(name="Pipe in Table")
table = doc.add_table(data=TableData(num_rows=1, num_cols=1))
# TODO: properly handle nested tables, for now just escape the pipe
doc.add_table_cell(
table,
TableCell(
start_row_offset_idx=0,
end_row_offset_idx=1,
start_col_offset_idx=0,
end_col_offset_idx=1,
text="Fruits | Veggies",
),
)
ser = doc.export_to_markdown()
assert ser == "| Fruits | Veggies |\n|-------------------------|"
def test_cell_content_has_table_detects_descendant_table():
"""Ensure nested tables are detected through non-table parent nodes."""
doc = DoclingDocument(name="descendant_table")
wrapper = doc.add_group()
nested_table = doc.add_table(data=TableData(num_rows=1, num_cols=1), parent=wrapper)
doc.add_table_cell(
nested_table,
TableCell(
text="inner",
start_row_offset_idx=0,
end_row_offset_idx=1,
start_col_offset_idx=0,
end_col_offset_idx=1,
),
)
assert _cell_content_has_table(wrapper, doc)
def _build_nested_rich_table_doc(depth: int) -> DoclingDocument:
"""Build a document with `depth` levels of nested RichTableCell tables.
Each level is a 1x2 table whose first cell is a RichTableCell referencing
the next-level table, and whose second cell is a plain TableCell.
This is the structure produced by the HTML backend for Wikipedia clade tables.
"""
doc = DoclingDocument(name="nested_tables")
def _add_level(parent, remaining: int):
table = doc.add_table(data=TableData(num_rows=1, num_cols=2), parent=parent)
if remaining > 0:
nested = _add_level(table, remaining - 1)
rich_cell: TableCell = RichTableCell(
ref=nested.get_ref(),
text="rich",
start_row_offset_idx=0,
end_row_offset_idx=1,
start_col_offset_idx=0,
end_col_offset_idx=1,
)
else:
rich_cell = TableCell(
text="leaf",
start_row_offset_idx=0,
end_row_offset_idx=1,
start_col_offset_idx=0,
end_col_offset_idx=1,
)
doc.add_table_cell(table, rich_cell)
doc.add_table_cell(
table,
TableCell(
text="plain",
start_row_offset_idx=0,
end_row_offset_idx=1,
start_col_offset_idx=1,
end_col_offset_idx=2,
),
)
return table
_add_level(doc.body, depth)
return doc
def test_md_nested_rich_table_no_hang():
"""Regression: export_to_markdown() must not hang on nested RichTableCells.
When a RichTableCell's content contains a nested table, the
``_nested_in_table`` flag passed through kwargs causes
MarkdownTableSerializer to flatten the inner table instead of
re-entering the full table serializer recursively. Without this
guard every level of nesting re-enters the table serializer, causing
exponential string growth and an indefinite hang.
"""
doc = _build_nested_rich_table_doc(depth=5)
result: list[str] = []
def _run() -> None:
result.append(doc.export_to_markdown())
t = threading.Thread(target=_run, daemon=True)
t.start()
t.join(timeout=5.0)
assert not t.is_alive(), "export_to_markdown() hung on a document with nested RichTableCells."
assert result, "export_to_markdown() produced no output"
# The outer table must be a valid 2-column markdown table.
# Without the pipe-escaping fix, inner-table pipes would leak into the outer
# table and produce dozens of phantom columns.
table_rows = [line for line in result[0].splitlines() if line.startswith("|")]
assert table_rows, "Expected at least one markdown table row in output"
col_counts = {line.count("|") - 1 for line in table_rows}
assert col_counts == {2}, f"Outer table must have exactly 2 columns throughout; got column counts: {col_counts}"
def test_md_compact_table():
"""Test compact table format removes padding and uses minimal separators."""
# Test the _compact_table method directly
padded_table = """| item | qty | description |
| ------ | ----: | :-------------------: |
| spam | 42 | A canned meat product |
| eggs | 451 | Fresh farm eggs |
| bacon | 0 | Out of stock |"""
expected_compact = """| item | qty | description |
| - | -: | :-: |
| spam | 42 | A canned meat product |
| eggs | 451 | Fresh farm eggs |
| bacon | 0 | Out of stock |"""
compact_result = MarkdownTableSerializer._compact_table(padded_table)
assert compact_result == expected_compact
# Verify space savings
assert len(compact_result) < len(padded_table)
def test_md_numeric_precision_preserved():
"""Test that numeric values in tables preserve their full precision.
Regression test for issue where tabulate's numparse would silently
truncate numeric strings to ~6 significant figures.
"""
doc = DoclingDocument(name="Numeric Precision Test")
precise_values = [
"225.8183",
"24797.34",
"20896.7184",
"17358.138",
"123.456789",
]
table = doc.add_table(data=TableData(num_rows=len(precise_values) + 1, num_cols=2))
# Add header row
doc.add_table_cell(
table_item=table,
cell=TableCell(
start_row_offset_idx=0,
end_row_offset_idx=1,
start_col_offset_idx=0,
end_col_offset_idx=1,
text="Description",
),
)
doc.add_table_cell(
table_item=table,
cell=TableCell(
start_row_offset_idx=0,
end_row_offset_idx=1,
start_col_offset_idx=1,
end_col_offset_idx=2,
text="Value",
),
)
# Add data rows with precise numeric values
for row_idx, value in enumerate(precise_values, start=1):
doc.add_table_cell(
table_item=table,
cell=TableCell(
start_row_offset_idx=row_idx,
end_row_offset_idx=row_idx + 1,
start_col_offset_idx=0,
end_col_offset_idx=1,
text=f"Item {row_idx}",
),
)
doc.add_table_cell(
table_item=table,
cell=TableCell(
start_row_offset_idx=row_idx,
end_row_offset_idx=row_idx + 1,
start_col_offset_idx=1,
end_col_offset_idx=2,
text=value,
),
)
markdown_output = doc.export_to_markdown()
for value in precise_values:
assert value in markdown_output, (
f"Numeric value '{value}' was not preserved in markdown output. "
"This indicates precision loss during table serialization."
)
def test_md_traverse_pictures():
"""Test traverse_pictures parameter to include text inside PictureItems."""
doc = DoclingDocument(name="Test Document")
doc.add_text(label=DocItemLabel.PARAGRAPH, text="Text before picture")
picture = doc.add_picture()
# Manually add a text item as child of picture
text_in_picture = TextItem(
self_ref=f"#/texts/{len(doc.texts)}",
parent=RefItem(cref=picture.self_ref),
label=DocItemLabel.PARAGRAPH,
text="Text inside picture",
orig="Text inside picture",
)
doc.texts.append(text_in_picture)
picture.children.append(RefItem(cref=text_in_picture.self_ref))
doc.add_text(label=DocItemLabel.PARAGRAPH, text="Text after picture")
# Test with traverse_pictures=False (default)
ser_no_traverse = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
image_mode=ImageRefMode.PLACEHOLDER,
image_placeholder="<!-- image -->",
traverse_pictures=False,
),
)
result_no_traverse = ser_no_traverse.serialize().text
# Should NOT contain text inside picture
assert "Text before picture" in result_no_traverse
assert "Text after picture" in result_no_traverse
assert "Text inside picture" not in result_no_traverse
assert "<!-- image -->" in result_no_traverse
# Test with traverse_pictures=True
ser_with_traverse = MarkdownDocSerializer(
doc=doc,
params=MarkdownParams(
image_mode=ImageRefMode.PLACEHOLDER,
image_placeholder="<!-- image -->",
traverse_pictures=True,
),
)
result_with_traverse = ser_with_traverse.serialize().text
# Should contain text inside picture
assert "Text before picture" in result_with_traverse
assert "Text after picture" in result_with_traverse
assert "Text inside picture" in result_with_traverse
assert "<!-- image -->" in result_with_traverse
def test_md_furniture_modes():
"""Test that furniture_mode correctly filters and deduplicates headers/footers."""
doc = DoclingDocument(name="Furniture Test")
# 1. Add standard body text
doc.add_text(label=DocItemLabel.PARAGRAPH, text="Main body paragraph text.")
# 2. Add headers/footers and tag them as FURNITURE layer items
h1 = doc.add_text(label=DocItemLabel.TEXT, text="Document Header Title")
h1.content_layer = ContentLayer.FURNITURE
h2 = doc.add_text(label=DocItemLabel.TEXT, text="Document Header Title") # Duplicate text
h2.content_layer = ContentLayer.FURNITURE
h3 = doc.add_text(label=DocItemLabel.TEXT, text="Different Footer Notice")
h3.content_layer = ContentLayer.FURNITURE
# Test Mode 1: NONE (Default) -> Should completely drop furniture items
ser_none = MarkdownDocSerializer(doc=doc, params=MarkdownParams(furniture_mode=FurnitureMode.NONE))
text_none = ser_none.serialize().text
assert "Main body paragraph text." in text_none
assert "Document Header Title" not in text_none
# Test Mode 2: ALL -> Should render every furniture item, including duplicates
ser_all = MarkdownDocSerializer(doc=doc, params=MarkdownParams(furniture_mode=FurnitureMode.ALL))
text_all = ser_all.serialize().text
assert "Main body paragraph text." in text_all
assert text_all.count("Document Header Title") == 2
assert "Different Footer Notice" in text_all
# Test Mode 3: DISTINCT -> Should render unique text items exactly once
ser_distinct = MarkdownDocSerializer(doc=doc, params=MarkdownParams(furniture_mode=FurnitureMode.DISTINCT))
text_distinct = ser_distinct.serialize().text
assert "Main body paragraph text." in text_distinct
assert text_distinct.count("Document Header Title") == 1
assert "Different Footer Notice" in text_distinct
# ===============================
# HTML tests
# ===============================
def test_html_table_serializer_get_header_and_body_lines():
"""Test HTMLTableSerializer.get_header_and_body_lines() method."""
serializer = HTMLTableSerializer()
# Test 1: Valid HTML with headers and data
valid_html = "<table><tr><th>Header1</th><th>Header2</th></tr><tr><td>Data1</td><td>Data2</td></tr></table>"
headers, body = serializer.get_header_and_body_lines(table_text=valid_html)
assert len(headers) > 0, "Should have headers"
assert len(body) > 0, "Should have body rows"
# Test 2: Row without closing </tr> tag
# Parser will find the row, but when we search for </tr> it won't be found
no_close_tr = "<tr><th>Header</th></tr><tr><td>Data1"
headers, body = serializer.get_header_and_body_lines(table_text=no_close_tr)
assert isinstance(headers, list)
assert isinstance(body, list)
# Test 3: Data rows with incomplete closing tags
# When collecting remaining rows, some </tr> tags are missing
incomplete_data = "<tr><th>H1</th></tr><tr><td>D1</td></tr><tr><td>D2"
headers, body = serializer.get_header_and_body_lines(table_text=incomplete_data)
assert isinstance(headers, list)
assert isinstance(body, list)
# Test 4: Force exception in parser
with patch("docling_core.transforms.serializer.html._SimpleHTMLTableParser") as mock_parser_class:
mock_parser = MagicMock()
mock_parser.feed.side_effect = Exception("Parser error")
mock_parser_class.return_value = mock_parser
broken_html = "<tr><th>Header</th></tr><tr><td>Data</td></tr>"
headers, body = serializer.get_header_and_body_lines(table_text=broken_html)
# Should use fallback logic
assert isinstance(headers, list)
assert isinstance(body, list)
# Test 5: Parser returns more rows than exist in HTML
# Mock parser to return extra rows that don't exist in the HTML
with patch("docling_core.transforms.serializer.html._SimpleHTMLTableParser") as mock_parser_class:
mock_parser = MagicMock()
# Create fake row data - more rows than actually exist in HTML
mock_parser.rows = [
{"th_cells": ["H1"], "td_cells": []},
{"th_cells": ["H2"], "td_cells": []},
{"th_cells": ["H3"], "td_cells": []}, # This row doesn't exist in HTML
{"th_cells": [], "td_cells": ["D1"]},
]
mock_parser_class.return_value = mock_parser
# HTML with only 2 rows, but parser claims 4
limited_html = "<tr><th>H1</th></tr><tr><th>H2</th></tr>"
headers, body = serializer.get_header_and_body_lines(table_text=limited_html)
assert isinstance(headers, list)
assert isinstance(body, list)
# Test 6: Specific case for line 485 - row_start found but row_end not found
# Create HTML where parser finds a row, but the actual HTML has <tr without </tr>
with patch("docling_core.transforms.serializer.html._SimpleHTMLTableParser") as mock_parser_class:
mock_parser = MagicMock()
# Parser reports a header row exists
mock_parser.rows = [
{"th_cells": ["Header"], "td_cells": []},
]
mock_parser_class.return_value = mock_parser
# But the HTML has <tr without matching </tr>
html_no_close = "<tr><th>Header"
headers, body = serializer.get_header_and_body_lines(table_text=html_no_close)
assert isinstance(headers, list)
assert isinstance(body, list)
# Test 7: Specific case for line 504 - data collection finds <tr but no </tr>
# Create HTML where we start collecting data rows but encounter incomplete row
with patch("docling_core.transforms.serializer.html._SimpleHTMLTableParser") as mock_parser_class:
mock_parser = MagicMock()
# Parser reports header then data rows
mock_parser.rows = [
{"th_cells": ["H"], "td_cells": []},
{"th_cells": [], "td_cells": ["D1"]}, # This triggers data collection
]
mock_parser_class.return_value = mock_parser
# HTML has complete header but incomplete data row
html_incomplete_data = "<tr><th>H</th></tr><tr><td>D1</td></tr><tr><td>D2"
headers, body = serializer.get_header_and_body_lines(table_text=html_incomplete_data)
assert isinstance(headers, list)
assert isinstance(body, list)
# Test 8: Table with footer content
with_footer = "<tr><th>H</th></tr><tr><td>D</td></tr>Footer content"
headers, body = serializer.get_header_and_body_lines(table_text=with_footer)
assert isinstance(headers, list)
assert isinstance(body, list)
# Footer should be in body
assert "Footer" in str(body)
def test_html_charts():
src = Path("./test/data/doc/barchart.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(
doc=doc,
params=HTMLParams(
image_mode=ImageRefMode.PLACEHOLDER,
),
)
actual = ser.serialize().text
verify(exp_file=src.with_suffix(".gt.html"), actual=actual)
def test_html_cross_page_list_page_break():
src = Path("./test/data/doc/activities.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(
doc=doc,
params=HTMLParams(
image_mode=ImageRefMode.PLACEHOLDER,
),
)
actual = ser.serialize().text
verify(exp_file=src.with_suffix(".gt.html"), actual=actual)
def test_html_cross_page_list_page_break_p1():
src = Path("./test/data/doc/activities.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(
doc=doc,
params=HTMLParams(
image_mode=ImageRefMode.PLACEHOLDER,
pages={1},
),
)
actual = ser.serialize().text
verify(exp_file=src.parent / f"{src.stem}_p1.gt.html", actual=actual)
def test_html_cross_page_list_page_break_p2():
src = Path("./test/data/doc/activities.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(
doc=doc,
params=HTMLParams(
image_mode=ImageRefMode.PLACEHOLDER,
pages={2},
),
)
actual = ser.serialize().text
verify(exp_file=src.parent / f"{src.stem}_p2.gt.html", actual=actual)
def test_html_split_page():
src = Path("./test/data/doc/2408.09869v3_enriched.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(
doc=doc,
params=HTMLParams(
image_mode=ImageRefMode.EMBEDDED,
output_style=HTMLOutputStyle.SPLIT_PAGE,
),
)
actual = ser.serialize().text
verify(exp_file=src.parent / f"{src.stem}_split.gt.html", actual=actual)
def test_html_split_page_p2():
src = Path("./test/data/doc/2408.09869v3_enriched.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(
doc=doc,
params=HTMLParams(
image_mode=ImageRefMode.EMBEDDED,
output_style=HTMLOutputStyle.SPLIT_PAGE,
pages={2},
),
)
actual = ser.serialize().text
verify(exp_file=src.parent / f"{src.stem}_split_p2.gt.html", actual=actual)
def test_html_split_page_p2_with_visualizer():
src = Path("./test/data/doc/2408.09869v3_enriched.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(
doc=doc,
params=HTMLParams(
image_mode=ImageRefMode.EMBEDDED,
output_style=HTMLOutputStyle.SPLIT_PAGE,
pages={2},
),
)
ser_res = ser.serialize(
visualizer=LayoutVisualizer(),
)
actual = ser_res.text
# pinning the result with visualizer appeared flaky, so at least ensure it contains
# a figure (for the page) and that it is different than without visualizer:
assert '<figure><img src="data:image/png;base64' in actual
file_without_viz = src.parent / f"{src.stem}_split_p2.gt.html"
with open(file_without_viz) as f:
data_without_viz = f.read()
assert actual.strip() != data_without_viz.strip()
def test_html_split_page_no_page_breaks():
src = Path("./test/data/doc/2408.09869_p1.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(
doc=doc,
params=HTMLParams(
image_mode=ImageRefMode.EMBEDDED,
output_style=HTMLOutputStyle.SPLIT_PAGE,
),
)
actual = ser.serialize().text
verify(exp_file=src.parent / f"{src.stem}_split.gt.html", actual=actual)
def test_html_include_annotations_false():
src = Path("./test/data/doc/2408.09869v3_enriched.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(
doc=doc,
params=HTMLParams(
image_mode=ImageRefMode.PLACEHOLDER,
include_annotations=False,
pages={1},
html_head="<head></head>", # keeping test output minimal
),
)
actual = ser.serialize().text
verify(
exp_file=src.parent / f"{src.stem}_p1_include_annotations_false.gt.html",
actual=actual,
)
def test_html_include_annotations_true():
src = Path("./test/data/doc/2408.09869v3_enriched.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(
doc=doc,
params=HTMLParams(
image_mode=ImageRefMode.PLACEHOLDER,
include_annotations=True,
pages={1},
html_head="<head></head>", # keeping test output minimal
),
)
actual = ser.serialize().text
verify(
exp_file=src.parent / f"{src.stem}_p1_include_annotations_true.gt.html",
actual=actual,
)
def test_html_list_item_markers(sample_doc):
root_dir = Path("./test/data/doc")
for orig in [False, True]:
ser = HTMLDocSerializer(
doc=sample_doc,
params=HTMLParams(
show_original_list_item_marker=orig,
),
)
actual = ser.serialize().text
verify(
root_dir / f"constructed_orig_{str(orig).lower()}.gt.html",
actual=actual,
)
def test_html_nested_lists():
src = Path("./test/data/doc/polymers.json")
doc = DoclingDocument.load_from_json(src)
ser = HTMLDocSerializer(doc=doc)
actual = ser.serialize().text
verify(exp_file=src.with_suffix(".gt.html"), actual=actual)
def test_html_rich_table(rich_table_doc):
exp_file = Path("./test/data/doc/rich_table.gt.html")
ser = HTMLDocSerializer(doc=rich_table_doc)
actual = ser.serialize().text
verify(exp_file=exp_file, actual=actual)
def test_html_rich_cell_textitem_ref_subtree_inside_and_not_outside():
"""Descendants of a RichTableCell's TextItem ref render inside the table.
With HTMLTextSerializer recursing into its item's children, the parent text
(CELL-TEXT) and its child (DEEP-LEAK) both render as siblings inside the
rich cell. The outer document iteration must not re-emit either of them as
standalone content after the table.
"""
doc = DoclingDocument(name="rich_cell_textitem_subtree")
table = doc.add_table(data=TableData(num_rows=1, num_cols=2))
rich_ref = doc.add_text(label=DocItemLabel.TEXT, text="CELL-TEXT", parent=table)
doc.add_text(label=DocItemLabel.TEXT, text="DEEP-LEAK", parent=rich_ref)
doc.add_table_cell(
table_item=table,
cell=RichTableCell(
start_row_offset_idx=0,
end_row_offset_idx=1,
start_col_offset_idx=0,
end_col_offset_idx=1,
ref=rich_ref.get_ref(),
text="cell 0,0",
),
)
doc.add_table_cell(
table_item=table,
cell=TableCell(
start_row_offset_idx=0,
end_row_offset_idx=1,
start_col_offset_idx=1,
end_col_offset_idx=2,
text="plain",
),
)
out = HTMLDocSerializer(doc=doc).serialize().text
body = out[out.find("<body>") : out.find("</body>") + len("</body>")]
table_end = body.find("</table>") + len("</table>")
inside_table = body[:table_end]
after_table = body[table_end:]