Skip to content

Commit

Permalink
RichText (native): remove HTML check in getFormatColors (#56684)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Dec 1, 2023
1 parent d9033b7 commit 37bca49
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,43 @@ import { getColorObjectByAttributeValues } from '../../../components/colors';

const FORMAT_TYPE = 'core/text-color';
const REGEX_TO_MATCH = /^has-(.*)-color$/;
const TAGS_TO_SEARCH = /\<mark/;

export function getFormatColors( value, formats, colors ) {
if ( value?.search( TAGS_TO_SEARCH ) !== -1 ) {
const newFormats = formats.slice();
export function getFormatColors( formats, colors ) {
const newFormats = formats.slice();

newFormats.forEach( ( format ) => {
format.forEach( ( currentFormat ) => {
if ( currentFormat?.type === FORMAT_TYPE ) {
const className = currentFormat?.attributes?.class;
currentFormat.attributes.style =
currentFormat.attributes.style.replace( / /g, '' );
// We are looping through a sparse array where empty indices will be
// skipped.
newFormats.forEach( ( format ) => {
format.forEach( ( currentFormat ) => {
if ( currentFormat?.type === FORMAT_TYPE ) {
const className = currentFormat?.attributes?.class;

className?.split( ' ' ).forEach( ( currentClass ) => {
const match = currentClass.match( REGEX_TO_MATCH );
if ( match ) {
const [ , colorSlug ] =
currentClass.match( REGEX_TO_MATCH );
const colorObject = getColorObjectByAttributeValues(
colors,
colorSlug
);
const currentStyles =
currentFormat?.attributes?.style;
if (
colorObject &&
( ! currentStyles ||
currentStyles?.indexOf(
colorObject.color
) === -1 )
) {
currentFormat.attributes.style = [
`color: ${ colorObject.color }`,
currentStyles,
].join( ';' );
}
className?.split( ' ' ).forEach( ( currentClass ) => {
const match = currentClass.match( REGEX_TO_MATCH );
if ( match ) {
const [ , colorSlug ] =
currentClass.match( REGEX_TO_MATCH );
const colorObject = getColorObjectByAttributeValues(
colors,
colorSlug
);
const currentStyles = currentFormat?.attributes?.style;
if (
colorObject &&
( ! currentStyles ||
currentStyles?.indexOf( colorObject.color ) ===
-1 )
) {
currentFormat.attributes.style = [
`color: ${ colorObject.color }`,
currentStyles,
].join( ';' );
}
} );
}
} );
}
} );
}
} );
} );

return newFormats;
}

return formats;
return newFormats;
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class RichText extends Component {

const { formats, replacements, text } = currentValue;
const { activeFormats } = this.state;
const newFormats = getFormatColors( value, formats, colorPalette );
const newFormats = getFormatColors( formats, colorPalette );

return {
formats: newFormats,
Expand Down

1 comment on commit 37bca49

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 37bca49.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7060582923
📝 Reported issues:

Please sign in to comment.