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'
18
const C_NAME = 'name'
fxy060608's avatar
init v3  
fxy060608 已提交
19

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

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

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

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

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

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

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

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

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

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

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

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