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

提交作业homework11-vdom #17

Open
wants to merge 2 commits 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
49 changes: 49 additions & 0 deletions vdom/vnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,65 @@ var currentInfo = {
currentNode: null,
currentParent: null
}
let stack = [];
let currentNode = null;
let currentParent = null;

function elementOpen(tagName) {
// TODO
let element = {
'tagName': tagName
}
if (stack.length === 0) {
currentInfo.currentNode = element;
currentNode = null;
}

currentParent = currentNode;
currentNode = element;

if (currentParent) {
!currentParent.children ? currentParent.children = [element] : currentParent.children.push(element);
}

stack.push(tagName);
}

function text(textContent) {
// TODO
currentNode.text = textContent;
}

function elementEnd(tagName) {
// TODO
if (stack[stack.length - 1] !== tagName) {
console.log('标签闭合错误');
return;
}

stack.pop();

currentNode = currentParent;

if (currentInfo.currentNode) {
// 当期操作节点已经到顶层了,不需要计算父亲节点
if (currentInfo.currentNode === currentNode) {
return;
}

// 计算父亲节点
let node = currentInfo.currentNode;
function findNode(curNode) {
for(let i = 0; i< curNode.children; i++) {
if(currentNode === curNode.children[i]) {
currentParent = curNode;
} else {
findNode(curNode.children[i])
}
}
}
findNode(node);
}
}
module.exports = {
elementOpen,
Expand Down