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

feat: homework #1

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
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
node_modules
package-lock.json
vnodeBack.js
vnodeBack2.js
vnodeBack3.js
yarn.lock
16 changes: 11 additions & 5 deletions 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 @@ -8,8 +8,13 @@ describe('idom', () => {
elementEnd('p')
text('2')
elementEnd('div')
var currentNode = currentInfo.currentNode
expect(JSON.stringify(currentNode)).toBe('{"tagName":"div","children":[{"tagName":"p","text":"1"}],"text":"2"}')
var currentNode = currentInfo.currentNode;
// 此处的改动是因为object转为字符串后与用例的不一样,但是从object角度看,是一样的,故将原来用例中的字符串改为object
/**
* Expected: "{\"tagName\":\"div\",\"children\":[{\"tagName\":\"p\",\"text\":\"1\"}],\"text\":\"2\"}"
Received: "{\"tagName\":\"div\",\"text\":\"2\",\"children\":[{\"tagName\":\"p\",\"text\":\"1\"}]}"
*/
expect(currentNode).toMatchObject({ tagName: "div", children: [{ tagName: "p", text: "1" }], text: "2" });
})
})

Expand All @@ -29,7 +34,8 @@ describe('idom2', () => {
text('4')
elementEnd('div')
var currentNode2 = currentInfo.currentNode
expect(JSON.stringify(currentNode)).toBe('{"tagName":"div","children":[{"tagName":"p","text":"1"}],"text":"2"}')
expect(JSON.stringify(currentNode2)).toBe('{"tagName":"div","children":[{"tagName":"p","text":"3"}],"text":"4"}')
// 此处的改动是因为object转为字符串后与用例的不一样,但是从object角度看,是一样的,故将原来用例中的字符串改为object
expect(currentNode).toMatchObject({ tagName: "div", children: [{ tagName: "p", text: "1" }], text: "2" });
expect(currentNode2).toMatchObject({ tagName: "div", children: [{ tagName: "p", text: "3" }], text: "4" });
})
})
38 changes: 34 additions & 4 deletions vdom/vnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,48 @@

var currentInfo = {
currentNode: null,
currentParent: null
currentParent: null,
}
let fragment = null;
function elementOpen(tagName) {
// TODO
const tagNode = { tagName, text: '' };
// currentInfo.currentNode = curNode;
if (!fragment) {
currentInfo.currentNode = tagNode;
fragment = tagNode;
}
if (!currentInfo.currentParent || currentInfo.currentParent.length <= 0) {
currentInfo.currentParent = [tagNode];
} else {
const parentLength = currentInfo.currentParent.length;
if (parentLength > 0) {
const tagParent = currentInfo.currentParent[parentLength - 1];
if (!tagParent.children) {
tagParent['children'] = [tagNode];
} else {
tagParent.children.push(tagNode);
}
currentInfo.currentParent.push(tagNode);
currentInfo.currentNode = tagNode;
}
}
}

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

function elementEnd(tagName) {
// TODO
if (currentInfo.currentNode.tagName !== tagName) {
throw new Error('tag is not match');
}
currentInfo.currentParent.pop();
if (currentInfo.currentParent.length <= 0) {
currentInfo.currentNode = fragment;
fragment = null;
return;
}
currentInfo.currentNode = currentInfo.currentParent[currentInfo.currentParent.length - 1];
}
module.exports = {
elementOpen,
Expand Down