Code coverage report for src/compile/compile.js

Statements: 100% (47 / 47)      Branches: 100% (23 / 23)      Functions: 100% (6 / 6)      Lines: 100% (47 / 47)      Ignored: none     

All files » src/compile/ » compile.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 1121         1   130 130 130 130 130 130 130   130 130   3 3                         131 131 131 131 131 128 128   128   128                       230 230   230   59   34     59     171     230     542 9     533   130 128     65 65     1 1     32 29     12     2 2     291 291       225        
module.exports = function(Velocity, utils) {
 
  /**
   * compile
   */
  utils.mixin(Velocity.prototype, {
    init: function() {
      this.context = {};
      this.macros = {};
      this.defines = {};
      this.conditions = [];
      this.local = {};
      this.silence = false;
      this.unescape = {};
 
      var self = this;
      this.directive = {
        stop: function() {
          self._state.stop = true;
          return '';
        }
      };
    },
 
    /**
     * @param context {object} 上下文环境,数据对象
     * @param macro   {object} self defined #macro
     * @param silent {bool} 如果是true,$foo变量将原样输出
     * @return str
     */
    render: function(context, macros, silence) {
 
      this.silence = !!silence;
      this.context = context || {};
      this.jsmacros = utils.mixin(macros || {}, this.directive);
      var t1 = utils.now();
      var str = this._render();
      var t2 = utils.now();
      var cost = t2 - t1;
 
      this.cost = cost;
 
      return str;
    },
 
    /**
     * 解析入口函数
     * @param ast {array} 模板结构数组
     * @param contextId {number} 执行环境id,对于macro有局部作用域,变量的设置和
     * 取值,都放在一个this.local下,通过contextId查找
     * @return {string}解析后的字符串
     */
    _render: function(asts, contextId) {
 
      var str = '';
      asts = asts || this.asts;
 
      if (contextId) {
 
        if (contextId !== this.condition &&
            utils.indexOf(contextId, this.conditions) === -1) {
          this.conditions.unshift(contextId);
        }
 
        this.condition = contextId;
 
      } else {
        this.condition = null;
      }
 
      utils.forEach(asts, function(ast) {
 
        // 进入stop,直接退出
        if (this._state.stop === true) {
          return false;
        }
 
        switch (ast.type) {
          case 'references':
            str += this.getReferences(ast, true);
          break;
 
          case 'set':
            this.setValue(ast);
          break;
 
          case 'break':
            this._state.break = true;
          break;
 
          case 'macro_call':
            str += this.getMacro(ast);
          break;
 
          case 'comment':
          break;
 
          case 'raw':
            str += ast.value;
          break;
 
          default:
            str += typeof ast === 'string' ? ast : this.getBlock(ast);
          break;
        }
      }, this);
 
      return str;
    }
  });
};