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 #18

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
18 changes: 17 additions & 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 All @@ -13,6 +13,22 @@ describe('idom', () => {
})
})

describe('idom', () => {
test('校验多层子节点的idom结构', async () => {
elementOpen('div')
elementOpen('p')
text('1')
elementEnd('p')
elementOpen('p')
text('1')
elementEnd('p')
text('2')
elementEnd('div')
var currentNode = currentInfo.currentNode
expect(JSON.stringify(currentNode)).toBe('{"tagName":"div","children":[{"tagName":"p","text":"1"},{"tagName":"p","text":"1"}],"text":"2"}')
})
})

describe('idom2', () => {
test('重复调用的时候,生成结果独立互不影响', async () => {
elementOpen('div')
Expand Down
43 changes: 32 additions & 11 deletions vdom/vnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,44 @@
*/

var currentInfo = {
currentNode: null,
currentParent: null
}
currentNode: null,
currentParent: [],
};

function elementOpen(tagName) {
// TODO
currentInfo.currentNode = null;
currentInfo.currentParent.push({
tagName,
});
}

function text(textContent) {
// TODO
currentInfo.currentParent[currentInfo.currentParent.length - 1].text =
textContent;
}

function elementEnd(tagName) {
// TODO
const { currentParent } = currentInfo;
let tempNode = currentParent.pop();
let length = currentInfo.currentParent.length
let nextParent = currentParent[length - 1]
if (tempNode.tagName === tagName) {
if(length) {
nextParent.children = nextParent.children || []
nextParent.children.push(
tempNode
);
} else {
currentInfo.currentNode = tempNode;
}
} else {
// TODO
}
}

module.exports = {
elementOpen,
text,
elementEnd,
currentInfo
};
elementOpen,
text,
elementEnd,
currentInfo,
};