Skip to content

Commit

Permalink
Normalise formats should handle non sparse arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Oct 8, 2018
1 parent 7363b63 commit a776108
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
4 changes: 3 additions & 1 deletion packages/rich-text/src/normalise-formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) => {
Expand All @@ -29,6 +29,8 @@ export function normaliseFormats( { formats, text, start, end } ) {
} );

newFormats[ index ] = newFormatsAtIndex;
} else if ( ! formatsAtIndex ) {
delete newFormats[ index ];
}
} );

Expand Down
35 changes: 25 additions & 10 deletions packages/rich-text/src/test/normalise-formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
} );
} );
9 changes: 4 additions & 5 deletions packages/rich-text/src/test/to-html-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<strong>test</strong>' );
} ) ).toEqual( 'a<strong>b</strong>c' );
} );
} );

0 comments on commit a776108

Please sign in to comment.