From baeed3ed66da94d477f6be8bc20eb21596ebfb24 Mon Sep 17 00:00:00 2001 From: Josh Date: Sat, 4 Jan 2025 22:10:22 -0600 Subject: [PATCH] simplify --- crates/djls-template-ast/src/ast.rs | 53 --- crates/djls-template-ast/src/lexer.rs | 126 ++----- crates/djls-template-ast/src/parser.rs | 268 ++++---------- ..._ast__lexer__tests__tokenize_comments.snap | 103 ++---- ...st__lexer__tests__tokenize_everything.snap | 328 +++++++++++++----- ...late_ast__lexer__tests__tokenize_html.snap | 26 +- ...te_ast__lexer__tests__tokenize_script.snap | 117 +++++-- ...ate_ast__lexer__tests__tokenize_style.snap | 67 +++- ...rser__tests__comments__parse_comments.snap | 3 +- ...r__tests__django__parse_mixed_content.snap | 12 +- ...__tests__errors__parse_error_recovery.snap | 48 +-- ...ests__errors__parse_unclosed_html_tag.snap | 6 +- ..._tests__errors__parse_unclosed_script.snap | 6 +- ...__tests__errors__parse_unclosed_style.snap | 6 +- ...er__tests__full_templates__parse_full.snap | 177 +++------- ...rser__tests__html__parse_html_doctype.snap | 3 +- ...__parser__tests__html__parse_html_tag.snap | 9 +- ..._parser__tests__html__parse_html_void.snap | 7 +- ...__parser__tests__script__parse_script.snap | 17 +- ...st__parser__tests__style__parse_style.snap | 13 +- 20 files changed, 617 insertions(+), 778 deletions(-) diff --git a/crates/djls-template-ast/src/ast.rs b/crates/djls-template-ast/src/ast.rs index 60224d1..2789ba6 100644 --- a/crates/djls-template-ast/src/ast.rs +++ b/crates/djls-template-ast/src/ast.rs @@ -36,9 +36,6 @@ impl Ast { #[derive(Clone, Debug, Serialize)] pub enum Node { Django(DjangoNode), - Html(HtmlNode), - Script(ScriptNode), - Style(StyleNode), Text(String), } @@ -82,56 +79,6 @@ impl DjangoFilter { } } -#[derive(Clone, Debug, Serialize)] -pub enum HtmlNode { - Comment(String), - Doctype(String), - Element { - tag_name: String, - attributes: Attributes, - children: Vec, - }, - Void { - tag_name: String, - attributes: Attributes, - }, -} - -#[derive(Clone, Debug, Serialize)] -pub enum ScriptNode { - Comment { - content: String, - kind: ScriptCommentKind, - }, - Element { - attributes: Attributes, - children: Vec, - }, -} - -#[derive(Clone, Debug, Serialize)] -pub enum ScriptCommentKind { - SingleLine, // // - MultiLine, // /* */ -} - -#[derive(Clone, Debug, Serialize)] -pub enum StyleNode { - Comment(String), - Element { - attributes: Attributes, - children: Vec, - }, -} - -#[derive(Clone, Debug, Serialize)] -pub enum AttributeValue { - Value(String), - Boolean, -} - -pub type Attributes = BTreeMap; - #[derive(Clone, Debug, Error, Serialize)] pub enum AstError { #[error("Empty AST")] diff --git a/crates/djls-template-ast/src/lexer.rs b/crates/djls-template-ast/src/lexer.rs index 83b55b8..b88a830 100644 --- a/crates/djls-template-ast/src/lexer.rs +++ b/crates/djls-template-ast/src/lexer.rs @@ -53,107 +53,47 @@ impl Lexer { self.consume_n(2)?; // #} TokenType::Comment(content, "{#".to_string(), Some("#}".to_string())) } - _ => { - self.consume()?; // { - TokenType::Text(String::from("{")) - } - }, - - '<' => match self.peek_next()? { - '/' => { - self.consume_n(2)?; // ")?; - self.consume()?; // > - TokenType::HtmlTagClose(tag) - } - '!' if self.matches("")?; - self.consume_n(3)?; // --> - TokenType::Comment(content, "".to_string())) - } - _ => { - self.consume()?; // consume < - let tag = self.consume_until(">")?; - self.consume()?; // consume > - if tag.starts_with("script") { - TokenType::ScriptTagOpen(tag) - } else if tag.starts_with("style") { - TokenType::StyleTagOpen(tag) - } else if tag.ends_with("/") { - TokenType::HtmlTagVoid(tag.trim_end_matches("/").to_string()) - } else { - TokenType::HtmlTagOpen(tag) - } - } - }, - - '/' => match self.peek_next()? { - '/' => { - self.consume_n(2)?; // // - let content = self.consume_until("\n")?; - TokenType::Comment(content, "//".to_string(), None) - } - '*' => { - self.consume_n(2)?; // /* - let content = self.consume_until("*/")?; - self.consume_n(2)?; // */ - TokenType::Comment(content, "/*".to_string(), Some("*/".to_string())) - } _ => { self.consume()?; - TokenType::Text("/".to_string()) + TokenType::Text("{".to_string()) } }, - - c if c.is_whitespace() => { - if c == '\n' || c == '\r' { - self.consume()?; // \r or \n - if c == '\r' && self.peek()? == '\n' { - self.consume()?; // \n of \r\n - } - TokenType::Newline - } else { - self.consume()?; // Consume the first whitespace - while !self.is_at_end() && self.peek()?.is_whitespace() { - if self.peek()? == '\n' || self.peek()? == '\r' { - break; - } - self.consume()?; - } - let whitespace_count = self.current - self.start; - TokenType::Whitespace(whitespace_count) - } + '\n' => { + self.consume()?; + self.line += 1; + TokenType::Newline + } + ' ' | '\t' | '\r' => { + let mut count = 1; + self.consume()?; + while let Ok(c) = self.peek() { + if c != ' ' && c != '\t' && c != '\r' { + break; + } + self.consume()?; + count += 1; + } + TokenType::Whitespace(count) } - _ => { let mut text = String::new(); while !self.is_at_end() { - let c = self.peek()?; - if c == '{' || c == '<' || c == '\n' { - break; + match self.peek()? { + '{' => break, + '\n' | ' ' | '\t' | '\r' => break, + _ => { + text.push(self.consume()?); + } } - text.push(c); - self.consume()?; + } + if text.is_empty() { + return Err(LexerError::EmptyToken(self.line)); } TokenType::Text(text) } }; - let token = Token::new(token_type, self.line, Some(self.start)); - - match self.peek_previous()? { - '\n' => self.line += 1, - '\r' => { - self.line += 1; - if self.peek()? == '\n' { - self.current += 1; - } - } - _ => {} - } - - Ok(token) + Ok(Token::new(token_type, self.line, Some(self.start))) } fn peek(&self) -> Result { @@ -310,15 +250,7 @@ mod tests { #[test] fn test_tokenize_comments() { let source = r#" -{# Django comment #} - -"#; +{# Django comment #}"#; let mut lexer = Lexer::new(source); let tokens = lexer.tokenize().unwrap(); insta::assert_yaml_snapshot!(tokens); @@ -357,7 +289,7 @@ mod tests { assert!(Lexer::new("{{ user.name").tokenize().is_err()); // No closing }} assert!(Lexer::new("{% if").tokenize().is_err()); // No closing %} assert!(Lexer::new("{#").tokenize().is_err()); // No closing #} - assert!(Lexer::new(" + assert!(Lexer::new(", but HTML is treated as text // Invalid characters or syntax within tokens assert!(Lexer::new("{{}}").tokenize().is_ok()); // Empty but valid diff --git a/crates/djls-template-ast/src/parser.rs b/crates/djls-template-ast/src/parser.rs index a31fbb8..156937e 100644 --- a/crates/djls-template-ast/src/parser.rs +++ b/crates/djls-template-ast/src/parser.rs @@ -1,10 +1,6 @@ -use crate::ast::{ - Ast, AstError, AttributeValue, DjangoFilter, DjangoNode, HtmlNode, Node, ScriptCommentKind, - ScriptNode, StyleNode, TagNode, -}; +use crate::ast::{Ast, AstError, DjangoFilter, DjangoNode, Node, TagNode}; use crate::tagspecs::TagSpec; use crate::tokens::{Token, TokenStream, TokenType}; -use std::collections::BTreeMap; use thiserror::Error; pub struct Parser { @@ -19,18 +15,13 @@ impl Parser { pub fn parse(&mut self) -> Result { let mut ast = Ast::default(); - let mut had_nodes = false; while !self.is_at_end() { match self.next_node() { Ok(node) => { ast.add_node(node); - had_nodes = true; } Err(ParserError::Ast(AstError::StreamError(kind))) if kind == "AtEnd" => { - if !had_nodes { - return Ok(ast.finalize()?); - } break; } Err(ParserError::ErrorSignal(Signal::SpecialTag(_))) => { @@ -58,43 +49,31 @@ impl Parser { return Err(ParserError::Ast(AstError::StreamError("AtEnd".to_string()))); } - let token = self.consume()?; + let token = self.peek()?; let node = match token.token_type() { - TokenType::Comment(s, start, end) => self.parse_comment(s, start, end.as_deref()), - TokenType::DjangoBlock(s) => self.parse_django_block(s), - TokenType::DjangoVariable(s) => self.parse_django_variable(s), - TokenType::Eof => { - if self.is_at_end() { - Err(ParserError::Ast(AstError::StreamError("AtEnd".to_string()))) - } else { - self.next_node() - } + TokenType::Comment(content, start, end) => { + self.consume()?; + self.parse_comment(content, start, end.as_deref()) } - TokenType::HtmlTagClose(tag) => { - self.backtrack(1)?; - Err(ParserError::ErrorSignal(Signal::ClosingTagFound( - tag.to_string(), - ))) + TokenType::DjangoBlock(content) => { + self.consume()?; + self.parse_django_block(content) } - TokenType::HtmlTagOpen(s) => self.parse_tag_open(s), - TokenType::HtmlTagVoid(s) => self.parse_html_tag_void(s), - TokenType::Newline => self.next_node(), - TokenType::ScriptTagClose(_) => { - self.backtrack(1)?; - Err(ParserError::ErrorSignal(Signal::ClosingTagFound( - "script".to_string(), - ))) + TokenType::DjangoVariable(content) => { + self.consume()?; + self.parse_django_variable(content) } - TokenType::ScriptTagOpen(s) => self.parse_tag_open(s), - TokenType::StyleTagClose(_) => { - self.backtrack(1)?; - Err(ParserError::ErrorSignal(Signal::ClosingTagFound( - "style".to_string(), - ))) - } - TokenType::StyleTagOpen(s) => self.parse_tag_open(s), - TokenType::Text(s) => Ok(Node::Text(s.to_string())), - TokenType::Whitespace(_) => self.next_node(), + TokenType::Text(_) + | TokenType::Whitespace(_) + | TokenType::Newline + | TokenType::HtmlTagOpen(_) + | TokenType::HtmlTagClose(_) + | TokenType::HtmlTagVoid(_) + | TokenType::ScriptTagOpen(_) + | TokenType::ScriptTagClose(_) + | TokenType::StyleTagOpen(_) + | TokenType::StyleTagClose(_) => self.parse_text(), + TokenType::Eof => Err(ParserError::Ast(AstError::StreamError("AtEnd".to_string()))), }?; Ok(node) } @@ -107,49 +86,12 @@ impl Parser { ) -> Result { match start { "{#" => Ok(Node::Django(DjangoNode::Comment(content.to_string()))), - "" + Text: "" + line: 1 + start: 18 +- token_type: Newline + line: 2 start: 21 - token_type: Comment: @@ -19,76 +40,6 @@ expression: tokens - "#}" line: 2 start: 22 -- token_type: Newline - line: 2 - start: 42 -- token_type: - ScriptTagOpen: script - line: 3 - start: 43 -- token_type: Newline - line: 3 - start: 51 -- token_type: - Whitespace: 4 - line: 4 - start: 52 -- token_type: - Comment: - - JS single line comment - - // - - ~ - line: 4 - start: 56 -- token_type: Newline - line: 4 - start: 81 -- token_type: - Whitespace: 4 - line: 5 - start: 82 -- token_type: - Comment: - - "JS multi-line\n comment" - - /* - - "*/" - line: 5 - start: 86 -- token_type: Newline - line: 5 - start: 120 -- token_type: - HtmlTagClose: script - line: 6 - start: 121 -- token_type: Newline - line: 6 - start: 130 -- token_type: - StyleTagOpen: style - line: 7 - start: 131 -- token_type: Newline - line: 7 - start: 138 -- token_type: - Whitespace: 4 - line: 8 - start: 139 -- token_type: - Comment: - - CSS comment - - /* - - "*/" - line: 8 - start: 143 -- token_type: Newline - line: 8 - start: 160 -- token_type: - HtmlTagClose: style - line: 9 - start: 161 - token_type: Eof - line: 9 + line: 2 start: ~ diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_everything.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_everything.snap index 6f1086c..0a91905 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_everything.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_everything.snap @@ -3,59 +3,100 @@ source: crates/djls-template-ast/src/lexer.rs expression: tokens --- - token_type: - HtmlTagOpen: "!DOCTYPE html" + Text: " + line: 1 + start: 10 +- token_type: Newline + line: 2 start: 15 - token_type: - HtmlTagOpen: html + Text: "" line: 2 start: 16 - token_type: Newline - line: 2 + line: 3 start: 22 - token_type: - HtmlTagOpen: head + Text: "" line: 3 start: 23 - token_type: Newline - line: 3 + line: 4 start: 29 - token_type: Whitespace: 4 line: 4 start: 30 - token_type: - StyleTagOpen: "style type=\"text/css\"" + Text: "" + line: 4 + start: 41 +- token_type: Newline + line: 5 start: 57 - token_type: Whitespace: 8 line: 5 start: 58 - token_type: - Comment: - - Style header - - /* - - "*/" + Text: /* line: 5 start: 66 -- token_type: Newline +- token_type: + Whitespace: 1 line: 5 + start: 68 +- token_type: + Text: Style + line: 5 + start: 69 +- token_type: + Whitespace: 1 + line: 5 + start: 74 +- token_type: + Text: header + line: 5 + start: 75 +- token_type: + Whitespace: 1 + line: 5 + start: 81 +- token_type: + Text: "*/" + line: 5 + start: 82 +- token_type: Newline + line: 6 start: 84 - token_type: Whitespace: 8 line: 6 start: 85 - token_type: - Text: ".header " + Text: ".header" line: 6 start: 93 +- token_type: + Whitespace: 1 + line: 6 + start: 100 - token_type: Text: "{" line: 6 @@ -65,87 +106,165 @@ expression: tokens line: 6 start: 102 - token_type: - Text: "color: blue; }" + Text: "color:" line: 6 start: 103 -- token_type: Newline +- token_type: + Whitespace: 1 line: 6 + start: 109 +- token_type: + Text: blue; + line: 6 + start: 110 +- token_type: + Whitespace: 1 + line: 6 + start: 115 +- token_type: + Text: "}" + line: 6 + start: 116 +- token_type: Newline + line: 7 start: 117 - token_type: Whitespace: 4 line: 7 start: 118 - token_type: - HtmlTagClose: style + Text: "" line: 7 start: 122 - token_type: Newline - line: 7 + line: 8 start: 130 - token_type: Whitespace: 4 line: 8 start: 131 - token_type: - ScriptTagOpen: "script type=\"text/javascript\"" + Text: "" + line: 8 + start: 143 +- token_type: Newline + line: 9 start: 166 - token_type: Whitespace: 8 line: 9 start: 167 - token_type: - Comment: - - Init app - - // - - ~ + Text: // line: 9 start: 175 -- token_type: Newline +- token_type: + Whitespace: 1 line: 9 + start: 177 +- token_type: + Text: Init + line: 9 + start: 178 +- token_type: + Whitespace: 1 + line: 9 + start: 182 +- token_type: + Text: app + line: 9 + start: 183 +- token_type: Newline + line: 10 start: 186 - token_type: Whitespace: 8 line: 10 start: 187 - token_type: - Text: "const app = " + Text: const line: 10 start: 195 +- token_type: + Whitespace: 1 + line: 10 + start: 200 +- token_type: + Text: app + line: 10 + start: 201 +- token_type: + Whitespace: 1 + line: 10 + start: 204 +- token_type: + Text: "=" + line: 10 + start: 205 +- token_type: + Whitespace: 1 + line: 10 + start: 206 - token_type: Text: "{" line: 10 start: 207 - token_type: Newline - line: 10 + line: 11 start: 208 - token_type: Whitespace: 12 line: 11 start: 209 - token_type: - Comment: - - Config - - /* - - "*/" + Text: /* line: 11 start: 221 -- token_type: Newline +- token_type: + Whitespace: 1 line: 11 + start: 223 +- token_type: + Text: Config + line: 11 + start: 224 +- token_type: + Whitespace: 1 + line: 11 + start: 230 +- token_type: + Text: "*/" + line: 11 + start: 231 +- token_type: Newline + line: 12 start: 233 - token_type: Whitespace: 12 line: 12 start: 234 - token_type: - Text: "debug: true" + Text: "debug:" line: 12 start: 246 -- token_type: Newline +- token_type: + Whitespace: 1 line: 12 + start: 252 +- token_type: + Text: "true" + line: 12 + start: 253 +- token_type: Newline + line: 13 start: 257 - token_type: Whitespace: 8 @@ -156,57 +275,110 @@ expression: tokens line: 13 start: 266 - token_type: Newline - line: 13 + line: 14 start: 268 - token_type: Whitespace: 4 line: 14 start: 269 - token_type: - HtmlTagClose: script + Text: "" line: 14 start: 273 - token_type: Newline - line: 14 + line: 15 start: 282 - token_type: - HtmlTagClose: head + Text: "" line: 15 start: 283 - token_type: Newline - line: 15 + line: 16 start: 290 - token_type: - HtmlTagOpen: body + Text: "" line: 16 start: 291 - token_type: Newline - line: 16 + line: 17 start: 297 - token_type: Whitespace: 4 line: 17 start: 298 - token_type: - Comment: - - Header section - - "" + Text: "" + line: 17 + start: 322 +- token_type: Newline + line: 18 start: 325 - token_type: Whitespace: 4 line: 18 start: 326 - token_type: - HtmlTagOpen: "div class=\"header\" id=\"main\" data-value=\"123\" disabled" + Text: " + line: 18 + start: 377 +- token_type: Newline + line: 19 start: 386 - token_type: Whitespace: 8 @@ -217,7 +389,7 @@ expression: tokens line: 19 start: 395 - token_type: Newline - line: 19 + line: 20 start: 425 - token_type: Whitespace: 12 @@ -231,34 +403,30 @@ expression: tokens line: 20 start: 438 - token_type: Newline - line: 20 + line: 21 start: 459 - token_type: Whitespace: 12 line: 21 start: 460 - token_type: - HtmlTagOpen: h1 + Text: "

Welcome," line: 21 start: 472 - token_type: - Text: "Welcome, " + Whitespace: 1 line: 21 - start: 476 + start: 484 - token_type: DjangoVariable: "user.name|default:\"Guest\"|title" line: 21 start: 485 - token_type: - Text: "!" + Text: "!

" line: 21 start: 522 -- token_type: - HtmlTagClose: h1 - line: 21 - start: 523 - token_type: Newline - line: 21 + line: 22 start: 528 - token_type: Whitespace: 12 @@ -269,26 +437,18 @@ expression: tokens line: 22 start: 541 - token_type: Newline - line: 22 + line: 23 start: 563 - token_type: Whitespace: 16 line: 23 start: 564 - token_type: - HtmlTagOpen: span + Text: "Admin" line: 23 start: 580 -- token_type: - Text: Admin - line: 23 - start: 586 -- token_type: - HtmlTagClose: span - line: 23 - start: 591 - token_type: Newline - line: 23 + line: 24 start: 598 - token_type: Whitespace: 12 @@ -299,26 +459,18 @@ expression: tokens line: 24 start: 611 - token_type: Newline - line: 24 + line: 25 start: 621 - token_type: Whitespace: 16 line: 25 start: 622 - token_type: - HtmlTagOpen: span + Text: "User" line: 25 start: 638 -- token_type: - Text: User - line: 25 - start: 644 -- token_type: - HtmlTagClose: span - line: 25 - start: 648 - token_type: Newline - line: 25 + line: 26 start: 655 - token_type: Whitespace: 12 @@ -329,7 +481,7 @@ expression: tokens line: 26 start: 668 - token_type: Newline - line: 26 + line: 27 start: 679 - token_type: Whitespace: 8 @@ -340,28 +492,28 @@ expression: tokens line: 27 start: 688 - token_type: Newline - line: 27 + line: 28 start: 699 - token_type: Whitespace: 4 line: 28 start: 700 - token_type: - HtmlTagClose: div + Text: "" line: 28 start: 704 - token_type: Newline - line: 28 + line: 29 start: 710 - token_type: - HtmlTagClose: body + Text: "" line: 29 start: 711 - token_type: Newline - line: 29 + line: 30 start: 718 - token_type: - HtmlTagClose: html + Text: "" line: 30 start: 719 - token_type: Eof diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_html.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_html.snap index ff82c53..b096b6e 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_html.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_html.snap @@ -3,13 +3,33 @@ source: crates/djls-template-ast/src/lexer.rs expression: tokens --- - token_type: - HtmlTagOpen: "div class=\"container\" id=\"main\" disabled" + Text: " + line: 1 + start: 33 - token_type: Eof line: 1 start: ~ diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_script.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_script.snap index 36e765b..30c74b0 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_script.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_script.snap @@ -3,66 +3,143 @@ source: crates/djls-template-ast/src/lexer.rs expression: tokens --- - token_type: - ScriptTagOpen: "script type=\"text/javascript\"" + Text: "" + line: 1 + start: 8 +- token_type: Newline + line: 2 start: 31 - token_type: Whitespace: 4 line: 2 start: 32 - token_type: - Comment: - - Single line comment - - // - - ~ + Text: // line: 2 start: 36 -- token_type: Newline +- token_type: + Whitespace: 1 line: 2 + start: 38 +- token_type: + Text: Single + line: 2 + start: 39 +- token_type: + Whitespace: 1 + line: 2 + start: 45 +- token_type: + Text: line + line: 2 + start: 46 +- token_type: + Whitespace: 1 + line: 2 + start: 50 +- token_type: + Text: comment + line: 2 + start: 51 +- token_type: Newline + line: 3 start: 58 - token_type: Whitespace: 4 line: 3 start: 59 - token_type: - Text: const x = 1; + Text: const line: 3 start: 63 -- token_type: Newline +- token_type: + Whitespace: 1 line: 3 + start: 68 +- token_type: + Text: x + line: 3 + start: 69 +- token_type: + Whitespace: 1 + line: 3 + start: 70 +- token_type: + Text: "=" + line: 3 + start: 71 +- token_type: + Whitespace: 1 + line: 3 + start: 72 +- token_type: + Text: 1; + line: 3 + start: 73 +- token_type: Newline + line: 4 start: 75 - token_type: Whitespace: 4 line: 4 start: 76 - token_type: - Comment: - - "Multi-line\n comment" - - /* - - "*/" + Text: /* line: 4 start: 80 -- token_type: Newline +- token_type: + Whitespace: 1 line: 4 + start: 82 +- token_type: + Text: Multi-line + line: 4 + start: 83 +- token_type: Newline + line: 5 + start: 93 +- token_type: + Whitespace: 7 + line: 5 + start: 94 +- token_type: + Text: comment + line: 5 + start: 101 +- token_type: + Whitespace: 1 + line: 5 + start: 108 +- token_type: + Text: "*/" + line: 5 + start: 109 +- token_type: Newline + line: 6 start: 111 - token_type: Whitespace: 4 - line: 5 + line: 6 start: 112 - token_type: Text: console.log(x); - line: 5 + line: 6 start: 116 - token_type: Newline - line: 5 + line: 7 start: 131 - token_type: - HtmlTagClose: script - line: 6 + Text: "" + line: 7 start: 132 - token_type: Eof - line: 6 + line: 7 start: ~ diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_style.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_style.snap index 3699fd0..9c2385e 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_style.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__lexer__tests__tokenize_style.snap @@ -3,51 +3,92 @@ source: crates/djls-template-ast/src/lexer.rs expression: tokens --- - token_type: - StyleTagOpen: "style type=\"text/css\"" + Text: "" + line: 1 + start: 7 +- token_type: Newline + line: 2 start: 23 - token_type: Whitespace: 4 line: 2 start: 24 - token_type: - Comment: - - Header styles - - /* - - "*/" + Text: /* line: 2 start: 28 -- token_type: Newline +- token_type: + Whitespace: 1 line: 2 + start: 30 +- token_type: + Text: Header + line: 2 + start: 31 +- token_type: + Whitespace: 1 + line: 2 + start: 37 +- token_type: + Text: styles + line: 2 + start: 38 +- token_type: + Whitespace: 1 + line: 2 + start: 44 +- token_type: + Text: "*/" + line: 2 + start: 45 +- token_type: Newline + line: 3 start: 47 - token_type: Whitespace: 4 line: 3 start: 48 - token_type: - Text: ".header " + Text: ".header" line: 3 start: 52 +- token_type: + Whitespace: 1 + line: 3 + start: 59 - token_type: Text: "{" line: 3 start: 60 - token_type: Newline - line: 3 + line: 4 start: 61 - token_type: Whitespace: 8 line: 4 start: 62 - token_type: - Text: "color: blue;" + Text: "color:" line: 4 start: 70 -- token_type: Newline +- token_type: + Whitespace: 1 line: 4 + start: 76 +- token_type: + Text: blue; + line: 4 + start: 77 +- token_type: Newline + line: 5 start: 82 - token_type: Whitespace: 4 @@ -58,10 +99,10 @@ expression: tokens line: 5 start: 87 - token_type: Newline - line: 5 + line: 6 start: 88 - token_type: - HtmlTagClose: style + Text: "" line: 6 start: 89 - token_type: Eof 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 e1ad42e..f4d1297 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,8 +3,7 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Html: - Comment: HTML comment + - Text: "" - Django: Comment: Django comment errors: [] 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 aa96c18..aef217a 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 @@ -12,6 +12,7 @@ nodes: - if - user.is_authenticated children: + - Text: "\n " - Django: Variable: bits: @@ -23,6 +24,7 @@ nodes: - name: default arguments: - "'Guest'" + - Text: "\n " - Django: Tag: Block: @@ -33,6 +35,7 @@ nodes: - in - user.groups children: + - Text: "\n " - Django: Tag: Block: @@ -47,12 +50,14 @@ nodes: Closing: name: endif bits: [] + - Text: "\n " - Django: Variable: bits: - group - name filters: [] + - Text: "\n " - Django: Tag: Block: @@ -68,6 +73,7 @@ nodes: Closing: name: endif bits: [] + - Text: "\n " - Django: Tag: Block: @@ -82,25 +88,27 @@ nodes: Closing: name: endif bits: [] + - Text: "\n " - Django: Tag: Branch: name: empty bits: [] children: - - Text: (no groups) + - Text: "\n (no groups)\n " - Django: Tag: Closing: name: endfor bits: [] + - Text: "\n" - Django: Tag: Branch: name: else bits: [] children: - - Text: Guest + - Text: "\n Guest\n" - Django: Tag: Closing: 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 87a4263..a4f0836 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 @@ -3,48 +3,6 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Html: - Element: - tag_name: div - attributes: - class: - Value: container - children: - - Html: - Element: - tag_name: h1 - attributes: {} - children: - - Text: Header - - Django: - Tag: - Block: - name: if - bits: - - if - - user.is_authenticated - children: - - Html: - Element: - tag_name: p - attributes: {} - children: - - Text: "Welcome " - - Django: - Variable: - bits: - - user - - name - filters: [] - - Django: - Tag: - Closing: - name: endif - bits: [] - - Html: - Element: - tag_name: footer - attributes: {} - children: - - Text: Page Footer -errors: [] + - Text: "
\n

Header

\n " +errors: + - UnclosedTag: if diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_html_tag.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_html_tag.snap index 2ec339b..cba380d 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_html_tag.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_html_tag.snap @@ -2,6 +2,6 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- -nodes: [] -errors: - - UnclosedTag: div +nodes: + - Text: "
" +errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_script.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_script.snap index 207da5a..570968a 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_script.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_script.snap @@ -2,6 +2,6 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- -nodes: [] -errors: - - UnclosedTag: script +nodes: + - Text: "\n \n \n \n
\n " + - Django: + Tag: + Block: + name: if + bits: + - if + - user.is_authenticated + children: + - Text: "\n " + - Django: + Comment: Welcome message + - Text: "\n

Welcome, " + - Django: + Variable: + bits: + - user + - name + filters: + - name: title + arguments: [] + - name: default + arguments: + - "'Guest'" + - Text: "!

\n " + - Django: + Tag: + Block: + name: if + bits: + - if + - user.is_staff + children: + - Text: "\n Admin\n " + - Django: + Tag: + Branch: + name: else + bits: [] + children: + - Text: "\n User\n " + - Django: + Tag: + Closing: + name: endif + bits: [] + - Text: "\n " + - Django: + Tag: + Closing: + name: endif + bits: [] + - Text: "\n
\n \n" errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_doctype.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_doctype.snap index 432b91f..382062d 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_doctype.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_doctype.snap @@ -3,6 +3,5 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Html: - Doctype: "!DOCTYPE html" + - Text: "" errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_tag.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_tag.snap index 272ced9..ba58268 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_tag.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_tag.snap @@ -3,12 +3,5 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Html: - Element: - tag_name: div - attributes: - class: - Value: container - children: - - Text: Hello + - Text: "
Hello
" errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_void.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_void.snap index 52faa53..aa6771b 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_void.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__html__parse_html_void.snap @@ -3,10 +3,5 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Html: - Void: - tag_name: input - attributes: - type: - Value: text + - Text: "" errors: [] 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 51115b6..a0ea1da 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 @@ -3,20 +3,5 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Script: - Element: - attributes: - type: - Value: text/javascript - children: - - Script: - Comment: - content: Single line comment - kind: SingleLine - - Text: const x = 1; - - Script: - Comment: - content: "Multi-line\n comment" - kind: MultiLine - - Text: console.log(x); + - Text: "" errors: [] 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 5f8c0a9..3256ac9 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 @@ -3,16 +3,5 @@ source: crates/djls-template-ast/src/parser.rs expression: ast --- nodes: - - Style: - Element: - attributes: - type: - Value: text/css - children: - - Style: - Comment: Header styles - - Text: ".header " - - Text: "{" - - Text: "color: blue;" - - Text: "}" + - Text: "" errors: []