templebars 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,39 +1,80 @@
1
- // lib/handlebars/base.js
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.
2
22
 
3
- /*jshint eqnull:true*/
4
- this.Handlebars = {};
23
+ */
5
24
 
6
- (function(Handlebars) {
25
+ // lib/handlebars/browser-prefix.js
26
+ var Handlebars = {};
7
27
 
8
- Handlebars.VERSION = "1.0.rc.1";
28
+ (function(Handlebars, undefined) {
29
+ ;
30
+ // lib/handlebars/base.js
31
+
32
+ Handlebars.VERSION = "1.0.0";
33
+ Handlebars.COMPILER_REVISION = 4;
34
+
35
+ Handlebars.REVISION_CHANGES = {
36
+ 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
37
+ 2: '== 1.0.0-rc.3',
38
+ 3: '== 1.0.0-rc.4',
39
+ 4: '>= 1.0.0'
40
+ };
9
41
 
10
42
  Handlebars.helpers = {};
11
43
  Handlebars.partials = {};
12
44
 
45
+ var toString = Object.prototype.toString,
46
+ functionType = '[object Function]',
47
+ objectType = '[object Object]';
48
+
13
49
  Handlebars.registerHelper = function(name, fn, inverse) {
14
- if(inverse) { fn.not = inverse; }
15
- this.helpers[name] = fn;
50
+ if (toString.call(name) === objectType) {
51
+ if (inverse || fn) { throw new Handlebars.Exception('Arg not supported with multiple helpers'); }
52
+ Handlebars.Utils.extend(this.helpers, name);
53
+ } else {
54
+ if (inverse) { fn.not = inverse; }
55
+ this.helpers[name] = fn;
56
+ }
16
57
  };
17
58
 
18
59
  Handlebars.registerPartial = function(name, str) {
19
- this.partials[name] = str;
60
+ if (toString.call(name) === objectType) {
61
+ Handlebars.Utils.extend(this.partials, name);
62
+ } else {
63
+ this.partials[name] = str;
64
+ }
20
65
  };
21
66
 
22
67
  Handlebars.registerHelper('helperMissing', function(arg) {
23
68
  if(arguments.length === 2) {
24
69
  return undefined;
25
70
  } else {
26
- throw new Error("Could not find property '" + arg + "'");
71
+ throw new Error("Missing helper: '" + arg + "'");
27
72
  }
28
73
  });
29
74
 
30
- var toString = Object.prototype.toString, functionType = "[object Function]";
31
-
32
75
  Handlebars.registerHelper('blockHelperMissing', function(context, options) {
33
76
  var inverse = options.inverse || function() {}, fn = options.fn;
34
77
 
35
-
36
- var ret = "";
37
78
  var type = toString.call(context);
38
79
 
39
80
  if(type === functionType) { context = context.call(this); }
@@ -62,63 +103,97 @@ Handlebars.createFrame = Object.create || function(object) {
62
103
  return obj;
63
104
  };
64
105
 
106
+ Handlebars.logger = {
107
+ DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
108
+
109
+ methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'},
110
+
111
+ // can be overridden in the host environment
112
+ log: function(level, obj) {
113
+ if (Handlebars.logger.level <= level) {
114
+ var method = Handlebars.logger.methodMap[level];
115
+ if (typeof console !== 'undefined' && console[method]) {
116
+ console[method].call(console, obj);
117
+ }
118
+ }
119
+ }
120
+ };
121
+
122
+ Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); };
123
+
65
124
  Handlebars.registerHelper('each', function(context, options) {
66
125
  var fn = options.fn, inverse = options.inverse;
67
- var ret = "", data;
126
+ var i = 0, ret = "", data;
127
+
128
+ var type = toString.call(context);
129
+ if(type === functionType) { context = context.call(this); }
68
130
 
69
131
  if (options.data) {
70
132
  data = Handlebars.createFrame(options.data);
71
133
  }
72
134
 
73
- if(context && context.length > 0) {
74
- for(var i=0, j=context.length; i<j; i++) {
75
- if (data) { data.index = i; }
76
- ret = ret + fn(context[i], { data: data });
135
+ if(context && typeof context === 'object') {
136
+ if(context instanceof Array){
137
+ for(var j = context.length; i<j; i++) {
138
+ if (data) { data.index = i; }
139
+ ret = ret + fn(context[i], { data: data });
140
+ }
141
+ } else {
142
+ for(var key in context) {
143
+ if(context.hasOwnProperty(key)) {
144
+ if(data) { data.key = key; }
145
+ ret = ret + fn(context[key], {data: data});
146
+ i++;
147
+ }
148
+ }
77
149
  }
78
- } else {
150
+ }
151
+
152
+ if(i === 0){
79
153
  ret = inverse(this);
80
154
  }
155
+
81
156
  return ret;
82
157
  });
83
158
 
84
- Handlebars.registerHelper('if', function(context, options) {
85
- var type = toString.call(context);
86
- if(type === functionType) { context = context.call(this); }
159
+ Handlebars.registerHelper('if', function(conditional, options) {
160
+ var type = toString.call(conditional);
161
+ if(type === functionType) { conditional = conditional.call(this); }
87
162
 
88
- if(!context || Handlebars.Utils.isEmpty(context)) {
163
+ if(!conditional || Handlebars.Utils.isEmpty(conditional)) {
89
164
  return options.inverse(this);
90
165
  } else {
91
166
  return options.fn(this);
92
167
  }
93
168
  });
94
169
 
95
- Handlebars.registerHelper('unless', function(context, options) {
96
- var fn = options.fn, inverse = options.inverse;
97
- options.fn = inverse;
98
- options.inverse = fn;
99
-
100
- return Handlebars.helpers['if'].call(this, context, options);
170
+ Handlebars.registerHelper('unless', function(conditional, options) {
171
+ return Handlebars.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn});
101
172
  });
102
173
 
103
174
  Handlebars.registerHelper('with', function(context, options) {
104
- return options.fn(context);
105
- });
175
+ var type = toString.call(context);
176
+ if(type === functionType) { context = context.call(this); }
106
177
 
107
- Handlebars.registerHelper('log', function(context) {
108
- Handlebars.log(context);
178
+ if (!Handlebars.Utils.isEmpty(context)) return options.fn(context);
109
179
  });
110
180
 
111
- }(this.Handlebars));
181
+ Handlebars.registerHelper('log', function(context, options) {
182
+ var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
183
+ Handlebars.log(level, context);
184
+ });
112
185
  ;
113
186
  // lib/handlebars/utils.js
187
+
188
+ var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
189
+
114
190
  Handlebars.Exception = function(message) {
115
191
  var tmp = Error.prototype.constructor.apply(this, arguments);
116
192
 
117
- for (var p in tmp) {
118
- if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; }
193
+ // Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
194
+ for (var idx = 0; idx < errorProps.length; idx++) {
195
+ this[errorProps[idx]] = tmp[errorProps[idx]];
119
196
  }
120
-
121
- this.message = tmp.message;
122
197
  };
123
198
  Handlebars.Exception.prototype = new Error();
124
199
 
@@ -130,52 +205,61 @@ Handlebars.SafeString.prototype.toString = function() {
130
205
  return this.string.toString();
131
206
  };
132
207
 
133
- (function() {
134
- var escape = {
135
- "&": "&amp;",
136
- "<": "&lt;",
137
- ">": "&gt;",
138
- '"': "&quot;",
139
- "'": "&#x27;",
140
- "`": "&#x60;"
141
- };
142
-
143
- var badChars = /[&<>"'`]/g;
144
- var possible = /[&<>"'`]/;
145
-
146
- var escapeChar = function(chr) {
147
- return escape[chr] || "&amp;";
148
- };
149
-
150
- Handlebars.Utils = {
151
- escapeExpression: function(string) {
152
- // don't escape SafeStrings, since they're already safe
153
- if (string instanceof Handlebars.SafeString) {
154
- return string.toString();
155
- } else if (string == null || string === false) {
156
- return "";
157
- }
208
+ var escape = {
209
+ "&": "&amp;",
210
+ "<": "&lt;",
211
+ ">": "&gt;",
212
+ '"': "&quot;",
213
+ "'": "&#x27;",
214
+ "`": "&#x60;"
215
+ };
216
+
217
+ var badChars = /[&<>"'`]/g;
218
+ var possible = /[&<>"'`]/;
158
219
 
159
- if(!possible.test(string)) { return string; }
160
- return string.replace(badChars, escapeChar);
161
- },
162
-
163
- isEmpty: function(value) {
164
- if (typeof value === "undefined") {
165
- return true;
166
- } else if (value === null) {
167
- return true;
168
- } else if (value === false) {
169
- return true;
170
- } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
171
- return true;
172
- } else {
173
- return false;
220
+ var escapeChar = function(chr) {
221
+ return escape[chr] || "&amp;";
222
+ };
223
+
224
+ Handlebars.Utils = {
225
+ extend: function(obj, value) {
226
+ for(var key in value) {
227
+ if(value.hasOwnProperty(key)) {
228
+ obj[key] = value[key];
174
229
  }
175
230
  }
176
- };
177
- })();;
231
+ },
232
+
233
+ escapeExpression: function(string) {
234
+ // don't escape SafeStrings, since they're already safe
235
+ if (string instanceof Handlebars.SafeString) {
236
+ return string.toString();
237
+ } else if (string == null || string === false) {
238
+ return "";
239
+ }
240
+
241
+ // Force a string conversion as this will be done by the append regardless and
242
+ // the regex test will do this transparently behind the scenes, causing issues if
243
+ // an object's to string has escaped characters in it.
244
+ string = string.toString();
245
+
246
+ if(!possible.test(string)) { return string; }
247
+ return string.replace(badChars, escapeChar);
248
+ },
249
+
250
+ isEmpty: function(value) {
251
+ if (!value && value !== 0) {
252
+ return true;
253
+ } else if(toString.call(value) === "[object Array]" && value.length === 0) {
254
+ return true;
255
+ } else {
256
+ return false;
257
+ }
258
+ }
259
+ };
260
+ ;
178
261
  // lib/handlebars/runtime.js
262
+
179
263
  Handlebars.VM = {
180
264
  template: function(templateSpec) {
181
265
  // Just add water
@@ -186,39 +270,73 @@ Handlebars.VM = {
186
270
  program: function(i, fn, data) {
187
271
  var programWrapper = this.programs[i];
188
272
  if(data) {
189
- return Handlebars.VM.program(fn, data);
190
- } else if(programWrapper) {
191
- return programWrapper;
192
- } else {
193
- programWrapper = this.programs[i] = Handlebars.VM.program(fn);
194
- return programWrapper;
273
+ programWrapper = Handlebars.VM.program(i, fn, data);
274
+ } else if (!programWrapper) {
275
+ programWrapper = this.programs[i] = Handlebars.VM.program(i, fn);
195
276
  }
277
+ return programWrapper;
278
+ },
279
+ merge: function(param, common) {
280
+ var ret = param || common;
281
+
282
+ if (param && common) {
283
+ ret = {};
284
+ Handlebars.Utils.extend(ret, common);
285
+ Handlebars.Utils.extend(ret, param);
286
+ }
287
+ return ret;
196
288
  },
197
289
  programWithDepth: Handlebars.VM.programWithDepth,
198
- noop: Handlebars.VM.noop
290
+ noop: Handlebars.VM.noop,
291
+ compilerInfo: null
199
292
  };
200
293
 
201
294
  return function(context, options) {
202
295
  options = options || {};
203
- return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
296
+ var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
297
+
298
+ var compilerInfo = container.compilerInfo || [],
299
+ compilerRevision = compilerInfo[0] || 1,
300
+ currentRevision = Handlebars.COMPILER_REVISION;
301
+
302
+ if (compilerRevision !== currentRevision) {
303
+ if (compilerRevision < currentRevision) {
304
+ var runtimeVersions = Handlebars.REVISION_CHANGES[currentRevision],
305
+ compilerVersions = Handlebars.REVISION_CHANGES[compilerRevision];
306
+ throw "Template was precompiled with an older version of Handlebars than the current runtime. "+
307
+ "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").";
308
+ } else {
309
+ // Use the embedded version info since the runtime doesn't know about this revision yet
310
+ throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+
311
+ "Please update your runtime to a newer version ("+compilerInfo[1]+").";
312
+ }
313
+ }
314
+
315
+ return result;
204
316
  };
205
317
  },
206
318
 
207
- programWithDepth: function(fn, data, $depth) {
208
- var args = Array.prototype.slice.call(arguments, 2);
319
+ programWithDepth: function(i, fn, data /*, $depth */) {
320
+ var args = Array.prototype.slice.call(arguments, 3);
209
321
 
210
- return function(context, options) {
322
+ var program = function(context, options) {
211
323
  options = options || {};
212
324
 
213
325
  return fn.apply(this, [context, options.data || data].concat(args));
214
326
  };
327
+ program.program = i;
328
+ program.depth = args.length;
329
+ return program;
215
330
  },
216
- program: function(fn, data) {
217
- return function(context, options) {
331
+ program: function(i, fn, data) {
332
+ var program = function(context, options) {
218
333
  options = options || {};
219
334
 
220
335
  return fn(context, options.data || data);
221
336
  };
337
+ program.program = i;
338
+ program.depth = 0;
339
+ return program;
222
340
  },
223
341
  noop: function() { return ""; },
224
342
  invokePartial: function(partial, name, context, helpers, partials, data) {
@@ -239,3 +357,6 @@ Handlebars.VM = {
239
357
 
240
358
  Handlebars.template = Handlebars.VM.template;
241
359
  ;
360
+ // lib/handlebars/browser-suffix.js
361
+ })(Handlebars);
362
+ ;
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: templebars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-19 00:00:00.000000000 Z
12
+ date: 2013-06-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sprockets
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 2.0.3
21
+ version: 2.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,39 +26,39 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 2.0.3
29
+ version: 2.0.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: execjs
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 1.4.0
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 1.4.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: tilt
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
- - - ! '>='
51
+ - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 1.4.1
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ! '>='
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 1.4.1
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rake
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -84,7 +84,6 @@ extra_rdoc_files: []
84
84
  files:
85
85
  - .gitignore
86
86
  - Gemfile
87
- - Gemfile.lock
88
87
  - LICENSE
89
88
  - README.md
90
89
  - Rakefile
data/Gemfile.lock DELETED
@@ -1,30 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- templebars (0.2.0)
5
- execjs
6
- sprockets (>= 2.0.3)
7
- tilt
8
-
9
- GEM
10
- remote: http://rubygems.org/
11
- specs:
12
- execjs (1.4.0)
13
- multi_json (~> 1.0)
14
- hike (1.2.1)
15
- multi_json (1.3.6)
16
- rack (1.4.1)
17
- rake (0.9.2.2)
18
- sprockets (2.5.0)
19
- hike (~> 1.2)
20
- multi_json (~> 1.0)
21
- rack (~> 1.0)
22
- tilt (~> 1.1, != 1.3.0)
23
- tilt (1.3.3)
24
-
25
- PLATFORMS
26
- ruby
27
-
28
- DEPENDENCIES
29
- rake
30
- templebars!