保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 26a09f08cf1ed43c640879f23fdad56c5c9282f7
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
/* @flow */
 
import { addIfCondition } from 'compiler/parser/index'
import { getAndRemoveAttr, addRawAttr } from 'compiler/helpers'
 
function hasConditionDirective (el: ASTElement): boolean {
  for (const attr in el.attrsMap) {
    if (/^v\-if|v\-else|v\-else\-if$/.test(attr)) {
      return true
    }
  }
  return false
}
 
function getPreviousConditions (el: ASTElement): Array<string> {
  const conditions = []
  if (el.parent && el.parent.children) {
    for (let c = 0, n = el.parent.children.length; c < n; ++c) {
      // $flow-disable-line
      const ifConditions = el.parent.children[c].ifConditions
      if (ifConditions) {
        for (let i = 0, l = ifConditions.length; i < l; ++i) {
          const condition = ifConditions[i]
          if (condition && condition.exp) {
            conditions.push(condition.exp)
          }
        }
      }
    }
  }
  return conditions
}
 
export function preTransformVIf (el: ASTElement, options: WeexCompilerOptions) {
  if (hasConditionDirective(el)) {
    let exp
    const ifExp = getAndRemoveAttr(el, 'v-if', true /* remove from attrsMap */)
    const elseifExp = getAndRemoveAttr(el, 'v-else-if', true)
    // don't need the value, but remove it to avoid being generated as a
    // custom directive
    getAndRemoveAttr(el, 'v-else', true)
    if (ifExp) {
      exp = ifExp
      addIfCondition(el, { exp: ifExp, block: el })
    } else {
      elseifExp && addIfCondition(el, { exp: elseifExp, block: el })
      const prevConditions = getPreviousConditions(el)
      if (prevConditions.length) {
        const prevMatch = prevConditions.join(' || ')
        exp = elseifExp
          ? `!(${prevMatch}) && (${elseifExp})` // v-else-if
          : `!(${prevMatch})` // v-else
      } else if (process.env.NODE_ENV !== 'production' && options.warn) {
        options.warn(
          `v-${elseifExp ? ('else-if="' + elseifExp + '"') : 'else'} ` +
          `used on element <${el.tag}> without corresponding v-if.`
        )
        return
      }
    }
    addRawAttr(el, '[[match]]', exp)
  }
}