PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/yuilib/3.17.2/handlebars-base/handlebars-base.js

https://bitbucket.org/moodle/moodle
JavaScript | 463 lines | 381 code | 39 blank | 43 comment | 46 complexity | f2c3ae82b44d3f999a37205a9ffde284 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. /*
  2. YUI 3.17.2 (build 9c3c78e)
  3. Copyright 2014 Yahoo! Inc. All rights reserved.
  4. Licensed under the BSD License.
  5. http://yuilibrary.com/license/
  6. */
  7. YUI.add('handlebars-base', function (Y, NAME) {
  8. /*!
  9. Handlebars.js - Copyright (C) 2011 Yehuda Katz
  10. https://raw.github.com/wycats/handlebars.js/master/LICENSE
  11. */
  12. // This file contains YUI-specific wrapper code and overrides for the
  13. // handlebars-base module.
  14. /**
  15. Handlebars is a simple template language inspired by Mustache.
  16. This is a YUI port of the original Handlebars project, which can be found at
  17. <https://github.com/wycats/handlebars.js>.
  18. @module handlebars
  19. @main handlebars
  20. @since 3.5.0
  21. */
  22. /**
  23. Provides basic Handlebars template rendering functionality. Use this module when
  24. you only need to render pre-compiled templates.
  25. @module handlebars
  26. @submodule handlebars-base
  27. */
  28. /**
  29. Handlebars is a simple template language inspired by Mustache.
  30. This is a YUI port of the original Handlebars project, which can be found at
  31. <https://github.com/wycats/handlebars.js>.
  32. @class Handlebars
  33. @since 3.5.0
  34. */
  35. var Handlebars = Y.namespace('Handlebars');
  36. /* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */
  37. Handlebars.VERSION = "1.0.0";
  38. Handlebars.COMPILER_REVISION = 4;
  39. Handlebars.REVISION_CHANGES = {
  40. 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
  41. 2: '== 1.0.0-rc.3',
  42. 3: '== 1.0.0-rc.4',
  43. 4: '>= 1.0.0'
  44. };
  45. Handlebars.helpers = {};
  46. Handlebars.partials = {};
  47. var toString = Object.prototype.toString,
  48. functionType = '[object Function]',
  49. objectType = '[object Object]';
  50. Handlebars.registerHelper = function(name, fn, inverse) {
  51. if (toString.call(name) === objectType) {
  52. if (inverse || fn) { throw new Handlebars.Exception('Arg not supported with multiple helpers'); }
  53. Handlebars.Utils.extend(this.helpers, name);
  54. } else {
  55. if (inverse) { fn.not = inverse; }
  56. this.helpers[name] = fn;
  57. }
  58. };
  59. Handlebars.registerPartial = function(name, str) {
  60. if (toString.call(name) === objectType) {
  61. Handlebars.Utils.extend(this.partials, name);
  62. } else {
  63. this.partials[name] = str;
  64. }
  65. };
  66. Handlebars.registerHelper('helperMissing', function(arg) {
  67. if(arguments.length === 2) {
  68. return undefined;
  69. } else {
  70. throw new Error("Missing helper: '" + arg + "'");
  71. }
  72. });
  73. Handlebars.registerHelper('blockHelperMissing', function(context, options) {
  74. var inverse = options.inverse || function() {}, fn = options.fn;
  75. var type = toString.call(context);
  76. if(type === functionType) { context = context.call(this); }
  77. if(context === true) {
  78. return fn(this);
  79. } else if(context === false || context == null) {
  80. return inverse(this);
  81. } else if(type === "[object Array]") {
  82. if(context.length > 0) {
  83. return Handlebars.helpers.each(context, options);
  84. } else {
  85. return inverse(this);
  86. }
  87. } else {
  88. return fn(context);
  89. }
  90. });
  91. Handlebars.K = function() {};
  92. Handlebars.createFrame = Object.create || function(object) {
  93. Handlebars.K.prototype = object;
  94. var obj = new Handlebars.K();
  95. Handlebars.K.prototype = null;
  96. return obj;
  97. };
  98. Handlebars.logger = {
  99. DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
  100. methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'},
  101. // can be overridden in the host environment
  102. log: function(level, obj) {
  103. if (Handlebars.logger.level <= level) {
  104. var method = Handlebars.logger.methodMap[level];
  105. if (typeof console !== 'undefined' && console[method]) {
  106. console[method].call(console, obj);
  107. }
  108. }
  109. }
  110. };
  111. Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); };
  112. Handlebars.registerHelper('each', function(context, options) {
  113. var fn = options.fn, inverse = options.inverse;
  114. var i = 0, ret = "", data;
  115. var type = toString.call(context);
  116. if(type === functionType) { context = context.call(this); }
  117. if (options.data) {
  118. data = Handlebars.createFrame(options.data);
  119. }
  120. if(context && typeof context === 'object') {
  121. if(context instanceof Array){
  122. for(var j = context.length; i<j; i++) {
  123. if (data) { data.index = i; }
  124. ret = ret + fn(context[i], { data: data });
  125. }
  126. } else {
  127. for(var key in context) {
  128. if(context.hasOwnProperty(key)) {
  129. if(data) { data.key = key; }
  130. ret = ret + fn(context[key], {data: data});
  131. i++;
  132. }
  133. }
  134. }
  135. }
  136. if(i === 0){
  137. ret = inverse(this);
  138. }
  139. return ret;
  140. });
  141. Handlebars.registerHelper('if', function(conditional, options) {
  142. var type = toString.call(conditional);
  143. if(type === functionType) { conditional = conditional.call(this); }
  144. if(!conditional || Handlebars.Utils.isEmpty(conditional)) {
  145. return options.inverse(this);
  146. } else {
  147. return options.fn(this);
  148. }
  149. });
  150. Handlebars.registerHelper('unless', function(conditional, options) {
  151. return Handlebars.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn});
  152. });
  153. Handlebars.registerHelper('with', function(context, options) {
  154. var type = toString.call(context);
  155. if(type === functionType) { context = context.call(this); }
  156. if (!Handlebars.Utils.isEmpty(context)) return options.fn(context);
  157. });
  158. Handlebars.registerHelper('log', function(context, options) {
  159. var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
  160. Handlebars.log(level, context);
  161. });
  162. /* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */
  163. var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
  164. Handlebars.Exception = function(message) {
  165. var tmp = Error.prototype.constructor.apply(this, arguments);
  166. // Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
  167. for (var idx = 0; idx < errorProps.length; idx++) {
  168. this[errorProps[idx]] = tmp[errorProps[idx]];
  169. }
  170. };
  171. Handlebars.Exception.prototype = new Error();
  172. // Build out our basic SafeString type
  173. Handlebars.SafeString = function(string) {
  174. this.string = string;
  175. };
  176. Handlebars.SafeString.prototype.toString = function() {
  177. return this.string.toString();
  178. };
  179. var escape = {
  180. "&": "&amp;",
  181. "<": "&lt;",
  182. ">": "&gt;",
  183. '"': "&quot;",
  184. "'": "&#x27;",
  185. "`": "&#x60;"
  186. };
  187. var badChars = /[&<>"'`]/g;
  188. var possible = /[&<>"'`]/;
  189. var escapeChar = function(chr) {
  190. return escape[chr] || "&amp;";
  191. };
  192. Handlebars.Utils = {
  193. extend: function(obj, value) {
  194. for(var key in value) {
  195. if(value.hasOwnProperty(key)) {
  196. obj[key] = value[key];
  197. }
  198. }
  199. },
  200. escapeExpression: function(string) {
  201. // don't escape SafeStrings, since they're already safe
  202. if (string instanceof Handlebars.SafeString) {
  203. return string.toString();
  204. } else if (string == null || string === false) {
  205. return "";
  206. }
  207. // Force a string conversion as this will be done by the append regardless and
  208. // the regex test will do this transparently behind the scenes, causing issues if
  209. // an object's to string has escaped characters in it.
  210. string = string.toString();
  211. if(!possible.test(string)) { return string; }
  212. return string.replace(badChars, escapeChar);
  213. },
  214. isEmpty: function(value) {
  215. if (!value && value !== 0) {
  216. return true;
  217. } else if(toString.call(value) === "[object Array]" && value.length === 0) {
  218. return true;
  219. } else {
  220. return false;
  221. }
  222. }
  223. };
  224. /* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */
  225. Handlebars.VM = {
  226. template: function(templateSpec) {
  227. // Just add water
  228. var container = {
  229. escapeExpression: Handlebars.Utils.escapeExpression,
  230. invokePartial: Handlebars.VM.invokePartial,
  231. programs: [],
  232. program: function(i, fn, data) {
  233. var programWrapper = this.programs[i];
  234. if(data) {
  235. programWrapper = Handlebars.VM.program(i, fn, data);
  236. } else if (!programWrapper) {
  237. programWrapper = this.programs[i] = Handlebars.VM.program(i, fn);
  238. }
  239. return programWrapper;
  240. },
  241. merge: function(param, common) {
  242. var ret = param || common;
  243. if (param && common) {
  244. ret = {};
  245. Handlebars.Utils.extend(ret, common);
  246. Handlebars.Utils.extend(ret, param);
  247. }
  248. return ret;
  249. },
  250. programWithDepth: Handlebars.VM.programWithDepth,
  251. noop: Handlebars.VM.noop,
  252. compilerInfo: null
  253. };
  254. return function(context, options) {
  255. options = options || {};
  256. var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
  257. var compilerInfo = container.compilerInfo || [],
  258. compilerRevision = compilerInfo[0] || 1,
  259. currentRevision = Handlebars.COMPILER_REVISION;
  260. if (compilerRevision !== currentRevision) {
  261. if (compilerRevision < currentRevision) {
  262. var runtimeVersions = Handlebars.REVISION_CHANGES[currentRevision],
  263. compilerVersions = Handlebars.REVISION_CHANGES[compilerRevision];
  264. throw "Template was precompiled with an older version of Handlebars than the current runtime. "+
  265. "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").";
  266. } else {
  267. // Use the embedded version info since the runtime doesn't know about this revision yet
  268. throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+
  269. "Please update your runtime to a newer version ("+compilerInfo[1]+").";
  270. }
  271. }
  272. return result;
  273. };
  274. },
  275. programWithDepth: function(i, fn, data /*, $depth */) {
  276. var args = Array.prototype.slice.call(arguments, 3);
  277. var program = function(context, options) {
  278. options = options || {};
  279. return fn.apply(this, [context, options.data || data].concat(args));
  280. };
  281. program.program = i;
  282. program.depth = args.length;
  283. return program;
  284. },
  285. program: function(i, fn, data) {
  286. var program = function(context, options) {
  287. options = options || {};
  288. return fn(context, options.data || data);
  289. };
  290. program.program = i;
  291. program.depth = 0;
  292. return program;
  293. },
  294. noop: function() { return ""; },
  295. invokePartial: function(partial, name, context, helpers, partials, data) {
  296. var options = { helpers: helpers, partials: partials, data: data };
  297. if(partial === undefined) {
  298. throw new Handlebars.Exception("The partial " + name + " could not be found");
  299. } else if(partial instanceof Function) {
  300. return partial(context, options);
  301. } else if (!Handlebars.compile) {
  302. throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
  303. } else {
  304. partials[name] = Handlebars.compile(partial, {data: data !== undefined});
  305. return partials[name](context, options);
  306. }
  307. }
  308. };
  309. Handlebars.template = Handlebars.VM.template;
  310. // This file contains YUI-specific wrapper code and overrides for the
  311. // handlebars-base module.
  312. Handlebars.VERSION += '-yui';
  313. /**
  314. Registers a helper function that will be made available to all templates.
  315. Helper functions receive the current template context as the `this` object, and
  316. can also receive arguments passed by the template.
  317. @example
  318. Y.Handlebars.registerHelper('linkify', function () {
  319. return '<a href="' + Y.Escape.html(this.url) + '">' +
  320. Y.Escape.html(this.text) + '</a>';
  321. });
  322. var source = '<ul>{{#links}}<li>{{{linkify}}}</li>{{/links}}</ul>';
  323. Y.Handlebars.render(source, {
  324. links: [
  325. {url: '/foo', text: 'Foo'},
  326. {url: '/bar', text: 'Bar'},
  327. {url: '/baz', text: 'Baz'}
  328. ]
  329. });
  330. @method registerHelper
  331. @param {String} name Name of this helper.
  332. @param {Function} fn Helper function.
  333. @param {Boolean} [inverse=false] If `true`, this helper will be considered an
  334. "inverse" helper, like "unless". This means it will only be called if the
  335. expression given in the template evaluates to a false or empty value.
  336. */
  337. /**
  338. Registers a partial that will be made available to all templates.
  339. A partial is another template that can be used to render part of a larger
  340. template. For example, a website with a common header and footer across all its
  341. pages might use a template for each page, which would call shared partials to
  342. render the headers and footers.
  343. Partials may be specified as uncompiled template strings or as compiled template
  344. functions.
  345. @example
  346. Y.Handlebars.registerPartial('header', '<h1>{{title}}</h1>');
  347. Y.Handlebars.registerPartial('footer', 'Copyright (c) 2011 by Me.');
  348. var source = '{{> header}} <p>Mustaches are awesome!</p> {{> footer}}';
  349. Y.Handlebars.render(source, {title: 'My Page About Mustaches'});
  350. @method registerPartial
  351. @param {String} name Name of this partial.
  352. @param {Function|String} partial Template string or compiled template function.
  353. */
  354. /**
  355. Converts a precompiled template into a renderable template function.
  356. @example
  357. <script src="precompiled-template.js"></script>
  358. <script>
  359. YUI().use('handlebars-base', function (Y) {
  360. // Convert the precompiled template function into a renderable template
  361. // function.
  362. var template = Y.Handlebars.template(precompiledTemplate);
  363. // Render it.
  364. template({pie: 'Pumpkin'});
  365. });
  366. </script>
  367. @method template
  368. @param {Function} template Precompiled Handlebars template function.
  369. @return {Function} Compiled template function.
  370. */
  371. // Alias for Y.Handlebars.template(), used by Y.Template.
  372. Handlebars.revive = Handlebars.template;
  373. // Make Y.Template.Handlebars an alias for Y.Handlebars.
  374. Y.namespace('Template').Handlebars = Handlebars;
  375. }, '3.17.2', {"requires": []});