Skip to content

Don't strip whitespace between multiple partials on one line - #852

Closed
afonsojanu wants to merge 1 commit into
janl:masterfrom
afonsojanu:fix/multiple-partials-same-line-whitespace
Closed

afonsojanu wants to merge 1 commit into
janl:masterfrom
afonsojanu:fix/multiple-partials-same-line-whitespace

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes #826.

stripSpace() decides a line is "standalone" (tag plus only whitespace, nothing else) by checking that the line has a tag and no other non-whitespace text. That's the right check for a line with a single tag, but it doesn't look at how many tags are actually on the line, so a line with several partials separated only by spaces gets the exact same treatment as a single standalone tag and loses the spaces between them:

mustache.render('{{> a}} {{> b}} {{> c}}', {}, {a: 'A', b: 'B', c: 'C'})
// 'ABC' instead of 'A B C'

I traced this back to the commit that added indentation support for standalone partials (69e2bf3, part of the 3.0.2 release) - it added an unconditional stripSpace() call at the end of parsing so a standalone tag at the very end of a template with no trailing newline gets trimmed too. That's a legitimate fix on its own (it's needed for the spec's "Standalone Without Newline" case), but it also exposed this pre-existing gap in the standalone check for the common "no trailing newline" case. Turns out the same gap already existed for lines that do end in a newline, just less visibly, since 3.0.1 didn't call stripSpace() at all for the final unterminated line.

The parser already tracks how many tags have been seen on the current line (tagIndex, used for the "is this the first tag on the line" indentation logic), so the fix just adds that count to the existing standalone check - it only strips surrounding whitespace when there's exactly one tag on the line.

Ran mustache.js's own official spec test fixtures (test/spec/specs/*.json, all 106 non-lambda, non-"Without Newline" cases matching this repo's own skip list) directly against the patch - all pass, nothing regresses. Also ran every existing case in test/partial-test.js by hand, one by one, same result. I couldn't get the mocha suite itself running here (the esm dependency in test/helper.js doesn't load on a current Node), so I verified this way instead rather than skip verification.

Added a test to test/partial-test.js for the exact case from the issue - confirmed it fails on the old code and passes with the fix.

stripSpace() treats any line that has a tag and no other non-whitespace
text as a standalone line, so its surrounding whitespace gets removed.
That's correct when the line really only has one tag on it, but the
check never looked at how many tags were actually on the line, so a
line with several partials separated only by spaces got the same
treatment and lost the spaces between them.

The tag count for the current line was already being tracked (it's
used to know whether a partial is the first tag on its line, for
indentation purposes), so this just adds that same count to the
existing condition. A standalone line now has to have exactly one
tag, matching what the spec actually describes.

Fixes #826.

@vibhor-aggr vibhor-aggr 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.

Automated review assisted by OpenAI Codex. Executed against exact head fdd46a125c436de5aaaad9d506ec34483a3a0c25; not a human approval or security audit.

The spacing fix interacts with renderPartial's indentation handling. Once the outer whitespace is retained, the first partial still gets that indentation added again:

Mustache.render('  {{>a}} {{>b}}\n', {}, {a: 'A', b: 'B'});
// actual:   '    A B\n'
// expected: '  A B\n'

Mustache.render('  {{>a}} {{>b}}\n', {}, {a: 'A\nAA', b: 'B'});
// actual:   '    A\n  AA B\n'
// expected: '  A\nAA B\n'

This duplicates the space/tab prefix and also applies standalone indentation to multiline inline content. Upstream 972fd2b27a036888acfcb60d6119317744fac7ee does not duplicate the leading prefix, although its missing inter-partial whitespace is the original bug being fixed.

Counting all tags also changes empty-section whitespace, unrelated to partials. For example, Mustache.render('a\n{{#a}}{{/a}}\nb', {a: true}) changes from 'a\nb' to 'a\n\nb'. I ran the existing parser/context/partial/scanner/render tests: this head gives 171 passing, 4 failing, versus 174 passing upstream. Three failures are unchanged empty-section token expectations; the fourth is the multiple-partial expectation that legitimately needs updating for retained whitespace.

I validated a separate correction that counts partials instead of all tags, and clears the first partial's standalone indentation when a multi-partial line retains outer whitespace. It preserves single standalone partials, empty sections, and the project's existing explicit-text multiline behavior. Six repository regressions cover space/tab prefixes and LF/CRLF/EOF; only the intended multiple-partial token expectation is updated, leaving the three empty-section expectations unchanged.

Final correction validation: 181 selected repository tests passing, plus 37 independent checks passing. The independent cases include the 24 indentation/complete-output cases, six empty-section controls, and seven standalone/legacy/original-spacing controls. On this PR, 30 of those independent checks fail and seven pass; all 24 upstream-applicable controls pass upstream.

Environment: Node 24.16.0 on Linux; actual source imported directly, no source transformation or npm installation/build hooks. The inspected helper's global/bootstrap setup was replaced in the task launcher to avoid the old esm loader, using existing Mocha 10.8.2/Chai 5.3.3 rather than the project's older dependency versions. Repository-config lint passes with existing ESLint 5.16.0; diff check passes. Original npm pipeline, CLI/browser/bundle, spec submodule and older Node versions are not claimed. Tested suggestion: local commit d6231142a40769aa4bbbb7dbd6a53336a24fba36; no competing PR or author-branch push.

@afonsojanu

Copy link
Copy Markdown
Author

Part of a general cleanup on my end, closing this along with the rest of what I had open.

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.

Partials used with object of partials removes space

2 participants