From ad9d6e56b05a7f45c1a7adff8b7aec2eabc93fb5 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 6 Jan 2025 11:06:26 -0600 Subject: [PATCH] checkpoint --- crates/djls-template-ast/src/parser.rs | 23 +- ...rser__tests__comments__parse_comments.snap | 8 +- ...r__tests__django__parse_mixed_content.snap | 4 +- ...ests__django__parse_mixed_content.snap.new | 259 ------------------ ...__tests__errors__parse_error_recovery.snap | 16 +- ...sts__errors__parse_error_recovery.snap.new | 138 ---------- ...er__tests__full_templates__parse_full.snap | 66 ++--- ...tests__full_templates__parse_full.snap.new | 259 ------------------ ...__parser__tests__script__parse_script.snap | 24 +- ...rser__tests__script__parse_script.snap.new | 44 --- ...st__parser__tests__style__parse_style.snap | 14 +- ...parser__tests__style__parse_style.snap.new | 44 --- crates/djls-template-ast/src/tokens.rs | 15 +- 13 files changed, 83 insertions(+), 831 deletions(-) delete mode 100644 crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap.new delete mode 100644 crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap.new delete mode 100644 crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap.new delete mode 100644 crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap.new delete mode 100644 crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap.new diff --git a/crates/djls-template-ast/src/parser.rs b/crates/djls-template-ast/src/parser.rs index 8ec6b39..be50fb7 100644 --- a/crates/djls-template-ast/src/parser.rs +++ b/crates/djls-template-ast/src/parser.rs @@ -261,12 +261,10 @@ impl Parser { let start_token = self.peek_previous()?; let start_pos = start_token.start().unwrap_or(0); - // If we start with a newline, skip it if matches!(start_token.token_type(), TokenType::Newline) { return self.next_node(); } - // Use TokenType's Display implementation for formatting let mut text = start_token.token_type().to_string(); let mut total_length: u32 = u32::try_from(text.len()).unwrap(); @@ -286,7 +284,6 @@ impl Parser { } } - // Skip empty text nodes if text.trim().is_empty() { self.next_node() } else { @@ -303,14 +300,18 @@ impl Parser { start: &str, end: Option<&str>, ) -> Result { - let start_token = self.peek_previous()?; - let start_pos = start_token.start().unwrap_or(0); - let total_length = (content.len() + start.len() + end.map_or(0, |e| e.len())) as u32; - let span = Span::new(start_pos, total_length); - Ok(Node::Comment { - content: content.to_string(), - span, - }) + let token = self.peek_previous()?; + let start_pos = token.start().unwrap_or(0); + + // Only treat Django comments as Comment nodes + if start == "{#" && end == Some("#}") { + Ok(Node::Comment { + content: content.to_string(), + span: Span::new(start_pos, token.token_type().len().unwrap_or(0) as u32), + }) + } else { + self.parse_text() + } } fn peek(&self) -> Result { diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap index b872009..296fda7 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap @@ -3,15 +3,15 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Comment: - content: HTML comment + - Text: + content: "" span: start: 0 - length: 19 + length: 21 - Comment: content: Django comment span: start: 21 - length: 18 + length: 20 line_offsets: - 0 diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap index 7353de2..ec83ae1 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap @@ -188,7 +188,7 @@ nodes: content: " (no groups)" span: start: 290 - length: 20 + length: 19 closing: Closing: tag: @@ -221,7 +221,7 @@ nodes: content: " Guest" span: start: 338 - length: 10 + length: 9 closing: Closing: tag: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap.new b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap.new deleted file mode 100644 index 0d2e194..0000000 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap.new +++ /dev/null @@ -1,259 +0,0 @@ ---- -source: crates/djls-template-ast/src/parser.rs -assertion_line: 516 -expression: ast -snapshot_kind: text ---- -nodes: - - Text: - content: "Welcome, " - span: - start: 0 - length: 9 - - Block: - Block: - tag: - name: if - bits: - - if - - user.is_authenticated - span: - start: 9 - length: 24 - tag_span: - start: 9 - length: 24 - assignment: ~ - nodes: - - Variable: - bits: - - user - - name - filters: - - name: title - args: [] - span: - start: 48 - length: 31 - - name: default - args: - - "'Guest'" - span: - start: 48 - length: 31 - span: - start: 47 - length: 31 - - Block: - Block: - tag: - name: for - bits: - - for - - group - - in - - user.groups - span: - start: 86 - length: 24 - tag_span: - start: 86 - length: 24 - assignment: ~ - nodes: - - Block: - Block: - tag: - name: if - bits: - - if - - forloop.first - span: - start: 125 - length: 16 - tag_span: - start: 125 - length: 16 - assignment: ~ - nodes: - - Text: - content: ( - span: - start: 147 - length: 1 - closing: - Closing: - tag: - name: endif - bits: - - endif - span: - start: 148 - length: 5 - tag_span: - start: 148 - length: 5 - assignment: ~ - assignments: ~ - - Variable: - bits: - - group - - name - filters: [] - span: - start: 171 - length: 10 - - Block: - Block: - tag: - name: if - bits: - - if - - not - - forloop.last - span: - start: 193 - length: 19 - tag_span: - start: 193 - length: 19 - assignment: ~ - nodes: - - Text: - content: ", " - span: - start: 218 - length: 2 - closing: - Closing: - tag: - name: endif - bits: - - endif - span: - start: 220 - length: 5 - tag_span: - start: 220 - length: 5 - assignment: ~ - assignments: ~ - - Block: - Block: - tag: - name: if - bits: - - if - - forloop.last - span: - start: 240 - length: 15 - tag_span: - start: 240 - length: 15 - assignment: ~ - nodes: - - Text: - content: ) - span: - start: 261 - length: 1 - closing: - Closing: - tag: - name: endif - bits: - - endif - span: - start: 262 - length: 5 - tag_span: - start: 262 - length: 5 - assignment: ~ - assignments: ~ - - Block: - Branch: - tag: - name: empty - bits: - - empty - span: - start: 278 - length: 5 - tag_span: - start: 278 - length: 5 - assignment: ~ - nodes: - - Text: - content: " (no groups)" - span: - start: 290 - length: 19 - closing: - Closing: - tag: - name: endfor - bits: - - endfor - span: - start: 314 - length: 6 - tag_span: - start: 314 - length: 6 - assignment: ~ - assignments: ~ - - Block: - Branch: - tag: - name: else - bits: - - else - span: - start: 327 - length: 4 - tag_span: - start: 327 - length: 4 - assignment: ~ - nodes: - - Text: - content: " Guest" - span: - start: 338 - length: 9 - closing: - Closing: - tag: - name: endif - bits: - - endif - span: - start: 348 - length: 5 - tag_span: - start: 348 - length: 5 - assignment: ~ - assignments: ~ - - Text: - content: "!" - span: - start: 359 - length: 1 -line_offsets: - - 0 - - 40 - - 82 - - 117 - - 160 - - 185 - - 232 - - 274 - - 290 - - 310 - - 327 - - 338 - - 348 diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap index 6f52c13..ea238a9 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap @@ -7,12 +7,12 @@ nodes: content: "
" span: start: 0 - length: 24 + length: 23 - Text: content: "

Header

" span: start: 24 - length: 20 + length: 19 - Block: Block: tag: @@ -32,7 +32,7 @@ nodes: content: This if is unclosed which does matter span: start: 87 - length: 41 + length: 43 - Text: content: "

Welcome " span: @@ -50,17 +50,17 @@ nodes: content: "

" span: start: 165 - length: 5 + length: 4 - Text: content: "
" span: start: 170 - length: 14 + length: 13 - Comment: content: "This div is unclosed which doesn't matter" span: start: 196 - length: 45 + length: 47 - Block: Block: tag: @@ -94,7 +94,7 @@ nodes: content: "" span: start: 304 - length: 8 + length: 7 closing: Closing: tag: @@ -113,7 +113,7 @@ nodes: content: "
Page Footer
" span: start: 333 - length: 33 + length: 32 - Text: content: "
" span: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap.new b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap.new deleted file mode 100644 index cb59410..0000000 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap.new +++ /dev/null @@ -1,138 +0,0 @@ ---- -source: crates/djls-template-ast/src/parser.rs -assertion_line: 637 -expression: ast -snapshot_kind: text ---- -nodes: - - Text: - content: "
" - span: - start: 0 - length: 23 - - Text: - content: "

Header

" - span: - start: 24 - length: 19 - - Block: - Block: - tag: - name: if - bits: - - if - - user.is_authenticated - span: - start: 48 - length: 24 - tag_span: - start: 48 - length: 24 - assignment: ~ - nodes: - - Comment: - content: This if is unclosed which does matter - span: - start: 87 - length: 41 - - Text: - content: "

Welcome " - span: - start: 131 - length: 19 - - Variable: - bits: - - user - - name - filters: [] - span: - start: 153 - length: 9 - - Text: - content: "

" - span: - start: 165 - length: 4 - - Text: - content: "
" - span: - start: 170 - length: 13 - - Comment: - content: "This div is unclosed which doesn't matter" - span: - start: 196 - length: 45 - - Block: - Block: - tag: - name: for - bits: - - for - - item - - in - - items - span: - start: 252 - length: 17 - tag_span: - start: 252 - length: 17 - assignment: ~ - nodes: - - Text: - content: " " - span: - start: 276 - length: 18 - - Variable: - bits: - - item - filters: [] - span: - start: 297 - length: 4 - - Text: - content: "" - span: - start: 304 - length: 7 - closing: - Closing: - tag: - name: endfor - bits: - - endfor - span: - start: 320 - length: 6 - tag_span: - start: 320 - length: 6 - assignment: ~ - assignments: ~ - - Text: - content: "
Page Footer
" - span: - start: 333 - length: 32 - - Text: - content: "
" - span: - start: 366 - length: 6 - closing: ~ - assignments: ~ -line_offsets: - - 0 - - 24 - - 44 - - 79 - - 131 - - 170 - - 184 - - 244 - - 276 - - 312 - - 333 - - 366 diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap index 2fd9949..c2657c0 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap @@ -7,92 +7,92 @@ nodes: content: "" span: start: 0 - length: 16 + length: 15 - Text: content: "" span: start: 16 - length: 7 + length: 6 - Text: content: " " span: start: 23 - length: 11 + length: 10 - Text: content: " " span: start: 134 - length: 17 + length: 16 - Text: content: " " span: start: 313 - length: 18 + length: 17 - Text: content: " " span: start: 331 - length: 12 + length: 11 - Text: content: " " span: start: 343 - length: 11 + length: 10 - Text: - content: " " + content: "" span: - start: 354 - length: 32 + start: 362 + length: 23 - Text: content: "
" span: start: 386 - length: 65 + length: 64 - Block: Block: tag: @@ -112,7 +112,7 @@ nodes: content: Welcome message span: start: 510 - length: 19 + length: 21 - Text: content: "

Welcome, " span: @@ -141,7 +141,7 @@ nodes: content: "!

" span: start: 598 - length: 7 + length: 6 - Block: Block: tag: @@ -161,7 +161,7 @@ nodes: content: " Admin" span: start: 644 - length: 39 + length: 38 - Block: Branch: tag: @@ -180,7 +180,7 @@ nodes: content: " User" span: start: 710 - length: 38 + length: 37 closing: Closing: tag: @@ -213,12 +213,12 @@ nodes: content: "
" span: start: 800 - length: 15 + length: 14 - Text: content: " " span: start: 815 - length: 12 + length: 11 - Text: content: "" span: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap.new b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap.new deleted file mode 100644 index 7c27ba1..0000000 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap.new +++ /dev/null @@ -1,259 +0,0 @@ ---- -source: crates/djls-template-ast/src/parser.rs -assertion_line: 682 -expression: ast -snapshot_kind: text ---- -nodes: - - Text: - content: "" - span: - start: 0 - length: 15 - - Text: - content: "" - span: - start: 16 - length: 6 - - Text: - content: " " - span: - start: 23 - length: 10 - - Text: - content: " " - span: - start: 134 - length: 16 - - Text: - content: " " - span: - start: 313 - length: 17 - - Text: - content: " " - span: - start: 331 - length: 11 - - Text: - content: " " - span: - start: 343 - length: 10 - - Comment: - content: Header section - span: - start: 362 - length: 21 - - Text: - content: "
" - span: - start: 386 - length: 64 - - Block: - Block: - tag: - name: if - bits: - - if - - user.is_authenticated - span: - start: 463 - length: 24 - tag_span: - start: 463 - length: 24 - assignment: ~ - nodes: - - Comment: - content: Welcome message - span: - start: 510 - length: 19 - - Text: - content: "

Welcome, " - span: - start: 532 - length: 29 - - Variable: - bits: - - user - - name - filters: - - name: title - args: [] - span: - start: 565 - length: 31 - - name: default - args: - - "'Guest'" - span: - start: 565 - length: 31 - span: - start: 564 - length: 31 - - Text: - content: "!

" - span: - start: 598 - length: 6 - - Block: - Block: - tag: - name: if - bits: - - if - - user.is_staff - span: - start: 621 - length: 16 - tag_span: - start: 621 - length: 16 - assignment: ~ - nodes: - - Text: - content: " Admin" - span: - start: 644 - length: 38 - - Block: - Branch: - tag: - name: else - bits: - - else - span: - start: 699 - length: 4 - tag_span: - start: 699 - length: 4 - assignment: ~ - nodes: - - Text: - content: " User" - span: - start: 710 - length: 37 - closing: - Closing: - tag: - name: endif - bits: - - endif - span: - start: 764 - length: 5 - tag_span: - start: 764 - length: 5 - assignment: ~ - assignments: ~ - closing: - Closing: - tag: - name: endif - bits: - - endif - span: - start: 788 - length: 5 - tag_span: - start: 788 - length: 5 - assignment: ~ - assignments: ~ - - Text: - content: "
" - span: - start: 800 - length: 14 - - Text: - content: " " - span: - start: 815 - length: 11 - - Text: - content: "" - span: - start: 827 - length: 7 -line_offsets: - - 0 - - 16 - - 23 - - 34 - - 66 - - 97 - - 134 - - 151 - - 191 - - 215 - - 241 - - 270 - - 298 - - 313 - - 331 - - 343 - - 354 - - 386 - - 451 - - 494 - - 532 - - 605 - - 644 - - 683 - - 710 - - 748 - - 776 - - 800 - - 815 - - 827 diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap index b33d1d8..b3290d2 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap @@ -7,32 +7,27 @@ nodes: content: "" span: @@ -43,6 +38,5 @@ line_offsets: - 32 - 59 - 76 - - 94 - 113 - 133 diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap.new b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap.new deleted file mode 100644 index 04000cd..0000000 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__script__parse_script.snap.new +++ /dev/null @@ -1,44 +0,0 @@ ---- -source: crates/djls-template-ast/src/parser.rs -assertion_line: 534 -expression: ast -snapshot_kind: text ---- -nodes: - - Text: - content: "" - span: - start: 133 - length: 9 -line_offsets: - - 0 - - 32 - - 59 - - 76 - - 113 - - 133 diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap index d28323c..0238992 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap @@ -7,27 +7,27 @@ nodes: content: "" span: diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap.new b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap.new deleted file mode 100644 index 4fdd26b..0000000 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__style__parse_style.snap.new +++ /dev/null @@ -1,44 +0,0 @@ ---- -source: crates/djls-template-ast/src/parser.rs -assertion_line: 551 -expression: ast -snapshot_kind: text ---- -nodes: - - Text: - content: "" - span: - start: 89 - length: 8 -line_offsets: - - 0 - - 24 - - 48 - - 62 - - 83 - - 89 diff --git a/crates/djls-template-ast/src/tokens.rs b/crates/djls-template-ast/src/tokens.rs index 33fc6ce..525a15d 100644 --- a/crates/djls-template-ast/src/tokens.rs +++ b/crates/djls-template-ast/src/tokens.rs @@ -33,10 +33,11 @@ impl TokenType { | TokenType::StyleTagOpen(s) | TokenType::StyleTagClose(s) | TokenType::Text(s) => Some(s.len()), - TokenType::Comment(content, start, end) => { - Some(content.len() + start.len() + end.as_ref().map_or(0, |e| e.len())) - } - TokenType::Whitespace(len) => Some(len.clone()), + TokenType::Comment(content, start, end) => match end { + Some(end) => Some(start.len() + 1 + content.len() + 1 + end.len()), + None => Some(start.len() + 1 + content.len()), + }, + TokenType::Whitespace(len) => Some(*len), TokenType::Newline => Some(1), TokenType::Eof => None, } @@ -54,7 +55,7 @@ impl TokenType { | TokenType::StyleTagOpen(s) | TokenType::StyleTagClose(s) | TokenType::Text(s) => s, - TokenType::Comment(content, _, _) => content, // Just return the content + TokenType::Comment(content, _, _) => content, TokenType::Whitespace(_) => " ", TokenType::Newline => "\n", TokenType::Eof => "", @@ -68,8 +69,8 @@ impl fmt::Display for TokenType { match self { Comment(content, start, end) => match end { - Some(end) => write!(f, "{}{}{}", start, content, end), - None => write!(f, "{}{}", start, content), + Some(end) => write!(f, "{} {} {}", start, content, end), + None => write!(f, "{} {}", start, content), }, DjangoBlock(s) => write!(f, "{{% {} %}}", s), DjangoVariable(s) => write!(f, "{{{{ {} }}}}", s),