PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/handlebars.js/1.0.0-rc.3/handlebars.runtime.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 321 lines | 254 code | 38 blank | 29 comment | 37 complexity | 3c23b09d120121a635c000491e39c958 MD5 | raw file
  1. /*
  2. Copyright (C) 2011 by Yehuda Katz
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. // lib/handlebars/base.js
  20. /*jshint eqnull:true*/
  21. this.Handlebars = {};
  22. (function(Handlebars) {
  23. Handlebars.VERSION = "1.0.0-rc.3";
  24. Handlebars.COMPILER_REVISION = 2;
  25. Handlebars.REVISION_CHANGES = {
  26. 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
  27. 2: '>= 1.0.0-rc.3'
  28. };
  29. Handlebars.helpers = {};
  30. Handlebars.partials = {};
  31. Handlebars.registerHelper = function(name, fn, inverse) {
  32. if(inverse) { fn.not = inverse; }
  33. this.helpers[name] = fn;
  34. };
  35. Handlebars.registerPartial = function(name, str) {
  36. this.partials[name] = str;
  37. };
  38. Handlebars.registerHelper('helperMissing', function(arg) {
  39. if(arguments.length === 2) {
  40. return undefined;
  41. } else {
  42. throw new Error("Could not find property '" + arg + "'");
  43. }
  44. });
  45. var toString = Object.prototype.toString, functionType = "[object Function]";
  46. Handlebars.registerHelper('blockHelperMissing', function(context, options) {
  47. var inverse = options.inverse || function() {}, fn = options.fn;
  48. var ret = "";
  49. var type = toString.call(context);
  50. if(type === functionType) { context = context.call(this); }
  51. if(context === true) {
  52. return fn(this);
  53. } else if(context === false || context == null) {
  54. return inverse(this);
  55. } else if(type === "[object Array]") {
  56. if(context.length > 0) {
  57. return Handlebars.helpers.each(context, options);
  58. } else {
  59. return inverse(this);
  60. }
  61. } else {
  62. return fn(context);
  63. }
  64. });
  65. Handlebars.K = function() {};
  66. Handlebars.createFrame = Object.create || function(object) {
  67. Handlebars.K.prototype = object;
  68. var obj = new Handlebars.K();
  69. Handlebars.K.prototype = null;
  70. return obj;
  71. };
  72. Handlebars.logger = {
  73. DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
  74. methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'},
  75. // can be overridden in the host environment
  76. log: function(level, obj) {
  77. if (Handlebars.logger.level <= level) {
  78. var method = Handlebars.logger.methodMap[level];
  79. if (typeof console !== 'undefined' && console[method]) {
  80. console[method].call(console, obj);
  81. }
  82. }
  83. }
  84. };
  85. Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); };
  86. Handlebars.registerHelper('each', function(context, options) {
  87. var fn = options.fn, inverse = options.inverse;
  88. var i = 0, ret = "", data;
  89. if (options.data) {
  90. data = Handlebars.createFrame(options.data);
  91. }
  92. if(context && typeof context === 'object') {
  93. if(context instanceof Array){
  94. for(var j = context.length; i<j; i++) {
  95. if (data) { data.index = i; }
  96. ret = ret + fn(context[i], { data: data });
  97. }
  98. } else {
  99. for(var key in context) {
  100. if(context.hasOwnProperty(key)) {
  101. if(data) { data.key = key; }
  102. ret = ret + fn(context[key], {data: data});
  103. i++;
  104. }
  105. }
  106. }
  107. }
  108. if(i === 0){
  109. ret = inverse(this);
  110. }
  111. return ret;
  112. });
  113. Handlebars.registerHelper('if', function(context, options) {
  114. var type = toString.call(context);
  115. if(type === functionType) { context = context.call(this); }
  116. if(!context || Handlebars.Utils.isEmpty(context)) {
  117. return options.inverse(this);
  118. } else {
  119. return options.fn(this);
  120. }
  121. });
  122. Handlebars.registerHelper('unless', function(context, options) {
  123. var fn = options.fn, inverse = options.inverse;
  124. options.fn = inverse;
  125. options.inverse = fn;
  126. return Handlebars.helpers['if'].call(this, context, options);
  127. });
  128. Handlebars.registerHelper('with', function(context, options) {
  129. return options.fn(context);
  130. });
  131. Handlebars.registerHelper('log', function(context, options) {
  132. var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
  133. Handlebars.log(level, context);
  134. });
  135. }(this.Handlebars));
  136. ;
  137. // lib/handlebars/utils.js
  138. var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
  139. Handlebars.Exception = function(message) {
  140. var tmp = Error.prototype.constructor.apply(this, arguments);
  141. // Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
  142. for (var idx = 0; idx < errorProps.length; idx++) {
  143. this[errorProps[idx]] = tmp[errorProps[idx]];
  144. }
  145. };
  146. Handlebars.Exception.prototype = new Error();
  147. // Build out our basic SafeString type
  148. Handlebars.SafeString = function(string) {
  149. this.string = string;
  150. };
  151. Handlebars.SafeString.prototype.toString = function() {
  152. return this.string.toString();
  153. };
  154. (function() {
  155. var escape = {
  156. "&": "&amp;",
  157. "<": "&lt;",
  158. ">": "&gt;",
  159. '"': "&quot;",
  160. "'": "&#x27;",
  161. "`": "&#x60;"
  162. };
  163. var badChars = /[&<>"'`]/g;
  164. var possible = /[&<>"'`]/;
  165. var escapeChar = function(chr) {
  166. return escape[chr] || "&amp;";
  167. };
  168. Handlebars.Utils = {
  169. escapeExpression: function(string) {
  170. // don't escape SafeStrings, since they're already safe
  171. if (string instanceof Handlebars.SafeString) {
  172. return string.toString();
  173. } else if (string == null || string === false) {
  174. return "";
  175. }
  176. if(!possible.test(string)) { return string; }
  177. return string.replace(badChars, escapeChar);
  178. },
  179. isEmpty: function(value) {
  180. if (!value && value !== 0) {
  181. return true;
  182. } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
  183. return true;
  184. } else {
  185. return false;
  186. }
  187. }
  188. };
  189. })();;
  190. // lib/handlebars/runtime.js
  191. Handlebars.VM = {
  192. template: function(templateSpec) {
  193. // Just add water
  194. var container = {
  195. escapeExpression: Handlebars.Utils.escapeExpression,
  196. invokePartial: Handlebars.VM.invokePartial,
  197. programs: [],
  198. program: function(i, fn, data) {
  199. var programWrapper = this.programs[i];
  200. if(data) {
  201. return Handlebars.VM.program(fn, data);
  202. } else if(programWrapper) {
  203. return programWrapper;
  204. } else {
  205. programWrapper = this.programs[i] = Handlebars.VM.program(fn);
  206. return programWrapper;
  207. }
  208. },
  209. programWithDepth: Handlebars.VM.programWithDepth,
  210. noop: Handlebars.VM.noop,
  211. compilerInfo: null
  212. };
  213. return function(context, options) {
  214. options = options || {};
  215. var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
  216. var compilerInfo = container.compilerInfo || [],
  217. compilerRevision = compilerInfo[0] || 1,
  218. currentRevision = Handlebars.COMPILER_REVISION;
  219. if (compilerRevision !== currentRevision) {
  220. if (compilerRevision < currentRevision) {
  221. var runtimeVersions = Handlebars.REVISION_CHANGES[currentRevision],
  222. compilerVersions = Handlebars.REVISION_CHANGES[compilerRevision];
  223. throw "Template was precompiled with an older version of Handlebars than the current runtime. "+
  224. "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").";
  225. } else {
  226. // Use the embedded version info since the runtime doesn't know about this revision yet
  227. throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+
  228. "Please update your runtime to a newer version ("+compilerInfo[1]+").";
  229. }
  230. }
  231. return result;
  232. };
  233. },
  234. programWithDepth: function(fn, data, $depth) {
  235. var args = Array.prototype.slice.call(arguments, 2);
  236. return function(context, options) {
  237. options = options || {};
  238. return fn.apply(this, [context, options.data || data].concat(args));
  239. };
  240. },
  241. program: function(fn, data) {
  242. return function(context, options) {
  243. options = options || {};
  244. return fn(context, options.data || data);
  245. };
  246. },
  247. noop: function() { return ""; },
  248. invokePartial: function(partial, name, context, helpers, partials, data) {
  249. var options = { helpers: helpers, partials: partials, data: data };
  250. if(partial === undefined) {
  251. throw new Handlebars.Exception("The partial " + name + " could not be found");
  252. } else if(partial instanceof Function) {
  253. return partial(context, options);
  254. } else if (!Handlebars.compile) {
  255. throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
  256. } else {
  257. partials[name] = Handlebars.compile(partial, {data: data !== undefined});
  258. return partials[name](context, options);
  259. }
  260. }
  261. };
  262. Handlebars.template = Handlebars.VM.template;
  263. ;