util.js 6.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
const {
  parseExpression
} = require('@babel/parser')
const t = require('@babel/types')
fxy060608's avatar
init v3  
fxy060608 已提交
5 6

const ID = '_i'
fxy060608's avatar
fxy060608 已提交
7 8
const ITERATOR1 = '$1'
const ITERATOR2 = '$2'
fxy060608's avatar
fxy060608 已提交
9 10 11
const ITERATOR3 = '$3'
const SET_DATA = '_$s'
const GET_DATA = '_$g'
fxy060608's avatar
fxy060608 已提交
12
const SET_MP_CLASS = '_$smc'
fxy060608's avatar
fxy060608 已提交
13 14 15
const GET_CHANGE_DATA = '_$gc' // wxs

const C_IS = 'is'
fxy060608's avatar
init v3  
fxy060608 已提交
16

fxy060608's avatar
fxy060608 已提交
17 18 19 20
const V_FOR = 'f'
const V_IF = 'i'
const V_ELSE_IF = 'e'

fxy060608's avatar
init v3  
fxy060608 已提交
21 22 23 24
function isVar (str) {
  if (!str) {
    return false
  }
fxy060608's avatar
fxy060608 已提交
25
  const expr = parseExpression(str)
fxy060608's avatar
init v3  
fxy060608 已提交
26
  if (
fxy060608's avatar
fxy060608 已提交
27 28 29 30
    t.isStringLiteral(expr) ||
    t.isNumericLiteral(expr) ||
    t.isBooleanLiteral(expr) ||
    t.isNullLiteral(expr)
fxy060608's avatar
init v3  
fxy060608 已提交
31 32 33 34 35 36
  ) {
    return false
  }
  return true
}

fxy060608's avatar
fxy060608 已提交
37
function addRawAttr (el, name, value) {
fxy060608's avatar
init v3  
fxy060608 已提交
38 39 40 41 42 43 44
  el.attrsMap[name] = value
  el.attrsList.push({
    name,
    value
  })
}

fxy060608's avatar
fxy060608 已提交
45
function updateEleId (el, it, state) {
fxy060608's avatar
init v3  
fxy060608 已提交
46 47 48
  if (el.type !== 1) {
    return
  }
fxy060608's avatar
fxy060608 已提交
49
  const newId = getNewId(el.attrsMap[ID], it)
fxy060608's avatar
fxy060608 已提交
50
  addRawAttr(el, ID, newId)
fxy060608's avatar
fxy060608 已提交
51 52 53 54
  if (el.attrs) {
    const attr = el.attrs.find(attr => attr.name === ID)
    attr.value = newId
  }
fxy060608's avatar
init v3  
fxy060608 已提交
55
  el.children.forEach(child => {
fxy060608's avatar
fxy060608 已提交
56 57 58 59 60 61
    if (!child.for) { // 忽略嵌套 for
      updateEleId(child, it)
    } else {
      child.$parentIterator3 = (child.$parentIterator3 ? (child.$parentIterator3 + '+') : '') + it
      child.forId = `${child.forId}+'-'+${it}`
    }
fxy060608's avatar
init v3  
fxy060608 已提交
62
  })
fxy060608's avatar
fxy060608 已提交
63 64 65
  el.ifConditions && el.ifConditions.forEach((con, index) => {
    index !== 0 && updateEleId(con.block, it, state)
  })
fxy060608's avatar
init v3  
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
}

function getBindingAttr (el, name) {
  return getAndRemoveAttr(el, ':' + name) ||
    getAndRemoveAttr(el, 'v-bind:' + name)
}

function getAndRemoveAttr (el, name) {
  let val
  if ((val = el.attrsMap[name]) != null) {
    const list = el.attrsList
    for (let i = 0, l = list.length; i < l; i++) {
      if (list[i].name === name) {
        list.splice(i, 1)
        break
      }
    }
  }
  delete el.attrsMap[name]
  return val
}

fxy060608's avatar
fxy060608 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
function updateForIterator (el, state) {
  if (!el.for) {
    return
  }
  // 简单处理,确保所有 for 循环,均包含 1,2,3
  const forIteratorId = state.forIteratorId++
  if (!el.iterator1) {
    el.iterator1 = ITERATOR1 + forIteratorId
  }
  if (!el.iterator2) {
    el.iterator2 = ITERATOR2 + forIteratorId
  }
  if (!el.iterator3) {
    el.iterator3 = ITERATOR3 + forIteratorId
  }
}

function updateForEleId (el, state) {
  updateForIterator(el, state)
107
  if (el.for) {
108
    const it = el.$parentIterator3 ? (el.$parentIterator3 + '+' + "'-'" + '+' + el.iterator3) : el.iterator3
fxy060608's avatar
fxy060608 已提交
109
    updateEleId(el, it, state)
fxy060608's avatar
init v3  
fxy060608 已提交
110 111 112
  }
}

fxy060608's avatar
fxy060608 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125
function getNewId (id, it) {
  return Number.isInteger(id) ? `("${id}-"+${it})` : `(${id}+${it})`
}

function updateScopedSlotEleId (el, state) {
  // TODO 暂不考虑 scopedSlot 嵌套情况
  if (el.slotScope) {
    const updateEleId = function (el) {
      if (el.type !== 1) {
        return
      }
      const it = '_si'
      const newId = getNewId(el.attrsMap[ID], it)
fxy060608's avatar
fxy060608 已提交
126 127 128
      if (el.forId) {
        el.forId = getNewId(el.forId, it)
      }
fxy060608's avatar
fxy060608 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      addRawAttr(el, ID, newId)
      if (el.attrs) {
        const attr = el.attrs.find(attr => attr.name === ID)
        attr.value = newId
      }
      el.children.forEach(child => {
        if (!child.slotScope) { // 忽略嵌套 scopedSlot
          updateEleId(child, state)
        }
      })
    }
    if (el.tag === 'template' && el.slotTarget) { // new v-slot
      el.children.forEach(child => {
        if (!child.slotScope) { // 忽略嵌套 scopedSlot
          updateEleId(child, state)
        }
      })
    } else { // old slot-scope
      updateEleId(el)
    }
  }
}

fxy060608's avatar
init v3  
fxy060608 已提交
152 153 154 155
function getForEl (el) {
  if (el.for) {
    return el
  }
fxy060608's avatar
fxy060608 已提交
156
  if (el.parent && el.parent.for && (el.parent.tag === 'template' || el.parent.tag === 'block')) {
fxy060608's avatar
init v3  
fxy060608 已提交
157 158 159 160 161 162
    return el.parent
  }
}

function processForKey (el) {
  const forEl = getForEl(el)
fxy060608's avatar
fxy060608 已提交
163
  if (forEl && !el.key) { // 占位的 text 标签也无需添加 key
fxy060608's avatar
init v3  
fxy060608 已提交
164 165 166
    if (!isVar(forEl.for)) { // <view v-for="10"></view>
      return
    }
fxy060608's avatar
fxy060608 已提交
167 168
    const it = forEl.iterator3
    if (forEl.tag === 'template' || forEl.tag === 'block') {
fxy060608's avatar
init v3  
fxy060608 已提交
169 170
      if (forEl !== el) {
        const keyIndex = forEl.children.indexOf(el)
fxy060608's avatar
fxy060608 已提交
171
        el.key = `${forEl.forId}+'-${keyIndex}'+${it}`
fxy060608's avatar
init v3  
fxy060608 已提交
172
      } else { // 当 template 下只有文本节点
173 174 175 176 177
        if (
          el.children &&
          el.children.length &&
          !el.children.find(child => child.type === 1)
        ) {
fxy060608's avatar
fxy060608 已提交
178
          el.children[0].parent = el
179 180 181
          if (!el.children.find(child => child.key)) {
            el.children[0].key = `${forEl.forId}+'-0'+${it}`
          }
fxy060608's avatar
init v3  
fxy060608 已提交
182 183 184 185
          return true
        }
      }
    } else {
fxy060608's avatar
fxy060608 已提交
186
      el.key = `${forEl.forId}+'-'+${it}`
fxy060608's avatar
init v3  
fxy060608 已提交
187 188 189 190
    }
  }
}

fxy060608's avatar
fxy060608 已提交
191 192 193 194
function hasOwn (obj, key) {
  return hasOwnProperty.call(obj, key)
}

fxy060608's avatar
fxy060608 已提交
195 196
function traverseNode (el, parent, state, isScopedSlot) {
  state.transformNode(el, parent, state, isScopedSlot)
197 198 199 200
  el.children && el.children.forEach((child, index) => {
    state.childIndex = index
    traverseNode(child, el, state, isScopedSlot)
  })
fxy060608's avatar
fxy060608 已提交
201
  el.ifConditions && el.ifConditions.forEach((con, index) => {
202 203 204 205
    if (index !== 0) {
      state.childIndex = index
      traverseNode(con.block, el, state, isScopedSlot)
    }
fxy060608's avatar
fxy060608 已提交
206
  })
207 208
  el.scopedSlots && Object.values(el.scopedSlots).forEach((slot, index) => {
    state.childIndex = index
fxy060608's avatar
fxy060608 已提交
209 210
    slot.slotScope = `${slot.slotScope}, _svm, _si`
    traverseNode(slot, el, state, true)
fxy060608's avatar
fxy060608 已提交
211 212 213
  })
}

fxy060608's avatar
fxy060608 已提交
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
function addAttr (el, name, value, dynamic) {
  const attrs = dynamic
    ? (el.dynamicAttrs || (el.dynamicAttrs = []))
    : (el.attrs || (el.attrs = []))
  attrs.push({
    name,
    value,
    dynamic
  })
  el.plain = false
}

function removeRawAttr (el, name) {
  delete el.attrsMap[name]
  const index = el.attrsList.findIndex(attr => attr.name === name)
  index !== -1 && el.attrsList.splice(index, 1)
}

function removeRawBindingAttr (el, name) {
  removeRawAttr(el, ':' + name)
  removeRawAttr(el, 'v-bind:' + name)
}

function addHandler (el, name, value, important) {
  const events = el.events || (el.events = {})
  const handlers = events[name]
  const newHandler = {
    value: value.trim(),
    dynamic: undefined
  }
  if (Array.isArray(handlers)) {
    important ? handlers.unshift(newHandler) : handlers.push(newHandler)
  } else if (handlers) {
    events[name] = important ? [newHandler, handlers] : [handlers, newHandler]
  } else {
    events[name] = newHandler
  }
  el.plain = false
}

fxy060608's avatar
fxy060608 已提交
254
module.exports = {
fxy060608's avatar
fxy060608 已提交
255
  C_IS,
fxy060608's avatar
fxy060608 已提交
256 257 258
  V_FOR,
  V_IF,
  V_ELSE_IF,
fxy060608's avatar
fxy060608 已提交
259
  ID,
fxy060608's avatar
fxy060608 已提交
260
  SET_DATA,
fxy060608's avatar
fxy060608 已提交
261
  GET_DATA,
fxy060608's avatar
fxy060608 已提交
262
  SET_MP_CLASS,
fxy060608's avatar
fxy060608 已提交
263
  GET_CHANGE_DATA,
fxy060608's avatar
init v3  
fxy060608 已提交
264
  isVar,
fxy060608's avatar
fxy060608 已提交
265
  hasOwn,
fxy060608's avatar
init v3  
fxy060608 已提交
266
  addAttr,
fxy060608's avatar
fxy060608 已提交
267 268 269
  addRawAttr,
  removeRawAttr,
  removeRawBindingAttr,
fxy060608's avatar
fxy060608 已提交
270
  getNewId,
fxy060608's avatar
init v3  
fxy060608 已提交
271
  getForEl,
fxy060608's avatar
fxy060608 已提交
272
  addHandler,
fxy060608's avatar
init v3  
fxy060608 已提交
273 274
  processForKey,
  updateForEleId,
fxy060608's avatar
fxy060608 已提交
275
  updateScopedSlotEleId,
fxy060608's avatar
init v3  
fxy060608 已提交
276
  getBindingAttr,
fxy060608's avatar
fxy060608 已提交
277 278
  getAndRemoveAttr,
  traverseNode
fxy060608's avatar
init v3  
fxy060608 已提交
279
}