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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | 3 3 1 2 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 | /*global Handlebars, handlebarsEnv, shouldThrow */ describe('ast', function() { if (!Handlebars.AST) { return; } var LOCATION_INFO = { last_line: 0, first_line: 0, first_column: 0, last_column: 0 }; function testLocationInfoStorage(node){ var properties = [ 'firstLine', 'lastLine', 'firstColumn', 'lastColumn' ], property, propertiesLen = properties.length, i; for (i = 0; i < propertiesLen; i++){ property = properties[0]; equals(node[property], 0); } } describe('MustacheNode', function() { function testEscape(open, expected) { var mustache = new handlebarsEnv.AST.MustacheNode([{}], {}, open, false); equals(mustache.escaped, expected); } it('should store args', function() { var id = {isSimple: true}, hash = {}, mustache = new handlebarsEnv.AST.MustacheNode([id, 'param1'], hash, '', false, LOCATION_INFO); equals(mustache.type, 'mustache'); equals(mustache.hash, hash); equals(mustache.escaped, true); equals(mustache.id, id); equals(mustache.params.length, 1); equals(mustache.params[0], 'param1'); equals(!!mustache.isHelper, true); testLocationInfoStorage(mustache); }); it('should accept token for escape', function() { testEscape('{{', true); testEscape('{{~', true); testEscape('{{#', true); testEscape('{{~#', true); testEscape('{{/', true); testEscape('{{~/', true); testEscape('{{^', true); testEscape('{{~^', true); testEscape('{', true); testEscape('{', true); testEscape('{{&', false); testEscape('{{~&', false); testEscape('{{{', false); testEscape('{{~{', false); }); it('should accept boolean for escape', function() { testEscape(true, true); testEscape({}, true); testEscape(false, false); testEscape(undefined, false); }); }); describe('BlockNode', function() { it('should throw on mustache mismatch (old sexpr-less version)', function() { shouldThrow(function() { var mustacheNode = new handlebarsEnv.AST.MustacheNode([{ original: 'foo'}], null, '{{', {}); new handlebarsEnv.AST.BlockNode(mustacheNode, {}, {}, {path: {original: 'bar'}}); }, Handlebars.Exception, "foo doesn't match bar"); }); it('should throw on mustache mismatch', function() { shouldThrow(function() { var sexprNode = new handlebarsEnv.AST.SexprNode([{ original: 'foo'}], null); var mustacheNode = new handlebarsEnv.AST.MustacheNode(sexprNode, null, '{{', {}); new handlebarsEnv.AST.BlockNode(mustacheNode, {}, {}, {path: {original: 'bar'}}, {first_line: 2, first_column: 2}); }, Handlebars.Exception, "foo doesn't match bar - 2:2"); }); it('stores location info', function(){ var sexprNode = new handlebarsEnv.AST.SexprNode([{ original: 'foo'}], null); var mustacheNode = new handlebarsEnv.AST.MustacheNode(sexprNode, null, '{{', {}); var block = new handlebarsEnv.AST.BlockNode(mustacheNode, {statements: [], strip: {}}, {statements: [], strip: {}}, { strip: {}, path: {original: 'foo'} }, LOCATION_INFO); testLocationInfoStorage(block); }); }); describe('IdNode', function() { it('should throw on invalid path', function() { shouldThrow(function() { new handlebarsEnv.AST.IdNode([ {part: 'foo'}, {part: '..'}, {part: 'bar'} ], {first_line: 1, first_column: 1}); }, Handlebars.Exception, "Invalid path: foo.. - 1:1"); shouldThrow(function() { new handlebarsEnv.AST.IdNode([ {part: 'foo'}, {part: '.'}, {part: 'bar'} ], {first_line: 1, first_column: 1}); }, Handlebars.Exception, "Invalid path: foo. - 1:1"); shouldThrow(function() { new handlebarsEnv.AST.IdNode([ {part: 'foo'}, {part: 'this'}, {part: 'bar'} ], {first_line: 1, first_column: 1}); }, Handlebars.Exception, "Invalid path: foothis - 1:1"); }); it('stores location info', function(){ var idNode = new handlebarsEnv.AST.IdNode([], LOCATION_INFO); testLocationInfoStorage(idNode); }); }); describe("HashNode", function(){ it('stores location info', function(){ var hash = new handlebarsEnv.AST.HashNode([], LOCATION_INFO); testLocationInfoStorage(hash); }); }); describe("ContentNode", function(){ it('stores location info', function(){ var content = new handlebarsEnv.AST.ContentNode("HI", LOCATION_INFO); testLocationInfoStorage(content); }); }); describe("CommentNode", function(){ it('stores location info', function(){ var comment = new handlebarsEnv.AST.CommentNode("HI", LOCATION_INFO); testLocationInfoStorage(comment); }); }); describe("NumberNode", function(){ it('stores location info', function(){ var integer = new handlebarsEnv.AST.NumberNode("6", LOCATION_INFO); testLocationInfoStorage(integer); }); }); describe("StringNode", function(){ it('stores location info', function(){ var string = new handlebarsEnv.AST.StringNode("6", LOCATION_INFO); testLocationInfoStorage(string); }); }); describe("BooleanNode", function(){ it('stores location info', function(){ var bool = new handlebarsEnv.AST.BooleanNode("true", LOCATION_INFO); testLocationInfoStorage(bool); }); }); describe("DataNode", function(){ it('stores location info', function(){ var data = new handlebarsEnv.AST.DataNode("YES", LOCATION_INFO); testLocationInfoStorage(data); }); }); describe("PartialNameNode", function(){ it('stores location info', function(){ var pnn = new handlebarsEnv.AST.PartialNameNode({original: "YES"}, LOCATION_INFO); testLocationInfoStorage(pnn); }); }); describe("PartialNode", function(){ it('stores location info', function(){ var pn = new handlebarsEnv.AST.PartialNode("so_partial", {}, {}, {}, LOCATION_INFO); testLocationInfoStorage(pn); }); }); describe("ProgramNode", function(){ describe("storing location info", function(){ it("stores when `inverse` argument isn't passed", function(){ var pn = new handlebarsEnv.AST.ProgramNode(false, [], LOCATION_INFO); testLocationInfoStorage(pn); }); it("stores when `inverse` or `stripInverse` arguments passed", function(){ var pn = new handlebarsEnv.AST.ProgramNode(false, [], {strip: {}}, undefined, LOCATION_INFO); testLocationInfoStorage(pn); var clone = { strip: {}, firstLine: 0, lastLine: 0, firstColumn: 0, lastColumn: 0 }; pn = new handlebarsEnv.AST.ProgramNode(false, [], {strip: {}}, [ clone ], LOCATION_INFO); testLocationInfoStorage(pn); // Assert that the newly created ProgramNode has the same location // information as the inverse testLocationInfoStorage(pn.inverse); }); }); }); describe("Line Numbers", function(){ var ast, statements; function testColumns(node, firstLine, lastLine, firstColumn, lastColumn){ equals(node.firstLine, firstLine); equals(node.lastLine, lastLine); equals(node.firstColumn, firstColumn); equals(node.lastColumn, lastColumn); } ast = Handlebars.parse("line 1 {{line1Token}}\n line 2 {{line2token}}\n line 3 {{#blockHelperOnLine3}}\nline 4{{line4token}}\n" + "line5{{else}}\n{{line6Token}}\n{{/blockHelperOnLine3}}"); statements = ast.statements; it('gets ContentNode line numbers', function(){ var contentNode = statements[0]; testColumns(contentNode, 1, 1, 0, 7); }); it('gets MustacheNode line numbers', function(){ var mustacheNode = statements[1]; testColumns(mustacheNode, 1, 1, 7, 21); }); it('gets line numbers correct when newlines appear', function(){ testColumns(statements[2], 1, 2, 21, 0); testColumns(statements[3], 2, 2, 0, 8); }); it('gets MustacheNode line numbers correct across newlines', function(){ var secondMustacheNode = statements[4]; testColumns(secondMustacheNode, 2, 2, 8, 22); }); it('gets the block helper information correct', function(){ var blockHelperNode = statements[7]; testColumns(blockHelperNode, 3, 7, 8, 23); }); it('correctly records the line numbers of an inverse of a block helper', function(){ var blockHelperNode = statements[7], inverse = blockHelperNode.inverse; testColumns(inverse, 5, 6, 13, 0); }); }); describe('standalone flags', function(){ describe('mustache', function() { it('does not mark mustaches as standalone', function() { var ast = Handlebars.parse(' {{comment}} '); equals(ast.statements[0].omit, undefined); equals(ast.statements[2].omit, undefined); }); }); describe('blocks', function() { it('marks block mustaches as standalone', function() { var ast = Handlebars.parse(' {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '), block = ast.statements[1]; equals(ast.statements[0].omit, true); equals(block.program.statements[0].omit, true); equals(block.program.statements[1].string, 'foo\n'); equals(block.program.statements[2].omit, true); equals(block.inverse.statements[0].omit, true); equals(block.inverse.statements[1].string, ' bar \n'); equals(block.inverse.statements[2].omit, true); equals(ast.statements[2].omit, true); }); it('marks initial block mustaches as standalone', function() { var ast = Handlebars.parse('{{# comment}} \nfoo\n {{/comment}}'), block = ast.statements[0]; equals(block.program.statements[0].omit, true); equals(block.program.statements[1].string, 'foo\n'); equals(block.program.statements[2].omit, true); }); it('marks mustaches with children as standalone', function() { var ast = Handlebars.parse('{{# comment}} \n{{foo}}\n {{/comment}}'), block = ast.statements[0]; equals(block.program.statements[0].omit, true); equals(block.program.statements[1].id.original, 'foo'); equals(block.program.statements[2].omit, undefined); equals(block.program.statements[3].omit, true); }); it('marks nested block mustaches as standalone', function() { var ast = Handlebars.parse('{{#foo}} \n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} \n{{/foo}}'), statements = ast.statements[0].program.statements, block = statements[1]; equals(statements[0].omit, true); equals(block.program.statements[0].omit, true); equals(block.program.statements[1].string, 'foo\n'); equals(block.program.statements[2].omit, true); equals(block.inverse.statements[0].omit, true); equals(block.inverse.statements[1].string, ' bar \n'); equals(block.inverse.statements[2].omit, true); equals(statements[0].omit, true); }); it('does not mark nested block mustaches as standalone', function() { var ast = Handlebars.parse('{{#foo}} {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} {{/foo}}'), statements = ast.statements[0].program.statements, block = statements[1]; equals(statements[0].omit, undefined); equals(block.program.statements[0].omit, undefined); equals(block.program.statements[1].string, 'foo\n'); equals(block.program.statements[2].omit, true); equals(block.inverse.statements[0].omit, true); equals(block.inverse.statements[1].string, ' bar \n'); equals(block.inverse.statements[2].omit, undefined); equals(statements[0].omit, undefined); }); it('does not mark nested initial block mustaches as standalone', function() { var ast = Handlebars.parse('{{#foo}}{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}}{{/foo}}'), statements = ast.statements[0].program.statements, block = statements[0]; equals(block.program.statements[0].omit, undefined); equals(block.program.statements[1].string, 'foo\n'); equals(block.program.statements[2].omit, true); equals(block.inverse.statements[0].omit, true); equals(block.inverse.statements[1].string, ' bar \n'); equals(block.inverse.statements[2].omit, undefined); equals(statements[0].omit, undefined); }); it('marks column 0 block mustaches as standalone', function() { var ast = Handlebars.parse('test\n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '), block = ast.statements[1]; equals(ast.statements[0].omit, undefined); equals(block.program.statements[0].omit, true); equals(block.program.statements[1].string, 'foo\n'); equals(block.program.statements[2].omit, true); equals(block.inverse.statements[0].omit, true); equals(block.inverse.statements[1].string, ' bar \n'); equals(block.inverse.statements[2].omit, true); equals(ast.statements[2].omit, true); }); }); describe('partials', function() { it('marks partial as standalone', function() { var ast = Handlebars.parse('{{> partial }} '); equals(ast.statements[1].omit, true); }); it('marks indented partial as standalone', function() { var ast = Handlebars.parse(' {{> partial }} '); equals(ast.statements[0].omit, true); equals(ast.statements[1].indent, ' '); equals(ast.statements[2].omit, true); }); it('marks those around content as not standalone', function() { var ast = Handlebars.parse('a{{> partial }}'); equals(ast.statements[0].omit, undefined); ast = Handlebars.parse('{{> partial }}a'); equals(ast.statements[1].omit, undefined); }); }); describe('comments', function() { it('marks comment as standalone', function() { var ast = Handlebars.parse('{{! comment }} '); equals(ast.statements[1].omit, true); }); it('marks indented comment as standalone', function() { var ast = Handlebars.parse(' {{! comment }} '); equals(ast.statements[0].omit, true); equals(ast.statements[2].omit, true); }); it('marks those around content as not standalone', function() { var ast = Handlebars.parse('a{{! comment }}'); equals(ast.statements[0].omit, undefined); ast = Handlebars.parse('{{! comment }}a'); equals(ast.statements[1].omit, undefined); }); }); }); }); |