Skip to content
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
110 changes: 73 additions & 37 deletions src/widget-core/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1050,52 +1050,88 @@ export function renderer(renderer: () => WNode | VNode): Renderer {
});
}

function createKeyMap(wrappers: DNodeWrapper[]): (string | number)[] | false {
const keys: (string | number)[] = [];
for (let i = 0; i < wrappers.length; i++) {
const wrapper = wrappers[i];
if (wrapper.node.properties.key != null) {
keys.push(wrapper.node.properties.key);
} else {
return false;
}
}
return keys;
}

function _process(current: DNodeWrapper[], next: DNodeWrapper[], meta: ProcessMeta = {}): void {
let { mergeNodes = [], oldIndex = 0, newIndex = 0 } = meta;
const currentLength = current.length;
const nextLength = next.length;
const hasPreviousSiblings = currentLength > 1 || (currentLength > 0 && currentLength < nextLength);
const instructions: Instruction[] = [];
if (newIndex < nextLength) {
let currentWrapper = oldIndex < currentLength ? current[oldIndex] : undefined;
const nextWrapper = next[newIndex];
nextWrapper.hasPreviousSiblings = hasPreviousSiblings;

_processMergeNodes(nextWrapper, mergeNodes);

if (currentWrapper && same(currentWrapper, nextWrapper)) {
oldIndex++;
newIndex++;
if (isVNodeWrapper(currentWrapper) && isVNodeWrapper(nextWrapper)) {
nextWrapper.inserted = currentWrapper.inserted;
let instructions: Instruction[] = [];
let replace = false;
if (oldIndex === 0 && newIndex === 0 && currentLength) {
const currentKeys = createKeyMap(current);
if (currentKeys) {
const nextKeys = createKeyMap(next);
if (nextKeys) {
for (let i = 0; i < currentKeys.length; i++) {
if (nextKeys.indexOf(currentKeys[i]) !== -1) {
instructions = [];
replace = false;
break;
}
replace = true;
instructions.push({ current: current[i], next: undefined });
}
}
instructions.push({ current: currentWrapper, next: nextWrapper });
} else if (!currentWrapper || findIndexOfChild(current, nextWrapper, oldIndex + 1) === -1) {
has('dojo-debug') && current.length && registerDistinguishableCallback(next, newIndex);
instructions.push({ current: undefined, next: nextWrapper });
newIndex++;
} else if (findIndexOfChild(next, currentWrapper, newIndex + 1) === -1) {
has('dojo-debug') && registerDistinguishableCallback(current, oldIndex);
instructions.push({ current: currentWrapper, next: undefined });
oldIndex++;
} else {
has('dojo-debug') && registerDistinguishableCallback(next, newIndex);
has('dojo-debug') && registerDistinguishableCallback(current, oldIndex);
instructions.push({ current: currentWrapper, next: undefined });
instructions.push({ current: undefined, next: nextWrapper });
oldIndex++;
newIndex++;
}
}

if (newIndex < nextLength) {
_processQueue.push({ current, next, meta: { mergeNodes, oldIndex, newIndex } });
}

if (currentLength > oldIndex && newIndex >= nextLength) {
for (let i = oldIndex; i < currentLength; i++) {
has('dojo-debug') && registerDistinguishableCallback(current, i);
instructions.push({ current: current[i], next: undefined });
if (replace || (currentLength === 0 && !_mountOptions.merge)) {
for (let i = 0; i < next.length; i++) {
instructions.push({ current: undefined, next: next[i] });
}
} else {
if (newIndex < nextLength) {
let currentWrapper = oldIndex < currentLength ? current[oldIndex] : undefined;
const nextWrapper = next[newIndex];
nextWrapper.hasPreviousSiblings = hasPreviousSiblings;

_processMergeNodes(nextWrapper, mergeNodes);

if (currentWrapper && same(currentWrapper, nextWrapper)) {
oldIndex++;
newIndex++;
if (isVNodeWrapper(currentWrapper) && isVNodeWrapper(nextWrapper)) {
nextWrapper.inserted = currentWrapper.inserted;
}
instructions.push({ current: currentWrapper, next: nextWrapper });
} else if (!currentWrapper || findIndexOfChild(current, nextWrapper, oldIndex + 1) === -1) {
has('dojo-debug') && current.length && registerDistinguishableCallback(next, newIndex);
instructions.push({ current: undefined, next: nextWrapper });
newIndex++;
} else if (findIndexOfChild(next, currentWrapper, newIndex + 1) === -1) {
has('dojo-debug') && registerDistinguishableCallback(current, oldIndex);
instructions.push({ current: currentWrapper, next: undefined });
oldIndex++;
} else {
has('dojo-debug') && registerDistinguishableCallback(next, newIndex);
has('dojo-debug') && registerDistinguishableCallback(current, oldIndex);
instructions.push({ current: currentWrapper, next: undefined });
instructions.push({ current: undefined, next: nextWrapper });
oldIndex++;
newIndex++;
}
}
if (newIndex < nextLength) {
_processQueue.push({ current, next, meta: { mergeNodes, oldIndex, newIndex } });
}
if (currentLength > oldIndex && newIndex >= nextLength) {
for (let i = oldIndex; i < currentLength; i++) {
has('dojo-debug') && registerDistinguishableCallback(current, i);
instructions.push({ current: current[i], next: undefined });
}
}
}

Expand Down
49 changes: 49 additions & 0 deletions tests/widget-core/unit/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,55 @@ jsdomDescribe('vdom', () => {
assert.strictEqual(deferredPropertyCallCount, 12);
});

it('should replace keyed nodes', () => {
let swap: any;
class App extends WidgetBase {
private _swap = false;
constructor() {
super();
swap = () => {
this._swap = !this._swap;
this.invalidate();
};
}
render() {
if (this._swap) {
return v('div', [
v('div', { id: '1', key: '1' }),
v('div', { id: '2', key: '2' }),
v('div', { id: '3', key: '3' }),
v('div', { id: '4', key: '4' }),
v('div', { id: '5', key: '5' }),
v('div', { id: '6', key: '6' }),
v('div', { id: '7', key: '7' })
]);
}
return v('div', [
v('div', { id: '11', key: '11' }),
v('div', { id: '12', key: '12' }),
v('div', { id: '13', key: '13' }),
v('div', { id: '14', key: '14' }),
v('div', { id: '15', key: '15' }),
v('div', { id: '16', key: '16' }),
v('div', { id: '17', key: '17' })
]);
}
}
const r = renderer(() => w(App, {}));
const root = document.createElement('div');
r.mount({ domNode: root });
assert.strictEqual(
root.outerHTML,
'<div><div><div id="11"></div><div id="12"></div><div id="13"></div><div id="14"></div><div id="15"></div><div id="16"></div><div id="17"></div></div></div>'
);
swap();
resolvers.resolve();
assert.strictEqual(
root.outerHTML,
'<div><div><div id="1"></div><div id="2"></div><div id="3"></div><div id="4"></div><div id="5"></div><div id="6"></div><div id="7"></div></div></div>'
);
});

describe('supports merging with a widget returned a the top level', () => {
it('Supports merging DNodes onto existing HTML', () => {
const iframe = document.createElement('iframe');
Expand Down