Skip to content

Collapse whitespace between adjacent TextNodes (#2347) - #2349

Merged
jhy merged 2 commits into
jhy:masterfrom
GeorgEchterling:2347-pretty-print-whitespace
Jun 27, 2025
Merged

jhy merged 2 commits into
jhy:masterfrom
GeorgEchterling:2347-pretty-print-whitespace

Conversation

@GeorgEchterling

Copy link
Copy Markdown
Contributor

Fixes #2347

I'm not sure if this can be fixed more elegantly than looping over sequential text nodes. I'd prefer handling multiple text nodes in a single call to Entities.doEscape, but that would involve larger code changes.

@GeorgEchterling

Copy link
Copy Markdown
Contributor Author

I accidentally auto-formatted a file and introduced a bug during refactoring. These issues should both be fixed now.

I'll take a look at the failing tests. I hadn't run all tests locally because I keep getting compilation errors in the java11 sources:

[ERROR] /home/gechterling/dev/open-source/jsoup/src/main/java11/org/jsoup/helper/HttpClientExecutor.java:[3,17] cannot find symbol
  symbol:   class Connection
  location: package org.jsoup
... (and many similar errors)

@GeorgEchterling

Copy link
Copy Markdown
Contributor Author

Nevermind, mvn clean fixed the compilation errors.

@GeorgEchterling
GeorgEchterling force-pushed the 2347-pretty-print-whitespace branch from fe8cc29 to 94a29cc Compare June 26, 2025 14:30
@GeorgEchterling

Copy link
Copy Markdown
Contributor Author

The failing test case was NodeTest.unwrapNoChildren. It essentally checked the opposite of my expectation that common whitespace between subsequent text nodes should be collapsed. From the name of the test, I'm assuming that this was not the intended purpose. I've changed the test to expect whitespace to collapse.

@jhy

jhy commented Jun 27, 2025

Copy link
Copy Markdown
Owner

Thanks, looks good! I simplified the impl to use the existing nextNonBlank method.

I'd prefer handling multiple text nodes in a single call to Entities.doEscape

We actually used to do that something like that, by peeking into the last character of the Appender, and then not trimming leading. That only worked if the Appender was a StringBuilder. I guess we could track it now on QuiteAppender#append, but it seems likes a lot of overhead. I think this flow works well in the rest of the context of the pretty printer.

@jhy
jhy merged commit 8363915 into jhy:master Jun 27, 2025
@jhy jhy added the improvement An improvement / new feature idea label Jun 27, 2025
@jhy jhy added this to the 1.21.2 milestone Jun 27, 2025
@GeorgEchterling
GeorgEchterling deleted the 2347-pretty-print-whitespace branch June 27, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement An improvement / new feature idea

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pretty printing consecutive text nodes should collapse consecutive whitespace

2 participants