Skip to content

Commit

Permalink
refactor(runtime): 注释节点不进行 setData,fix #9369
Browse files Browse the repository at this point in the history
Vue 会在使用 v-if 等指令时,会生成注释节点用于 diff,因此 Taro DOM 需要保留这些注释节点,在 hydrate 阶段进行去除。

本应该在 hydrate 阶段把“空文本节点”也去除掉,但 hydrate 阶段去除节点,在某些情况下(如空文本节点变为非空了)会影响其它兄弟节点的 _path,也就是 index 值。所以目前只对比较稳定的注释节点进行优化。
  • Loading branch information
Chen-jj committed Jun 15, 2021
1 parent 9db91c3 commit 4996978
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
6 changes: 6 additions & 0 deletions commitlint.config.js
@@ -0,0 +1,6 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'body-max-line-length': [0, 'always', Infinity]
}
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -31,7 +31,7 @@
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS --config commitlint.config.js",
"pre-commit": "lint-staged"
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/taro-runtime/src/constants/index.ts
Expand Up @@ -37,3 +37,4 @@ export const DATE = 'Date'
export const SET_TIMEOUT = 'setTimeout'
export const CATCHMOVE = 'catchMove'
export const CATCH_VIEW = 'catch-view'
export const COMMENT = 'comment'
7 changes: 5 additions & 2 deletions packages/taro-runtime/src/dom/document.ts
Expand Up @@ -7,7 +7,8 @@ import { eventSource } from '../dom/event-source'
import { ElementNames, InstanceFactory, InstanceNamedFactory } from '../interface'
import {
ROOT_STR,
DOCUMENT_ELEMENT_NAME
DOCUMENT_ELEMENT_NAME,
COMMENT
} from '../constants'

import type { FormElement } from '../dom/form'
Expand Down Expand Up @@ -71,6 +72,8 @@ export class TaroDocument extends TaroElement {

// @TODO: @PERF: 在 hydrate 移除掉空的 node
public createComment (): TaroText {
return this._getText('')
const textnode = this._getText('')
textnode.nodeName = COMMENT
return textnode
}
}
6 changes: 4 additions & 2 deletions packages/taro-runtime/src/dom/node.ts
Expand Up @@ -2,7 +2,7 @@ import { inject, injectable } from 'inversify'
import { Shortcuts, ensure } from '@tarojs/shared'
import SERVICE_IDENTIFIER from '../constants/identifiers'
import { NodeType } from './node_types'
import { incrementId } from '../utils'
import { incrementId, isComment } from '../utils'
import { TaroEventTarget } from './event-target'
import { hydrate } from '../hydrate'
import { eventSource } from './event-source'
Expand Down Expand Up @@ -75,7 +75,9 @@ export class TaroNode extends TaroEventTarget {
const parentNode = this.parentNode

if (parentNode) {
const indexOfNode = parentNode.findIndex(this)
// 计算路径时,先过滤掉 comment 节点
const list = parentNode.childNodes.filter(node => !isComment(node))
const indexOfNode = list.indexOf(this)
const index = this.hooks.getPathIndex(indexOfNode)

return `${parentNode._path}.${Shortcuts.Childnodes}.${index}`
Expand Down
9 changes: 7 additions & 2 deletions packages/taro-runtime/src/hydrate.ts
@@ -1,5 +1,5 @@
import { Shortcuts, toCamelCase } from '@tarojs/shared'
import { isText, isHasExtractProp } from './utils'
import { isText, isHasExtractProp, isComment } from './utils'
import {
SPECIAL_NODES,
VIEW,
Expand Down Expand Up @@ -35,7 +35,7 @@ export function hydrate (node: TaroElement | TaroText): MiniData {
[Shortcuts.NodeName]: nodeName,
uid: node.uid
}
const { props, childNodes } = node
const { props } = node

if (!node.isAnyEventBinded() && SPECIAL_NODES.indexOf(nodeName) > -1) {
data[Shortcuts.NodeName] = `static-${nodeName}`
Expand All @@ -60,6 +60,11 @@ export function hydrate (node: TaroElement | TaroText): MiniData {
}
}

let { childNodes } = node

// 过滤 comment 节点
childNodes = childNodes.filter(node => !isComment(node))

if (childNodes.length > 0) {
data[Shortcuts.Childnodes] = childNodes.map(hydrate)
} else {
Expand Down
7 changes: 6 additions & 1 deletion packages/taro-runtime/src/utils/index.ts
Expand Up @@ -5,7 +5,8 @@ import {
STYLE,
ID,
UID,
CLASS
CLASS,
COMMENT
} from '../constants'

import type { TaroElement } from '../dom/element'
Expand All @@ -25,6 +26,10 @@ export function isText (node: TaroNode): node is TaroText {
return node.nodeType === NodeType.TEXT_NODE
}

export function isComment (node: TaroNode): boolean {
return node.nodeName === COMMENT
}

export function isHasExtractProp (el: TaroElement): boolean {
const res = Object.keys(el.props).find(prop => {
return !(/^(class|style|id)$/.test(prop) || prop.startsWith('data-'))
Expand Down

0 comments on commit 4996978

Please sign in to comment.