Beispiel #1
0
export function astEqual(actual, expected, message?) {
  if (typeof actual === 'string') {
    actual = parse(actual);
  }
  if (typeof expected === 'string') {
    expected = parse(expected);
  }

  actual = normalizeNode(actual);
  expected = normalizeNode(expected);

  deepEqual(actual, expected, message);
}
Beispiel #2
0
test("html elements with nested blocks", function() {
  let ast = parse(`
    <div>
      {{#if isSingleError}}
        Single error here!
      {{else if errors}}
        Multiple errors here!
      {{else}}
        No errors found!
      {{/if}} <p>Hi there!</p>
    </div>
  `);

  let [,div] = ast.body;
  let [,ifBlock,,p] = div.children;
  let inverseBlock = ifBlock.inverse;
  let [nestedIfBlock] = inverseBlock.body;
  let nestedIfInverseBlock = nestedIfBlock.inverse;

  locEqual(div, 2, 4, 10, 10, 'div element');
  locEqual(ifBlock, 3, 6, 9, 13, 'outer if block');
  locEqual(inverseBlock, 5, 6, 9, 6, 'inverse block');
  locEqual(nestedIfBlock, 5, 6, 9, 6, 'nested if block');
  locEqual(nestedIfInverseBlock, 7, 6, 9, 6, 'nested inverse block');
  locEqual(p, 9, 14, 9, 30, 'p');
});
Beispiel #3
0
test("allow {{undefined}} to be passed as a param", function() {
  let ast = parse("{{foo undefined}}");

  astEqual(ast, b.program([
    b.mustache(b.path('foo'), [b.undefined()])
  ]));
});
test('Mustaches', function() {
  let ast = parse(
    `{{mustache}}` +
    `{{mustache param1 param2 key1=value key2=value}}`
  );

  traversalEqual(ast, [
    ['enter', ast],
    ['enter', ast.body[0]],
    ['enter', ast.body[0].path],
    ['exit',  ast.body[0].path],
    ['enter', ast.body[0].hash],
    ['exit',  ast.body[0].hash],
    ['exit',  ast.body[0]],
    ['enter', ast.body[1]],
    ['enter', ast.body[1].path],
    ['exit',  ast.body[1].path],
    ['enter', ast.body[1].params[0]],
    ['exit',  ast.body[1].params[0]],
    ['enter', ast.body[1].params[1]],
    ['exit',  ast.body[1].params[1]],
    ['enter', ast.body[1].hash],
    ['enter', ast.body[1].hash.pairs[0]],
    ['enter', ast.body[1].hash.pairs[0].value],
    ['exit',  ast.body[1].hash.pairs[0].value],
    ['exit',  ast.body[1].hash.pairs[0]],
    ['enter', ast.body[1].hash.pairs[1]],
    ['enter', ast.body[1].hash.pairs[1].value],
    ['exit',  ast.body[1].hash.pairs[1].value],
    ['exit',  ast.body[1].hash.pairs[1]],
    ['exit',  ast.body[1].hash],
    ['exit',  ast.body[1]],
    ['exit',  ast]
  ]);
});
Beispiel #5
0
test('AST plugins can be chained', function() {
  expect(2);

  let expected = "OOOPS, MESSED THAT UP!";

  function Plugin() { }
  Plugin.prototype.transform = function() {
    return expected;
  };

  function SecondaryPlugin() { }
  SecondaryPlugin.prototype.transform = function(ast) {
    equal(ast, expected, 'return value from AST transform is used');

    return 'BOOM!';
  };

  let ast = parse('<div></div>', {
    plugins: {
      ast: [
        Plugin,
        SecondaryPlugin
      ]
    }
  });

  equal(ast, 'BOOM!', 'return value from last AST transform is used');
});
test('Element modifiers', function() {
  let ast = parse(`<div {{modifier}}{{modifier param1 param2 key1=value key2=value}}></div>`);

  traversalEqual(ast, [
    ['enter', ast],
    ['enter', ast.body[0]],
    ['enter', ast.body[0].modifiers[0]],
    ['enter', ast.body[0].modifiers[0].path],
    ['exit',  ast.body[0].modifiers[0].path],
    ['enter', ast.body[0].modifiers[0].hash],
    ['exit',  ast.body[0].modifiers[0].hash],
    ['exit',  ast.body[0].modifiers[0]],
    ['enter', ast.body[0].modifiers[1]],
    ['enter', ast.body[0].modifiers[1].path],
    ['exit',  ast.body[0].modifiers[1].path],
    ['enter', ast.body[0].modifiers[1].params[0]],
    ['exit',  ast.body[0].modifiers[1].params[0]],
    ['enter', ast.body[0].modifiers[1].params[1]],
    ['exit',  ast.body[0].modifiers[1].params[1]],
    ['enter', ast.body[0].modifiers[1].hash],
    ['enter', ast.body[0].modifiers[1].hash.pairs[0]],
    ['enter', ast.body[0].modifiers[1].hash.pairs[0].value],
    ['exit',  ast.body[0].modifiers[1].hash.pairs[0].value],
    ['exit' , ast.body[0].modifiers[1].hash.pairs[0]],
    ['enter', ast.body[0].modifiers[1].hash.pairs[1]],
    ['enter', ast.body[0].modifiers[1].hash.pairs[1].value],
    ['exit',  ast.body[0].modifiers[1].hash.pairs[1].value],
    ['exit' , ast.body[0].modifiers[1].hash.pairs[1]],
    ['exit',  ast.body[0].modifiers[1].hash],
    ['exit',  ast.body[0].modifiers[1]],
    ['exit',  ast.body[0]],
    ['exit',  ast]
  ]);
});
Beispiel #7
0
test("allow {{undefined}} to be passed as helper name", function() {
  let ast = parse("{{undefined}}");

  astEqual(ast, b.program([
    b.mustache(b.undefined())
  ]));
});
Beispiel #8
0
test("allow simple AST to be passed", function() {
  let ast = parse(handlebarsParse("simple"));

  astEqual(ast, b.program([
    b.text("simple")
  ]));
});
Beispiel #9
0
test("element attribute", function() {
  let ast = parse(`
    <div data-foo="blah"
      data-derp="lolol"
data-barf="herpy"
  data-qux=lolnoquotes
    data-hurky="some {{thing}} here">
      Hi, fivetanley!
    </div>
  `);

  let [,div] = ast.body;
  let [dataFoo,dataDerp,dataBarf, dataQux, dataHurky] = div.attributes;

  locEqual(dataFoo, 2, 9, 2, 24);
  locEqual(dataDerp, 3, 6, 3, 23);
  locEqual(dataBarf, 4, 0, 4, 17);
  locEqual(dataQux, 5, 2, 5, 22);

  locEqual(dataFoo.value, 2, 18, 2, 24);
  locEqual(dataDerp.value, 3, 16, 3, 23);
  locEqual(dataBarf.value, 4, 10, 4, 17);
  locEqual(dataQux.value, 5, 11, 5, 22);
  locEqual(dataHurky.value, 6, 15, 6, 36);
});
Beispiel #10
0
test('Elements and attributes', function() {
  let ast = parse(`<div id="id" class="large {{classes}}" value={{value}}><b></b><b></b></div>`);

  traversalEqual(ast, [
    ['enter', ast],
    ['enter', ast.body[0]],
    ['enter', ast.body[0].attributes[0]],
    ['enter', ast.body[0].attributes[0].value],
    ['exit',  ast.body[0].attributes[0].value],
    ['exit',  ast.body[0].attributes[0]],
    ['enter', ast.body[0].attributes[1]],
    ['enter', ast.body[0].attributes[1].value],
    ['enter', ast.body[0].attributes[1].value.parts[0]],
    ['exit',  ast.body[0].attributes[1].value.parts[0]],
    ['enter', ast.body[0].attributes[1].value.parts[1]],
    ['exit',  ast.body[0].attributes[1].value.parts[1]],
    ['exit',  ast.body[0].attributes[1].value],
    ['exit',  ast.body[0].attributes[1]],
    ['enter', ast.body[0].attributes[2]],
    ['enter', ast.body[0].attributes[2].value],
    ['enter', ast.body[0].attributes[2].value.path],
    ['exit',  ast.body[0].attributes[2].value.path],
    ['enter', ast.body[0].attributes[2].value.hash],
    ['exit',  ast.body[0].attributes[2].value.hash],
    ['exit',  ast.body[0].attributes[2].value],
    ['exit',  ast.body[0].attributes[2]],
    ['enter', ast.body[0].children[0]],
    ['exit',  ast.body[0].children[0]],
    ['enter', ast.body[0].children[1]],
    ['exit',  ast.body[0].children[1]],
    ['exit',  ast.body[0]],
    ['exit',  ast]
  ]);
});