Skip to content

fix: stop SAMI and DFXP writers doubling spaces around styled spans - #470

Open
kolinabir wants to merge 1 commit into
pbs:mainfrom
kolinabir:fix/inline-style-spacing
Open

kolinabir wants to merge 1 commit into
pbs:mainfrom
kolinabir:fix/inline-style-spacing

Conversation

@kolinabir

Copy link
Copy Markdown

Description

Fixes #467

A caption with an inline-styled word picked up an extra space around the styled span each time it went through SAMIWriter or DFXPWriter, so Hello <b>world</b> end became Hello <b>world</b> end, then three spaces on the next round trip, and so on.

Both writers add a separator space of their own: SAMIWriter after every text node and after a closing tag, DFXPWriter after 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_spans expected That 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 TestInlineStyleSpacing in tests/test_webvtt_conversion.py:

  • test_sami_writer_does_not_double_spaces and test_dfxp_writer_does_not_double_spaces: the cue from the issue, written once
  • test_spacing_is_stable_across_round_trips: three WebVTT -> SAMI/DFXP -> WebVTT round trips, for both formats
  • test_separator_is_still_added_between_unspaced_nodes: nodes without their own whitespace get the same output as before

The first four cases fail on main and 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 passed on main, the difference is the new tests). ruff check and ruff format --check are 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 main as well as on this branch (mostly KeyError: 'tts:origin' in the DFXP tests). test_requirements.txt has lxml>=4.9.1 with no upper bound, so a fresh install picks 6.2. I can open a separate issue for it if that's useful.

Checklist

  • New tests included
  • All tests pass locally

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 pbs#467
@kolinabir
kolinabir requested a review from a team as a code owner November 27, 2026 17:33
Copilot AI balanced review requested due to automatic review settings November 27, 2026 17:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inline-styled captions gain an extra space around every styled span on each conversion (grows every round-trip)

2 participants