Fix #249: preserve whitespace around stripped block-level elements#265
Open
apoorvdarshan wants to merge 1 commit into
Open
Fix #249: preserve whitespace around stripped block-level elements#265apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
…el elements Whitespace immediately outside a block-level element is collapsed because a converted block already provides its own line separation. But when a block element such as <div> is stripped (or excluded via convert=[...]), it is emitted inline, so removing the surrounding whitespace runs the neighbouring words together (e.g. '<span>a <div>b</div> c</span>' with strip=['div'] became 'ab c' -> 'abc'). Only treat a block-level sibling as a whitespace boundary when it is actually being converted; a stripped sibling now keeps the separating whitespace. Added a regression test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #249. When a block-level element such as
<div>is stripped (viastrip=[...]) or excluded from aconvert=[...]whitelist, the whitespace separating it from its neighbouring text was dropped, running the words together.Root cause
process_text()removes whitespace immediately outside a block-level sibling (should_remove_whitespace_outside). That is correct for a converted block, which emits its own\n\nseparation. But a stripped block is emitted inline, so stripping the surrounding whitespace leaves no separator at all.Fix
Only treat a block-level sibling as a whitespace boundary when it is actually being converted. A new helper
_removes_adjacent_whitespace()combines the existing block-level check withshould_convert_tag(), so a stripped (inline-rendered) sibling now keeps the separating whitespace. Converted blocks are unaffected.The reporter's own workaround (overriding
convert_divto pad with spaces) left a trailing space; this approach avoids that because the stripped element's own leading/trailing whitespace is still normalised — only the inter-element separator is preserved.Verification
test_strip_block_element_preserves_surrounding_whitespacecovering bothstrip=['div']andconvert=['span']. It fails ondevelopand passes with this change.flake8 --ignore=E501,W503 markdownify testsclean.<p>a</p><p>b</p>→a\n\nb) and for stripped inline elements (strip=['b']still keeps spaces).Disclosure: prepared with AI assistance; reviewed and verified locally against the test suite.