From b3eb29b31622d5d987175c732a9134c11323eb77 Mon Sep 17 00:00:00 2001 From: Arsh624 Date: Fri, 13 Nov 2026 12:05:12 -0800 Subject: [PATCH 1/2] fix(webvtt): keep the line break after a span end tag When a cue line ended with an end tag, the text after the tag started with the line break and an empty first line. `_create_text_components` skips empty lines, so the line terminator was lost and the next line was joined to the span: `a` and `b` on two lines became `ab`. Put that terminator on the span that just closed, which is where the model keeps a line break that follows a component. Signed-off-by: Arsh624 --- docling_core/types/doc/webvtt.py | 13 +++++++++---- tests/test_webvtt.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/docling_core/types/doc/webvtt.py b/docling_core/types/doc/webvtt.py index 068575751..85d1e548e 100644 --- a/docling_core/types/doc/webvtt.py +++ b/docling_core/types/doc/webvtt.py @@ -595,14 +595,20 @@ def _close_top_span() -> None: closed = tag_stack.pop() stack[-1].append(WebVTTCueComponentWithTerminator(component=cls._create_span(closed, children))) + def _add_text(text: str) -> None: + # A line break right after an end tag belongs to the span that just closed. + components = stack[-1] + if text.startswith("\n") and components and components[-1].terminator is None: + components[-1].terminator = WebVTTLineTerminator.LF + components.extend(cls._create_text_components(text)) + pos = 0 matches = list(cls._pattern_tag.finditer(cue_text)) i = 0 while i < len(matches): match = matches[i] if match.start() > pos: - text = cue_text[pos : match.start()] - stack[-1].extend(cls._create_text_components(text)) + _add_text(cue_text[pos : match.start()]) gps = {k: (v if v else None) for k, v in match.groupdict().items()} # Following WebVTT 1, 6.4, only applies directly inside ; otherwise its @@ -625,8 +631,7 @@ def _close_top_span() -> None: i += 1 if pos < len(cue_text): - text = cue_text[pos:] - stack[-1].extend(cls._create_text_components(text)) + _add_text(cue_text[pos:]) # Close any remaining unclosed spans at the end of the cue. while tag_stack: diff --git a/tests/test_webvtt.py b/tests/test_webvtt.py index a88c6f2eb..3c019d36c 100644 --- a/tests/test_webvtt.py +++ b/tests/test_webvtt.py @@ -654,3 +654,18 @@ def test_webvtt_file_cues_out_of_start_time_order() -> None: # A model built directly still requires the cues to be in order. with pytest.raises(ValidationError): WebVTTFile(cue_blocks=[vtt[3], vtt[0]]) + + +@pytest.mark.parametrize( + "cue_text", + [ + "Previously on the show\nwe met the team.", + "Hello there\nHi Ann", + "Text bold\nitalic text", + ], +) +def test_webvtt_parse_line_break_after_end_tag(cue_text: str) -> None: + """A line that ends with an end tag keeps its line terminator.""" + timings = "00:01.000 --> 00:02.000\n" + block = WebVTTCueBlock.parse(timings + cue_text) + assert str(block) == timings + cue_text + "\n" From a2824d2d57d9af988737fc86b257d30099fbba2d Mon Sep 17 00:00:00 2001 From: Arsh624 Date: Fri, 27 Nov 2026 01:09:00 -0800 Subject: [PATCH 2/2] test(webvtt): cover a ruby span and a stray end tag before a line break Signed-off-by: Arsh624 --- tests/test_webvtt.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_webvtt.py b/tests/test_webvtt.py index 3c019d36c..7e8e1cb84 100644 --- a/tests/test_webvtt.py +++ b/tests/test_webvtt.py @@ -662,6 +662,7 @@ def test_webvtt_file_cues_out_of_start_time_order() -> None: "Previously on the show\nwe met the team.", "Hello there\nHi Ann", "Text bold\nitalic text", + "ab\nc", ], ) def test_webvtt_parse_line_break_after_end_tag(cue_text: str) -> None: @@ -669,3 +670,9 @@ def test_webvtt_parse_line_break_after_end_tag(cue_text: str) -> None: timings = "00:01.000 --> 00:02.000\n" block = WebVTTCueBlock.parse(timings + cue_text) assert str(block) == timings + cue_text + "\n" + + +def test_webvtt_parse_line_break_after_stray_end_tag() -> None: + """A stray end tag is ignored, and the line break after it is kept.""" + block = WebVTTCueBlock.parse("00:01.000 --> 00:02.000\na\nb") + assert str(block) == "00:01.000 --> 00:02.000\na\nb\n"