diff options
Diffstat (limited to 'lib/handlebars')
-rw-r--r-- | lib/handlebars/base.js | 9 | ||||
-rw-r--r-- | lib/handlebars/compiler/ast.js | 282 | ||||
-rw-r--r-- | lib/handlebars/compiler/base.js | 2 | ||||
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 2 | ||||
-rw-r--r-- | lib/handlebars/runtime.js | 5 | ||||
-rw-r--r-- | lib/handlebars/utils.js | 3 |
6 files changed, 159 insertions, 144 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 56eb016..fe03dff 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -1,4 +1,3 @@ -/*globals Exception, Utils */ module Utils from "./utils"; import Exception from "./exception"; @@ -93,7 +92,7 @@ function registerDefaultHelpers(instance) { for(var j = context.length; i<j; i++) { if (data) { data.index = i; - data.first = (i === 0) + data.first = (i === 0); data.last = (i === (context.length-1)); } ret = ret + fn(context[i], { data: data }); @@ -101,7 +100,11 @@ function registerDefaultHelpers(instance) { } else { for(var key in context) { if(context.hasOwnProperty(key)) { - if(data) { data.key = key; } + if(data) { + data.key = key; + data.index = i; + data.first = (i === 0); + } ret = ret + fn(context[key], {data: data}); i++; } diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index f6229e2..64cd3b7 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -1,145 +1,157 @@ import Exception from "../exception"; -export function ProgramNode(statements, inverseStrip, inverse) { - this.type = "program"; - this.statements = statements; - this.strip = {}; - - if(inverse) { - this.inverse = new ProgramNode(inverse, inverseStrip); - this.strip.right = inverseStrip.left; - } else if (inverseStrip) { - this.strip.left = inverseStrip.right; - } -} - -export function MustacheNode(rawParams, hash, open, strip) { - this.type = "mustache"; - this.hash = hash; - this.strip = strip; - - var escapeFlag = open[3] || open[2]; - this.escaped = escapeFlag !== '{' && escapeFlag !== '&'; - - var id = this.id = rawParams[0]; - var params = this.params = rawParams.slice(1); - - // a mustache is an eligible helper if: - // * its id is simple (a single part, not `this` or `..`) - var eligibleHelper = this.eligibleHelper = id.isSimple; - - // a mustache is definitely a helper if: - // * it is an eligible helper, and - // * it has at least one parameter or hash segment - this.isHelper = eligibleHelper && (params.length || hash); - - // if a mustache is an eligible helper but not a definite - // helper, it is ambiguous, and will be resolved in a later - // pass or at runtime. -} - -export function PartialNode(partialName, context, strip) { - this.type = "partial"; - this.partialName = partialName; - this.context = context; - this.strip = strip; -} - -export function BlockNode(mustache, program, inverse, close) { - if(mustache.id.original !== close.path.original) { - throw new Exception(mustache.id.original + " doesn't match " + close.path.original); - } - - this.type = "block"; - this.mustache = mustache; - this.program = program; - this.inverse = inverse; - - this.strip = { - left: mustache.strip.left, - right: close.strip.right - }; - - (program || inverse).strip.left = mustache.strip.right; - (inverse || program).strip.right = close.strip.left; - - if (inverse && !program) { - this.isInverse = true; - } -} - -export function ContentNode(string) { - this.type = "content"; - this.string = string; -} +var AST = { + ProgramNode: function(statements, inverseStrip, inverse) { + this.type = "program"; + this.statements = statements; + this.strip = {}; + + if(inverse) { + this.inverse = new AST.ProgramNode(inverse, inverseStrip); + this.strip.right = inverseStrip.left; + } else if (inverseStrip) { + this.strip.left = inverseStrip.right; + } + }, + + MustacheNode: function(rawParams, hash, open, strip) { + this.type = "mustache"; + this.hash = hash; + this.strip = strip; + + // Open may be a string parsed from the parser or a passed boolean flag + if (open != null && open.charAt) { + // Must use charAt to support IE pre-10 + var escapeFlag = open.charAt(3) || open.charAt(2); + this.escaped = escapeFlag !== '{' && escapeFlag !== '&'; + } else { + this.escaped = !!open; + } -export function HashNode(pairs) { - this.type = "hash"; - this.pairs = pairs; -} + var id = this.id = rawParams[0]; + var params = this.params = rawParams.slice(1); + + // a mustache is an eligible helper if: + // * its id is simple (a single part, not `this` or `..`) + var eligibleHelper = this.eligibleHelper = id.isSimple; + + // a mustache is definitely a helper if: + // * it is an eligible helper, and + // * it has at least one parameter or hash segment + this.isHelper = eligibleHelper && (params.length || hash); + + // if a mustache is an eligible helper but not a definite + // helper, it is ambiguous, and will be resolved in a later + // pass or at runtime. + }, + + PartialNode: function(partialName, context, strip) { + this.type = "partial"; + this.partialName = partialName; + this.context = context; + this.strip = strip; + }, + + BlockNode: function(mustache, program, inverse, close) { + if(mustache.id.original !== close.path.original) { + throw new Exception(mustache.id.original + " doesn't match " + close.path.original); + } -export function IdNode(parts) { - this.type = "ID"; + this.type = "block"; + this.mustache = mustache; + this.program = program; + this.inverse = inverse; - var original = "", - dig = [], - depth = 0; + this.strip = { + left: mustache.strip.left, + right: close.strip.right + }; - for(var i=0,l=parts.length; i<l; i++) { - var part = parts[i].part; - original += (parts[i].separator || '') + part; + (program || inverse).strip.left = mustache.strip.right; + (inverse || program).strip.right = close.strip.left; - if (part === ".." || part === "." || part === "this") { - if (dig.length > 0) { throw new Exception("Invalid path: " + original); } - else if (part === "..") { depth++; } - else { this.isScoped = true; } + if (inverse && !program) { + this.isInverse = true; + } + }, + + ContentNode: function(string) { + this.type = "content"; + this.string = string; + }, + + HashNode: function(pairs) { + this.type = "hash"; + this.pairs = pairs; + }, + + IdNode: function(parts) { + this.type = "ID"; + + var original = "", + dig = [], + depth = 0; + + for(var i=0,l=parts.length; i<l; i++) { + var part = parts[i].part; + original += (parts[i].separator || '') + part; + + if (part === ".." || part === "." || part === "this") { + if (dig.length > 0) { throw new Exception("Invalid path: " + original); } + else if (part === "..") { depth++; } + else { this.isScoped = true; } + } + else { dig.push(part); } } - else { dig.push(part); } + + this.original = original; + this.parts = dig; + this.string = dig.join('.'); + this.depth = depth; + + // an ID is simple if it only has one part, and that part is not + // `..` or `this`. + this.isSimple = parts.length === 1 && !this.isScoped && depth === 0; + + this.stringModeValue = this.string; + }, + + PartialNameNode: function(name) { + this.type = "PARTIAL_NAME"; + this.name = name.original; + }, + + DataNode: function(id) { + this.type = "DATA"; + this.id = id; + }, + + StringNode: function(string) { + this.type = "STRING"; + this.original = + this.string = + this.stringModeValue = string; + }, + + IntegerNode: function(integer) { + this.type = "INTEGER"; + this.original = + this.integer = integer; + this.stringModeValue = Number(integer); + }, + + BooleanNode: function(bool) { + this.type = "BOOLEAN"; + this.bool = bool; + this.stringModeValue = bool === "true"; + }, + + CommentNode: function(comment) { + this.type = "comment"; + this.comment = comment; } +}; - this.original = original; - this.parts = dig; - this.string = dig.join('.'); - this.depth = depth; - - // an ID is simple if it only has one part, and that part is not - // `..` or `this`. - this.isSimple = parts.length === 1 && !this.isScoped && depth === 0; - - this.stringModeValue = this.string; -} - -export function PartialNameNode(name) { - this.type = "PARTIAL_NAME"; - this.name = name.original; -} - -export function DataNode(id) { - this.type = "DATA"; - this.id = id; -} - -export function StringNode(string) { - this.type = "STRING"; - this.original = - this.string = - this.stringModeValue = string; -} - -export function IntegerNode(integer) { - this.type = "INTEGER"; - this.original = - this.integer = integer; - this.stringModeValue = Number(integer); -} - -export function BooleanNode(bool) { - this.type = "BOOLEAN"; - this.bool = bool; - this.stringModeValue = bool === "true"; -} - -export function CommentNode(comment) { - this.type = "comment"; - this.comment = comment; -} +// Must be exported as an object rather than the root of the module as the jison lexer +// most modify the object to operate properly. +export default AST; diff --git a/lib/handlebars/compiler/base.js b/lib/handlebars/compiler/base.js index d6cb06e..722f09a 100644 --- a/lib/handlebars/compiler/base.js +++ b/lib/handlebars/compiler/base.js @@ -1,5 +1,5 @@ import parser from "./parser"; -module AST from "./ast"; +import AST from "./ast"; export { parser }; diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 4f232eb..daea307 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -1,7 +1,7 @@ import Exception from "../exception"; import { parse } from "./base"; import JavaScriptCompiler from "./javascript-compiler"; -module AST from "./ast"; +import AST from "./ast"; export function Compiler() {} diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index fe5fef4..aa0f13e 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -1,4 +1,3 @@ -/*global Utils */ module Utils from "./utils"; import Exception from "./exception"; import { COMPILER_REVISION, REVISION_CHANGES } from "./base"; @@ -34,7 +33,7 @@ export function template(templateSpec, env) { // TODO : Check this for all inputs and the options handling (partial flag, etc). This feels // like there should be a common exec path var result = invokePartial.apply(this, arguments); - if (result) { return result; } + if (result != null) { return result; } var options = { helpers: helpers, partials: partials, data: data }; partials[name] = env.compile(partial, { data: data !== undefined }, env); @@ -43,7 +42,7 @@ export function template(templateSpec, env) { } else { invokePartialWrapper = function(partial, name /* , context, helpers, partials, data */) { var result = invokePartial.apply(this, arguments); - if (result) { return result; } + if (result != null) { return result; } throw new Exception("The partial " + name + " could not be compiled when running in runtime-only mode"); }; } diff --git a/lib/handlebars/utils.js b/lib/handlebars/utils.js index 65e91f9..ed2e1d8 100644 --- a/lib/handlebars/utils.js +++ b/lib/handlebars/utils.js @@ -1,3 +1,4 @@ +/*jshint -W004 */ import SafeString from "./safe-string"; var escape = { @@ -18,7 +19,7 @@ function escapeChar(chr) { export function extend(obj, value) { for(var key in value) { - if(value.hasOwnProperty(key)) { + if(Object.prototype.hasOwnProperty.call(value, key)) { obj[key] = value[key]; } } |