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

fix: incremental-dom #6

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
69 changes: 40 additions & 29 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -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"}')
})
})
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"}'
);
});
});
50 changes: 39 additions & 11 deletions vdom/vnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
elementOpen,
text,
elementEnd,
currentInfo,
};