handle content edge cases

This commit is contained in:
danhanson committed 2026-09-14 21:40:26 -05:00
1 parent fa0d880e68
commit ccb80972b6
7 files changed
+48630 -46855

No files matched your search

+24
View File
@@ -326,6 +326,26 @@
]
},
"content": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_text"
},
{
"type": "SYMBOL",
"name": "_text_brace"
}
]
}
}
},
"_text": {
"type": "PATTERN",
"value": "(?:[^\\{]|\\{[^\\{#%])+"
},
@@ -8107,6 +8127,10 @@
{
"type": "SYMBOL",
"name": "_group_held"
},
{
"type": "SYMBOL",
"name": "_text_brace"
}
],
"inline": [
+5 -4
View File
@@ -712,6 +712,11 @@
]
}
},
{
"type": "content",
"named": true,
"fields": {}
},
{
"type": "csp_nonce_attr_tag",
"named": true,
@@ -4045,10 +4050,6 @@
"type": "comment_content",
"named": true
},
{
"type": "content",
"named": true
},
{
"type": "context",
"named": false
+48467 -46849
View File
File diff suppressed because it is too large. Load diff
+37
View File
@@ -57,6 +57,9 @@ enum TokenType {
* the innermost group already holds a tag by that name, which makes this the
* likelier stray of the two */
GroupHeld,
/* a "{" that is text although another "{" follows it, because the second
* one opens a tag or a comment, or although nothing follows it at all */
TextBrace,
};
/* The options blocktranslate takes, in the order of the bits recording them.
@@ -773,8 +776,33 @@ typedef enum {
/* no marker, but input was read to decide that, so nothing else may be
* scanned from here */
NotMissingAfterReading,
/* no marker, but the "{" read to decide that was text, and was scanned */
TextBraceScanned,
} MissingResult;
/* Scans TextBrace once its "{" has been read. Django's lexer takes a template
* apart with ({%.*?%}|{{.*?}}|{#.*?#}), so a "{" is text unless one of those
* starts at it: before "{%" or "{#" the tag or comment starts one character
* later, and at the end of input nothing starts at all. The content regex
* cannot look that far ahead, so these are scanned here; "{{{" is not one of
* them, since "{{" does start a variable at its first brace. */
static bool scan_text_brace_rest(TSLexer *const lexer) {
lexer->mark_end(lexer);
if (lexer->eof(lexer)) {
lexer->result_symbol = TextBrace;
return true;
}
if (lexer->lookahead != '{') {
return false;
}
lexer->advance(lexer, false);
if (lexer->lookahead == '%' || lexer->lookahead == '#') {
lexer->result_symbol = TextBrace;
return true;
}
return false;
}
/* Scans the zero-width marker for the innermost group's missing tag. The
* marker is valid only where that group could close, which is after a body,
* where nothing else of this scanner is valid but the raw-text tokens, and
@@ -809,6 +837,10 @@ static MissingResult scan_missing_tag(
}
lexer->advance(lexer, false);
if (lexer->lookahead != '%') {
// not a tag, but the "{" may be text the grammar still has to take
if (valid_symbols[TextBrace] && scan_text_brace_rest(lexer)) {
return TextBraceScanned;
}
return NotMissingAfterReading;
}
lexer->advance(lexer, false);
@@ -950,6 +982,7 @@ bool tree_sitter_django_external_scanner_scan(
if (valid_symbols[MissingTag]) {
switch (scan_missing_tag(scanner, lexer, valid_symbols)) {
case Missing:
case TextBraceScanned:
return true;
case NotMissingAfterReading:
return false;
@@ -957,6 +990,10 @@ bool tree_sitter_django_external_scanner_scan(
break;
}
}
if (valid_symbols[TextBrace] && lexer->lookahead == '{') {
lexer->advance(lexer, false);
return scan_text_brace_rest(lexer);
}
if (valid_symbols[GroupOpenTagRead]) {
scan_group_open_tag_read(scanner, lexer);
return true;