SO - normalizing nested blocks (WIP)
http://stackoverflow.com/questions/8439329/normalize-nested-blocks-with-jquery-or-javascript
by dzejkej
HTML
<div>
Heading 1
<p> Heading 2
<span>Heading 3</span>
</p>
</div>
<!-- this scenario is currently not working as desired, the div is not split into 2 -->
<div>
Hello 1
<div>Hello 2</div>
Hello 3
</div>
JavaScript
function normalize(list) {
// prepare the list for selectors
var joined = list.join(',');
// get all the elements that should not be nested within each other
var elements = $(document).find(joined);
$.each(elements, function(i, el) {
$el = $(el);
// look for parents that are not from the list
$parents = $el.parents(":not(" + joined +")");
// detach the node from
$el.detach();
// last() seems to be the closes one for me (not much testing)
$parents.last().append($el);
});
}
normalize(['p', 'span', 'div']);