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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | 1 1 1 1 1 1 129 129 45 84 279 279 68 16 16 4 3 13 1595 1595 1595 1595 1595 1595 1595 1595 1595 646 5054 1595 5302 1595 1595 3165 1593 1593 2 1593 631 631 631 631 631 52 52 2 631 583 583 505 583 126 583 583 583 232 351 32 32 32 32 32 319 319 319 319 319 583 60 60 60 96 60 96 60 86 86 86 2 84 86 4 82 82 86 86 1553 1419 913 911 851 60 894 894 894 894 894 894 186 186 38 148 108 40 40 40 186 468 468 210 258 2 256 256 256 965 965 154 811 236 575 1588 1588 1588 1588 16 1572 68 68 42 50 40 14161 1636 56 54 54 1548 1548 1548 1548 912 912 10 902 8 1548 1080 186 468 502 598 62 6 62 62 62 4 536 54 536 468 468 468 468 468 58 410 468 1 801 2 799 799 767 799 180 799 785 783 1 155 2 153 153 71 153 48 153 1 153 153 153 153 153 174 150 174 153 2 1 2 153 4 2 4 153 1 621 241 380 312 342 81 231 | "use strict"; var Exception = require("../exception")["default"]; var isArray = require("../utils").isArray; var slice = [].slice; function Compiler() {} exports.Compiler = Compiler;// the foundHelper register will disambiguate helper lookup from finding a // function in a context. This is necessary for mustache compatibility, which // requires that context functions in blocks are evaluated by blockHelperMissing, // and then proceed as if the resulting value was provided to blockHelperMissing. Compiler.prototype = { compiler: Compiler, equals: function(other) { var len = this.opcodes.length; if (other.opcodes.length !== len) { return false; } for (var i = 0; i < len; i++) { var opcode = this.opcodes[i], otherOpcode = other.opcodes[i]; if (opcode.opcode !== otherOpcode.opcode || !argEquals(opcode.args, otherOpcode.args)) { return false; } } // We know that length is the same between the two arrays because they are directly tied // to the opcode behavior above. len = this.children.length; for (i = 0; i < len; i++) { if (!this.children[i].equals(other.children[i])) { return false; } } return true; }, guid: 0, compile: function(program, options) { this.opcodes = []; this.children = []; this.depths = {list: []}; this.options = options; this.stringParams = options.stringParams; this.trackIds = options.trackIds; // These changes will propagate to the other compiler components var knownHelpers = this.options.knownHelpers; this.options.knownHelpers = { 'helperMissing': true, 'blockHelperMissing': true, 'each': true, 'if': true, 'unless': true, 'with': true, 'log': true, 'lookup': true }; if (knownHelpers) { for (var name in knownHelpers) { this.options.knownHelpers[name] = knownHelpers[name]; } } return this.accept(program); }, accept: function(node) { return this[node.type](node); }, program: function(program) { var statements = program.statements; for(var i=0, l=statements.length; i<l; i++) { this.accept(statements[i]); } this.isSimple = l === 1; this.depths.list = this.depths.list.sort(function(a, b) { return a - b; }); return this; }, compileProgram: function(program) { var result = new this.compiler().compile(program, this.options); var guid = this.guid++, depth; this.usePartial = this.usePartial || result.usePartial; this.children[guid] = result; for(var i=0, l=result.depths.list.length; i<l; i++) { depth = result.depths.list[i]; if(depth < 2) { continue; } else { this.addDepth(depth - 1); } } return guid; }, block: function(block) { var mustache = block.mustache, program = block.program, inverse = block.inverse; if (program) { program = this.compileProgram(program); } if (inverse) { inverse = this.compileProgram(inverse); } var sexpr = mustache.sexpr; var type = this.classifySexpr(sexpr); if (type === "helper") { this.helperSexpr(sexpr, program, inverse); } else if (type === "simple") { this.simpleSexpr(sexpr); // now that the simple mustache is resolved, we need to // evaluate it by executing `blockHelperMissing` this.opcode('pushProgram', program); this.opcode('pushProgram', inverse); this.opcode('emptyHash'); this.opcode('blockValue', sexpr.id.original); } else { this.ambiguousSexpr(sexpr, program, inverse); // now that the simple mustache is resolved, we need to // evaluate it by executing `blockHelperMissing` this.opcode('pushProgram', program); this.opcode('pushProgram', inverse); this.opcode('emptyHash'); this.opcode('ambiguousBlockValue'); } this.opcode('append'); }, hash: function(hash) { var pairs = hash.pairs, i, l; this.opcode('pushHash'); for(i=0, l=pairs.length; i<l; i++) { this.pushParam(pairs[i][1]); } while(i--) { this.opcode('assignToHash', pairs[i][0]); } this.opcode('popHash'); }, partial: function(partial) { var partialName = partial.partialName; this.usePartial = true; if (partial.hash) { this.accept(partial.hash); } else { this.opcode('push', 'undefined'); } if (partial.context) { this.accept(partial.context); } else { this.opcode('getContext', 0); this.opcode('pushContext'); } this.opcode('invokePartial', partialName.name, partial.indent || ''); this.opcode('append'); }, content: function(content) { if (content.string) { this.opcode('appendContent', content.string); } }, mustache: function(mustache) { this.sexpr(mustache.sexpr); if(mustache.escaped && !this.options.noEscape) { this.opcode('appendEscaped'); } else { this.opcode('append'); } }, ambiguousSexpr: function(sexpr, program, inverse) { var id = sexpr.id, name = id.parts[0], isBlock = program != null || inverse != null; this.opcode('getContext', id.depth); this.opcode('pushProgram', program); this.opcode('pushProgram', inverse); this.ID(id); this.opcode('invokeAmbiguous', name, isBlock); }, simpleSexpr: function(sexpr) { var id = sexpr.id; if (id.type === 'DATA') { this.DATA(id); } else if (id.parts.length) { this.ID(id); } else { // Simplified ID for `this` this.addDepth(id.depth); this.opcode('getContext', id.depth); this.opcode('pushContext'); } this.opcode('resolvePossibleLambda'); }, helperSexpr: function(sexpr, program, inverse) { var params = this.setupFullMustacheParams(sexpr, program, inverse), id = sexpr.id, name = id.parts[0]; if (this.options.knownHelpers[name]) { this.opcode('invokeKnownHelper', params.length, name); } else if (this.options.knownHelpersOnly) { throw new Exception("You specified knownHelpersOnly, but used the unknown helper " + name, sexpr); } else { id.falsy = true; this.ID(id); this.opcode('invokeHelper', params.length, id.original, id.isSimple); } }, sexpr: function(sexpr) { var type = this.classifySexpr(sexpr); if (type === "simple") { this.simpleSexpr(sexpr); } else if (type === "helper") { this.helperSexpr(sexpr); } else { this.ambiguousSexpr(sexpr); } }, ID: function(id) { this.addDepth(id.depth); this.opcode('getContext', id.depth); var name = id.parts[0]; if (!name) { // Context reference, i.e. `{{foo .}}` or `{{foo ..}}` this.opcode('pushContext'); } else { this.opcode('lookupOnContext', id.parts, id.falsy, id.isScoped); } }, DATA: function(data) { this.options.data = true; this.opcode('lookupData', data.id.depth, data.id.parts); }, STRING: function(string) { this.opcode('pushString', string.string); }, NUMBER: function(number) { this.opcode('pushLiteral', number.number); }, BOOLEAN: function(bool) { this.opcode('pushLiteral', bool.bool); }, comment: function() {}, // HELPERS opcode: function(name) { this.opcodes.push({ opcode: name, args: slice.call(arguments, 1) }); }, addDepth: function(depth) { if(depth === 0) { return; } if(!this.depths[depth]) { this.depths[depth] = true; this.depths.list.push(depth); } }, classifySexpr: function(sexpr) { var isHelper = sexpr.isHelper; var isEligible = sexpr.eligibleHelper; var options = this.options; // if ambiguous, we can possibly resolve the ambiguity now // An eligible helper is one that does not have a complex path, i.e. `this.foo`, `../foo` etc. if (isEligible && !isHelper) { var name = sexpr.id.parts[0]; if (options.knownHelpers[name]) { isHelper = true; } else if (options.knownHelpersOnly) { isEligible = false; } } if (isHelper) { return "helper"; } else if (isEligible) { return "ambiguous"; } else { return "simple"; } }, pushParams: function(params) { for(var i=0, l=params.length; i<l; i++) { this.pushParam(params[i]); } }, pushParam: function(val) { if (this.stringParams) { if(val.depth) { this.addDepth(val.depth); } this.opcode('getContext', val.depth || 0); this.opcode('pushStringParam', val.stringModeValue, val.type); if (val.type === 'sexpr') { // Subexpressions get evaluated and passed in // in string params mode. this.sexpr(val); } } else { if (this.trackIds) { this.opcode('pushId', val.type, val.idName || val.stringModeValue); } this.accept(val); } }, setupFullMustacheParams: function(sexpr, program, inverse) { var params = sexpr.params; this.pushParams(params); this.opcode('pushProgram', program); this.opcode('pushProgram', inverse); if (sexpr.hash) { this.hash(sexpr.hash); } else { this.opcode('emptyHash'); } return params; } }; function precompile(input, options, env) { if (input == null || (typeof input !== 'string' && input.constructor !== env.AST.ProgramNode)) { throw new Exception("You must pass a string or Handlebars AST to Handlebars.precompile. You passed " + input); } options = options || {}; if (!('data' in options)) { options.data = true; } if (options.compat) { options.useDepths = true; } var ast = env.parse(input); var environment = new env.Compiler().compile(ast, options); return new env.JavaScriptCompiler().compile(environment, options); } exports.precompile = precompile;function compile(input, options, env) { if (input == null || (typeof input !== 'string' && input.constructor !== env.AST.ProgramNode)) { throw new Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input); } options = options || {}; if (!('data' in options)) { options.data = true; } if (options.compat) { options.useDepths = true; } var compiled; function compileInput() { var ast = env.parse(input); var environment = new env.Compiler().compile(ast, options); var templateSpec = new env.JavaScriptCompiler().compile(environment, options, undefined, true); return env.template(templateSpec); } // Template is only compiled on first use and cached after that point. var ret = function(context, options) { if (!compiled) { compiled = compileInput(); } return compiled.call(this, context, options); }; ret._setup = function(options) { if (!compiled) { compiled = compileInput(); } return compiled._setup(options); }; ret._child = function(i, data, depths) { if (!compiled) { compiled = compileInput(); } return compiled._child(i, data, depths); }; return ret; } exports.compile = compile;function argEquals(a, b) { if (a === b) { return true; } if (isArray(a) && isArray(b) && a.length === b.length) { for (var i = 0; i < a.length; i++) { if (!argEquals(a[i], b[i])) { return false; } } return true; } } |