From b11a1400a2012f40db357d83342b3c183fa098a2 Mon Sep 17 00:00:00 2001 From: Fahmid Arman Date: Fri, 13 Nov 2026 21:33:31 +0600 Subject: [PATCH] fix(webvtt): escape cue text and voice names when exporting to WebVTT (#972) * fix(webvtt): escape cue text and voice names when exporting to WebVTT Signed-off-by: Fahmid Arman * test(webvtt): cover plain ampersand text and voice names in the export round trip Revert the serializer to the state on main, where the escaping was already added by #926, and keep the cases from the original change as parameters of test_webvtt_export_keeps_text_literal. Signed-off-by: Cesar Berrospi Ramis --------- Signed-off-by: Fahmid Arman Signed-off-by: Cesar Berrospi Ramis Co-authored-by: Cesar Berrospi Ramis --- tests/test_webvtt.py | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/tests/test_webvtt.py b/tests/test_webvtt.py index c865fd72..21c0529c 100644 --- a/tests/test_webvtt.py +++ b/tests/test_webvtt.py @@ -435,27 +435,34 @@ def test_webvtt_cue_html_character_references() -> None: assert str(vtt.cue_blocks[3]).rstrip("\n").endswith("R&foo;d &") -def test_webvtt_export_keeps_text_literal() -> None: - """Test that document text resembling a character reference is exported literally.""" +@pytest.mark.parametrize( + ("text", "voice"), + [ + ("Ask © and & me", "Tom & Jerry > Co"), + ("x < y > z", "Tom & Jerry > Co"), + ("Tom & Jerry", None), + ("Hello", "Tom & Jerry > Co"), + ], +) +def test_webvtt_export_keeps_text_literal(text: str, voice: str | None) -> None: + """Test that document text and voice names resembling markup or references are exported literally.""" doc = DoclingDocument(name="refs") - texts = ["Ask © and & me", "x < y > z"] - for idx, text in enumerate(texts): - doc.add_text( - label=DocItemLabel.TEXT, - text=text, - source=TrackSource(start_time=float(idx), end_time=idx + 1.0, voice="Tom & Jerry > Co"), - ) + doc.add_text( + label=DocItemLabel.TEXT, + text=text, + source=TrackSource(start_time=0.0, end_time=1.0, voice=voice), + ) vtt = WebVTTFile.parse(doc.export_to_vtt()) - assert len(vtt) == len(texts) - for block, text in zip(vtt.cue_blocks, texts): - voice = block.payload[0].component - assert isinstance(voice, WebVTTCueVoiceSpan) - assert voice.start_tag.annotation == "Tom & Jerry > Co" - span = voice.internal_text.components[0].component - assert isinstance(span, WebVTTCueTextSpan) - assert span.text == text + assert len(vtt) == 1 + component = vtt.cue_blocks[0].payload[0].component + if voice is not None: + assert isinstance(component, WebVTTCueVoiceSpan) + assert component.start_tag.annotation == voice + component = component.internal_text.components[0].component + assert isinstance(component, WebVTTCueTextSpan) + assert component.text == text def test_webvtt_cue_language_span_start_tag():