util.js 6.3 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 13 14
const GET_CHANGE_DATA = '_$gc' // wxs

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

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

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

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

fxy060608's avatar
fxy060608 已提交
44
function updateEleId (el, it, state) {
fxy060608's avatar
init v3  
fxy060608 已提交
45 46 47
  if (el.type !== 1) {
    return
  }
fxy060608's avatar
fxy060608 已提交
48
  const newId = getNewId(el.attrsMap[ID], it)
fxy060608's avatar
fxy060608 已提交
49
  addRawAttr(el, ID, newId)
fxy060608's avatar
fxy060608 已提交
50 51 52 53
  if (el.attrs) {
    const attr = el.attrs.find(attr => attr.name === ID)
    attr.value = newId
  }
fxy060608's avatar
init v3  
fxy060608 已提交
54
  el.children.forEach(child => {
fxy060608's avatar
fxy060608 已提交
55 56 57 58 59 60
    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 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  })
}

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 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
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)
fxy060608's avatar
init v3  
fxy060608 已提交
103
  if (el.for) {
fxy060608's avatar
fxy060608 已提交
104 105
    const it = el.$parentIterator3 ? (el.$parentIterator3 + '+' + el.iterator3) : el.iterator3
    updateEleId(el, it, state)
fxy060608's avatar
init v3  
fxy060608 已提交
106 107 108
  }
}

fxy060608's avatar
fxy060608 已提交
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
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)
      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 已提交
145 146 147 148
function getForEl (el) {
  if (el.for) {
    return el
  }
fxy060608's avatar
fxy060608 已提交
149
  if (el.parent && el.parent.for && (el.parent.tag === 'template' || el.parent.tag === 'block')) {
fxy060608's avatar
init v3  
fxy060608 已提交
150 151 152 153 154 155
    return el.parent
  }
}

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

fxy060608's avatar
fxy060608 已提交
178 179 180 181
function hasOwn (obj, key) {
  return hasOwnProperty.call(obj, key)
}

fxy060608's avatar
fxy060608 已提交
182 183 184
function traverseNode (el, parent, state, isScopedSlot) {
  state.transformNode(el, parent, state, isScopedSlot)
  el.children && el.children.forEach(child => traverseNode(child, el, state, isScopedSlot))
fxy060608's avatar
fxy060608 已提交
185
  el.ifConditions && el.ifConditions.forEach((con, index) => {
fxy060608's avatar
fxy060608 已提交
186 187 188 189 190
    index !== 0 && traverseNode(con.block, el, state, isScopedSlot)
  })
  el.scopedSlots && Object.values(el.scopedSlots).forEach(slot => {
    slot.slotScope = `${slot.slotScope}, _svm, _si`
    traverseNode(slot, el, state, true)
fxy060608's avatar
fxy060608 已提交
191 192 193
  })
}

fxy060608's avatar
fxy060608 已提交
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
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)
  console.log(el)
  throw new Error('123')
}

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 已提交
236
module.exports = {
fxy060608's avatar
fxy060608 已提交
237
  C_IS,
fxy060608's avatar
fxy060608 已提交
238 239 240
  V_FOR,
  V_IF,
  V_ELSE_IF,
fxy060608's avatar
fxy060608 已提交
241
  ID,
fxy060608's avatar
fxy060608 已提交
242 243
  SET_DATA,
  GET_DATA,
fxy060608's avatar
fxy060608 已提交
244
  GET_CHANGE_DATA,
fxy060608's avatar
init v3  
fxy060608 已提交
245
  isVar,
fxy060608's avatar
fxy060608 已提交
246
  hasOwn,
fxy060608's avatar
init v3  
fxy060608 已提交
247
  addAttr,
fxy060608's avatar
fxy060608 已提交
248 249 250
  addRawAttr,
  removeRawAttr,
  removeRawBindingAttr,
fxy060608's avatar
fxy060608 已提交
251
  getNewId,
fxy060608's avatar
init v3  
fxy060608 已提交
252
  getForEl,
fxy060608's avatar
fxy060608 已提交
253
  addHandler,
fxy060608's avatar
init v3  
fxy060608 已提交
254 255
  processForKey,
  updateForEleId,
fxy060608's avatar
fxy060608 已提交
256
  updateScopedSlotEleId,
fxy060608's avatar
init v3  
fxy060608 已提交
257
  getBindingAttr,
fxy060608's avatar
fxy060608 已提交
258 259
  getAndRemoveAttr,
  traverseNode
fxy060608's avatar
init v3  
fxy060608 已提交
260
}