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

Remove nodes that could not be merged #158

Merged
merged 1 commit into from
Nov 7, 2018
Merged
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
16 changes: 13 additions & 3 deletions src/widget-core/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export function renderer(renderer: () => WNode | VNode): Renderer {
let _afterRenderCallbacks: Function[] = [];
let _deferredRenderCallbacks: Function[] = [];
let parentInvalidate: () => void;
let _allMergedNodes: Node[] = [];

function nodeOperation(
propName: string,
Expand Down Expand Up @@ -724,6 +725,10 @@ export function renderer(renderer: () => WNode | VNode): Renderer {
});
_runProcessQueue();
_mountOptions.merge = false;
let mergedNode: Node | undefined;
while ((mergedNode = _allMergedNodes.pop())) {
mergedNode.parentNode && mergedNode.parentNode.removeChild(mergedNode);
}
_runDomInstructionQueue();
_runCallbacks();
}
Expand Down Expand Up @@ -892,6 +897,10 @@ export function renderer(renderer: () => WNode | VNode): Renderer {
for (let i = 0; i < mergeNodes.length; i++) {
const domElement = mergeNodes[i] as Element;
if (tag.toUpperCase() === (domElement.tagName || '')) {
const mergeNodeIndex = _allMergedNodes.indexOf(domElement);
if (mergeNodeIndex !== -1) {
_allMergedNodes.splice(mergeNodeIndex, 1);
}
mergeNodes.splice(i, 1);
next.domNode = domElement;
break;
Expand Down Expand Up @@ -1099,12 +1108,13 @@ export function renderer(renderer: () => WNode | VNode): Renderer {
}
}
} else {
next.merged = true;
}
if (next.domNode) {
if (_mountOptions.merge) {
mergeNodes = arrayFrom(next.domNode.childNodes);
_allMergedNodes = [..._allMergedNodes, ...mergeNodes];
}
next.merged = true;
}
if (next.domNode) {
if (next.node.children) {
next.childrenWrappers = renderedToWrapper(next.node.children, next, null);
}
Expand Down
24 changes: 12 additions & 12 deletions tests/widget-core/unit/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2415,7 +2415,7 @@ jsdomDescribe('vdom', () => {
document.body.removeChild(iframe);
});

it('Skips unknown nodes when merging', () => {
it('Removes unknown nodes when merging', () => {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.contentDocument.write(`
Expand Down Expand Up @@ -2497,11 +2497,11 @@ jsdomDescribe('vdom', () => {
childElementCount,
'should have the same number of children'
);
assert.strictEqual(label, root.childNodes[1], 'should have been reused');
assert.strictEqual(select, root.childNodes[3], 'should have been reused');
assert.strictEqual(button, root.childNodes[5], 'should have been reused');
assert.strictEqual(span, root.childNodes[7], 'should have been reused');
assert.strictEqual(div, root.childNodes[9], 'should have been reused');
assert.strictEqual(label, root.childNodes[0], 'should have been reused');
assert.strictEqual(select, root.childNodes[1], 'should have been reused');
assert.strictEqual(button, root.childNodes[2], 'should have been reused');
assert.strictEqual(span, root.childNodes[3], 'should have been reused');
assert.strictEqual(div, root.childNodes[4], 'should have been reused');
assert.isFalse(select.disabled, 'select should be enabled');
assert.isFalse(button.disabled, 'button should be enabled');

Expand Down Expand Up @@ -4459,7 +4459,7 @@ jsdomDescribe('vdom', () => {
assert.isTrue(onclickListener.called, 'onclickListener should have been called');
document.body.removeChild(iframe);
});
it('Skips unknown nodes when merging', () => {
it('Removes unknown nodes when merging', () => {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.contentDocument.write(`
Expand Down Expand Up @@ -4531,11 +4531,11 @@ jsdomDescribe('vdom', () => {
r.mount({ domNode: iframe.contentDocument.body });
assert.strictEqual(root.className, 'foo bar', 'should have added bar class');
assert.strictEqual(root.childElementCount, childElementCount, 'should have the same number of children');
assert.strictEqual(label, root.childNodes[1], 'should have been reused');
assert.strictEqual(select, root.childNodes[3], 'should have been reused');
assert.strictEqual(button, root.childNodes[5], 'should have been reused');
assert.strictEqual(span, root.childNodes[7], 'should have been reused');
assert.strictEqual(div, root.childNodes[9], 'should have been reused');
assert.strictEqual(label, root.childNodes[0], 'should have been reused');
assert.strictEqual(select, root.childNodes[1], 'should have been reused');
assert.strictEqual(button, root.childNodes[2], 'should have been reused');
assert.strictEqual(span, root.childNodes[3], 'should have been reused');
assert.strictEqual(div, root.childNodes[4], 'should have been reused');
assert.isFalse(select.disabled, 'select should be enabled');
assert.isFalse(button.disabled, 'button should be enabled');
assert.strictEqual(select.value, 'foo', 'foo should be selected');
Expand Down