Skip to content

Commit ad9d6e5

Browse files
checkpoint
1 parent 6d26d78 commit ad9d6e5

13 files changed

+82
-830
lines changed

crates/djls-template-ast/src/parser.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,10 @@ impl Parser {
261261
let start_token = self.peek_previous()?;
262262
let start_pos = start_token.start().unwrap_or(0);
263263

264-
// If we start with a newline, skip it
265264
if matches!(start_token.token_type(), TokenType::Newline) {
266265
return self.next_node();
267266
}
268267

269-
// Use TokenType's Display implementation for formatting
270268
let mut text = start_token.token_type().to_string();
271269
let mut total_length: u32 = u32::try_from(text.len()).unwrap();
272270

@@ -286,7 +284,6 @@ impl Parser {
286284
}
287285
}
288286

289-
// Skip empty text nodes
290287
if text.trim().is_empty() {
291288
self.next_node()
292289
} else {
@@ -303,14 +300,18 @@ impl Parser {
303300
start: &str,
304301
end: Option<&str>,
305302
) -> Result<Node, ParserError> {
306-
let start_token = self.peek_previous()?;
307-
let start_pos = start_token.start().unwrap_or(0);
308-
let total_length = (content.len() + start.len() + end.map_or(0, |e| e.len())) as u32;
309-
let span = Span::new(start_pos, total_length);
310-
Ok(Node::Comment {
311-
content: content.to_string(),
312-
span,
313-
})
303+
let token = self.peek_previous()?;
304+
let start_pos = token.start().unwrap_or(0);
305+
306+
// Only treat Django comments as Comment nodes
307+
if start == "{#" && end == Some("#}") {
308+
Ok(Node::Comment {
309+
content: content.to_string(),
310+
span: Span::new(start_pos, token.token_type().len().unwrap_or(0) as u32),
311+
})
312+
} else {
313+
self.parse_text()
314+
}
314315
}
315316

316317
fn peek(&self) -> Result<Token, ParserError> {

crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__comments__parse_comments.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ source: crates/djls-template-ast/src/parser.rs
33
expression: ast
44
---
55
nodes:
6-
- Comment:
7-
content: HTML comment
6+
- Text:
7+
content: "<!-- HTML comment -->"
88
span:
99
start: 0
10-
length: 19
10+
length: 21
1111
- Comment:
1212
content: Django comment
1313
span:
1414
start: 21
15-
length: 18
15+
length: 20
1616
line_offsets:
1717
- 0

crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ nodes:
188188
content: " (no groups)"
189189
span:
190190
start: 290
191-
length: 20
191+
length: 19
192192
closing:
193193
Closing:
194194
tag:
@@ -221,7 +221,7 @@ nodes:
221221
content: " Guest"
222222
span:
223223
start: 338
224-
length: 10
224+
length: 9
225225
closing:
226226
Closing:
227227
tag:

crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap.new

Lines changed: 0 additions & 259 deletions
This file was deleted.

0 commit comments

Comments
 (0)