Skip to content

Commit

Permalink
Rich text: only merge neighbouring equal formats when applying a form…
Browse files Browse the repository at this point in the history
…at (#35016)
  • Loading branch information
ellatrix authored Sep 21, 2021
1 parent 3ebfdfa commit b12bbe4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
9 changes: 1 addition & 8 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { select } from '@wordpress/data';
* Internal dependencies
*/
import { store as richTextStore } from './store';
import { isFormatEqual } from './is-format-equal';
import { createElement } from './create-element';
import { mergePair } from './concat';
import {
Expand Down Expand Up @@ -424,16 +423,10 @@ function createFromElement( {
continue;
}

const lastFormats =
accumulator.formats[ accumulator.formats.length - 1 ];
const lastFormat = lastFormats && lastFormats[ lastFormats.length - 1 ];
const newFormat = toFormat( {
const format = toFormat( {
type,
attributes: getAttributes( { element: node } ),
} );
const format = isFormatEqual( newFormat, lastFormat )
? lastFormat
: newFormat;

if (
multilineWrapperTags &&
Expand Down
19 changes: 19 additions & 0 deletions packages/rich-text/src/test/apply-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,23 @@ describe( 'applyFormat', () => {
expect( result ).not.toBe( record );
expect( getSparseArrayLength( result.formats ) ).toBe( 3 );
} );

it( 'should merge equal neighbouring formats', () => {
const record = {
// Use a different reference but equal content.
formats: [ , , [ { ...em } ], [ { ...em } ] ],
text: 'test',
};
const expected = {
...record,
activeFormats: [ em ],
// All references should be the same.
formats: [ [ em ], [ em ], [ em ], [ em ] ],
};
const result = applyFormat( deepFreeze( record ), em, 0, 2 );

expect( result ).toEqual( expected );
expect( result ).not.toBe( record );
expect( getSparseArrayLength( result.formats ) ).toBe( 4 );
} );
} );
4 changes: 2 additions & 2 deletions packages/rich-text/src/test/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ describe( 'create', () => {
expect( value.formats[ 2 ] ).toBe( value.formats[ 3 ] );
} );

it( 'should use same reference for equal format', () => {
it( 'should use different reference for equal format', () => {
const value = create( { html: '<a href="#">a</a><a href="#">a</a>' } );

// Format objects.
expect( value.formats[ 0 ][ 0 ] ).toBe( value.formats[ 1 ][ 0 ] );
expect( value.formats[ 0 ][ 0 ] ).not.toBe( value.formats[ 1 ][ 0 ] );

// Format arrays per index.
expect( value.formats[ 0 ] ).not.toBe( value.formats[ 1 ] );
Expand Down
3 changes: 1 addition & 2 deletions packages/rich-text/src/test/to-html-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ describe( 'toHTMLString', () => {
it( 'should serialize neighbouring same formats', () => {
const HTML = '<a href="a">a</a><a href="a">a</a>';
const element = createNode( `<p>${ HTML }</p>` );
const expectedHTML = '<a href="a">aa</a>';

expect( toHTMLString( { value: create( { element } ) } ) ).toEqual(
expectedHTML
HTML
);
} );
} );

0 comments on commit b12bbe4

Please sign in to comment.