src/js/libs/mustache.js
author ymh <ymh.work@gmail.com>
Fri, 18 Sep 2015 17:42:42 +0200
changeset 1057 3f20f286d43e
parent 1001 3210bf928a11
permissions -rw-r--r--
update libs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
     1
/*!
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
     2
 * mustache.js - Logic-less {{mustache}} templates with JavaScript
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
     3
 * http://github.com/janl/mustache.js
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
     4
 */
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     5
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
     6
/*global define: false Mustache: true*/
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     7
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
     8
(function defineMustache (global, factory) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
     9
  if (typeof exports === 'object' && exports && typeof exports.nodeName !== 'string') {
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    10
    factory(exports); // CommonJS
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    11
  } else if (typeof define === 'function' && define.amd) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    12
    define(['exports'], factory); // AMD
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    13
  } else {
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    14
    global.Mustache = {};
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    15
    factory(Mustache); // script, wsh, asp
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    16
  }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    17
}(this, function mustacheFactory (mustache) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    18
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    19
  var objectToString = Object.prototype.toString;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    20
  var isArray = Array.isArray || function isArrayPolyfill (object) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    21
    return objectToString.call(object) === '[object Array]';
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    22
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    23
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    24
  function isFunction (object) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    25
    return typeof object === 'function';
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    26
  }
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    27
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    28
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    29
   * More correct typeof string handling array
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    30
   * which normally returns typeof 'object'
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    31
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    32
  function typeStr (obj) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    33
    return isArray(obj) ? 'array' : typeof obj;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    34
  }
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
    35
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    36
  function escapeRegExp (string) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    37
    return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    38
  }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    39
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    40
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    41
   * Null safe way of checking whether or not an object,
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    42
   * including its prototype, has a given property
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    43
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    44
  function hasProperty (obj, propName) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    45
    return obj != null && typeof obj === 'object' && (propName in obj);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    46
  }
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    47
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    48
  // Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    49
  // See https://github.com/janl/mustache.js/issues/189
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    50
  var regExpTest = RegExp.prototype.test;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    51
  function testRegExp (re, string) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    52
    return regExpTest.call(re, string);
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
    53
  }
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
    54
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    55
  var nonSpaceRe = /\S/;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    56
  function isWhitespace (string) {
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    57
    return !testRegExp(nonSpaceRe, string);
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
    58
  }
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
    59
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    60
  var entityMap = {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    61
    '&': '&amp;',
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    62
    '<': '&lt;',
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    63
    '>': '&gt;',
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    64
    '"': '&quot;',
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    65
    "'": '&#39;',
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    66
    '/': '&#x2F;'
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    67
  };
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    68
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    69
  function escapeHtml (string) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    70
    return String(string).replace(/[&<>"'\/]/g, function fromEntityMap (s) {
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    71
      return entityMap[s];
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
    72
    });
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
    73
  }
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
    74
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    75
  var whiteRe = /\s*/;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    76
  var spaceRe = /\s+/;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    77
  var equalsRe = /\s*=/;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    78
  var curlyRe = /\s*\}/;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    79
  var tagRe = /#|\^|\/|>|\{|&|=|!/;
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    80
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
    81
  /**
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    82
   * Breaks up the given `template` string into a tree of tokens. If the `tags`
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    83
   * argument is given here it must be an array with two string values: the
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    84
   * opening and closing tags used in the template (e.g. [ "<%", "%>" ]). Of
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    85
   * course, the default is to use mustaches (i.e. mustache.tags).
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    86
   *
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    87
   * A token is an array with at least 4 elements. The first element is the
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    88
   * mustache symbol that was used inside the tag, e.g. "#" or "&". If the tag
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    89
   * did not contain a symbol (i.e. {{myValue}}) this element is "name". For
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    90
   * all text that appears outside a symbol this element is "text".
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    91
   *
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    92
   * The second element of a token is its "value". For mustache tags this is
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    93
   * whatever else was inside the tag besides the opening symbol. For text tokens
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    94
   * this is the text itself.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    95
   *
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    96
   * The third and fourth elements of the token are the start and end indices,
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    97
   * respectively, of the token in the original template.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    98
   *
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
    99
   * Tokens that are the root node of a subtree contain two more elements: 1) an
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   100
   * array of tokens in the subtree and 2) the index in the original template at
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   101
   * which the closing tag for that section begins.
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   102
   */
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   103
  function parseTemplate (template, tags) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   104
    if (!template)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   105
      return [];
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   106
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   107
    var sections = [];     // Stack to hold section tokens
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   108
    var tokens = [];       // Buffer to hold the tokens
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   109
    var spaces = [];       // Indices of whitespace tokens on the current line
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   110
    var hasTag = false;    // Is there a {{tag}} on the current line?
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   111
    var nonSpace = false;  // Is there a non-space char on the current line?
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   112
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   113
    // Strips all whitespace tokens array for the current line
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   114
    // if there was a {{#tag}} on it and otherwise only space.
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   115
    function stripSpace () {
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   116
      if (hasTag && !nonSpace) {
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   117
        while (spaces.length)
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   118
          delete tokens[spaces.pop()];
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   119
      } else {
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   120
        spaces = [];
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   121
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   122
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   123
      hasTag = false;
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   124
      nonSpace = false;
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   125
    }
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
   126
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   127
    var openingTagRe, closingTagRe, closingCurlyRe;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   128
    function compileTags (tagsToCompile) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   129
      if (typeof tagsToCompile === 'string')
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   130
        tagsToCompile = tagsToCompile.split(spaceRe, 2);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   131
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   132
      if (!isArray(tagsToCompile) || tagsToCompile.length !== 2)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   133
        throw new Error('Invalid tags: ' + tagsToCompile);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   134
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   135
      openingTagRe = new RegExp(escapeRegExp(tagsToCompile[0]) + '\\s*');
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   136
      closingTagRe = new RegExp('\\s*' + escapeRegExp(tagsToCompile[1]));
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   137
      closingCurlyRe = new RegExp('\\s*' + escapeRegExp('}' + tagsToCompile[1]));
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   138
    }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   139
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   140
    compileTags(tags || mustache.tags);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   141
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   142
    var scanner = new Scanner(template);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   143
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   144
    var start, type, value, chr, token, openSection;
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   145
    while (!scanner.eos()) {
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   146
      start = scanner.pos;
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
   147
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   148
      // Match any text between tags.
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   149
      value = scanner.scanUntil(openingTagRe);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   150
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   151
      if (value) {
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   152
        for (var i = 0, valueLength = value.length; i < valueLength; ++i) {
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   153
          chr = value.charAt(i);
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
   154
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   155
          if (isWhitespace(chr)) {
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   156
            spaces.push(tokens.length);
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   157
          } else {
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   158
            nonSpace = true;
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   159
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   160
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   161
          tokens.push([ 'text', chr, start, start + 1 ]);
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   162
          start += 1;
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   163
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   164
          // Check for whitespace on the current line.
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   165
          if (chr === '\n')
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   166
            stripSpace();
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   167
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   168
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   169
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   170
      // Match the opening tag.
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   171
      if (!scanner.scan(openingTagRe))
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   172
        break;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   173
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   174
      hasTag = true;
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   175
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   176
      // Get the tag type.
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   177
      type = scanner.scan(tagRe) || 'name';
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   178
      scanner.scan(whiteRe);
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   179
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   180
      // Get the tag value.
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   181
      if (type === '=') {
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   182
        value = scanner.scanUntil(equalsRe);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   183
        scanner.scan(equalsRe);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   184
        scanner.scanUntil(closingTagRe);
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   185
      } else if (type === '{') {
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   186
        value = scanner.scanUntil(closingCurlyRe);
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   187
        scanner.scan(curlyRe);
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   188
        scanner.scanUntil(closingTagRe);
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   189
        type = '&';
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
   190
      } else {
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   191
        value = scanner.scanUntil(closingTagRe);
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   192
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   193
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   194
      // Match the closing tag.
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   195
      if (!scanner.scan(closingTagRe))
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   196
        throw new Error('Unclosed tag at ' + scanner.pos);
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   197
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   198
      token = [ type, value, start, scanner.pos ];
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   199
      tokens.push(token);
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   200
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   201
      if (type === '#' || type === '^') {
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   202
        sections.push(token);
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   203
      } else if (type === '/') {
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   204
        // Check section nesting.
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   205
        openSection = sections.pop();
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   206
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   207
        if (!openSection)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   208
          throw new Error('Unopened section "' + value + '" at ' + start);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   209
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   210
        if (openSection[1] !== value)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   211
          throw new Error('Unclosed section "' + openSection[1] + '" at ' + start);
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   212
      } else if (type === 'name' || type === '{' || type === '&') {
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   213
        nonSpace = true;
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   214
      } else if (type === '=') {
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   215
        // Set the tags for the next time around.
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   216
        compileTags(value);
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   217
      }
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   218
    }
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   219
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   220
    // Make sure there are no open sections when we're done.
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   221
    openSection = sections.pop();
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   222
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   223
    if (openSection)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   224
      throw new Error('Unclosed section "' + openSection[1] + '" at ' + scanner.pos);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   225
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   226
    return nestTokens(squashTokens(tokens));
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   227
  }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   228
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   229
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   230
   * Combines the values of consecutive text tokens in the given `tokens` array
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   231
   * to a single token.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   232
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   233
  function squashTokens (tokens) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   234
    var squashedTokens = [];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   235
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   236
    var token, lastToken;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   237
    for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   238
      token = tokens[i];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   239
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   240
      if (token) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   241
        if (token[0] === 'text' && lastToken && lastToken[0] === 'text') {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   242
          lastToken[1] += token[1];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   243
          lastToken[3] = token[3];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   244
        } else {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   245
          squashedTokens.push(token);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   246
          lastToken = token;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   247
        }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   248
      }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   249
    }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   250
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   251
    return squashedTokens;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   252
  }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   253
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   254
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   255
   * Forms the given array of `tokens` into a nested tree structure where
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   256
   * tokens that represent a section have two additional items: 1) an array of
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   257
   * all tokens that appear in that section and 2) the index in the original
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   258
   * template that represents the end of that section.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   259
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   260
  function nestTokens (tokens) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   261
    var nestedTokens = [];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   262
    var collector = nestedTokens;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   263
    var sections = [];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   264
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   265
    var token, section;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   266
    for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   267
      token = tokens[i];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   268
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   269
      switch (token[0]) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   270
      case '#':
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   271
      case '^':
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   272
        collector.push(token);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   273
        sections.push(token);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   274
        collector = token[4] = [];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   275
        break;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   276
      case '/':
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   277
        section = sections.pop();
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   278
        section[5] = token[2];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   279
        collector = sections.length > 0 ? sections[sections.length - 1][4] : nestedTokens;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   280
        break;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   281
      default:
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   282
        collector.push(token);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   283
      }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   284
    }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   285
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   286
    return nestedTokens;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   287
  }
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   288
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   289
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   290
   * A simple string scanner that is used by the template parser to find
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   291
   * tokens in template strings.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   292
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   293
  function Scanner (string) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   294
    this.string = string;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   295
    this.tail = string;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   296
    this.pos = 0;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   297
  }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   298
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   299
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   300
   * Returns `true` if the tail is empty (end of string).
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   301
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   302
  Scanner.prototype.eos = function eos () {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   303
    return this.tail === '';
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   304
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   305
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   306
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   307
   * Tries to match the given regular expression at the current position.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   308
   * Returns the matched text if it can match, the empty string otherwise.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   309
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   310
  Scanner.prototype.scan = function scan (re) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   311
    var match = this.tail.match(re);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   312
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   313
    if (!match || match.index !== 0)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   314
      return '';
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   315
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   316
    var string = match[0];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   317
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   318
    this.tail = this.tail.substring(string.length);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   319
    this.pos += string.length;
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   320
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   321
    return string;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   322
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   323
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   324
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   325
   * Skips all text until the given regular expression can be matched. Returns
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   326
   * the skipped string, which is the entire tail if no match can be made.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   327
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   328
  Scanner.prototype.scanUntil = function scanUntil (re) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   329
    var index = this.tail.search(re), match;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   330
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   331
    switch (index) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   332
    case -1:
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   333
      match = this.tail;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   334
      this.tail = '';
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   335
      break;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   336
    case 0:
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   337
      match = '';
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   338
      break;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   339
    default:
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   340
      match = this.tail.substring(0, index);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   341
      this.tail = this.tail.substring(index);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   342
    }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   343
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   344
    this.pos += match.length;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   345
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   346
    return match;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   347
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   348
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   349
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   350
   * Represents a rendering context by wrapping a view object and
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   351
   * maintaining a reference to the parent context.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   352
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   353
  function Context (view, parentContext) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   354
    this.view = view;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   355
    this.cache = { '.': this.view };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   356
    this.parent = parentContext;
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   357
  }
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
   358
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   359
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   360
   * Creates a new context using the given view with this context
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   361
   * as the parent.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   362
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   363
  Context.prototype.push = function push (view) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   364
    return new Context(view, this);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   365
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   366
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   367
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   368
   * Returns the value of the given name in this context, traversing
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   369
   * up the context hierarchy if the value is absent in this context's view.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   370
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   371
  Context.prototype.lookup = function lookup (name) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   372
    var cache = this.cache;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   373
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   374
    var value;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   375
    if (cache.hasOwnProperty(name)) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   376
      value = cache[name];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   377
    } else {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   378
      var context = this, names, index, lookupHit = false;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   379
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   380
      while (context) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   381
        if (name.indexOf('.') > 0) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   382
          value = context.view;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   383
          names = name.split('.');
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   384
          index = 0;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   385
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   386
          /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   387
           * Using the dot notion path in `name`, we descend through the
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   388
           * nested objects.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   389
           *
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   390
           * To be certain that the lookup has been successful, we have to
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   391
           * check if the last object in the path actually has the property
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   392
           * we are looking for. We store the result in `lookupHit`.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   393
           *
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   394
           * This is specially necessary for when the value has been set to
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   395
           * `undefined` and we want to avoid looking up parent contexts.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   396
           **/
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   397
          while (value != null && index < names.length) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   398
            if (index === names.length - 1)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   399
              lookupHit = hasProperty(value, names[index]);
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   400
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   401
            value = value[names[index++]];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   402
          }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   403
        } else {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   404
          value = context.view[name];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   405
          lookupHit = hasProperty(context.view, name);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   406
        }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   407
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   408
        if (lookupHit)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   409
          break;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   410
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   411
        context = context.parent;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   412
      }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   413
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   414
      cache[name] = value;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   415
    }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   416
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   417
    if (isFunction(value))
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   418
      value = value.call(this.view);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   419
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   420
    return value;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   421
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   422
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   423
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   424
   * A Writer knows how to take a stream of tokens and render them to a
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   425
   * string, given a context. It also maintains a cache of templates to
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   426
   * avoid the need to parse the same template twice.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   427
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   428
  function Writer () {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   429
    this.cache = {};
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   430
  }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   431
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   432
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   433
   * Clears all cached templates in this writer.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   434
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   435
  Writer.prototype.clearCache = function clearCache () {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   436
    this.cache = {};
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   437
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   438
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   439
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   440
   * Parses and caches the given `template` and returns the array of tokens
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   441
   * that is generated from the parse.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   442
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   443
  Writer.prototype.parse = function parse (template, tags) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   444
    var cache = this.cache;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   445
    var tokens = cache[template];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   446
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   447
    if (tokens == null)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   448
      tokens = cache[template] = parseTemplate(template, tags);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   449
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   450
    return tokens;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   451
  };
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   452
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   453
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   454
   * High-level method that is used to render the given `template` with
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   455
   * the given `view`.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   456
   *
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   457
   * The optional `partials` argument may be an object that contains the
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   458
   * names and templates of partials that are used in the template. It may
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   459
   * also be a function that is used to load partial templates on the fly
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   460
   * that takes a single argument: the name of the partial.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   461
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   462
  Writer.prototype.render = function render (template, view, partials) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   463
    var tokens = this.parse(template);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   464
    var context = (view instanceof Context) ? view : new Context(view);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   465
    return this.renderTokens(tokens, context, partials, template);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   466
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   467
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   468
  /**
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   469
   * Low-level method that renders the given array of `tokens` using
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   470
   * the given `context` and `partials`.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   471
   *
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   472
   * Note: The `originalTemplate` is only ever used to extract the portion
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   473
   * of the original template that was contained in a higher-order section.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   474
   * If the template doesn't use higher-order sections, this argument may
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   475
   * be omitted.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   476
   */
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   477
  Writer.prototype.renderTokens = function renderTokens (tokens, context, partials, originalTemplate) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   478
    var buffer = '';
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   479
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   480
    var token, symbol, value;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   481
    for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   482
      value = undefined;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   483
      token = tokens[i];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   484
      symbol = token[0];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   485
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   486
      if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   487
      else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   488
      else if (symbol === '>') value = this.renderPartial(token, context, partials, originalTemplate);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   489
      else if (symbol === '&') value = this.unescapedValue(token, context);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   490
      else if (symbol === 'name') value = this.escapedValue(token, context);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   491
      else if (symbol === 'text') value = this.rawValue(token);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   492
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   493
      if (value !== undefined)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   494
        buffer += value;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   495
    }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   496
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   497
    return buffer;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   498
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   499
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   500
  Writer.prototype.renderSection = function renderSection (token, context, partials, originalTemplate) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   501
    var self = this;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   502
    var buffer = '';
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   503
    var value = context.lookup(token[1]);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   504
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   505
    // This function is used to render an arbitrary template
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   506
    // in the current context by higher-order sections.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   507
    function subRender (template) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   508
      return self.render(template, context, partials);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   509
    }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   510
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   511
    if (!value) return;
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   512
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   513
    if (isArray(value)) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   514
      for (var j = 0, valueLength = value.length; j < valueLength; ++j) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   515
        buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   516
      }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   517
    } else if (typeof value === 'object' || typeof value === 'string' || typeof value === 'number') {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   518
      buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   519
    } else if (isFunction(value)) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   520
      if (typeof originalTemplate !== 'string')
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   521
        throw new Error('Cannot use higher-order sections without the original template');
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   522
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   523
      // Extract the portion of the original template that the section contains.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   524
      value = value.call(context.view, originalTemplate.slice(token[3], token[5]), subRender);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   525
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   526
      if (value != null)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   527
        buffer += value;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   528
    } else {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   529
      buffer += this.renderTokens(token[4], context, partials, originalTemplate);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   530
    }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   531
    return buffer;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   532
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   533
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   534
  Writer.prototype.renderInverted = function renderInverted (token, context, partials, originalTemplate) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   535
    var value = context.lookup(token[1]);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   536
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   537
    // Use JavaScript's definition of falsy. Include empty arrays.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   538
    // See https://github.com/janl/mustache.js/issues/186
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   539
    if (!value || (isArray(value) && value.length === 0))
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   540
      return this.renderTokens(token[4], context, partials, originalTemplate);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   541
  };
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   542
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   543
  Writer.prototype.renderPartial = function renderPartial (token, context, partials) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   544
    if (!partials) return;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   545
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   546
    var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   547
    if (value != null)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   548
      return this.renderTokens(this.parse(value), context, partials, value);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   549
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   550
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   551
  Writer.prototype.unescapedValue = function unescapedValue (token, context) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   552
    var value = context.lookup(token[1]);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   553
    if (value != null)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   554
      return value;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   555
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   556
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   557
  Writer.prototype.escapedValue = function escapedValue (token, context) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   558
    var value = context.lookup(token[1]);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   559
    if (value != null)
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   560
      return mustache.escape(value);
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   561
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   562
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   563
  Writer.prototype.rawValue = function rawValue (token) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   564
    return token[1];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   565
  };
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   566
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   567
  mustache.name = 'mustache.js';
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   568
  mustache.version = '2.1.3';
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   569
  mustache.tags = [ '{{', '}}' ];
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   570
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   571
  // All high-level mustache.* functions use this writer.
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   572
  var defaultWriter = new Writer();
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   573
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   574
  /**
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   575
   * Clears all cached templates in the default writer.
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   576
   */
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   577
  mustache.clearCache = function clearCache () {
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   578
    return defaultWriter.clearCache();
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   579
  };
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   580
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   581
  /**
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   582
   * Parses and caches the given template in the default writer and returns the
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   583
   * array of tokens it contains. Doing this ahead of time avoids the need to
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   584
   * parse templates on the fly as they are rendered.
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   585
   */
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   586
  mustache.parse = function parse (template, tags) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   587
    return defaultWriter.parse(template, tags);
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   588
  };
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
   589
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   590
  /**
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   591
   * Renders the `template` with the given `view` and `partials` using the
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   592
   * default writer.
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   593
   */
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   594
  mustache.render = function render (template, view, partials) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   595
    if (typeof template !== 'string') {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   596
      throw new TypeError('Invalid template! Template should be a "string" ' +
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   597
                          'but "' + typeStr(template) + '" was given as the first ' +
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   598
                          'argument for mustache#render(template, view, partials)');
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   599
    }
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   600
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   601
    return defaultWriter.render(template, view, partials);
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   602
  };
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
   603
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   604
  // This is here for backwards compatibility with 0.4.x.,
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   605
  /*eslint-disable */ // eslint wants camel cased function name
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   606
  mustache.to_html = function to_html (template, view, partials, send) {
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   607
    /*eslint-enable*/
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   608
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   609
    var result = mustache.render(template, view, partials);
583
310f5517a2ea bumped mustache.js version to get access to the new features.
hamidouk
parents: 110
diff changeset
   610
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   611
    if (isFunction(send)) {
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   612
      send(result);
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   613
    } else {
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   614
      return result;
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   615
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   616
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   617
1057
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   618
  // Export the escaping function so that the user may override it.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   619
  // See https://github.com/janl/mustache.js/issues/244
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   620
  mustache.escape = escapeHtml;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   621
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   622
  // Export these mainly for testing, but also for advanced usage.
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   623
  mustache.Scanner = Scanner;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   624
  mustache.Context = Context;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   625
  mustache.Writer = Writer;
3f20f286d43e update libs
ymh <ymh.work@gmail.com>
parents: 1001
diff changeset
   626
1001
3210bf928a11 Enabled loading widgets without the widgeting framework
veltr
parents: 583
diff changeset
   627
}));