@@ -64,10 +64,7 @@ const BaseFormatter = Module({
6464 this . injectHooks ( rootElement ) ;
6565
6666 const rangeCoordinates = mediator . get ( 'selection:range:coordinates' ) ;
67- const clonedNodes = this . cloneNodes ( rootElement ) ;
68- clonedNodes . forEach ( ( node ) => {
69- DOM . trimNodeText ( node ) ;
70- } ) ;
67+ const clonedNodes = DOM . cloneNodes ( rootElement , { trim : true } ) ;
7168
7269 mediator . exec ( 'canvas:content' , clonedNodes ) ;
7370 mediator . exec ( 'canvas:select:by:coordinates' , rangeCoordinates ) ;
@@ -141,14 +138,6 @@ const BaseFormatter = Module({
141138 /**
142139 * PRIVATE METHODS:
143140 */
144- cloneNodes ( rootElement ) {
145- let clonedNodes = [ ] ;
146- rootElement . childNodes . forEach ( ( node ) => {
147- clonedNodes . push ( node . cloneNode ( true ) ) ;
148- } ) ;
149- return clonedNodes ;
150- } ,
151-
152141 injectHooks ( rootElement ) {
153142 while ( ! / \w + / . test ( rootElement . firstChild . textContent ) ) {
154143 DOM . removeNode ( rootElement . firstChild ) ;
@@ -235,9 +224,10 @@ const BaseFormatter = Module({
235224 const isLastChild = brNode === brNode . parentNode . lastChild ;
236225 const isDoubleBreak = brNode . nextSibling && brNode . nextSibling . nodeName === 'BR' ;
237226 const isInBlock = DOM . isIn ( brNode , blockTags , rootElem ) ;
227+ const isOrphan = brNode . parentNode === rootElem ;
238228
239- if ( isLastChild ) {
240- brNodesToRemove . push ( isLastChild ) ;
229+ if ( isLastChild || isOrphan ) {
230+ brNodesToRemove . push ( brNode ) ;
241231 return ;
242232 }
243233
@@ -291,8 +281,9 @@ const BaseFormatter = Module({
291281 let isInvalid = validTags . indexOf ( currentNode . nodeName ) < 0 ;
292282 let isBrNode = currentNode . nodeName === 'BR' ; // BR nodes are handled elsewhere
293283 let isTypesterElem = currentNode . className && / t y p e s t e r / . test ( currentNode . className ) ;
284+ let isElement = currentNode . nodeType !== Node . TEXT_NODE ;
294285
295- if ( isInvalid && ! isBrNode && ! isTypesterElem ) {
286+ if ( isInvalid && ! isBrNode && ! isTypesterElem && isElement ) {
296287 invalidElements . unshift ( currentNode ) ;
297288 }
298289 }
@@ -312,6 +303,7 @@ const BaseFormatter = Module({
312303
313304 defaultOrphanedTextNodes ( rootElem ) {
314305 const { childNodes } = rootElem ;
306+
315307 for ( let i = 0 ; i < childNodes . length ; i ++ ) {
316308 let childNode = childNodes [ i ] ;
317309 if ( childNode . nodeType === Node . TEXT_NODE && / \w + / . test ( childNode . textContent ) ) {
0 commit comments