mustache-js-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mustache-js-rails might be problematic. Click here for more details.

@@ -0,0 +1,610 @@
1
+ /*!
2
+ * mustache.js - Logic-less {{mustache}} templates with JavaScript
3
+ * http://github.com/janl/mustache.js
4
+ */
5
+
6
+ /*global define: false*/
7
+
8
+ (function (root, factory) {
9
+ if (typeof exports === "object" && exports) {
10
+ module.exports = factory; // CommonJS
11
+ } else if (typeof define === "function" && define.amd) {
12
+ define(factory); // AMD
13
+ } else {
14
+ root.Mustache = factory; // <script>
15
+ }
16
+ }(this, (function () {
17
+
18
+ var exports = {};
19
+
20
+ exports.name = "mustache.js";
21
+ exports.version = "0.7.2";
22
+ exports.tags = ["{{", "}}"];
23
+
24
+ exports.Scanner = Scanner;
25
+ exports.Context = Context;
26
+ exports.Writer = Writer;
27
+
28
+ var whiteRe = /\s*/;
29
+ var spaceRe = /\s+/;
30
+ var nonSpaceRe = /\S/;
31
+ var eqRe = /\s*=/;
32
+ var curlyRe = /\s*\}/;
33
+ var tagRe = /#|\^|\/|>|\{|&|=|!/;
34
+
35
+ // Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
36
+ // See https://github.com/janl/mustache.js/issues/189
37
+ function testRe(re, string) {
38
+ return RegExp.prototype.test.call(re, string);
39
+ }
40
+
41
+ function isWhitespace(string) {
42
+ return !testRe(nonSpaceRe, string);
43
+ }
44
+
45
+ var isArray = Array.isArray || function (obj) {
46
+ return Object.prototype.toString.call(obj) === "[object Array]";
47
+ };
48
+
49
+ function escapeRe(string) {
50
+ return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
51
+ }
52
+
53
+ var entityMap = {
54
+ "&": "&amp;",
55
+ "<": "&lt;",
56
+ ">": "&gt;",
57
+ '"': '&quot;',
58
+ "'": '&#39;',
59
+ "/": '&#x2F;'
60
+ };
61
+
62
+ function escapeHtml(string) {
63
+ return String(string).replace(/[&<>"'\/]/g, function (s) {
64
+ return entityMap[s];
65
+ });
66
+ }
67
+
68
+ // Export the escaping function so that the user may override it.
69
+ // See https://github.com/janl/mustache.js/issues/244
70
+ exports.escape = escapeHtml;
71
+
72
+ function Scanner(string) {
73
+ this.string = string;
74
+ this.tail = string;
75
+ this.pos = 0;
76
+ }
77
+
78
+ /**
79
+ * Returns `true` if the tail is empty (end of string).
80
+ */
81
+ Scanner.prototype.eos = function () {
82
+ return this.tail === "";
83
+ };
84
+
85
+ /**
86
+ * Tries to match the given regular expression at the current position.
87
+ * Returns the matched text if it can match, the empty string otherwise.
88
+ */
89
+ Scanner.prototype.scan = function (re) {
90
+ var match = this.tail.match(re);
91
+
92
+ if (match && match.index === 0) {
93
+ this.tail = this.tail.substring(match[0].length);
94
+ this.pos += match[0].length;
95
+ return match[0];
96
+ }
97
+
98
+ return "";
99
+ };
100
+
101
+ /**
102
+ * Skips all text until the given regular expression can be matched. Returns
103
+ * the skipped string, which is the entire tail if no match can be made.
104
+ */
105
+ Scanner.prototype.scanUntil = function (re) {
106
+ var match, pos = this.tail.search(re);
107
+
108
+ switch (pos) {
109
+ case -1:
110
+ match = this.tail;
111
+ this.pos += this.tail.length;
112
+ this.tail = "";
113
+ break;
114
+ case 0:
115
+ match = "";
116
+ break;
117
+ default:
118
+ match = this.tail.substring(0, pos);
119
+ this.tail = this.tail.substring(pos);
120
+ this.pos += pos;
121
+ }
122
+
123
+ return match;
124
+ };
125
+
126
+ function Context(view, parent) {
127
+ this.view = view;
128
+ this.parent = parent;
129
+ this.clearCache();
130
+ }
131
+
132
+ Context.make = function (view) {
133
+ return (view instanceof Context) ? view : new Context(view);
134
+ };
135
+
136
+ Context.prototype.clearCache = function () {
137
+ this._cache = {};
138
+ };
139
+
140
+ Context.prototype.push = function (view) {
141
+ return new Context(view, this);
142
+ };
143
+
144
+ Context.prototype.lookup = function (name) {
145
+ var value = this._cache[name];
146
+
147
+ if (!value) {
148
+ if (name === ".") {
149
+ value = this.view;
150
+ } else {
151
+ var context = this;
152
+
153
+ while (context) {
154
+ if (name.indexOf(".") > 0) {
155
+ var names = name.split("."), i = 0;
156
+
157
+ value = context.view;
158
+
159
+ while (value && i < names.length) {
160
+ value = value[names[i++]];
161
+ }
162
+ } else {
163
+ value = context.view[name];
164
+ }
165
+
166
+ if (value != null) {
167
+ break;
168
+ }
169
+
170
+ context = context.parent;
171
+ }
172
+ }
173
+
174
+ this._cache[name] = value;
175
+ }
176
+
177
+ if (typeof value === "function") {
178
+ value = value.call(this.view);
179
+ }
180
+
181
+ return value;
182
+ };
183
+
184
+ function Writer() {
185
+ this.clearCache();
186
+ }
187
+
188
+ Writer.prototype.clearCache = function () {
189
+ this._cache = {};
190
+ this._partialCache = {};
191
+ };
192
+
193
+ Writer.prototype.compile = function (template, tags) {
194
+ var fn = this._cache[template];
195
+
196
+ if (!fn) {
197
+ var tokens = exports.parse(template, tags);
198
+ fn = this._cache[template] = this.compileTokens(tokens, template);
199
+ }
200
+
201
+ return fn;
202
+ };
203
+
204
+ Writer.prototype.compilePartial = function (name, template, tags) {
205
+ var fn = this.compile(template, tags);
206
+ this._partialCache[name] = fn;
207
+ return fn;
208
+ };
209
+
210
+ Writer.prototype.compileTokens = function (tokens, template) {
211
+ var fn = compileTokens(tokens);
212
+ var self = this;
213
+
214
+ return function (view, partials) {
215
+ if (partials) {
216
+ if (typeof partials === "function") {
217
+ self._loadPartial = partials;
218
+ } else {
219
+ for (var name in partials) {
220
+ self.compilePartial(name, partials[name]);
221
+ }
222
+ }
223
+ }
224
+
225
+ return fn(self, Context.make(view), template);
226
+ };
227
+ };
228
+
229
+ Writer.prototype.render = function (template, view, partials) {
230
+ return this.compile(template)(view, partials);
231
+ };
232
+
233
+ Writer.prototype._section = function (name, context, text, callback) {
234
+ var value = context.lookup(name);
235
+
236
+ switch (typeof value) {
237
+ case "object":
238
+ if (isArray(value)) {
239
+ var buffer = "";
240
+
241
+ for (var i = 0, len = value.length; i < len; ++i) {
242
+ buffer += callback(this, context.push(value[i]));
243
+ }
244
+
245
+ return buffer;
246
+ }
247
+
248
+ return value ? callback(this, context.push(value)) : "";
249
+ case "function":
250
+ var self = this;
251
+ var scopedRender = function (template) {
252
+ return self.render(template, context);
253
+ };
254
+
255
+ var result = value.call(context.view, text, scopedRender);
256
+ return result != null ? result : "";
257
+ default:
258
+ if (value) {
259
+ return callback(this, context);
260
+ }
261
+ }
262
+
263
+ return "";
264
+ };
265
+
266
+ Writer.prototype._inverted = function (name, context, callback) {
267
+ var value = context.lookup(name);
268
+
269
+ // Use JavaScript's definition of falsy. Include empty arrays.
270
+ // See https://github.com/janl/mustache.js/issues/186
271
+ if (!value || (isArray(value) && value.length === 0)) {
272
+ return callback(this, context);
273
+ }
274
+
275
+ return "";
276
+ };
277
+
278
+ Writer.prototype._partial = function (name, context) {
279
+ if (!(name in this._partialCache) && this._loadPartial) {
280
+ this.compilePartial(name, this._loadPartial(name));
281
+ }
282
+
283
+ var fn = this._partialCache[name];
284
+
285
+ return fn ? fn(context) : "";
286
+ };
287
+
288
+ Writer.prototype._name = function (name, context) {
289
+ var value = context.lookup(name);
290
+
291
+ if (typeof value === "function") {
292
+ value = value.call(context.view);
293
+ }
294
+
295
+ return (value == null) ? "" : String(value);
296
+ };
297
+
298
+ Writer.prototype._escaped = function (name, context) {
299
+ return exports.escape(this._name(name, context));
300
+ };
301
+
302
+ /**
303
+ * Low-level function that compiles the given `tokens` into a function
304
+ * that accepts three arguments: a Writer, a Context, and the template.
305
+ */
306
+ function compileTokens(tokens) {
307
+ var subRenders = {};
308
+
309
+ function subRender(i, tokens, template) {
310
+ if (!subRenders[i]) {
311
+ var fn = compileTokens(tokens);
312
+ subRenders[i] = function (writer, context) {
313
+ return fn(writer, context, template);
314
+ };
315
+ }
316
+
317
+ return subRenders[i];
318
+ }
319
+
320
+ return function (writer, context, template) {
321
+ var buffer = "";
322
+ var token, sectionText;
323
+
324
+ for (var i = 0, len = tokens.length; i < len; ++i) {
325
+ token = tokens[i];
326
+
327
+ switch (token[0]) {
328
+ case "#":
329
+ sectionText = template.slice(token[3], token[5]);
330
+ buffer += writer._section(token[1], context, sectionText, subRender(i, token[4], template));
331
+ break;
332
+ case "^":
333
+ buffer += writer._inverted(token[1], context, subRender(i, token[4], template));
334
+ break;
335
+ case ">":
336
+ buffer += writer._partial(token[1], context);
337
+ break;
338
+ case "&":
339
+ buffer += writer._name(token[1], context);
340
+ break;
341
+ case "name":
342
+ buffer += writer._escaped(token[1], context);
343
+ break;
344
+ case "text":
345
+ buffer += token[1];
346
+ break;
347
+ }
348
+ }
349
+
350
+ return buffer;
351
+ };
352
+ }
353
+
354
+ /**
355
+ * Forms the given array of `tokens` into a nested tree structure where
356
+ * tokens that represent a section have two additional items: 1) an array of
357
+ * all tokens that appear in that section and 2) the index in the original
358
+ * template that represents the end of that section.
359
+ */
360
+ function nestTokens(tokens) {
361
+ var tree = [];
362
+ var collector = tree;
363
+ var sections = [];
364
+
365
+ var token;
366
+ for (var i = 0, len = tokens.length; i < len; ++i) {
367
+ token = tokens[i];
368
+ switch (token[0]) {
369
+ case '#':
370
+ case '^':
371
+ sections.push(token);
372
+ collector.push(token);
373
+ collector = token[4] = [];
374
+ break;
375
+ case '/':
376
+ var section = sections.pop();
377
+ section[5] = token[2];
378
+ collector = sections.length > 0 ? sections[sections.length - 1][4] : tree;
379
+ break;
380
+ default:
381
+ collector.push(token);
382
+ }
383
+ }
384
+
385
+ return tree;
386
+ }
387
+
388
+ /**
389
+ * Combines the values of consecutive text tokens in the given `tokens` array
390
+ * to a single token.
391
+ */
392
+ function squashTokens(tokens) {
393
+ var squashedTokens = [];
394
+
395
+ var token, lastToken;
396
+ for (var i = 0, len = tokens.length; i < len; ++i) {
397
+ token = tokens[i];
398
+ if (token[0] === 'text' && lastToken && lastToken[0] === 'text') {
399
+ lastToken[1] += token[1];
400
+ lastToken[3] = token[3];
401
+ } else {
402
+ lastToken = token;
403
+ squashedTokens.push(token);
404
+ }
405
+ }
406
+
407
+ return squashedTokens;
408
+ }
409
+
410
+ function escapeTags(tags) {
411
+ return [
412
+ new RegExp(escapeRe(tags[0]) + "\\s*"),
413
+ new RegExp("\\s*" + escapeRe(tags[1]))
414
+ ];
415
+ }
416
+
417
+ /**
418
+ * Breaks up the given `template` string into a tree of token objects. If
419
+ * `tags` is given here it must be an array with two string values: the
420
+ * opening and closing tags used in the template (e.g. ["<%", "%>"]). Of
421
+ * course, the default is to use mustaches (i.e. Mustache.tags).
422
+ */
423
+ exports.parse = function (template, tags) {
424
+ template = template || '';
425
+ tags = tags || exports.tags;
426
+
427
+ if (typeof tags === 'string') tags = tags.split(spaceRe);
428
+ if (tags.length !== 2) {
429
+ throw new Error('Invalid tags: ' + tags.join(', '));
430
+ }
431
+
432
+ var tagRes = escapeTags(tags);
433
+ var scanner = new Scanner(template);
434
+
435
+ var sections = []; // Stack to hold section tokens
436
+ var tokens = []; // Buffer to hold the tokens
437
+ var spaces = []; // Indices of whitespace tokens on the current line
438
+ var hasTag = false; // Is there a {{tag}} on the current line?
439
+ var nonSpace = false; // Is there a non-space char on the current line?
440
+
441
+ // Strips all whitespace tokens array for the current line
442
+ // if there was a {{#tag}} on it and otherwise only space.
443
+ function stripSpace() {
444
+ if (hasTag && !nonSpace) {
445
+ while (spaces.length) {
446
+ tokens.splice(spaces.pop(), 1);
447
+ }
448
+ } else {
449
+ spaces = [];
450
+ }
451
+
452
+ hasTag = false;
453
+ nonSpace = false;
454
+ }
455
+
456
+ var start, type, value, chr;
457
+ while (!scanner.eos()) {
458
+ start = scanner.pos;
459
+ value = scanner.scanUntil(tagRes[0]);
460
+
461
+ if (value) {
462
+ for (var i = 0, len = value.length; i < len; ++i) {
463
+ chr = value.charAt(i);
464
+
465
+ if (isWhitespace(chr)) {
466
+ spaces.push(tokens.length);
467
+ } else {
468
+ nonSpace = true;
469
+ }
470
+
471
+ tokens.push(["text", chr, start, start + 1]);
472
+ start += 1;
473
+
474
+ if (chr === "\n") {
475
+ stripSpace(); // Check for whitespace on the current line.
476
+ }
477
+ }
478
+ }
479
+
480
+ start = scanner.pos;
481
+
482
+ // Match the opening tag.
483
+ if (!scanner.scan(tagRes[0])) {
484
+ break;
485
+ }
486
+
487
+ hasTag = true;
488
+ type = scanner.scan(tagRe) || "name";
489
+
490
+ // Skip any whitespace between tag and value.
491
+ scanner.scan(whiteRe);
492
+
493
+ // Extract the tag value.
494
+ if (type === "=") {
495
+ value = scanner.scanUntil(eqRe);
496
+ scanner.scan(eqRe);
497
+ scanner.scanUntil(tagRes[1]);
498
+ } else if (type === "{") {
499
+ var closeRe = new RegExp("\\s*" + escapeRe("}" + tags[1]));
500
+ value = scanner.scanUntil(closeRe);
501
+ scanner.scan(curlyRe);
502
+ scanner.scanUntil(tagRes[1]);
503
+ type = "&";
504
+ } else {
505
+ value = scanner.scanUntil(tagRes[1]);
506
+ }
507
+
508
+ // Match the closing tag.
509
+ if (!scanner.scan(tagRes[1])) {
510
+ throw new Error('Unclosed tag at ' + scanner.pos);
511
+ }
512
+
513
+ // Check section nesting.
514
+ if (type === '/') {
515
+ if (sections.length === 0) {
516
+ throw new Error('Unopened section "' + value + '" at ' + start);
517
+ }
518
+
519
+ var section = sections.pop();
520
+
521
+ if (section[1] !== value) {
522
+ throw new Error('Unclosed section "' + section[1] + '" at ' + start);
523
+ }
524
+ }
525
+
526
+ var token = [type, value, start, scanner.pos];
527
+ tokens.push(token);
528
+
529
+ if (type === '#' || type === '^') {
530
+ sections.push(token);
531
+ } else if (type === "name" || type === "{" || type === "&") {
532
+ nonSpace = true;
533
+ } else if (type === "=") {
534
+ // Set the tags for the next time around.
535
+ tags = value.split(spaceRe);
536
+
537
+ if (tags.length !== 2) {
538
+ throw new Error('Invalid tags at ' + start + ': ' + tags.join(', '));
539
+ }
540
+
541
+ tagRes = escapeTags(tags);
542
+ }
543
+ }
544
+
545
+ // Make sure there are no open sections when we're done.
546
+ var section = sections.pop();
547
+ if (section) {
548
+ throw new Error('Unclosed section "' + section[1] + '" at ' + scanner.pos);
549
+ }
550
+
551
+ return nestTokens(squashTokens(tokens));
552
+ };
553
+
554
+ // The high-level clearCache, compile, compilePartial, and render functions
555
+ // use this default writer.
556
+ var _writer = new Writer();
557
+
558
+ /**
559
+ * Clears all cached templates and partials in the default writer.
560
+ */
561
+ exports.clearCache = function () {
562
+ return _writer.clearCache();
563
+ };
564
+
565
+ /**
566
+ * Compiles the given `template` to a reusable function using the default
567
+ * writer.
568
+ */
569
+ exports.compile = function (template, tags) {
570
+ return _writer.compile(template, tags);
571
+ };
572
+
573
+ /**
574
+ * Compiles the partial with the given `name` and `template` to a reusable
575
+ * function using the default writer.
576
+ */
577
+ exports.compilePartial = function (name, template, tags) {
578
+ return _writer.compilePartial(name, template, tags);
579
+ };
580
+
581
+ /**
582
+ * Compiles the given array of tokens (the output of a parse) to a reusable
583
+ * function using the default writer.
584
+ */
585
+ exports.compileTokens = function (tokens, template) {
586
+ return _writer.compileTokens(tokens, template);
587
+ };
588
+
589
+ /**
590
+ * Renders the `template` with the given `view` and `partials` using the
591
+ * default writer.
592
+ */
593
+ exports.render = function (template, view, partials) {
594
+ return _writer.render(template, view, partials);
595
+ };
596
+
597
+ // This is here for backwards compatibility with 0.4.x.
598
+ exports.to_html = function (template, view, partials, send) {
599
+ var result = exports.render(template, view, partials);
600
+
601
+ if (typeof send === "function") {
602
+ send(result);
603
+ } else {
604
+ return result;
605
+ }
606
+ };
607
+
608
+ return exports;
609
+
610
+ }())));