Skip to content

Parse whitespace-only text as char_data - #59

Merged
hewdoe merged 2 commits into
mainfrom
fix-whitespace-before-django-delimiters
Nov 18, 2026
Merged

hewdoe merged 2 commits into
mainfrom
fix-whitespace-before-django-delimiters

Conversation

@hewdoe

@hewdoe hewdoe commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

You may be familiar with the limitation in this grammar where whitespace is folded into the following Django delimiter, causing whitespace-only text to effectively get lost if not handled explicitly. A sequence like {{ a }} {{ b }} would be parsed into 2 nodes: (Django expression: {{ a }}) and {Django expression: {{ b }}).
This whitespace is ultimately char data, whether it appears by itself or within a larger text segment.
The grammar already acknowledged that whitespace is char_data, but when its followed by the opening of a new tag {, the standard lexer will always favour the longer match, which would treat the whitespace as part of the opening tag (its valid Django syntax to have any amount of whitespace chars between delimiters/args/tags, it is Python after all).
So we get around this by defining a char data scanner in scanner.c. A reminder that these scanner rules take precedence (assuming we tell them to) over the standard length matches. char data matched via the scanner now reads up to the next opening tag and emits the char_data token.

A happy bonus: this also now resolves the limitation we had that would skip over whitespace/newline that appeared before branching/closing tags. Indentation is now preserved! (See the README limitation that's now been removed)

We're doing this to override the grammar rule that would otherwise preference a longer match (like consuming a following django expression)
@hewdoe
hewdoe requested a balanced review from Copilot November 18, 2026 05:12
@hewdoe hewdoe self-assigned this Nov 18, 2026
@hewdoe
hewdoe requested a review from a team as a code owner November 18, 2026 05:12
(name))))
(end_tag
(name)))
(char_data)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of the newline (after the </document> element) now being registered as its own node

Copilot AI 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.

🟢 Approval recommended

No active review issues remain for the XML/Django whitespace-preservation changes.

No open issues 🎉

What changed in this PR

This PR updates the XML/Django grammar to preserve whitespace-only text as char_data in element content and Django bodies, including indentation before branch and closing tags.

Changes:

  • Adds external character-data scanning to prevent whitespace from being absorbed into Django delimiters.
  • Expands corpus coverage and updates expected trees for preserved whitespace.
  • Removes the resolved whitespace limitation from the README and bumps the package to 0.7.4.
File Description
uv.lock Updates the local package version to 0.7.4.
test/​corpus/​xml.txt Expects preserved whitespace before a Django closing tag.
test/​corpus/​mixed.txt Updates whitespace expectations in mixed XML/Django content.
test/​corpus/​django_statements.txt Adds whitespace regressions for statements, comments, and nested blocks.
test/​corpus/​django_statements_paired.txt Covers indented comments, branch boundaries, and custom paired tags.
test/​corpus/​django_expressions.txt Covers expression-adjacent whitespace and document-level behavior.
src/​scanner.c Scans character data before whitespace-skipping tag recognition.
src/​node-types.json Updates generated metadata for the revised char_data rule.
src/​grammar.json Reflects the external token and internal text fallback.
README.md Removes the resolved whitespace-preservation limitation.
pyproject.toml Bumps the package version to 0.7.4.
grammar.js Routes character data through the scanner, retaining a fallback for text starting with {.

Review effort: Balanced

@laurentputz laurentputz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! It's handy to get this sorted, and even better that it helps with preserving some of the formatting of the template.

❓ It's okay if we don't know the answer, but to what extend does this solve the problem where the parse&unparsed template follows vastly different formatting to the original template? Is it only solving the formatting for certain nodes, or throughout the template?

@hewdoe

hewdoe commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

to what extend does this solve the problem where the parse&unparsed template follows vastly different formatting to the original template? Is it only solving the formatting for certain nodes, or throughout the template?

Nah this wouldn't resolve the formatting mismatch. char_data has to exist within RML content. Most of the indentation in a file exists outside of content Nodes, so are not preserved (eg. between a sequence of load lines at the top of the file there are newlines that are not char_data, so after parsing and unparsing the load lines will all be on a single line).
I don't believe the AST should be responsible for keeping track of indentation, rather I think committing to the "pretty print" script that runs after every template change that I tried drawing up on a SPA day here.
Not super important for now anyway, something to consider in the next iteration

@hewdoe
hewdoe merged commit 9c86a22 into main Nov 18, 2026
7 checks passed
@hewdoe
hewdoe deleted the fix-whitespace-before-django-delimiters branch November 18, 2026 23:39
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.

3 participants