@@ -74,7 +74,7 @@ function createRecord( element, range, settings = {} ) {
7474 ) {
7575 return {
7676 value : {
77- formats : Array ( 1 ) ,
77+ formats : [ undefined ] ,
7878 text : '\n' ,
7979 } ,
8080 selection : { } ,
@@ -131,7 +131,7 @@ function createRecord( element, range, settings = {} ) {
131131
132132 let format ;
133133
134- if ( ! unwrapNodeMatch ( node ) ) {
134+ if ( ! unwrapNodeMatch ( node ) && node . nodeName !== 'BR' ) {
135135 const type = node . nodeName . toLowerCase ( ) ;
136136 const attributes = getAttributes ( node , settings ) ;
137137
@@ -173,6 +173,10 @@ function createRecord( element, range, settings = {} ) {
173173 formats [ index ] = value . formats [ i ] ;
174174 }
175175 }
176+
177+ if ( ! formats [ index ] ) {
178+ formats [ index ] = undefined ;
179+ }
176180 }
177181 }
178182
@@ -229,7 +233,13 @@ export function apply( value, current, multiline ) {
229233 const range = current . ownerDocument . createRange ( ) ;
230234 const isCollapsed = startContainer === endContainer && startOffset === endOffset ;
231235
232- if ( isCollapsed && startOffset === 0 && startContainer . nodeType === TEXT_NODE ) {
236+ if (
237+ isCollapsed &&
238+ startOffset === 0 &&
239+ startContainer . previousSibling &&
240+ startContainer . previousSibling . nodeType === ELEMENT_NODE &&
241+ startContainer . previousSibling . nodeName !== 'BR'
242+ ) {
233243 startContainer . insertData ( 0 , '\uFEFF' ) ;
234244 range . setStart ( startContainer , 1 ) ;
235245 range . setEnd ( endContainer , 1 ) ;
@@ -357,18 +367,22 @@ export function toDOM( { value, selection = {} }, multiline, _tag ) {
357367 } ) ;
358368 }
359369
360- if ( pointer . nodeType === TEXT_NODE ) {
370+ if ( character === '\n' ) {
371+ pointer = pointer . parentNode . appendChild ( doc . createElement ( 'br' ) ) ;
372+ } else if ( pointer . nodeType === TEXT_NODE ) {
361373 pointer . appendData ( character ) ;
362374 } else {
363375 pointer = pointer . parentNode . appendChild ( doc . createTextNode ( character ) ) ;
364376 }
365377
366378 if ( start === i ) {
367- startPath = createPathToNode ( pointer , body , [ pointer . nodeValue . length - 1 ] ) ;
379+ const initialPath = pointer . nodeValue ? [ pointer . nodeValue . length - 1 ] : [ ] ;
380+ startPath = createPathToNode ( pointer , body , initialPath ) ;
368381 }
369382
370383 if ( end === i ) {
371- endPath = createPathToNode ( pointer , body , [ pointer . nodeValue . length - 1 ] ) ;
384+ const initialPath = pointer . nodeValue ? [ pointer . nodeValue . length - 1 ] : [ ] ;
385+ endPath = createPathToNode ( pointer , body , initialPath ) ;
372386 }
373387 }
374388
@@ -418,21 +432,24 @@ export function isEmpty( record ) {
418432 return text . length === 0 && formats . length === 0 ;
419433}
420434
421- export function splice ( { formats, text, selection, value } , start , deleteCount , textToInsert = '' , formatsToInsert = [ ] ) {
435+ export function splice ( { formats, text, selection, value } , start , deleteCount , textToInsert = '' , formatsToInsert ) {
422436 if ( value !== undefined ) {
437+ start = start || selection . start ;
438+ deleteCount = deleteCount || selection . end - selection . start ;
439+
423440 const diff = textToInsert . length - deleteCount ;
424441
425442 return {
426443 selection : {
427- start : selection . start + ( selection . start > start ? diff : 0 ) ,
428- end : selection . end + ( selection . end > start + diff ? diff : 0 ) ,
444+ start : selection . start + ( selection . start >= start ? diff : 0 ) ,
445+ end : selection . end + ( selection . end >= start ? diff : 0 ) ,
429446 } ,
430447 value : splice ( value , start , deleteCount , textToInsert , formatsToInsert ) ,
431448 } ;
432449 }
433450
434451 if ( ! Array . isArray ( formatsToInsert ) ) {
435- formatsToInsert = Array ( textToInsert . length ) . fill ( [ formatsToInsert ] ) ;
452+ formatsToInsert = Array ( textToInsert . length ) . fill ( formatsToInsert ? [ formatsToInsert ] : formatsToInsert ) ;
436453 }
437454
438455 formats . splice ( start , deleteCount , ...formatsToInsert ) ;
0 commit comments