@dolphinweex/weex-harmony 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dolphinweex/weex-harmony",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "weex harmony adapter",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -67,53 +67,60 @@ function hasTargetElement(tagName) {
67
67
  * 遍历html节点,将需要引入的组件添加到componentSet中
68
68
  * @param {*} node html节点
69
69
  */
70
- function traverseNode(node) {
70
+ function traverseNode(node, parent = null) {
71
71
  if (node.type === 'element') {
72
- const {hasElement, componentName} = hasTargetElement(node.tagName)
72
+ const { hasElement, componentName } = hasTargetElement(node.tagName);
73
73
  if (hasElement) {
74
- componentSet.add(componentName)
74
+ componentSet.add(componentName);
75
75
  }
76
- if (node.attributes) {
77
- node.attributes.forEach((attr, index) => {
78
- if (attr.key === 'v-bind') {
79
- //对象的话
80
- if (attr.value.trim().startsWith('{') && attr.value.trim().endsWith('}')) {
81
- const parsedAst = parser.parseExpression(attr.value);
82
- const styleProperty = parsedAst.properties.find(prop => prop.key && prop.key.name === 'style');
83
- if (styleProperty) {
84
- const styleValue = styleProperty.value;
85
76
 
86
- // style 的值套上 _px2rem 方法
87
- styleProperty.value = {
88
- type: 'CallExpression',
89
- callee: {
90
- type: 'Identifier',
91
- name: '_px2rem'
92
- },
93
- arguments: [styleValue] // 将原始的 style 对象作为参数传递给 _px2rem
77
+ // 判断父节点是否有 v-for,若父节点是 <template> 且有 v-for,则跳过
78
+ const isInsideVFor = parent && parent.attributes && parent.attributes.some(attr => attr.key === 'v-for');
79
+
80
+ if (!isInsideVFor) { // 如果不在 v-for 中,才处理 v-bind
81
+ if (node.attributes) {
82
+ node.attributes.forEach((attr, index) => {
83
+ if (attr.key === 'v-bind') {
84
+ // 如果是对象绑定,处理 style 属性
85
+ if (attr.value.trim().startsWith('{') && attr.value.trim().endsWith('}')) {
86
+ const parsedAst = parser.parseExpression(attr.value);
87
+ const styleProperty = parsedAst.properties.find(prop => prop.key && prop.key.name === 'style');
88
+ if (styleProperty) {
89
+ const styleValue = styleProperty.value;
90
+
91
+ // style 的值套上 _px2rem 方法
92
+ styleProperty.value = {
93
+ type: 'CallExpression',
94
+ callee: {
95
+ type: 'Identifier',
96
+ name: '_px2rem'
97
+ },
98
+ arguments: [styleValue] // 将原始的 style 对象作为参数传递给 _px2rem
99
+ };
100
+ }
101
+ node.attributes[index] = {
102
+ key: 'v-bind',
103
+ value: generator(parsedAst).code
94
104
  };
105
+ } else {
106
+ const customComputedName = `customVBindComputed${index}`; // 自定义的名字
107
+ vBindMap.set(customComputedName, {
108
+ computedName: attr.value,
109
+ customComputedName
110
+ });
111
+ // 更新属性值为自定义名字
112
+ node.attributes[index].value = customComputedName;
95
113
  }
96
- node.attributes[index] = {
97
- key: 'v-bind',
98
- value: generator(parsedAst).code
99
- };
100
- } else {
101
- const customComputedName = `customVBindComputed${index}`; // 自定义的名字
102
- vBindMap.set(customComputedName, {
103
- computedName: attr.value,
104
- customComputedName
105
- });
106
-
107
- // 更新属性值为自定义名字
108
- node.attributes[index].value = customComputedName;
109
114
  }
110
- }
111
- });
115
+ });
116
+ }
112
117
  }
118
+
119
+ // 继续递归处理子节点
113
120
  if (node.children) {
114
121
  node.children.forEach((child) => {
115
- traverseNode(child)
116
- })
122
+ traverseNode(child, node); // 将当前节点作为 parent 传递给子节点
123
+ });
117
124
  }
118
125
  }
119
126
  }