Code coverage report for src/compile/references.js

Statements: 98.48% (130 / 132)      Branches: 95.56% (86 / 90)      Functions: 100% (16 / 16)      Lines: 98.43% (125 / 127)      Ignored: none     

All files » src/compile/ » references.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 266 267 268 269 270 271 272 273 274 275 276 277 278 2791       1   4 1 3 1     2           1   112   83 83 83   83 761 761 757 1 1 756 4 4 752 4 4   748     4     761     83         1         1   1 1                         203 117 117 1   116   202   202 202 202 202 202   202   202 1       201 9     199   199   52     59         199 16     199   199               202 202 202   202 81 81 68 68     13     202                               59 59 59 59 25 34 32   2   59               2 2 2 1 1 1         2               34 34 34     34   4 1     3 3     4     30   2   2 2   28   1 1 1   27   1   26   1 1 5     1   25   4       21 21   21 19     21   21   21 2     21 21   2 2 2 2 2 2               19          
module.exports = function(Velocity, utils){
 
  'use strict';
 
  function getSize(obj){
 
    if (utils.isArray(obj)) {
      return obj.length;
    } else if (utils.isObject(obj)) {
      return utils.keys(obj).length;
    }
 
    return undefined;
  }
 
  /**
   * unicode转码
   */
  function convert(str){
 
    if (typeof str !== 'string') return str;
 
    var result = ""
    var escape = false
    var i, c, cstr;
 
    for(i = 0 ; i < str.length ; i++) {
      c = str.charAt(i);
      if((' ' <= c && c <= '~') || (c == '\r') || (c == '\n')) {
        if(c == '&') {
          cstr = "&amp;"
          escape = true
        } else if(c == '<') {
          cstr = "&lt;"
          escape = true
        } else if(c == '>') {
          cstr = "&gt;"
          escape = true
        } else {
          cstr = c.toString()
        }
      } else {
        cstr = "&#" + c.charCodeAt().toString() + ";"
      }
 
      result = result + cstr
    }
 
    return escape ? result : str
 
  }
  
 
  utils.mixin(Velocity.prototype, {
    
    //增加某些函数,不需要执行html转义
    addIgnoreEscpape: function(key){
 
      Eif (!utils.isArray(key)) key = [key]
 
      utils.forEach(key, function(key){
        this.config.unescape[key] = true
      }, this)
 
    },
 
    /**
     * 引用求值
     * @param {object} ast 结构来自velocity.yy
     * @param {bool} isVal 取值还是获取字符串,两者的区别在于,求值返回结果,求
     * 字符串,如果没有返回变量自身,比如$foo
     */
    getReferences: function(ast, isVal) {
 
      if (ast.prue) {
        var define = this.defines[ast.id];
        if (utils.isArray(define)) {
          return this._render(define);
        }
        if (ast.id in this.config.unescape) ast.prue = false;
      }
      var escape = this.config.escape;
 
      var isSilent = this.silence || ast.leader === "$!";
      var isfn     = ast.args !== undefined;
      var context  = this.context;
      var ret      = context[ast.id];
      var local    = this.getLocal(ast);
 
      var text = Velocity.Helper.getRefText(ast);
 
      if (text in context) {
        return (ast.prue && escape) ? convert(context[text]) : context[text];
      }
 
 
      if (ret !== undefined && isfn) {
        ret = this.getPropMethod(ast, context, ast);
      }
 
      if (local.isLocaled) ret = local['value'];
 
      if (ast.path && ret !== undefined) {
 
        utils.some(ast.path, function(property){
 
          //第三个参数,返回后面的参数ast
          ret = this.getAttributes(property, ret, ast);
 
        }, this);
      }
 
      if (isVal && ret === undefined) {
        ret = isSilent? '' : Velocity.Helper.getRefText(ast);
      }
 
      ret = (ast.prue && escape) ? convert(ret) : ret;
 
      return ret;
    },
 
    /**
     * 获取局部变量,在macro和foreach循环中使用
     */
    getLocal: function(ast){
 
      var id = ast.id;
      var local = this.local;
      var ret = false;
 
      var isLocaled = utils.some(this.conditions, function(contextId){
        var _local = local[contextId];
        if (id in _local) {
          ret = _local[id];
          return true;
        }
 
        return false;
      }, this);
 
      return {
        value: ret,
        isLocaled: isLocaled
      };
    },
    /**
     * $foo.bar 属性求值,最后面两个参数在用户传递的函数中用到
     * @param {object} property 属性描述,一个对象,主要包括id,type等定义
     * @param {object} baseRef 当前执行链结果,比如$a.b.c,第一次baseRef是$a,
     * 第二次是$a.b返回值
     * @private
     */
    getAttributes: function(property, baseRef, ast){
      /**
       * type对应着velocity.yy中的attribute,三种类型: method, index, property
       */
      var type = property.type;
      var ret;
      var id = property.id;
      if (type === 'method'){
        ret = this.getPropMethod(property, baseRef, ast);
      } else if (type === 'property') {
        ret = baseRef[id];
      } else {
        ret = this.getPropIndex(property, baseRef);
      }
      return ret;
    },
 
    /**
     * $foo.bar[1] index求值
     * @private
     */
    getPropIndex: function(property, baseRef){
      var ast = property.id;
      var key;
      if (ast.type === 'references'){
        key = this.getReferences(ast);
      } else Eif(ast.type === 'integer'){
        key = ast.value;
      } else {
        key = ast.value;
      }
 
      return baseRef[key];
    },
 
    /**
     * $foo.bar()求值
     */
    getPropMethod: function(property, baseRef, ast){
 
      var id         = property.id;
      var ret        = '';
      var _id        = id.slice(3);
 
      // getter 处理
      if (id.indexOf('get') === 0 && !(id in baseRef)) {
 
        if (_id) {
          ret = baseRef[_id];
        } else {
          //map 对应的get方法
          _id = this.getLiteral(property.args[0]);
          ret = baseRef[_id];
        }
 
        return ret;
 
      // setter 处理
      } else if (id.indexOf('set') === 0 && !baseRef[id]) {
 
        baseRef[_id] = this.getLiteral(property.args[0]);
        // $page.setName(123)
        baseRef.toString =  function() { return ''; };
        return baseRef;
 
      } else if (id.indexOf('is') === 0 && !(id in baseRef)) {
 
        _id = id.slice(2);
        ret = baseRef[_id];
        return ret;
 
      } else if (id === 'keySet') {
 
        return utils.keys(baseRef);
 
      } else if (id === 'entrySet') {
 
        ret = [];
        utils.forEach(baseRef, function(value, key){
          ret.push({key: key, value: value});
        });
 
        return ret;
 
      } else if (id === 'size') {
 
        return getSize(baseRef);
 
      } else {
 
        ret = baseRef[id];
        var args = [];
 
        utils.forEach(property.args, function(exp){
          args.push(this.getLiteral(exp));
        }, this);
 
        Eif (ret && ret.call) {
 
          var that = this;
 
          baseRef.eval = function() {
            return that.eval.apply(that, arguments);
          };
 
          try {
            ret = ret.apply(baseRef, args);
          } catch(e) {
            var pos = ast.pos;
            var text = Velocity.Helper.getRefText(ast);
            var err = ' on ' + text + ' at L/N ' + pos.first_line + ':' + pos.first_column;
            e.name = '';
            e.message += err;
            throw new Error(e);
          }
 
        } else {
          ret = undefined;
        }
      }
 
      return ret;
    }
  })
 
}