From a526d934cea17ff1b76ddad5c5f2934fb85c11cb Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Wed, 19 Aug 2026 08:09:12 +0200 Subject: fix(render): reject empty tag paths, sharpen the raw-form test F1: test_no_raw_or_partial_form's first assertion only excluded one literal shape (Ok [Var ["{name"]]), so it could not actually catch a future raw/unescaped constructor under a different name. Replace it with an assertion of the real parse result for {{{name}}} (Ok [Var ["{name"]; Text "}"]), documented behaviour rather than a guarantee this test cannot check -- the real guarantee is structural: node has exactly four constructors and none of them is raw. F2: {{.}}, {{#}}, {{^}} and {{/}} used to parse to a Var/Section/ Inverted with an empty path, reachable but never designed. This engine has no "current context" for a bare dot to mean, so a bare-dot or empty-sigil path is now a parse error at lex time, covering all four sigil forms via one path helper. The existing "empty tag {{}}" branch is unchanged and still reachable (a fully empty body is a distinct case from a sigil with an empty path). --- lib/render/template.ml | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/render/template.ml b/lib/render/template.ml index 8de91a6..3e35d53 100644 --- a/lib/render/template.ml +++ b/lib/render/template.ml @@ -38,13 +38,23 @@ let lex src = let b = trim body in if b = "" then Error "empty tag {{}}" else + (* Every sigil form (var, #, ^, /) resolves a dotted path; an + empty path ("{{.}}", "{{#}}", "{{^}}", "{{/}}") names + nothing -- there is no "current context" concept for a bare + dot to mean, so it is a parse error rather than an invented + behaviour Task 3 would have to define. *) + let with_path ctor rest = + match path rest with + | [] -> Error "empty tag path: {{.}} and {{#}} name nothing" + | p -> Ok (ctor p) + in match b.[0] with - | '#' -> Ok (TOpen (path (String.sub b 1 (String.length b - 1)))) - | '^' -> Ok (TInv (path (String.sub b 1 (String.length b - 1)))) - | '/' -> Ok (TClose (path (String.sub b 1 (String.length b - 1)))) + | '#' -> with_path (fun p -> TOpen p) (String.sub b 1 (String.length b - 1)) + | '^' -> with_path (fun p -> TInv p) (String.sub b 1 (String.length b - 1)) + | '/' -> with_path (fun p -> TClose p) (String.sub b 1 (String.length b - 1)) | '!' -> Ok TComment | '>' -> Error "partials are not supported: a template may not include another file" - | _ -> Ok (TVar (path b)) + | _ -> with_path (fun p -> TVar p) b in (match tag with | Error e -> Error e -- cgit v1.3