0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Normalise <br>/<#text> on setHTML and paste

Make sure all the block level elements have either a <br> or an empty text node
in the right place, as required by the browser.
This commit is contained in:
Neil Jenkins 2011-10-31 13:07:41 +11:00
parent 72141fd670
commit b8e2b5fb81

View file

@ -830,7 +830,7 @@ document.addEventListener( 'DOMContentLoaded', function () {
return newParent;
};
var convertBRtoDIV = function ( root ) {
var cleanupBRs = function ( root ) {
var brs = root.querySelectorAll( 'BR' ),
l = brs.length,
br, seenBlock, nodeAfterSplit, div, next,
@ -842,20 +842,20 @@ document.addEventListener( 'DOMContentLoaded', function () {
while ( l-- ) {
br = brs[l];
// Ignore cursor fixes.
if ( !br.nextSibling && !br.previousSibling ) {
continue;
}
seenBlock = false;
nodeAfterSplit = br.parentNode.split( br, stopCondition );
if ( nodeAfterSplit.isInline() ) {
div = createElement( 'DIV' );
nodeAfterSplit.parentNode.insertBefore( div, nodeAfterSplit );
do {
next = nodeAfterSplit.nextSibling;
div.appendChild( nodeAfterSplit );
nodeAfterSplit = next;
} while ( nodeAfterSplit && nodeAfterSplit.isInline() );
// Only split elements if actually dividing
if ( br.nextSibling && br.previousSibling ) {
seenBlock = false;
nodeAfterSplit = br.parentNode.split( br, stopCondition );
if ( nodeAfterSplit.isInline() ) {
div = createElement( 'DIV' );
nodeAfterSplit.parentNode
.insertBefore( div, nodeAfterSplit );
do {
next = nodeAfterSplit.nextSibling;
div.appendChild( nodeAfterSplit );
nodeAfterSplit = next;
} while ( nodeAfterSplit && nodeAfterSplit.isInline() );
}
}
br.detach();
}
@ -882,7 +882,7 @@ document.addEventListener( 'DOMContentLoaded', function () {
setTimeout( function () {
// Get the pasted content and clean
var frag = cleanTree( pasteArea.detach(), null, false );
convertBRtoDIV( frag );
cleanupBRs( frag );
// Restore the previous selection
range.setStart( startContainer, startOffset );
@ -1166,7 +1166,7 @@ document.addEventListener( 'DOMContentLoaded', function () {
div.innerHTML = html;
cleanTree( div, frag, true );
convertBRtoDIV( frag );
cleanupBRs( frag );
// Wrap top-level inline nodes in div.
children = frag.childNodes;
@ -1189,6 +1189,12 @@ document.addEventListener( 'DOMContentLoaded', function () {
frag.appendChild( div );
}
// Fix cursor
var node = frag;
while ( node = node.getNextBlock() ) {
node.fixCursor();
}
// Remove existing body children
while ( child = body.lastChild ) {
body.removeChild( child );