Skip to content

Commit

Permalink
Fix inserting line separator without previous formats (#11859)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Nov 14, 2018
1 parent 5757ec7 commit 56d09e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/rich-text/src/insert-line-separator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export function insertLineSeparator(
) {
const beforeText = getTextContent( value ).slice( 0, startIndex );
const previousLineSeparatorIndex = beforeText.lastIndexOf( LINE_SEPARATOR );
const previousLineSeparatorFormats = value.formats[ previousLineSeparatorIndex ];
let formats = [ , ];

if ( previousLineSeparatorIndex !== -1 ) {
formats = [ value.formats[ previousLineSeparatorIndex ] ];
if ( previousLineSeparatorFormats ) {
formats = [ previousLineSeparatorFormats ];
}

const valueToInsert = {
Expand Down
22 changes: 21 additions & 1 deletion packages/rich-text/src/test/insert-line-separator.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe( 'insertLineSeparator', () => {
expect( getSparseArrayLength( result.formats ) ).toBe( 0 );
} );

it( 'should insert line separator in nested item', () => {
it( 'should insert line separator with previous line separator formats', () => {
const value = {
formats: [ , , , [ ol ], , ],
text: `1${ LINE_SEPARATOR }2${ LINE_SEPARATOR }a`,
Expand All @@ -73,4 +73,24 @@ describe( 'insertLineSeparator', () => {
expect( result ).toEqual( expected );
expect( getSparseArrayLength( result.formats ) ).toBe( 2 );
} );

it( 'should insert line separator without formats if previous line separator did not have any', () => {
const value = {
formats: [ , , , , , ],
text: `1${ LINE_SEPARATOR }2${ LINE_SEPARATOR }a`,
start: 5,
end: 5,
};
const expected = {
formats: [ , , , , , , ],
text: `1${ LINE_SEPARATOR }2${ LINE_SEPARATOR }a${ LINE_SEPARATOR }`,
start: 6,
end: 6,
};
const result = insertLineSeparator( deepFreeze( value ) );

expect( result ).not.toBe( value );
expect( result ).toEqual( expected );
expect( getSparseArrayLength( result.formats ) ).toBe( 0 );
} );
} );

0 comments on commit 56d09e5

Please sign in to comment.