From b8e2b5fb810855620d29f9eb2572a2fb3be0ccf1 Mon Sep 17 00:00:00 2001 From: Neil Jenkins Date: Mon, 31 Oct 2011 13:07:41 +1100 Subject: [PATCH] Normalise
/<#text> on setHTML and paste Make sure all the block level elements have either a
or an empty text node in the right place, as required by the browser. --- source/Editor.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/source/Editor.js b/source/Editor.js index 40bf9ee..00a5221 100644 --- a/source/Editor.js +++ b/source/Editor.js @@ -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 );