I'm confused with $.parseHTML()
function. I thought that if the source html has a single root node this function should return an array with a single DOM node (the root node) with all the content attached as children.
It works that way most of the time, for example:
var nodes = $.parseHTML('<div><div>foo</div></div>');
console.debug(nodes.length); // prints 1
However if the root node is <p>
then the structure of DOM nodes is lost and the HTML gets exploded to multiple disconnected nodes:
var nodes = $.parseHTML('<p><div>foo</div></p>');
console.debug(nodes.length); // prints 3
I've created a jsfiddle where you can try these cases.
It doesn't make any difference if I use $(html)
or $.parseHTML(html)
.
What is so special about <p>
? Why is it parsed in a different way?
It this a bug or a feature?