Repository navigation
Parse whitespace-only text as char_data - #59
Conversation
We're doing this to override the grammar rule that would otherwise preference a longer match (like consuming a following django expression)
| (name)))) | ||
| (end_tag | ||
| (name))) | ||
| (char_data) |
There was a problem hiding this comment.
Example of the newline (after the </document> element) now being registered as its own node
There was a problem hiding this comment.
🟢 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
left a comment
There was a problem hiding this comment.
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?
Nah this wouldn't resolve the formatting mismatch. |
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 thechar_datatoken.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)