Code coverage report for src/compile/blocks.js

Statements: 96.55% (112 / 116)      Branches: 86.79% (46 / 53)      Functions: 100% (13 / 13)      Lines: 96.55% (112 / 116)      Ignored: none     

All files » src/compile/ » blocks.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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265  1         1           55 55   55   25 25   16 16   12 12   1 1   1 1         55             1 1 1   1             12 12 12   12                   32 32   32   18 18 18   18   18 15     18 18 9 9       18 18   3 3   3 3 3 3           14 14 14 14 14 14   14 17 17           14     29                       25   7     7         18 18 18   18   14   4   4       18   18 18 18 18 18   18                       16 16 16 16 16         16 16 16   16 16       16   16   43 2     41       41         41   41 41       16   16 16 16   16                 25 25   25   84   28 1   27   56 4 1   3 52 32     82       25        
'use strict';
module.exports = function(Velocity, utils) {
 
  /**
   * blocks语法处理
   */
  utils.mixin(Velocity.prototype, {
    /**
     * 处理代码库: if foreach macro
     */
    getBlock: function(block) {
 
      var ast = block[0];
      var ret = '';
 
      switch (ast.type) {
        case 'if':
          ret = this.getBlockIf(block);
          break;
        case 'foreach':
          ret = this.getBlockEach(block);
          break;
        case 'macro':
          this.setBlockMacro(block);
          break;
        case 'noescape':
          ret = this._render(block.slice(1));
          break;
        case 'define':
          this.setBlockDefine(block);
          break;
        default:
          ret = this._render(block);
      }
 
      return ret || '';
    },
 
    /**
     * define
     */
    setBlockDefine: function(block) {
      var ast = block[0];
      var _block = block.slice(1);
      var defines = this.defines;
 
      defines[ast.id] = _block;
    },
 
    /**
     * define macro
     */
    setBlockMacro: function(block) {
      var ast = block[0];
      var _block = block.slice(1);
      var macros = this.macros;
 
      macros[ast.id] = {
        asts: _block,
        args: ast.args
      };
    },
 
    /**
     * parse macro call
     */
    getMacro: function(ast) {
      var macro = this.macros[ast.id];
      var ret = '';
 
      if (!macro) {
 
        var jsmacros = this.jsmacros;
        macro = jsmacros[ast.id];
        var jsArgs = [];
 
        Eif (macro && macro.apply) {
 
          utils.forEach(ast.args, function(a) {
            jsArgs.push(this.getLiteral(a));
          }, this);
 
          var self = this;
          if (!jsmacros.eval) {
            jsmacros.eval = function() {
              return self.eval.apply(self, arguments);
            };
          }
 
          try {
            ret = macro.apply(jsmacros, jsArgs);
          } catch (e) {
            var pos = ast.pos;
            var text = Velocity.Helper.getRefText(ast);
            // throws error tree
            var err = '\n      at ' + text + ' L/N ' + pos.first_line + ':' + pos.first_column;
            e.name = '';
            e.message += err;
            throw new Error(e);
          }
 
        }
 
      } else {
        var asts = macro.asts;
        var args = macro.args;
        var callArgs = ast.args;
        var local = {};
        var guid = utils.guid();
        var contextId = 'macro:' + ast.id + ':' + guid;
 
        utils.forEach(args, function(ref, i) {
          Eif (callArgs[i]) {
            local[ref.id] = this.getLiteral(callArgs[i]);
          } else {
            local[ref.id] = undefined;
          }
        }, this);
 
        ret = this.eval(asts, local, contextId);
      }
 
      return ret;
    },
 
    /**
     * eval
     * @param str {array|string} 需要解析的字符串
     * @param local {object} 局部变量
     * @param contextId {string}
     * @return {string}
     */
    eval: function(str, local, contextId) {
 
      if (!local) {
 
        Iif (utils.isArray(str)) {
          return this._render(str);
        } else {
          return this.evalStr(str);
        }
 
      } else {
 
        var asts = [];
        var parse = Velocity.parse;
        contextId = contextId || ('eval:' + utils.guid());
 
        if (utils.isArray(str)) {
 
          asts = str;
 
        } else Eif (parse) {
 
          asts = parse(str);
 
        }
 
        Eif (asts.length) {
 
          this.local[contextId] = local;
          var ret = this._render(asts, contextId);
          this.local[contextId] = {};
          this.conditions.shift();
          this.condition = this.conditions[0] || '';
 
          return ret;
        }
 
      }
 
    },
 
    /**
     * parse #foreach
     */
    getBlockEach: function(block) {
 
      var ast = block[0];
      var _from = this.getLiteral(ast.from);
      var _block = block.slice(1);
      var _to = ast.to;
      var local = {
        foreach: {
          count: 0
        }
      };
      var ret = '';
      var guid = utils.guid();
      var contextId = 'foreach:' + guid;
 
      var type = ({}).toString.call(_from);
      Iif (!_from || (type !== '[object Array]' && type !== '[object Object]')) {
        return '';
      }
 
      var len = utils.isArray(_from) ? _from.length : utils.keys(_from).length;
 
      utils.forEach(_from, function(val, i) {
 
        if (this._state.break) {
          return;
        }
        // 构造临时变量
        local[_to] = val;
        // TODO: here, the foreach variable give to local, when _from is not an
        // array, count and hasNext would be undefined, also i is not the
        // index.
        local.foreach = {
          count: i + 1,
          index: i,
          hasNext: i + 1 < len
        };
        local.velocityCount = i + 1;
 
        this.local[contextId] = local;
        ret += this._render(_block, contextId);
 
      }, this);
 
      this._state.break = false;
      // 删除临时变量
      this.local[contextId] = {};
      this.conditions.shift();
      this.condition = this.conditions[0] || '';
 
      return ret;
 
    },
 
    /**
     * parse #if
     */
    getBlockIf: function(block) {
 
      var received = false;
      var asts = [];
 
      utils.some(block, function(ast) {
 
        if (ast.condition) {
 
          if (received) {
            return true;
          }
          received = this.getExpression(ast.condition);
 
        } else if (ast.type === 'else') {
          if (received) {
            return true;
          }
          received = true;
        } else if (received) {
          asts.push(ast);
        }
 
        return false;
 
      }, this);
 
      return this._render(asts);
    }
  });
};