Blame view

node_modules/vue/src/server/optimizing-compiler/modules.js 3.04 KB
aaac7fed   liuqimichale   add
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
125
126
  /* @flow */
  
  import {
    RAW,
    // INTERPOLATION,
    EXPRESSION
  } from './codegen'
  
  import {
    propsToAttrMap,
    isRenderableAttr
  } from 'web/server/util'
  
  import {
    isBooleanAttr,
    isEnumeratedAttr
  } from 'web/util/attrs'
  
  import type { StringSegment } from './codegen'
  import type { CodegenState } from 'compiler/codegen/index'
  
  type Attr = { name: string; value: string };
  
  const plainStringRE = /^"(?:[^"\\]|\\.)*"$|^'(?:[^'\\]|\\.)*'$/
  
  // let the model AST transform translate v-model into appropriate
  // props bindings
  export function applyModelTransform (el: ASTElement, state: CodegenState) {
    if (el.directives) {
      for (let i = 0; i < el.directives.length; i++) {
        const dir = el.directives[i]
        if (dir.name === 'model') {
          state.directives.model(el, dir, state.warn)
          // remove value for textarea as its converted to text
          if (el.tag === 'textarea' && el.props) {
            el.props = el.props.filter(p => p.name !== 'value')
          }
          break
        }
      }
    }
  }
  
  export function genAttrSegments (
    attrs: Array<Attr>
  ): Array<StringSegment> {
    return attrs.map(({ name, value }) => genAttrSegment(name, value))
  }
  
  export function genDOMPropSegments (
    props: Array<Attr>,
    attrs: ?Array<Attr>
  ): Array<StringSegment> {
    const segments = []
    props.forEach(({ name, value }) => {
      name = propsToAttrMap[name] || name.toLowerCase()
      if (isRenderableAttr(name) &&
        !(attrs && attrs.some(a => a.name === name))
      ) {
        segments.push(genAttrSegment(name, value))
      }
    })
    return segments
  }
  
  function genAttrSegment (name: string, value: string): StringSegment {
    if (plainStringRE.test(value)) {
      // force double quote
      value = value.replace(/^'|'$/g, '"')
      // force enumerated attr to "true"
      if (isEnumeratedAttr(name) && value !== `"false"`) {
        value = `"true"`
      }
      return {
        type: RAW,
        value: isBooleanAttr(name)
          ? ` ${name}="${name}"`
          : value === '""'
            ? ` ${name}`
            : ` ${name}="${JSON.parse(value)}"`
      }
    } else {
      return {
        type: EXPRESSION,
        value: `_ssrAttr(${JSON.stringify(name)},${value})`
      }
    }
  }
  
  export function genClassSegments (
    staticClass: ?string,
    classBinding: ?string
  ): Array<StringSegment> {
    if (staticClass && !classBinding) {
      return [{ type: RAW, value: ` class=${staticClass}` }]
    } else {
      return [{
        type: EXPRESSION,
        value: `_ssrClass(${staticClass || 'null'},${classBinding || 'null'})`
      }]
    }
  }
  
  export function genStyleSegments (
    staticStyle: ?string,
    parsedStaticStyle: ?string,
    styleBinding: ?string,
    vShowExpression: ?string
  ): Array<StringSegment> {
    if (staticStyle && !styleBinding && !vShowExpression) {
      return [{ type: RAW, value: ` style=${JSON.stringify(staticStyle)}` }]
    } else {
      return [{
        type: EXPRESSION,
        value: `_ssrStyle(${
          parsedStaticStyle || 'null'
        },${
          styleBinding || 'null'
        }, ${
          vShowExpression
            ? `{ display: (${vShowExpression}) ? '' : 'none' }`
            : 'null'
        })`
      }]
    }
  }