summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/helpers.js
blob: 1a2bd262dd4bfca2f80f63648ba168045d332568 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import Exception from "../exception";
import AST from "./ast";

export function stripFlags(open, close) {
  return {
    left: open.charAt(2) === '~',
    right: close.charAt(close.length-3) === '~'
  };
}

export function prepareBlock(mustache, program, inverseAndProgram, close, inverted, locInfo) {
  if (mustache.sexpr.id.original !== close.path.original) {
    throw new Exception(mustache.sexpr.id.original + " doesn't match " + close.path.original, mustache);
  }

  var inverse, strip;

  strip = {
    left: mustache.strip.left,
    right: close.strip.right
  };

  if (inverseAndProgram) {
    inverse = inverseAndProgram.program;
    var inverseStrip = inverseAndProgram.strip;

    program.strip.left = mustache.strip.right;
    program.strip.right = inverseStrip.left;
    inverse.strip.left = inverseStrip.right;
    inverse.strip.right = close.strip.left;
  } else {
    program.strip.left = mustache.strip.right;
    program.strip.right = close.strip.left;
  }

  if (inverted) {
    return new AST.BlockNode(mustache, inverse, program, strip, locInfo);
  } else {
    return new AST.BlockNode(mustache, program, inverse, strip, locInfo);
  }
}