Skip to content

Commit

Permalink
Merge pull request #81 from AutoSponge/support-cached-nodes
Browse files Browse the repository at this point in the history
provides simple mechanism to support using cached nodes
  • Loading branch information
patrick-steele-idem authored Sep 6, 2016
2 parents 2a60451 + df2a69f commit 3d4be15
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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

0 comments on commit 3d4be15

Please sign in to comment.