mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
const {
  ID,
  C_IS,
  C_REF,
  C_NAME,
  V_IF,
  V_FOR,
  V_ELSE_IF,
  isVar
} = require('../util')
 
const parseTextExpr = require('./text-parser')
 
function parseRef (el, genVar) {
  if (el.ref && isVar(el.ref)) {
    el.ref = genVar(C_REF, el.ref)
  }
}
 
function parseSlotName (el, genVar) {
  if (el.slotName && isVar(el.slotName)) {
    el.slotName = genVar(C_NAME, el.slotName)
  }
}
 
function parseIs (el, genVar) {
  if (!el.component) {
    return
  }
  if (isVar(el.component)) {
    el.component = genVar(C_IS, el.component)
  }
}
 
function isProcessed (exp) {
  return String(exp).indexOf('_$') === 0
}
// 当根节点是由if,elseif,else组成,会调用多次parseIf来解析root
function parseIf (el, createGenVar, isScopedSlot) {
  if (!el.if) {
    return
  }
  if (el.slotTarget && el.tag === 'template') { // new v-slot
    isScopedSlot = false
  }
  el.ifConditions.forEach(con => {
    if (!isProcessed(con.exp) && isVar(con.exp)) {
      con.exp = createGenVar(con.block.attrsMap[ID], isScopedSlot)(con.block.elseif ? V_ELSE_IF : V_IF, con.exp)
    }
  })
  if (!isProcessed(el.if)) {
    el.if = createGenVar(el.attrsMap[ID], isScopedSlot)(V_IF, el.if)
  }
}
 
function parseFor (el, createGenVar, isScopedSlot, fill = false) {
  if (el.for && isVar(el.for)) {
    el.for = createGenVar(el.forId, isScopedSlot)(
      V_FOR,
      fill
        ? `{forItems:${el.for},fill:true}`
        : `{forItems:${el.for}}`
    )
    return true
  }
}
 
function parseBinding (el, genVar) {
  el.staticClass && (el.staticClass = genVar('sc', el.staticClass))
  el.classBinding && (el.classBinding = genVar('c', el.classBinding))
  el.styleBinding && (el.styleBinding = genVar('s', el.styleBinding))
}
 
function parseDirs (el, genVar, ignoreDirs = []) {
  el.directives && el.directives.forEach(dir => {
    if (ignoreDirs.indexOf(dir.name) === -1) {
      dir.value && (dir.value = genVar('v-' + dir.name, dir.value))
      dir.isDynamicArg && (dir.arg = genVar('v-' + dir.name + '-arg', dir.arg))
    }
  })
}
 
function parseAttrs (el, genVar) {
  el.attrs && el.attrs.forEach(attr => {
    const {
      name,
      value
    } = attr
    if (
      name !== ID &&
      // name.indexOf('data-') !== 0 && // TODO dataset 保留
      name.indexOf('change:') !== 0 && // wxs change:prop
      isVar(value) &&
      value.indexOf('_$') !== 0 // 已被提前处理过了,如 wxs prop:_$gc(2,'change:prop')
    ) {
      attr.value = genVar('a-' + name, value)
    }
  })
}
 
function parseProps (el, genVar) {
  el.props && el.props.forEach(prop => {
    isVar(prop.value) && (prop.value = genVar('a-' + prop.name, prop.value))
  })
}
 
function parseText (el, parent, state) {
  // fixed by xxxxxx 注意:保持平台一致性,trim 一下
  el.parent && (el.parent = parent)
  el.expression = parseTextExpr(el.text.trim(), false, state).expression
}
 
module.exports = {
  parseIs,
  parseRef,
  parseSlotName,
  parseIf,
  parseFor,
  parseText,
  parseDirs,
  parseAttrs,
  parseProps,
  parseBinding
}