handlebars-amd-rails 1.0.0.rc.3

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