diff --git a/test/index.spec.js b/test/index.spec.js index 94eb243..538954e 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -1,35 +1,46 @@ -const { elementOpen, text, elementEnd, currentInfo } = require('../vdom/vnodeBack.js'); +const { + elementOpen, + text, + elementEnd, + currentInfo, +} = require('../vdom/vnode.js'); describe('idom', () => { test('校验idom结构', async () => { - elementOpen('div') - 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"}],"text":"2"}') - }) -}) + elementOpen('div'); + 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"}],"text":"2"}' + ); + }); +}); describe('idom2', () => { test('重复调用的时候,生成结果独立互不影响', async () => { - elementOpen('div') - elementOpen('p') - text('1') - elementEnd('p') - text('2') - elementEnd('div') - var currentNode = currentInfo.currentNode - elementOpen('div') - elementOpen('p') - text('3') - elementEnd('p') - 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"}') - }) -}) \ No newline at end of file + elementOpen('div'); + elementOpen('p'); + text('1'); + elementEnd('p'); + text('2'); + elementEnd('div'); + var currentNode = currentInfo.currentNode; + elementOpen('div'); + elementOpen('p'); + text('3'); + elementEnd('p'); + 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"}' + ); + }); +}); diff --git a/vdom/vnode.js b/vdom/vnode.js index 4839adb..883bc44 100644 --- a/vdom/vnode.js +++ b/vdom/vnode.js @@ -5,23 +5,51 @@ */ var currentInfo = { - currentNode: null, - currentParent: null -} + currentNode: null, + currentParent: null, +}; function elementOpen(tagName) { - // TODO + const tempNode = { + tagName: tagName, + }; + + currentInfo.currentNode = tempNode; + + currentInfo.currentNode.parent = currentInfo.currentParent; + + if (currentInfo.currentNode.parent !== null) { + if (Array.isArray(currentInfo.currentNode.parent.children)) { + currentInfo.currentNode.parent.children.push(currentInfo.currentNode); + } else { + currentInfo.currentNode.parent.children = [currentInfo.currentNode]; + } + } + + currentInfo.currentParent = currentInfo.currentNode; } function text(textContent) { - // TODO + if (currentInfo.currentNode !== null) { + currentInfo.currentNode.text = textContent; + } } function elementEnd(tagName) { - // TODO + if (currentInfo.currentNode.tagName === tagName) { + const parent = currentInfo.currentNode.parent; + + delete currentInfo.currentNode.parent; + + if (parent !== null) { + currentInfo.currentNode = parent; + } + + currentInfo.currentParent = parent; + } } module.exports = { - elementOpen, - text, - elementEnd, - currentInfo -}; \ No newline at end of file + elementOpen, + text, + elementEnd, + currentInfo, +};