Skip to content

Commit

Permalink
Reordered the sync flow, needs more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed Jan 9, 2016
1 parent df043df commit f6f7533
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 54 deletions.
39 changes: 13 additions & 26 deletions dist/diffhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,8 +1066,8 @@ function sync(oldTree, newTree, patches) {
}

// If the top level nodeValue has changed we should reflect it.
if (oldTree.nodeValue !== nodeValue && !(!oldTree.nodeValue && !nodeValue)) {
if (oldTree.nodeValue !== null) {
if (oldTree.nodeValue && nodeValue) {
if (oldTree.nodeValue !== null && newTree.nodeValue) {
patches[patches.length] = {
__do__: CHANGE_TEXT,
element: oldTree,
Expand Down Expand Up @@ -1108,24 +1108,13 @@ function sync(oldTree, newTree, patches) {

// Remove these elements.
if (oldChildNodesLength > childNodesLength) {
var cloneOldChildNodes = slice.call(oldChildNodes, 0);

// Find the correct elements to remove, is smart about keeping existing
// elements.
var toRemove = filter.call(cloneOldChildNodes, function (el, index) {
var newChild = childNodes[oldChildNodes.indexOf(el)];
var notSame = newChild ? el.nodeName !== newChild.nodeName : true;

if (notSame && oldChildNodes.indexOf(el) > -1) {
oldChildNodes.splice(oldChildNodes.indexOf(el), 1);
}

return notSame;
});
// For now just splice out the end items.
var diff = oldChildNodesLength - childNodesLength;
var toRemove = oldChildNodes.splice(oldChildNodesLength - diff, diff);

oldChildNodesLength = oldChildNodes.length;

if (oldChildNodesLength === 0) {
if (oldChildNodesLength === 0 && childNodesLength === 0) {
patches[patches.length] = {
__do__: REMOVE_ELEMENT_CHILDREN,
element: oldTree,
Expand All @@ -1148,12 +1137,8 @@ function sync(oldTree, newTree, patches) {
}

// Replace elements if they are different.
if (oldChildNodesLength) {
if (oldChildNodesLength >= childNodesLength) {
for (var i = 0; i < childNodesLength; i++) {
if (!oldChildNodes[i]) {
continue;
}

if (oldChildNodes[i].nodeName !== childNodes[i].nodeName) {
// Add to the patches.
patches[patches.length] = {
Expand Down Expand Up @@ -1329,7 +1314,11 @@ function process(element, patches) {
if (descriptor.nodeName === '#text' || descriptor.nodeName === 'text') {
var textPromises = transition.makePromises('textChanged', [element], null, descriptor.nodeValue);

element.innerHTML = descriptor.nodeValue;
if (descriptor.nodeName === 'text') {
element.innerText = descriptor.nodeValue;
} else {
element.innerHTML = descriptor.nodeValue;
}

triggerTransition('textChanged', textPromises, function (promises) {});
}
Expand Down Expand Up @@ -2132,9 +2121,7 @@ function makeParser() {
// if has content
text = data.slice(lastTextPos, kMarkupPattern.lastIndex - match[0].length);

if (text.trim()) {
currentParent.childNodes.push(TextNode(text));
}
currentParent.childNodes.push(TextNode(text));
}
}

Expand Down
27 changes: 7 additions & 20 deletions lib/node/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export default function sync(oldTree, newTree, patches) {
}

// If the top level nodeValue has changed we should reflect it.
if ((oldTree.nodeValue !== nodeValue) && !(!oldTree.nodeValue && !nodeValue)) {
if (oldTree.nodeValue !== null) {
if (oldTree.nodeValue && nodeValue) {
if (oldTree.nodeValue !== null && newTree.nodeValue) {
patches[patches.length] = {
__do__: CHANGE_TEXT,
element: oldTree,
Expand Down Expand Up @@ -115,24 +115,13 @@ export default function sync(oldTree, newTree, patches) {

// Remove these elements.
if (oldChildNodesLength > childNodesLength) {
let cloneOldChildNodes = slice.call(oldChildNodes, 0);

// Find the correct elements to remove, is smart about keeping existing
// elements.
let toRemove = filter.call(cloneOldChildNodes, function(el, index) {
var newChild = childNodes[oldChildNodes.indexOf(el)];
var notSame = newChild ? el.nodeName !== newChild.nodeName : true;

if (notSame && oldChildNodes.indexOf(el) > -1) {
oldChildNodes.splice(oldChildNodes.indexOf(el), 1);
}

return notSame;
});
// For now just splice out the end items.
let diff = oldChildNodesLength - childNodesLength;
let toRemove = oldChildNodes.splice(oldChildNodesLength - diff, diff);

oldChildNodesLength = oldChildNodes.length;

if (oldChildNodesLength === 0) {
if (oldChildNodesLength === 0 && childNodesLength === 0) {
patches[patches.length] = {
__do__: REMOVE_ELEMENT_CHILDREN,
element: oldTree,
Expand All @@ -157,10 +146,8 @@ export default function sync(oldTree, newTree, patches) {
}

// Replace elements if they are different.
if (oldChildNodesLength) {
if (oldChildNodesLength >= childNodesLength) {
for (let i = 0; i < childNodesLength; i++) {
if (!oldChildNodes[i]) { continue; }

if (oldChildNodes[i].nodeName !== childNodes[i].nodeName) {
// Add to the patches.
patches[patches.length] = {
Expand Down
7 changes: 5 additions & 2 deletions lib/patches/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export default function process(element, patches) {
let textPromises = transition.makePromises('textChanged',
[element], null, descriptor.nodeValue);

element.innerHTML = descriptor.nodeValue;
if (descriptor.nodeName === 'text') {
element.innerText = descriptor.nodeValue;
} else {
element.innerHTML = descriptor.nodeValue;
}

triggerTransition('textChanged', textPromises, promises => {});
}
Expand All @@ -42,7 +46,6 @@ export default function process(element, patches) {
fragment.appendChild(element);
}


return element;
};

Expand Down
4 changes: 1 addition & 3 deletions lib/util/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ export function makeParser() {
// if has content
text = data.slice(lastTextPos, kMarkupPattern.lastIndex - match[0].length);

if (text.trim()) {
currentParent.childNodes.push(TextNode(text));
}
currentParent.childNodes.push(TextNode(text));
}
}

Expand Down
10 changes: 8 additions & 2 deletions test/integration/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ describe('Integration: Basics', function() {
});

it('can track very-complex state', function(done) {
function filterOutText(elements) {
return [].slice.call(elements).filter(element => {
return element.nodeType === 1;
});
}

var count = 0;
var tagNames = ['p', 'strong', 'div', 'span'];

Expand All @@ -207,11 +213,11 @@ describe('Integration: Basics', function() {
count++;

if (count === 3) {
assert.equal(this.fixture.childNodes.length, 100);
assert.equal(this.fixture.querySelectorAll('p, strong, div, span').length, 100);
done();
}
else if (count === 1) {
assert.equal(this.fixture.childNodes.length, 1000);
assert.equal(this.fixture.querySelectorAll('p, strong, div, span').length, 1000);

diff.innerHTML(this.fixture, makePs(10), {
enableWorker: false
Expand Down
2 changes: 1 addition & 1 deletion test/integration/outer.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('Integration: outerHTML', function() {
`);

assert.equal(this.fixture.querySelector('p'), null);
assert.equal(this.fixture.firstChild.firstChild.textContent.trim(), `
assert.equal(this.fixture.childNodes[1].firstChild.textContent.trim(), `
var test = \"<p></p>\";
`.trim());
});
Expand Down

0 comments on commit f6f7533

Please sign in to comment.