vue3-yuanma/packages/runtime-test/src/nodeOps.ts

237 lines
4.7 KiB
TypeScript
Raw Normal View History

import { markNonReactive } from '@vue/reactivity'
2018-10-02 01:15:07 +08:00
export const enum NodeTypes {
TEXT = 'text',
2019-06-20 21:28:37 +08:00
ELEMENT = 'element',
COMMENT = 'comment'
2018-10-02 01:15:07 +08:00
}
2019-08-22 05:05:14 +08:00
export const enum NodeOpTypes {
CREATE = 'create',
INSERT = 'insert',
REMOVE = 'remove',
SET_TEXT = 'setText',
SET_ELEMENT_TEXT = 'setElementText',
PATCH = 'patch'
}
2018-10-02 01:15:07 +08:00
export interface TestElement {
id: number
type: NodeTypes.ELEMENT
parentNode: TestElement | null
tag: string
children: TestNode[]
props: Record<string, any>
2018-10-29 05:43:27 +08:00
eventListeners: Record<string, Function | Function[]> | null
2018-10-02 01:15:07 +08:00
}
export interface TestText {
id: number
type: NodeTypes.TEXT
parentNode: TestElement | null
text: string
}
2019-06-20 21:28:37 +08:00
export interface TestComment {
id: number
type: NodeTypes.COMMENT
parentNode: TestElement | null
text: string
}
export type TestNode = TestElement | TestText | TestComment
2018-10-02 01:15:07 +08:00
export interface NodeOp {
type: NodeOpTypes
2018-10-02 01:15:07 +08:00
nodeType?: NodeTypes
tag?: string
text?: string
targetNode?: TestNode
parentNode?: TestElement
2019-06-20 21:28:37 +08:00
refNode?: TestNode | null
propKey?: string
propPrevValue?: any
propNextValue?: any
2018-10-02 01:15:07 +08:00
}
let nodeId: number = 0
let recordedNodeOps: NodeOp[] = []
2018-10-02 01:15:07 +08:00
export function logNodeOp(op: NodeOp) {
recordedNodeOps.push(op)
2018-10-02 01:15:07 +08:00
}
export function resetOps() {
recordedNodeOps = []
2018-10-02 01:15:07 +08:00
}
export function dumpOps(): NodeOp[] {
const ops = recordedNodeOps.slice()
resetOps()
return ops
2018-10-02 01:15:07 +08:00
}
function createElement(tag: string): TestElement {
const node: TestElement = {
id: nodeId++,
type: NodeTypes.ELEMENT,
tag,
children: [],
props: {},
2018-10-29 05:43:27 +08:00
parentNode: null,
eventListeners: null
2018-10-02 01:15:07 +08:00
}
logNodeOp({
type: NodeOpTypes.CREATE,
2018-10-02 01:15:07 +08:00
nodeType: NodeTypes.ELEMENT,
targetNode: node,
tag
})
// avoid test nodes from being observed
markNonReactive(node)
2018-10-02 01:15:07 +08:00
return node
}
function createText(text: string): TestText {
const node: TestText = {
id: nodeId++,
type: NodeTypes.TEXT,
text,
parentNode: null
}
logNodeOp({
type: NodeOpTypes.CREATE,
2018-10-02 01:15:07 +08:00
nodeType: NodeTypes.TEXT,
targetNode: node,
text
})
// avoid test nodes from being observed
markNonReactive(node)
2018-10-02 01:15:07 +08:00
return node
}
2019-06-20 21:28:37 +08:00
function createComment(text: string): TestComment {
const node: TestComment = {
id: nodeId++,
type: NodeTypes.COMMENT,
text,
parentNode: null
}
logNodeOp({
2019-06-20 21:28:37 +08:00
type: NodeOpTypes.CREATE,
nodeType: NodeTypes.COMMENT,
2018-10-02 01:15:07 +08:00
targetNode: node,
text
})
// avoid test nodes from being observed
markNonReactive(node)
2019-06-20 21:28:37 +08:00
return node
2018-10-02 01:15:07 +08:00
}
2019-06-20 21:28:37 +08:00
function setText(node: TestText, text: string) {
logNodeOp({
2019-06-20 21:28:37 +08:00
type: NodeOpTypes.SET_TEXT,
targetNode: node,
text
2018-10-02 01:15:07 +08:00
})
2019-06-20 21:28:37 +08:00
node.text = text
2018-10-02 01:15:07 +08:00
}
2019-06-20 21:28:37 +08:00
function insert(child: TestNode, parent: TestElement, ref?: TestNode | null) {
let refIndex
if (ref != null) {
refIndex = parent.children.indexOf(ref)
if (refIndex === -1) {
console.error('ref: ', ref)
console.error('parent: ', parent)
throw new Error('ref is not a child of parent')
}
2018-10-02 01:15:07 +08:00
}
logNodeOp({
type: NodeOpTypes.INSERT,
2018-10-02 01:15:07 +08:00
targetNode: child,
parentNode: parent,
refNode: ref
})
2019-08-24 03:27:17 +08:00
// remove the node first, but don't log it as a REMOVE op
remove(child, false)
2019-06-20 21:28:37 +08:00
if (refIndex === undefined) {
parent.children.push(child)
child.parentNode = parent
} else {
parent.children.splice(refIndex, 0, child)
child.parentNode = parent
}
2018-10-02 01:15:07 +08:00
}
2019-08-24 03:27:17 +08:00
function remove(child: TestNode, logOp: boolean = true) {
2019-06-20 21:28:37 +08:00
const parent = child.parentNode
if (parent != null) {
2019-08-24 03:27:17 +08:00
if (logOp) {
logNodeOp({
type: NodeOpTypes.REMOVE,
targetNode: child,
parentNode: parent
})
}
2019-06-20 21:28:37 +08:00
const i = parent.children.indexOf(child)
if (i > -1) {
parent.children.splice(i, 1)
} else {
console.error('target: ', child)
console.error('parent: ', parent)
throw Error('target is not a childNode of parent')
}
child.parentNode = null
2018-10-02 01:15:07 +08:00
}
}
2019-06-20 21:28:37 +08:00
function setElementText(el: TestElement, text: string) {
logNodeOp({
2019-06-20 21:28:37 +08:00
type: NodeOpTypes.SET_ELEMENT_TEXT,
targetNode: el,
text
2018-10-02 01:15:07 +08:00
})
2019-06-20 21:28:37 +08:00
el.children.forEach(c => {
c.parentNode = null
})
2019-08-22 05:05:14 +08:00
el.children = [
{
id: nodeId++,
type: NodeTypes.TEXT,
text,
parentNode: el
}
]
2018-10-02 01:15:07 +08:00
}
function parentNode(node: TestNode): TestElement | null {
return node.parentNode
}
function nextSibling(node: TestNode): TestNode | null {
const parent = node.parentNode
if (!parent) {
return null
}
const i = parent.children.indexOf(node)
return parent.children[i + 1] || null
}
function querySelector() {
throw new Error('querySelector not supported in test renderer.')
}
export const nodeOps = {
2019-06-20 21:28:37 +08:00
insert,
remove,
2018-10-02 01:15:07 +08:00
createElement,
createText,
2019-06-20 21:28:37 +08:00
createComment,
2018-10-02 01:15:07 +08:00
setText,
2019-06-20 21:28:37 +08:00
setElementText,
2018-10-02 01:15:07 +08:00
parentNode,
nextSibling,
querySelector
}