util.js 7.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
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
fxy060608 已提交
16 17
const C_SLOT_TARGET = 'st'
const C_REF = 'ref'
fxy060608's avatar
init v3  
fxy060608 已提交
18

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

Q
qiang 已提交
23 24 25
// web components
const elements = ['uni-view']

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

fxy060608's avatar
fxy060608 已提交
42
function addRawAttr (el, name, value) {
fxy060608's avatar
init v3  
fxy060608 已提交
43 44 45 46 47 48 49
  el.attrsMap[name] = value
  el.attrsList.push({
    name,
    value
  })
}

fxy060608's avatar
fxy060608 已提交
50
function updateEleId (el, it, state) {
fxy060608's avatar
init v3  
fxy060608 已提交
51 52 53
  if (el.type !== 1) {
    return
  }
fxy060608's avatar
fxy060608 已提交
54
  const newId = getNewId(el.attrsMap[ID], it)
fxy060608's avatar
fxy060608 已提交
55
  addRawAttr(el, ID, newId)
fxy060608's avatar
fxy060608 已提交
56 57 58 59
  if (el.attrs) {
    const attr = el.attrs.find(attr => attr.name === ID)
    attr.value = newId
  }
fxy060608's avatar
init v3  
fxy060608 已提交
60
  el.children.forEach(child => {
fxy060608's avatar
fxy060608 已提交
61 62 63 64 65 66
    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 已提交
67
  })
fxy060608's avatar
fxy060608 已提交
68 69 70
  el.ifConditions && el.ifConditions.forEach((con, index) => {
    index !== 0 && updateEleId(con.block, it, state)
  })
71 72 73
  el.scopedSlots && Object.values(el.scopedSlots).forEach((slot, index) => {
    updateEleId(slot, it, state)
  })
fxy060608's avatar
init v3  
fxy060608 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

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 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
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)
115
  if (el.for) {
116
    const it = el.$parentIterator3 ? (el.$parentIterator3 + '+' + "'-'" + '+' + el.iterator3) : el.iterator3
fxy060608's avatar
fxy060608 已提交
117
    updateEleId(el, it, state)
fxy060608's avatar
init v3  
fxy060608 已提交
118 119 120
  }
}

fxy060608's avatar
fxy060608 已提交
121 122 123 124 125 126 127
function getNewId (id, it) {
  return Number.isInteger(id) ? `("${id}-"+${it})` : `(${id}+${it})`
}

function updateScopedSlotEleId (el, state) {
  // TODO 暂不考虑 scopedSlot 嵌套情况
  if (el.slotScope) {
128 129 130
    const getNewId = function (id, it) {
      return Number.isInteger(id) ? `("${id}-"+${it})` : `(${id}+"-"+${it})`
    }
fxy060608's avatar
fxy060608 已提交
131 132 133 134 135 136
    const updateEleId = function (el) {
      if (el.type !== 1) {
        return
      }
      const it = '_si'
      const newId = getNewId(el.attrsMap[ID], it)
fxy060608's avatar
fxy060608 已提交
137 138 139
      if (el.forId) {
        el.forId = getNewId(el.forId, it)
      }
fxy060608's avatar
fxy060608 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
      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 已提交
163 164 165 166
function getForEl (el) {
  if (el.for) {
    return el
  }
fxy060608's avatar
fxy060608 已提交
167
  if (el.parent && el.parent.for && (el.parent.tag === 'template' || el.parent.tag === 'block')) {
fxy060608's avatar
init v3  
fxy060608 已提交
168 169 170 171 172 173
    return el.parent
  }
}

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

fxy060608's avatar
fxy060608 已提交
202 203 204 205
function hasOwn (obj, key) {
  return hasOwnProperty.call(obj, key)
}

fxy060608's avatar
fxy060608 已提交
206 207
function traverseNode (el, parent, state, isScopedSlot) {
  state.transformNode(el, parent, state, isScopedSlot)
208 209 210 211
  el.children && el.children.forEach((child, index) => {
    state.childIndex = index
    traverseNode(child, el, state, isScopedSlot)
  })
fxy060608's avatar
fxy060608 已提交
212
  el.ifConditions && el.ifConditions.forEach((con, index) => {
213 214 215 216
    if (index !== 0) {
      state.childIndex = index
      traverseNode(con.block, el, state, isScopedSlot)
    }
fxy060608's avatar
fxy060608 已提交
217
  })
218 219
  el.scopedSlots && Object.values(el.scopedSlots).forEach((slot, index) => {
    state.childIndex = index
fxy060608's avatar
fxy060608 已提交
220
    slot.slotScope = `${slot.slotScope}, _svm, _si`
fxy060608's avatar
fxy060608 已提交
221
    if (slot.slotTargetDynamic && slot.slotTarget) {
fxy060608's avatar
fxy060608 已提交
222
      slot.slotTarget = state.createGenVar(slot.attrsMap[ID])(C_SLOT_TARGET, slot.slotTarget)
fxy060608's avatar
fxy060608 已提交
223
    }
fxy060608's avatar
fxy060608 已提交
224
    traverseNode(slot, el, state, true)
fxy060608's avatar
fxy060608 已提交
225 226 227
  })
}

fxy060608's avatar
fxy060608 已提交
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
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 已提交
268
module.exports = {
fxy060608's avatar
fxy060608 已提交
269
  C_IS,
fxy060608's avatar
fxy060608 已提交
270
  C_REF,
fxy060608's avatar
fxy060608 已提交
271 272 273
  V_FOR,
  V_IF,
  V_ELSE_IF,
fxy060608's avatar
fxy060608 已提交
274
  ID,
fxy060608's avatar
fxy060608 已提交
275
  SET_DATA,
fxy060608's avatar
fxy060608 已提交
276
  GET_DATA,
fxy060608's avatar
fxy060608 已提交
277
  SET_MP_CLASS,
fxy060608's avatar
fxy060608 已提交
278
  GET_CHANGE_DATA,
Q
qiang 已提交
279
  elements,
fxy060608's avatar
init v3  
fxy060608 已提交
280
  isVar,
fxy060608's avatar
fxy060608 已提交
281
  hasOwn,
fxy060608's avatar
init v3  
fxy060608 已提交
282
  addAttr,
fxy060608's avatar
fxy060608 已提交
283 284 285
  addRawAttr,
  removeRawAttr,
  removeRawBindingAttr,
fxy060608's avatar
fxy060608 已提交
286
  getNewId,
fxy060608's avatar
init v3  
fxy060608 已提交
287
  getForEl,
fxy060608's avatar
fxy060608 已提交
288
  addHandler,
fxy060608's avatar
init v3  
fxy060608 已提交
289 290
  processForKey,
  updateForEleId,
fxy060608's avatar
fxy060608 已提交
291
  updateScopedSlotEleId,
fxy060608's avatar
init v3  
fxy060608 已提交
292
  getBindingAttr,
fxy060608's avatar
fxy060608 已提交
293 294
  getAndRemoveAttr,
  traverseNode
Q
qiang 已提交
295
}