Skip to content

Commit

Permalink
Block Variations: Detect active variation correctly based on RichText…
Browse files Browse the repository at this point in the history
… attribute (#62325)

If a block variation has an `isActive` property that is an array of strings (i.e. attribute names), and one of the attributes referenced therein is a `RichText` value, compare the given `RichText` string correctly to the given block's. (This applies also to nested attributes, i.e. if a `RichText` value is contained in an object (that might be contained in another, and so on) inside an attribute.)

Co-authored-by: ockham <bernhard-reiter@git.wordpress.org>
Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
  • Loading branch information
4 people authored and ellatrix committed Jun 11, 2024
1 parent 97f1289 commit ab02b7d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import removeAccents from 'remove-accents';
* WordPress dependencies
*/
import { createSelector } from '@wordpress/data';
import { RichTextData } from '@wordpress/rich-text';

/**
* Internal dependencies
Expand Down Expand Up @@ -262,17 +263,21 @@ export function getActiveBlockVariation( state, blockName, attributes, scope ) {
continue;
}
const isMatch = definedAttributes.every( ( attribute ) => {
const attributeValue = getValueFromObjectPath(
attributes,
const variationAttributeValue = getValueFromObjectPath(
variation.attributes,
attribute
);
if ( attributeValue === undefined ) {
if ( variationAttributeValue === undefined ) {
return false;
}
return (
attributeValue ===
getValueFromObjectPath( variation.attributes, attribute )
let blockAttributeValue = getValueFromObjectPath(
attributes,
attribute
);
if ( blockAttributeValue instanceof RichTextData ) {
blockAttributeValue = blockAttributeValue.toHTMLString();
}
return variationAttributeValue === blockAttributeValue;
} );
if ( isMatch && definedAttributesLength > maxMatchedAttributes ) {
match = variation;
Expand Down
42 changes: 42 additions & 0 deletions packages/blocks/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import {
getActiveBlockVariation,
} from '../selectors';

/**
* WordPress dependencies
*/
import { RichTextData } from '@wordpress/rich-text';

const keyBlocksByName = ( blocks ) =>
blocks.reduce(
( result, block ) => ( { ...result, [ block.name ]: block } ),
Expand Down Expand Up @@ -452,6 +457,43 @@ describe( 'selectors', () => {
} )
).toEqual( variations[ 1 ] );
} );
it( 'should support RichText attributes in the isActive array', () => {
const variations = [
{
name: 'variation-1',
attributes: {
firstTestAttribute:
'This is a <strong>RichText</strong> attribute.',
},
isActive: [ 'firstTestAttribute' ],
},
{
name: 'variation-2',
attributes: {
firstTestAttribute:
'This is a <em>RichText</em> attribute.',
},
isActive: [ 'firstTestAttribute' ],
},
];
const state =
createBlockVariationsStateWithTestBlockType( variations );

expect(
getActiveBlockVariation( state, blockName, {
firstTestAttribute: RichTextData.fromHTMLString(
'This is a <strong>RichText</strong> attribute.'
),
} )
).toEqual( variations[ 0 ] );
expect(
getActiveBlockVariation( state, blockName, {
firstTestAttribute: RichTextData.fromHTMLString(
'This is a <em>RichText</em> attribute.'
),
} )
).toEqual( variations[ 1 ] );
} );
it( 'should return the active variation based on the given isActive array (multiple values)', () => {
const variations = [
{
Expand Down

0 comments on commit ab02b7d

Please sign in to comment.