'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const {
  module: autoComponentsModule,
  compileTemplate
} = require('@dcloudio/uni-template-compiler/lib/auto-components')
 
const {
  isUnaryTag
} = require('@dcloudio/uni-template-compiler/lib/util')
 
const TAGS = [
  'text',
  'image',
  'input',
  'textarea',
  'video',
  'web-view',
  // 'switch',
  'slider'
]
 
function isTextElement (node) {
  return node.type === 1 /* NodeTypes.ELEMENT */ && node.tag === 'text'
}
 
/*
  1. 转换 \\n 为 \n
  2. u-text 下只能有一个文本节点(不支持 children),需要移除子组件并合并文本
*/
function parseText (el) {
  const children = el.children
  if (children.length > 1) {
    const expression = []
    children.forEach(child => {
      if (child.type === 3) {
        expression.push(`" ${child.text}"`)
      } else if (child.type === 2) {
        expression.push(child.expression.replace(/\\n/g, ''))
      }
    })
    el.children = [{ type: 2, expression: expression.join('+'), tokens: [] }]
  } else if (children.length === 1 && children[0].text) {
    children[0].text = children[0].text.replace(/\n/g, '').replace(/\\n/g, '\n')
  }
}
 
const modules = [{
  // render-whole => append="tree"
  preTransformNode (el, options) {
    if (!Object.hasOwnProperty.call(el.attrsMap, 'append')) {
      const name = 'render-whole'
      const value = el.attrsMap[name]
      if (value === true || value === 'true') {
        // remove
        delete el.attrsMap.append
        const index = el.attrsList.findIndex(item => item.name === name)
        const attr = el.attrsList[index]
        el.attrsList.splice(index, 1)
 
        el.appendAsTree = true
        el.attrsMap.append = 'tree'
        el.attrsList.push({
          name: 'append',
          value: 'tree',
          bool: false,
          start: attr.start,
          end: attr.end
        })
      }
    }
  }
}]
 
const deprecated = {
  events: {
    tap: 'click'
  }
}
 
if (process.env.UNI_USING_NVUE_COMPILER) {
  const wrapperTextTag = function (el, options) {
    const tag = el.tag
    if (isTextElement(el)) {
      parseText(el)
    }
    if (tag === 'text' || tag === 'u-text' || tag === 'button') {
      return
    }
    const children = el.children
    children.forEach((child, index) => {
      if (child.text) {
        children.splice(index, 1, {
          type: 1,
          tag: 'u-text',
          attrsList: [],
          attrsMap: {},
          rawAttrsMap: {},
          parent: el,
          children: [child],
          plain: true
        })
      }
    })
  }
 
  modules.push({
    postTransformNode (el, options) {
      wrapperTextTag(el, options)
 
      if (TAGS.includes(el.tag)) {
        el.tag = 'u-' + el.tag
      }
      if (el.events) {
        const {
          events: eventsMap
        } = deprecated
        Object.keys(el.events).forEach(name => {
          // 过时事件类型转换
          if (eventsMap[name]) {
            if (!(name === 'tap' && el.tag === 'map')) { // map 的 tap 事件不做转换
              el.events[eventsMap[name]] = el.events[name]
              delete el.events[name]
              name = eventsMap[name]
            }
          }
        })
      }
      if (el.tag === 'u-video') {
        if (
          Array.isArray(el.children) &&
          el.children.length &&
          el.children[0].tag !== 'u-scalable'
        ) {
          el.children = [{
            type: 1,
            tag: 'u-scalable',
            attrsList: [],
            attrsMap: {
              style: 'position: absolute;left: 0;right: 0;top: 0;bottom: 0;'
            },
            rawAttrsMap: {
              style: {
                name: 'style',
                value: 'position: absolute;left: 0;right: 0;top: 0;bottom: 0;'
              }
            },
            plain: false,
            staticStyle: '{position:"absolute",left:"0",right:"0",top:"0",bottom:"0"}',
            children: el.children
          }]
        }
      }
    }
  })
}
 
const compiler = require('weex-template-compiler')
const oldCompile = compiler.compile
compiler.compile = function (source, options = {}) {
  (options.modules || (options.modules = [])).push(autoComponentsModule)
 
  options.modules.push(require('@dcloudio/uni-template-compiler/lib/asset-url'))
  options.modules.push(require('@dcloudio/uni-template-compiler/lib/bool-attr'))
 
  options.isUnaryTag = isUnaryTag
  // 将 autoComponents 挂在 isUnaryTag 上边
  options.isUnaryTag.autoComponents = new Set()
  options.preserveWhitespace = false
  return compileTemplate(source, options, oldCompile)
}
 
module.exports = {
  isAppNVue: true,
  compiler,
  compilerOptions: {
    modules
  }
}