Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,42 +101,6 @@ for editor use (syntax highlighting, language injection, symbol navigation).

## Limitations

### Whitespace or newlines before branch/close tags are not preserved as `char_data`

Leading whitespace (newlines, spaces) immediately before closing paired tags such as
`{% elif %}`, or `{% endXXX %}` is consumed by the external scanner with
`skip=true` as part of establishing the correct GLR parse path. When the whitespace
immediately precedes one of these tags with nothing else on the same line, it does not
appear as a `char_data` node in the parse tree:

```django
{% if a %}
{% elif b %}...{% endif %}
{# the \n between if-body and {% elif %} is silently dropped #}

{% mytag %}
{% endmytag %}
{# the \n between open and close is silently dropped #}

<para>This is well{% if use_whitespace %} {% elif use_hyphen %}-{% endif %}known fact</para>
{# Resolves to "This is wellknown fact" when use_whitespace=True #}
```

When there is any other content on the same line before the branch/close tag, normal
`char_data` nodes are produced for the surrounding whitespace:

```django
{% if a %}
<x/>
{% elif b %}...{% endif %}
{# \n after {% if a %} and \n after <x/> both become char_data nodes #}
```

This is a known limitation of the GLR path-resolution mechanism in the scanner.
Removing the `skip=true` whitespace advance causes GLR to commit to the wrong parse
path for certain body content (e.g. `<!DOCTYPE>` nodes), so the current approach
is retained.

### Generic paired tag whose close tag appears only inside a comment body

If the only occurrence of `{% endXXX %}` in the remaining input sits inside a
Expand Down
14 changes: 10 additions & 4 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default grammar({
// Consumed at the closing `{% endtagname %}` when the tag name (minus the
// "end" prefix) matches the top of the scanner's stack.
$._dj_generic_close_tag,
// Character data that does not start with '{'.
$._char_data,
],

extras: $ => [
Expand Down Expand Up @@ -235,10 +237,14 @@ export default grammar({
// =========================================================================

// Only {% ... %} (statements), {# ... #} (comments), and {{ ... }}
// (expressions) break char_data. prec(1) outbids the /\s/ extra so that
// whitespace-only text (e.g. the space between </b> and </para>) is kept
// as a char_data node instead of being silently consumed.
char_data: _ => token(prec(1, /([^<&{]|\{[^{%#])+/)),
// (expressions) break char_data. Text is lexed by the external scanner
// so that whitespace-only text (e.g. the space in `<p> {{ a }}</p>`) is
// kept as a char_data node instead of being absorbed by a following { token.
// Text starting with '{' is left to the internal lexer.
char_data: $ => choice(
$._char_data,
token(prec(1, /\{[^{%#]([^<&{]|\{[^{%#])*/)),
),

cdata_section: $ => seq($.cdata_start, optional($.cdata), ']]>'),

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 21 additions & 8 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/node-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading