# Compatibility with Handlebars.js

Templates render as Handlebars.js 4.7.10 renders them, and parse as @handlebars/parser 2.2.4
parses them. This page lists where they differ, and how that is tested.

## Where rendering differs

- Decorators other than `{{#*inline}}` are errors. A value called with arguments, like
  `{{name "x"}}`, is `name is not a function`, where Handlebars.js reports a JavaScript
  `TypeError` from its generated code. Functions in data aren't called as helpers.
- In strict mode, a missing value's error names the object it was looked up in `data`, where
  Handlebars.js writes it as `[object Object]`: `"first_name" not defined in data`. Other
  values are written as Handlebars.js writes them, as in `"first" not defined in undefined`.
- A missing value is `nil` to a helper, where Handlebars.js tells `undefined` from `null`.
- `#each` over a map can't follow the order keys were added in, since Elixir maps don't keep
  it. It walks keys that are array indexes in numeric order, then the others sorted. Decode
  JSON with `Braces.Handlebars.decode_json/1` to keep its order.
- `#with` and blocks only step into a new context, for `../`, when the value isn't equal to
  the current one. Handlebars.js compares objects by identity, and we compare them by value,
  so a block over an equal copy of the context doesn't add a level: in
  `{{#with a}}{{#with ../b}}{{../k}}{{/with}}{{/with}}` with equal `a` and `b`, `../k` is
  `a.k` in Handlebars.js and the root's `k` here.
- Output is joined as text. Handlebars.js joins a block's outputs with JavaScript's `+`, which
  adds numbers: `{{{a}}}{{{b}}}` with 1 and 2 gives `3` there, and `12` here.
- Templates are parsed by our port of @handlebars/parser 2.2.4, not by the parser Handlebars.js
  4.7.10 bundles. That parser rejects a few templates 2.2.4 accepts (`{{a.0}}`, `{{(helper)}}`),
  and strips the whitespace around a few `{{else if}}` chains differently.

## How it's tested

### Rendering

The tests compare it with Handlebars.js 4.7.10 rendering @handlebars/parser 2.2.4's AST: the
spec's renders, those with custom helpers through Elixir equivalents of its JavaScript helpers
(`test/support/corpus_helpers.ex`), cases for each feature in strict mode and not, and a
StreamData property of random templates and data.

### Compiled templates

The tests compile every template of the Handlebars.js corpus with a recorded render, with its
helpers and partials, and the random templates of the StreamData property, and check that each
renders as `Braces.Handlebars.render/3` does. A module with helpers and partial files is checked
against Handlebars.js too.

### Parser

- `test/handlebars/handlebars_js_corpus_test.exs` takes every template in handlebars.js 4.7.10's
  spec files and its Mustache spec, 731 of them. Our AST, locations included, and its printed
  form must equal what @handlebars/parser 2.2.4 gives, and templates it rejects we reject.
  `Braces.Handlebars.to_string/1` of each AST must parse back to the same AST.
  209 of them are rendered by Handlebars.js in the spec without custom helpers or partials,
  and 139 with them; all must render the same with `Braces.Handlebars.render/3`, the second with
  Elixir equivalents of the spec's JavaScript helpers, except the 3 renders whose helpers
  `test/support/corpus_helpers.ex` lists as having none, with why.
  `test/fixtures/handlebars_js_corpus.json` holds upstream's results;
  `test/js/extract_handlebars_corpus.js` rebuilds it from a handlebars.js checkout.
- `test/handlebars/parser_spec_test.exs` runs handlebars-parser's `spec/parser.js`, whose 105
  cases are copied into `test/fixtures/handlebars_parser_spec.json`, and
  `test/handlebars/ast_spec_test.exs` ports `spec/ast.js`.
