Skip to content

Commit 1c5740b

Browse files
correctly associate branch children
1 parent ec05c62 commit 1c5740b

5 files changed

+43
-25
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,29 @@ impl Parser {
177177

178178
let tag_spec = specs.get(tag_name.as_str()).cloned();
179179
let mut children = Vec::new();
180+
let mut current_branch: Option<(String, Vec<String>, Vec<Node>)> = None;
180181

181182
while !self.is_at_end() {
182183
match self.next_node() {
183184
Ok(node) => {
184-
children.push(node);
185+
if let Some((_, _, branch_children)) = &mut current_branch {
186+
branch_children.push(node);
187+
} else {
188+
children.push(node);
189+
}
185190
}
186191
Err(ParserError::ErrorSignal(Signal::SpecialTag(tag))) => {
187192
if let Some(spec) = &tag_spec {
188193
// Check if closing tag
189194
if Some(&tag) == spec.closing.as_ref() {
195+
// If we have a current branch, add it to children
196+
if let Some((name, bits, branch_children)) = current_branch {
197+
children.push(Node::Django(DjangoNode::Tag(TagNode::Branch {
198+
name,
199+
bits,
200+
children: branch_children,
201+
})));
202+
}
190203
children.push(Node::Django(DjangoNode::Tag(TagNode::Closing {
191204
name: tag,
192205
bits: vec![],
@@ -200,7 +213,15 @@ impl Parser {
200213
// Check if intermediate tag
201214
if let Some(intermediates) = &spec.intermediates {
202215
if let Some(intermediate) = intermediates.iter().find(|i| i.name == tag) {
203-
// Create branch node with the current children
216+
// If we have a current branch, add it to children
217+
if let Some((name, bits, branch_children)) = current_branch {
218+
children.push(Node::Django(DjangoNode::Tag(TagNode::Branch {
219+
name,
220+
bits,
221+
children: branch_children,
222+
})));
223+
}
224+
// Create new branch node
204225
let branch_bits = if intermediate.args {
205226
match &self.tokens[self.current - 1].token_type() {
206227
TokenType::DjangoBlock(content) => content
@@ -213,11 +234,7 @@ impl Parser {
213234
} else {
214235
vec![]
215236
};
216-
children.push(Node::Django(DjangoNode::Tag(TagNode::Branch {
217-
name: tag,
218-
bits: branch_bits,
219-
children: Vec::new(),
220-
})));
237+
current_branch = Some((tag, branch_bits, Vec::new()));
221238
continue;
222239
}
223240
}
@@ -666,7 +683,8 @@ mod tests {
666683

667684
#[test]
668685
fn test_parse_complex_if_elif() {
669-
let source = "{% if x > 0 %}Positive{% elif x < 0 %}Negative{% else %}Zero{% endif %}";
686+
let source =
687+
"{% if x > 0 %}Positive{% elif x < 0 %}Negative{% else %}Zero{% endif %}";
670688
let tokens = Lexer::new(source).tokenize().unwrap();
671689
let mut parser = Parser::new(tokens);
672690
let ast = parser.parse().unwrap();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ nodes:
2222
- x
2323
- "<"
2424
- "0"
25-
children: []
26-
- Text: Negative
25+
children:
26+
- Text: Negative
2727
- Django:
2828
Tag:
2929
Branch:
3030
name: else
3131
bits: []
32-
children: []
33-
- Text: Zero
32+
children:
33+
- Text: Zero
3434
- Django:
3535
Tag:
3636
Closing:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ nodes:
2323
Branch:
2424
name: empty
2525
bits: []
26-
children: []
27-
- Text: No items
26+
children:
27+
- Text: No items
2828
- Django:
2929
Tag:
3030
Closing:

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ nodes:
8787
Branch:
8888
name: empty
8989
bits: []
90-
children: []
91-
- Text: (no groups)
90+
children:
91+
- Text: (no groups)
9292
- Django:
9393
Tag:
9494
Closing:
@@ -99,8 +99,8 @@ nodes:
9999
Branch:
100100
name: else
101101
bits: []
102-
children: []
103-
- Text: Guest
102+
children:
103+
- Text: Guest
104104
- Django:
105105
Tag:
106106
Closing:

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ nodes:
111111
Branch:
112112
name: else
113113
bits: []
114-
children: []
115-
- Html:
116-
Element:
117-
tag_name: span
118-
attributes: {}
119-
children:
120-
- Text: User
114+
children:
115+
- Html:
116+
Element:
117+
tag_name: span
118+
attributes: {}
119+
children:
120+
- Text: User
121121
- Django:
122122
Tag:
123123
Closing:

0 commit comments

Comments
 (0)