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

provides simple mechanism to support using cached nodes #81

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
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ function morphdom(fromNode, toNode, options) {
delete fromNodesLookup[toElKey];
}

if (toNode.isSameNode && toNode.isSameNode(fromNode)) {
return;
}

if (!childrenOnly) {
if (onBeforeElUpdated(fromEl, toEl) === false) {
return;
Expand Down Expand Up @@ -399,6 +403,10 @@ function morphdom(fromNode, toNode, options) {
curToNodeKey = getNodeKey(curToNodeChild);

while (curFromNodeChild) {
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) {
return;
}

curFromNodeKey = getNodeKey(curFromNodeChild);
fromNextSibling = curFromNodeChild.nextSibling;

Expand Down
17 changes: 17 additions & 0 deletions test/browser/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,23 @@ function addTests() {
expect(el1.querySelector('#skipMeChild') != null).to.equal(true);
});

it('should use isSameNode to allow reference proxies', function() {
var el1 = document.createElement('div');
el1.innerHTML = 'stay gold';
var el2 = document.createElement('div');
el2.innerHTML = 'ponyboy';
el2.isSameNode = function (el) {return el.isSameNode(el1)};
morphdom(el1, el2);
expect(el1.innerHTML).to.equal('stay gold');

var containEl1 = document.createElement('div');
containEl1.appendChild(el1);
var containEl2 = document.createElement('div');
containEl2.appendChild(el2);
morphdom(containEl1, containEl2);
expect(el1.innerHTML).to.equal('stay gold');
});

// xit('should reuse DOM element with matching ID and class name (2)', function() {
// // NOTE: This test is currently failing. We need to improve the special case code
// // for handling incompatible root nodes.
Expand Down