Repository navigation
fix: don't indent structuredText blocks that follow whitespace-only text - #329
Open
OsamaAnsar wants to merge 1 commit into
Open
OsamaAnsar wants to merge 1 commit into
OsamaAnsar wants to merge 1 commit into
Conversation
A whitespace-only text node set prependWhitespace even when the current
block was still empty, and block lines were never trimmed. With
pretty-printed HTML every block after a whitespace node therefore got a
leading space ('<p>a</p> <p>b</p>' gave 'a\n b'), and a space left before
a <br> stayed as a trailing space ('a \n b').
Only remember the pending whitespace when the block already has content,
and trim each line after normalising its whitespace.
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.
structuredTextputs a leading space on every block that follows a whitespace-only text node, which is what pretty-printed HTML produces.Cause: the whitespace-only branch in
structuredTextsetprependWhitespaceeven when the current block was still empty, so the first text of the next block got aprefix. Block lines were also never trimmed, so a space before a<br>stayed as a trailing space.Fix: only remember pending whitespace when the block already has content, and
.trim()each line after the existing whitespace normalisation.One behaviour change beyond the pretty-printed case: since lines are now trimmed, a single leading/trailing space inside a line is dropped for compact HTML too (
<p> a</p>gave" a", now"a";<p>a </p>bgave"a \nb", now"a\nb"). Output for inputs without such spaces is unchanged, and the existingstructuredTexttests pass untouched. I took that to be the same bug, but say so if you would rather keep it.Tests: 4 added next to the existing
#structuredTexttests intest/tests/html.js(pretty-printed list/headings case,<p>a</p> <p>b</p>,<br>, and a guard that a single space between inline elements is kept). With only thesrc/change reverted all 4 fail; with it they pass.How I ran things: bun is not installed here, so I bundled
src/index.tswith esbuild (css-select 5.2.2 and entities 8.0.0, as inbun.lock) and ran mocha on Node 22 against that bundle, skippingissues/280.js(it needs the published npm packages).main: 303 passing, 4 failing. This branch: 307 passing, same 4 failing. The 4 failures are artifacts of my setup, not of this change: the twoModule Importtests and the twounclosed tag repairsubprocess tests needts-node/ a builtdist.tsc --noEmitis clean.Not tested:
bun run build/test:distagainst a realdist, eslint, Node 24/26.