Repository navigation
fix: stop SAMI and DFXP writers doubling spaces around styled spans - #470
Conversation
Both writers added a separator space after text nodes and closing tags even when the neighbouring text node already carried its own whitespace, so a styled word gained one more space on every conversion. The separator is now deferred and only written when neither side has whitespace at that position. Nodes without whitespace of their own get the same output as before. Fixes #467
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
OlteanuRares
left a comment
There was a problem hiding this comment.
Thanks for this, @kolinabir. It's a clear write-up, and the fix is nicely contained.
We reviewed it and are happy to merge. The full suite passes. We also compared SAMI and DFXP writer output on main and on your branch across all the fixtures. Every change we found was a doubled space next to a style tag becoming a single space, and nothing got worse. Unspaced nodes still get their separator, as you described.
We'll handle the version bump and changelog entry for this fix in our next release ticket.
Thanks also for flagging the lxml 6.2 failures.
Description
Fixes #467
A caption with an inline-styled word picked up an extra space around the styled span each time it went through
SAMIWriterorDFXPWriter, soHello <b>world</b> endbecameHello <b>world</b> end, then three spaces on the next round trip, and so on.Both writers add a separator space of their own:
SAMIWriterafter every text node and after a closing tag,DFXPWriterafter a closing</span>. That's needed when the nodes carry no whitespace (Hello,world,end), but text nodes read from WebVTT, SAMI or DFXP already keep the whitespace they had ('Hello ',' end'), so the separator landed on top of it.The separator is now only written when neither side already has whitespace there. Instead of appending
" "straight away, the writers remember that one is owed and add it when the next piece of the line is written, if it's still needed. The rest of the output is unchanged: unspaced nodes still get their separator, and trailing whitespace before a closing tag or a<br/>is still stripped.One fixture changes.
sample_sami_from_dfxp_with_nested_spansexpectedThat is <span ...>with two spaces, where the DFXP it's converted from has one, so it had the bug baked in.Not changed here: a closing tag followed directly by punctuation still gets a separator (
nested</span></span></span>.in DFXP comes out as...</span> .in SAMI). That's the existing behaviour for unspaced nodes and a different question from the doubling, so I left it alone.Testing
New
TestInlineStyleSpacingintests/test_webvtt_conversion.py:test_sami_writer_does_not_double_spacesandtest_dfxp_writer_does_not_double_spaces: the cue from the issue, written oncetest_spacing_is_stable_across_round_trips: three WebVTT -> SAMI/DFXP -> WebVTT round trips, for both formatstest_separator_is_still_added_between_unspaced_nodes: nodes without their own whitespace get the same output as beforeThe first four cases fail on
mainand pass here. The last two pass on both, they're there to pin the behaviour that shouldn't move.Full suite on Python 3.12 with lxml 5.4:
1428 passed(1422 passedonmain, the difference is the new tests).ruff checkandruff format --checkare clean on the touched files.One thing I ran into that isn't related to this PR: with lxml 6.2 installed, 40 tests fail on
mainas well as on this branch (mostlyKeyError: 'tts:origin'in the DFXP tests).test_requirements.txthaslxml>=4.9.1with no upper bound, so a fresh install picks 6.2. I can open a separate issue for it if that's useful.Checklist