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

commit homework #3

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
69 changes: 60 additions & 9 deletions vdom/vnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,75 @@
{"tagName":"div","children":[{"tagName":"p","text":"1"}],"text":"2"}
*/

// 这里的currentInfo.currentNode 只用作测试用例返回值,其实在我的代码中currentNode代表的是顶层root节点
var currentInfo = {
currentNode: null,
currentParent: null
currentNode: null,
currentParent: null
}

// 新开标签入栈,主要用栈检查是否正确关闭。currentNode 代表真正的当期节点。currentParent 代表真正的当期父节点
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,
text,
elementEnd,
currentInfo
elementOpen,
text,
elementEnd,
currentInfo
};