marcelklehr / vdom-virtualize

Virtualize a DOM node
MIT License
131 stars 27 forks source link

fromHTML(): Correcting an issue with whitespace nodes #32

Closed cmtt closed 9 years ago

cmtt commented 9 years ago

With the update to vdom-virtualize 0.0.11, fromHTML() changed its behavior due to the recent fix for #21. If a HTML string contains whitespace characters at the very beginning, it creates a VirtualText node instead. This might occur with certain template engines and configurations or when using HTML which wasn't generated by a machine.

This is due to the return value of Node.firstChild (which was introduced with the new version). It includes TextNodes which derive from any whitespace or control character in the provided HTML (see MDN) or this example:

var NL_HTML = '\n<div>Content</div>';
var node = document.createElement('div');
node.innerHTML = NL_HTML;

console.log('firstChild',node.firstChild); // Text
console.log('childNodes[0]',node.childNodes[0]); // Text
console.log('children[0]',node.children[0]); // Node

The old behavior of using Node.children should be restored for a better developing experience.

marcelklehr commented 9 years ago

But then what will happen if someone just feeds text to it? I think we should check if node.children.length and use firstChild if it is 0

cmtt commented 9 years ago

Indeed! I have updated the pull request for this use case.

marcelklehr commented 9 years ago

awesome, thank you!