handlebars.runtime-v2.0.0.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*!
  2. handlebars v2.0.0
  3. Copyright (C) 2011-2014 by Yehuda Katz
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. @license
  20. */
  21. /* exported Handlebars */
  22. (function (root, factory) {
  23. if (typeof define === 'function' && define.amd) {
  24. define([], factory);
  25. } else if (typeof exports === 'object') {
  26. module.exports = factory();
  27. } else {
  28. root.Handlebars = root.Handlebars || factory();
  29. }
  30. }(this, function () {
  31. // handlebars/safe-string.js
  32. var __module3__ = (function() {
  33. "use strict";
  34. var __exports__;
  35. // Build out our basic SafeString type
  36. function SafeString(string) {
  37. this.string = string;
  38. }
  39. SafeString.prototype.toString = function() {
  40. return "" + this.string;
  41. };
  42. __exports__ = SafeString;
  43. return __exports__;
  44. })();
  45. // handlebars/utils.js
  46. var __module2__ = (function(__dependency1__) {
  47. "use strict";
  48. var __exports__ = {};
  49. /*jshint -W004 */
  50. var SafeString = __dependency1__;
  51. var escape = {
  52. "&": "&",
  53. "<": "&lt;",
  54. ">": "&gt;",
  55. '"': "&quot;",
  56. "'": "&#x27;",
  57. "`": "&#x60;"
  58. };
  59. var badChars = /[&<>"'`]/g;
  60. var possible = /[&<>"'`]/;
  61. function escapeChar(chr) {
  62. return escape[chr];
  63. }
  64. function extend(obj /* , ...source */) {
  65. for (var i = 1; i < arguments.length; i++) {
  66. for (var key in arguments[i]) {
  67. if (Object.prototype.hasOwnProperty.call(arguments[i], key)) {
  68. obj[key] = arguments[i][key];
  69. }
  70. }
  71. }
  72. return obj;
  73. }
  74. __exports__.extend = extend;var toString = Object.prototype.toString;
  75. __exports__.toString = toString;
  76. // Sourced from lodash
  77. // https://github.com/bestiejs/lodash/blob/master/LICENSE.txt
  78. var isFunction = function(value) {
  79. return typeof value === 'function';
  80. };
  81. // fallback for older versions of Chrome and Safari
  82. /* istanbul ignore next */
  83. if (isFunction(/x/)) {
  84. isFunction = function(value) {
  85. return typeof value === 'function' && toString.call(value) === '[object Function]';
  86. };
  87. }
  88. var isFunction;
  89. __exports__.isFunction = isFunction;
  90. /* istanbul ignore next */
  91. var isArray = Array.isArray || function(value) {
  92. return (value && typeof value === 'object') ? toString.call(value) === '[object Array]' : false;
  93. };
  94. __exports__.isArray = isArray;
  95. function escapeExpression(string) {
  96. // don't escape SafeStrings, since they're already safe
  97. if (string instanceof SafeString) {
  98. return string.toString();
  99. } else if (string == null) {
  100. return "";
  101. } else if (!string) {
  102. return string + '';
  103. }
  104. // Force a string conversion as this will be done by the append regardless and
  105. // the regex test will do this transparently behind the scenes, causing issues if
  106. // an object's to string has escaped characters in it.
  107. string = "" + string;
  108. if(!possible.test(string)) { return string; }
  109. return string.replace(badChars, escapeChar);
  110. }
  111. __exports__.escapeExpression = escapeExpression;function isEmpty(value) {
  112. if (!value && value !== 0) {
  113. return true;
  114. } else if (isArray(value) && value.length === 0) {
  115. return true;
  116. } else {
  117. return false;
  118. }
  119. }
  120. __exports__.isEmpty = isEmpty;function appendContextPath(contextPath, id) {
  121. return (contextPath ? contextPath + '.' : '') + id;
  122. }
  123. __exports__.appendContextPath = appendContextPath;
  124. return __exports__;
  125. })(__module3__);
  126. // handlebars/exception.js
  127. var __module4__ = (function() {
  128. "use strict";
  129. var __exports__;
  130. var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
  131. function Exception(message, node) {
  132. var line;
  133. if (node && node.firstLine) {
  134. line = node.firstLine;
  135. message += ' - ' + line + ':' + node.firstColumn;
  136. }
  137. var tmp = Error.prototype.constructor.call(this, message);
  138. // Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
  139. for (var idx = 0; idx < errorProps.length; idx++) {
  140. this[errorProps[idx]] = tmp[errorProps[idx]];
  141. }
  142. if (line) {
  143. this.lineNumber = line;
  144. this.column = node.firstColumn;
  145. }
  146. }
  147. Exception.prototype = new Error();
  148. __exports__ = Exception;
  149. return __exports__;
  150. })();
  151. // handlebars/base.js
  152. var __module1__ = (function(__dependency1__, __dependency2__) {
  153. "use strict";
  154. var __exports__ = {};
  155. var Utils = __dependency1__;
  156. var Exception = __dependency2__;
  157. var VERSION = "2.0.0";
  158. __exports__.VERSION = VERSION;var COMPILER_REVISION = 6;
  159. __exports__.COMPILER_REVISION = COMPILER_REVISION;
  160. var REVISION_CHANGES = {
  161. 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
  162. 2: '== 1.0.0-rc.3',
  163. 3: '== 1.0.0-rc.4',
  164. 4: '== 1.x.x',
  165. 5: '== 2.0.0-alpha.x',
  166. 6: '>= 2.0.0-beta.1'
  167. };
  168. __exports__.REVISION_CHANGES = REVISION_CHANGES;
  169. var isArray = Utils.isArray,
  170. isFunction = Utils.isFunction,
  171. toString = Utils.toString,
  172. objectType = '[object Object]';
  173. function HandlebarsEnvironment(helpers, partials) {
  174. this.helpers = helpers || {};
  175. this.partials = partials || {};
  176. registerDefaultHelpers(this);
  177. }
  178. __exports__.HandlebarsEnvironment = HandlebarsEnvironment;HandlebarsEnvironment.prototype = {
  179. constructor: HandlebarsEnvironment,
  180. logger: logger,
  181. log: log,
  182. registerHelper: function(name, fn) {
  183. if (toString.call(name) === objectType) {
  184. if (fn) { throw new Exception('Arg not supported with multiple helpers'); }
  185. Utils.extend(this.helpers, name);
  186. } else {
  187. this.helpers[name] = fn;
  188. }
  189. },
  190. unregisterHelper: function(name) {
  191. delete this.helpers[name];
  192. },
  193. registerPartial: function(name, partial) {
  194. if (toString.call(name) === objectType) {
  195. Utils.extend(this.partials, name);
  196. } else {
  197. this.partials[name] = partial;
  198. }
  199. },
  200. unregisterPartial: function(name) {
  201. delete this.partials[name];
  202. }
  203. };
  204. function registerDefaultHelpers(instance) {
  205. instance.registerHelper('helperMissing', function(/* [args, ]options */) {
  206. if(arguments.length === 1) {
  207. // A missing field in a {{foo}} constuct.
  208. return undefined;
  209. } else {
  210. // Someone is actually trying to call something, blow up.
  211. throw new Exception("Missing helper: '" + arguments[arguments.length-1].name + "'");
  212. }
  213. });
  214. instance.registerHelper('blockHelperMissing', function(context, options) {
  215. var inverse = options.inverse,
  216. fn = options.fn;
  217. if(context === true) {
  218. return fn(this);
  219. } else if(context === false || context == null) {
  220. return inverse(this);
  221. } else if (isArray(context)) {
  222. if(context.length > 0) {
  223. if (options.ids) {
  224. options.ids = [options.name];
  225. }
  226. return instance.helpers.each(context, options);
  227. } else {
  228. return inverse(this);
  229. }
  230. } else {
  231. if (options.data && options.ids) {
  232. var data = createFrame(options.data);
  233. data.contextPath = Utils.appendContextPath(options.data.contextPath, options.name);
  234. options = {data: data};
  235. }
  236. return fn(context, options);
  237. }
  238. });
  239. instance.registerHelper('each', function(context, options) {
  240. if (!options) {
  241. throw new Exception('Must pass iterator to #each');
  242. }
  243. var fn = options.fn, inverse = options.inverse;
  244. var i = 0, ret = "", data;
  245. var contextPath;
  246. if (options.data && options.ids) {
  247. contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]) + '.';
  248. }
  249. if (isFunction(context)) { context = context.call(this); }
  250. if (options.data) {
  251. data = createFrame(options.data);
  252. }
  253. if(context && typeof context === 'object') {
  254. if (isArray(context)) {
  255. for(var j = context.length; i<j; i++) {
  256. if (data) {
  257. data.index = i;
  258. data.first = (i === 0);
  259. data.last = (i === (context.length-1));
  260. if (contextPath) {
  261. data.contextPath = contextPath + i;
  262. }
  263. }
  264. ret = ret + fn(context[i], { data: data });
  265. }
  266. } else {
  267. for(var key in context) {
  268. if(context.hasOwnProperty(key)) {
  269. if(data) {
  270. data.key = key;
  271. data.index = i;
  272. data.first = (i === 0);
  273. if (contextPath) {
  274. data.contextPath = contextPath + key;
  275. }
  276. }
  277. ret = ret + fn(context[key], {data: data});
  278. i++;
  279. }
  280. }
  281. }
  282. }
  283. if(i === 0){
  284. ret = inverse(this);
  285. }
  286. return ret;
  287. });
  288. instance.registerHelper('if', function(conditional, options) {
  289. if (isFunction(conditional)) { conditional = conditional.call(this); }
  290. // Default behavior is to render the positive path if the value is truthy and not empty.
  291. // The `includeZero` option may be set to treat the condtional as purely not empty based on the
  292. // behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.
  293. if ((!options.hash.includeZero && !conditional) || Utils.isEmpty(conditional)) {
  294. return options.inverse(this);
  295. } else {
  296. return options.fn(this);
  297. }
  298. });
  299. instance.registerHelper('unless', function(conditional, options) {
  300. return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash});
  301. });
  302. instance.registerHelper('with', function(context, options) {
  303. if (isFunction(context)) { context = context.call(this); }
  304. var fn = options.fn;
  305. if (!Utils.isEmpty(context)) {
  306. if (options.data && options.ids) {
  307. var data = createFrame(options.data);
  308. data.contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]);
  309. options = {data:data};
  310. }
  311. return fn(context, options);
  312. } else {
  313. return options.inverse(this);
  314. }
  315. });
  316. instance.registerHelper('log', function(message, options) {
  317. var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
  318. instance.log(level, message);
  319. });
  320. instance.registerHelper('lookup', function(obj, field) {
  321. return obj && obj[field];
  322. });
  323. }
  324. var logger = {
  325. methodMap: { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' },
  326. // State enum
  327. DEBUG: 0,
  328. INFO: 1,
  329. WARN: 2,
  330. ERROR: 3,
  331. level: 3,
  332. // can be overridden in the host environment
  333. log: function(level, message) {
  334. if (logger.level <= level) {
  335. var method = logger.methodMap[level];
  336. if (typeof console !== 'undefined' && console[method]) {
  337. console[method].call(console, message);
  338. }
  339. }
  340. }
  341. };
  342. __exports__.logger = logger;
  343. var log = logger.log;
  344. __exports__.log = log;
  345. var createFrame = function(object) {
  346. var frame = Utils.extend({}, object);
  347. frame._parent = object;
  348. return frame;
  349. };
  350. __exports__.createFrame = createFrame;
  351. return __exports__;
  352. })(__module2__, __module4__);
  353. // handlebars/runtime.js
  354. var __module5__ = (function(__dependency1__, __dependency2__, __dependency3__) {
  355. "use strict";
  356. var __exports__ = {};
  357. var Utils = __dependency1__;
  358. var Exception = __dependency2__;
  359. var COMPILER_REVISION = __dependency3__.COMPILER_REVISION;
  360. var REVISION_CHANGES = __dependency3__.REVISION_CHANGES;
  361. var createFrame = __dependency3__.createFrame;
  362. function checkRevision(compilerInfo) {
  363. var compilerRevision = compilerInfo && compilerInfo[0] || 1,
  364. currentRevision = COMPILER_REVISION;
  365. if (compilerRevision !== currentRevision) {
  366. if (compilerRevision < currentRevision) {
  367. var runtimeVersions = REVISION_CHANGES[currentRevision],
  368. compilerVersions = REVISION_CHANGES[compilerRevision];
  369. throw new Exception("Template was precompiled with an older version of Handlebars than the current runtime. "+
  370. "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").");
  371. } else {
  372. // Use the embedded version info since the runtime doesn't know about this revision yet
  373. throw new Exception("Template was precompiled with a newer version of Handlebars than the current runtime. "+
  374. "Please update your runtime to a newer version ("+compilerInfo[1]+").");
  375. }
  376. }
  377. }
  378. __exports__.checkRevision = checkRevision;// TODO: Remove this line and break up compilePartial
  379. function template(templateSpec, env) {
  380. /* istanbul ignore next */
  381. if (!env) {
  382. throw new Exception("No environment passed to template");
  383. }
  384. if (!templateSpec || !templateSpec.main) {
  385. throw new Exception('Unknown template object: ' + typeof templateSpec);
  386. }
  387. // Note: Using env.VM references rather than local var references throughout this section to allow
  388. // for external users to override these as psuedo-supported APIs.
  389. env.VM.checkRevision(templateSpec.compiler);
  390. var invokePartialWrapper = function(partial, indent, name, context, hash, helpers, partials, data, depths) {
  391. if (hash) {
  392. context = Utils.extend({}, context, hash);
  393. }
  394. var result = env.VM.invokePartial.call(this, partial, name, context, helpers, partials, data, depths);
  395. if (result == null && env.compile) {
  396. var options = { helpers: helpers, partials: partials, data: data, depths: depths };
  397. partials[name] = env.compile(partial, { data: data !== undefined, compat: templateSpec.compat }, env);
  398. result = partials[name](context, options);
  399. }
  400. if (result != null) {
  401. if (indent) {
  402. var lines = result.split('\n');
  403. for (var i = 0, l = lines.length; i < l; i++) {
  404. if (!lines[i] && i + 1 === l) {
  405. break;
  406. }
  407. lines[i] = indent + lines[i];
  408. }
  409. result = lines.join('\n');
  410. }
  411. return result;
  412. } else {
  413. throw new Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
  414. }
  415. };
  416. // Just add water
  417. var container = {
  418. lookup: function(depths, name) {
  419. var len = depths.length;
  420. for (var i = 0; i < len; i++) {
  421. if (depths[i] && depths[i][name] != null) {
  422. return depths[i][name];
  423. }
  424. }
  425. },
  426. lambda: function(current, context) {
  427. return typeof current === 'function' ? current.call(context) : current;
  428. },
  429. escapeExpression: Utils.escapeExpression,
  430. invokePartial: invokePartialWrapper,
  431. fn: function(i) {
  432. return templateSpec[i];
  433. },
  434. programs: [],
  435. program: function(i, data, depths) {
  436. var programWrapper = this.programs[i],
  437. fn = this.fn(i);
  438. if (data || depths) {
  439. programWrapper = program(this, i, fn, data, depths);
  440. } else if (!programWrapper) {
  441. programWrapper = this.programs[i] = program(this, i, fn);
  442. }
  443. return programWrapper;
  444. },
  445. data: function(data, depth) {
  446. while (data && depth--) {
  447. data = data._parent;
  448. }
  449. return data;
  450. },
  451. merge: function(param, common) {
  452. var ret = param || common;
  453. if (param && common && (param !== common)) {
  454. ret = Utils.extend({}, common, param);
  455. }
  456. return ret;
  457. },
  458. noop: env.VM.noop,
  459. compilerInfo: templateSpec.compiler
  460. };
  461. var ret = function(context, options) {
  462. options = options || {};
  463. var data = options.data;
  464. ret._setup(options);
  465. if (!options.partial && templateSpec.useData) {
  466. data = initData(context, data);
  467. }
  468. var depths;
  469. if (templateSpec.useDepths) {
  470. depths = options.depths ? [context].concat(options.depths) : [context];
  471. }
  472. return templateSpec.main.call(container, context, container.helpers, container.partials, data, depths);
  473. };
  474. ret.isTop = true;
  475. ret._setup = function(options) {
  476. if (!options.partial) {
  477. container.helpers = container.merge(options.helpers, env.helpers);
  478. if (templateSpec.usePartial) {
  479. container.partials = container.merge(options.partials, env.partials);
  480. }
  481. } else {
  482. container.helpers = options.helpers;
  483. container.partials = options.partials;
  484. }
  485. };
  486. ret._child = function(i, data, depths) {
  487. if (templateSpec.useDepths && !depths) {
  488. throw new Exception('must pass parent depths');
  489. }
  490. return program(container, i, templateSpec[i], data, depths);
  491. };
  492. return ret;
  493. }
  494. __exports__.template = template;function program(container, i, fn, data, depths) {
  495. var prog = function(context, options) {
  496. options = options || {};
  497. return fn.call(container, context, container.helpers, container.partials, options.data || data, depths && [context].concat(depths));
  498. };
  499. prog.program = i;
  500. prog.depth = depths ? depths.length : 0;
  501. return prog;
  502. }
  503. __exports__.program = program;function invokePartial(partial, name, context, helpers, partials, data, depths) {
  504. var options = { partial: true, helpers: helpers, partials: partials, data: data, depths: depths };
  505. if(partial === undefined) {
  506. throw new Exception("The partial " + name + " could not be found");
  507. } else if(partial instanceof Function) {
  508. return partial(context, options);
  509. }
  510. }
  511. __exports__.invokePartial = invokePartial;function noop() { return ""; }
  512. __exports__.noop = noop;function initData(context, data) {
  513. if (!data || !('root' in data)) {
  514. data = data ? createFrame(data) : {};
  515. data.root = context;
  516. }
  517. return data;
  518. }
  519. return __exports__;
  520. })(__module2__, __module4__, __module1__);
  521. // handlebars.runtime.js
  522. var __module0__ = (function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__) {
  523. "use strict";
  524. var __exports__;
  525. /*globals Handlebars: true */
  526. var base = __dependency1__;
  527. // Each of these augment the Handlebars object. No need to setup here.
  528. // (This is done to easily share code between commonjs and browse envs)
  529. var SafeString = __dependency2__;
  530. var Exception = __dependency3__;
  531. var Utils = __dependency4__;
  532. var runtime = __dependency5__;
  533. // For compatibility and usage outside of module systems, make the Handlebars object a namespace
  534. var create = function() {
  535. var hb = new base.HandlebarsEnvironment();
  536. Utils.extend(hb, base);
  537. hb.SafeString = SafeString;
  538. hb.Exception = Exception;
  539. hb.Utils = Utils;
  540. hb.escapeExpression = Utils.escapeExpression;
  541. hb.VM = runtime;
  542. hb.template = function(spec) {
  543. return runtime.template(spec, hb);
  544. };
  545. return hb;
  546. };
  547. var Handlebars = create();
  548. Handlebars.create = create;
  549. Handlebars['default'] = Handlebars;
  550. __exports__ = Handlebars;
  551. return __exports__;
  552. })(__module1__, __module3__, __module4__, __module2__, __module5__);
  553. return __module0__;
  554. }));