Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in case an operator is used as a delimiter #1095

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion askama_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ with-rocket = []
with-warp = []

[dependencies]
parser = { package = "askama_parser", version = "0.3", path = "../askama_parser" }
parser = { package = "askama_parser", version = "0.3.1", path = "../askama_parser" }
mime = "0.3"
mime_guess = "2"
proc-macro2 = "1"
Expand Down
30 changes: 17 additions & 13 deletions askama_derive/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{env, fs};
use serde::Deserialize;

use crate::{CompileError, CRATE};
use parser::expr::TWO_PLUS_CHAR_OPS;
use parser::node::Whitespace;
use parser::Syntax;

Expand Down Expand Up @@ -147,20 +148,23 @@ impl<'a> TryInto<Syntax<'a>> for RawSyntax<'a> {
comment_end: self.comment_end.unwrap_or(default.comment_end),
};

for s in [
syntax.block_start,
syntax.block_end,
syntax.expr_start,
syntax.expr_end,
syntax.comment_start,
syntax.comment_end,
for (s, kind) in [
(syntax.block_start, "block_start"),
(syntax.block_end, "block_end"),
(syntax.expr_start, "expr_start"),
(syntax.expr_end, "expr_end"),
(syntax.comment_start, "comment_start"),
(syntax.comment_end, "comment_end"),
] {
if s.len() < 2 {
return Err(
format!("delimiters must be at least two characters long: {s:?}").into(),
);
return Err(format!(
"{kind} delimiter must be at least two characters long: {s:?}"
)
.into());
} else if s.chars().any(|c| c.is_whitespace()) {
return Err(format!("delimiters may not contain white spaces: {s:?}").into());
return Err(format!("{kind} delimiter may not contain whitespace: {s:?}").into());
} else if TWO_PLUS_CHAR_OPS.contains(&s) {
return Err(format!("{kind} delimiter may not contain operators: {s:?}").into());
}
}

Expand Down Expand Up @@ -486,7 +490,7 @@ mod tests {
let config = Config::new(raw_config, None);
assert_eq!(
config.unwrap_err().msg,
r#"delimiters must be at least two characters long: "<""#,
r#"block_start delimiter must be at least two characters long: "<""#,
);

let raw_config = r#"
Expand All @@ -497,7 +501,7 @@ mod tests {
let config = Config::new(raw_config, None);
assert_eq!(
config.unwrap_err().msg,
r#"delimiters may not contain white spaces: " {{ ""#,
r#"block_start delimiter may not contain whitespace: " {{ ""#,
);

let raw_config = r#"
Expand Down
2 changes: 1 addition & 1 deletion askama_parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "askama_parser"
version = "0.3.0"
version = "0.3.1"
description = "Parser for Askama templates"
documentation = "https://docs.rs/askama"
keywords = ["markup", "template", "jinja2", "html"]
Expand Down
4 changes: 4 additions & 0 deletions askama_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl<'a> Expr<'a> {
))(i)
}

// Keep in sync with `TWO_PLUS_CHAR_OPS`, below
expr_prec_layer!(or, and, "||");
expr_prec_layer!(and, compare, "&&");
expr_prec_layer!(compare, bor, "==", "!=", ">=", ">", "<=", "<");
Expand Down Expand Up @@ -430,3 +431,6 @@ impl<'a> Suffix<'a> {
map(preceded(take_till(not_ws), char('?')), |_| Self::Try)(i)
}
}

pub const TWO_PLUS_CHAR_OPS: &[&str] =
&["||", "&&", "==", "!=", ">=", "<=", "<<", ">>", "..", "..="];
Loading