Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第六周作业 #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { elementOpen, text, elementEnd, currentInfo } = require('../vdom/vnodeBack.js');
const { elementOpen, text, elementEnd, currentInfo } = require('../vdom/vnode.js');

describe('idom', () => {
test('校验idom结构', async () => {
Expand Down
34 changes: 31 additions & 3 deletions vdom/vnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,43 @@ var currentInfo = {
currentParent: null
}
function elementOpen(tagName) {
// TODO
// 节点开始,当前节点(currentNode)赋值为一个新对象
currentInfo.currentNode = { tagName }
// 在当前节点(currentNode)中,通过 parent 属性记录一下它的父节点,父节点若为空,则代表当前节点为根节点
currentInfo.currentNode.parent = currentInfo.currentParent
// 父节点若不为空,则需要在父节点的 children 属性中添加上当前节点
if (currentInfo.currentNode.parent !== null) {
if (!currentInfo.currentNode.parent.children) {
currentInfo.currentNode.parent.children = [currentInfo.currentNode]
} else {
currentInfo.currentNode.parent.children.push(currentInfo.currentNode)
}
}
// 将当前父节点置为当前节点,方便在遍历当前节点的子元素时使用
currentInfo.currentParent = currentInfo.currentNode
}

function text(textContent) {
// TODO
// 给当前节点加上 text 属性
if (currentInfo.currentNode) {
currentInfo.currentNode.text = textContent
}
}

function elementEnd(tagName) {
// TODO
// 当前节点结束
if (currentInfo.currentNode.tagName === tagName) {
// 将当前节点的父节点取出来
const parent = currentInfo.currentNode.parent
// 删除当前节点的 parent 属性
delete currentInfo.currentNode.parent
// 如果当前节点的父节点不为空,则把当前节点(currentNode)的回滚为其父节点
if (parent !== null) {
currentInfo.currentNode = parent
}
// 当前父节点置为当前节点或为null,方便在遍历当前节点的子元素时使用
currentInfo.currentParent = parent
}
}
module.exports = {
elementOpen,
Expand Down