diff --git a/packages/rich-text/src/normalise-formats.js b/packages/rich-text/src/normalise-formats.js
index 349540df9dc256..24690f089b4c76 100644
--- a/packages/rich-text/src/normalise-formats.js
+++ b/packages/rich-text/src/normalise-formats.js
@@ -17,7 +17,7 @@ export function normaliseFormats( { formats, text, start, end } ) {
newFormats.forEach( ( formatsAtIndex, index ) => {
const lastFormatsAtIndex = newFormats[ index - 1 ];
- if ( lastFormatsAtIndex ) {
+ if ( lastFormatsAtIndex && formatsAtIndex ) {
const newFormatsAtIndex = formatsAtIndex.slice( 0 );
newFormatsAtIndex.forEach( ( format, formatIndex ) => {
@@ -29,6 +29,8 @@ export function normaliseFormats( { formats, text, start, end } ) {
} );
newFormats[ index ] = newFormatsAtIndex;
+ } else if ( ! formatsAtIndex ) {
+ delete newFormats[ index ];
}
} );
diff --git a/packages/rich-text/src/test/normalise-formats.js b/packages/rich-text/src/test/normalise-formats.js
index 26a6147961378d..f0a1b827d264d5 100644
--- a/packages/rich-text/src/test/normalise-formats.js
+++ b/packages/rich-text/src/test/normalise-formats.js
@@ -13,21 +13,36 @@ import { getSparseArrayLength } from './helpers';
describe( 'normaliseFormats', () => {
const strong = { type: 'strong' };
const em = { type: 'em' };
+ const record = deepFreeze( {
+ formats: [ , [ em ], [ { ...em }, { ...strong } ], [ em, strong ], , , ],
+ text: 'one two',
+ } );
- it( 'should normalise formats', () => {
- const record = {
- formats: [ , [ em ], [ { ...em }, { ...strong } ], [ em, strong ] ],
- text: 'one two three',
- };
- const result = normaliseFormats( deepFreeze( record ) );
-
- expect( result ).toEqual( record );
- expect( result ).not.toBe( record );
+ function isNormalised( result ) {
expect( getSparseArrayLength( result.formats ) ).toBe( 3 );
expect( result.formats[ 1 ][ 0 ] ).toBe( result.formats[ 2 ][ 0 ] );
expect( result.formats[ 1 ][ 0 ] ).toBe( result.formats[ 3 ][ 0 ] );
expect( result.formats[ 2 ][ 1 ] ).toBe( result.formats[ 3 ][ 1 ] );
- // Should be serializable.
+ }
+
+ it( 'should normalise formats', () => {
+ const result = normaliseFormats( record );
+
+ expect( result ).toEqual( record );
+ expect( result ).not.toBe( record );
+ isNormalised( result );
+ } );
+
+ it( 'should be serializable', () => {
+ const result = normaliseFormats( record );
expect( JSON.stringify( record ) ).toBe( JSON.stringify( result ) );
} );
+
+ it( 'should normalise serialized formats', () => {
+ const deserialized = JSON.parse( JSON.stringify( record ) );
+ const result = normaliseFormats( deserialized );
+
+ expect( result ).toEqual( record );
+ isNormalised( result );
+ } );
} );
diff --git a/packages/rich-text/src/test/to-html-string.js b/packages/rich-text/src/test/to-html-string.js
index 708d76eedd6f58..eaef8f2f651b62 100644
--- a/packages/rich-text/src/test/to-html-string.js
+++ b/packages/rich-text/src/test/to-html-string.js
@@ -81,13 +81,12 @@ describe( 'toHTMLString', () => {
it( 'should be able to work from serialized value (without references)', () => {
expect( toHTMLString( {
- text: 'test',
+ text: 'abc',
formats: [
+ null,
[ { type: 'strong' } ],
- [ { type: 'strong' } ],
- [ { type: 'strong' } ],
- [ { type: 'strong' } ],
+ null,
],
- } ) ).toEqual( 'test' );
+ } ) ).toEqual( 'abc' );
} );
} );