Skip to content

fix: Handle empty strings in StrictWhitespaceControlParser#1319

Merged
rewaddell merged 5 commits into
masterfrom
rw-correctly-parse-empty-string
Jun 22, 2026
Merged

fix: Handle empty strings in StrictWhitespaceControlParser#1319
rewaddell merged 5 commits into
masterfrom
rw-correctly-parse-empty-string

Conversation

@rewaddell

@rewaddell rewaddell commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

What

Add empty-string guards to StrictWhitespaceControlParser so hasLeftTrim,
hasRightTrim, stripLeft, and stripRight no longer index into an empty
token.

Why

hasLeftTrim called unwrapped.charAt(0) and hasRightTrim called
unwrapped.charAt(unwrapped.length() - 1) with no empty-string guard. Any
template parsed with withParseWhitespaceControlStrictly(true) that produces
an empty unwrapped token (e.g. {{}}) threw StringIndexOutOfBoundsException
during parsing.

The lenient parser already handles this gracefully — WhitespaceUtils.startsWith
/ endsWith return false for null/empty input. This change gives the strict
parser the same defensive guarantee, so the two implementations behave
consistently on empty tokens.

Fixing it here protects every consumer of the strict parser rather than
sanitizing input in a single downstream call path.

Behavior change

  • {{}} now renders to an empty string instead of throwing.
  • An empty tag token ({%%}) still errors, but with the expected Unknown tag
    error rather than a StringIndexOutOfBoundsException — correct downstream
    behavior, unchanged in intent.

Tests

  • StrictWhitespaceControlParserTest: empty token returns false from the
    hasX methods and is returned unchanged by the stripX methods; non-empty
    trim markers still detected and stripped.
  • LegacyWhitespaceControlParsingTest.itHandlesEmptyExpressionToken: {{}}
    renders to "" in strict mode.

PR description authored by Claude Code.

@rewaddell rewaddell marked this pull request as ready for review June 22, 2026 14:20
@rewaddell rewaddell merged commit 77ba2a2 into master Jun 22, 2026
7 checks passed
@rewaddell rewaddell deleted the rw-correctly-parse-empty-string branch June 22, 2026 15:22
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.

2 participants