Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichText (native): remove HTML check in getFormatColors #56684

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I presume this condition was added to improve performance, but as shared in the PR's description, looping the formats array doesn't look enough expensive to need it. Plus, a regex search is probably adding some unneeded overhead.

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, '' );
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit confused about the purpose of this line. On a first look, I thought we needed to reformat the style attribute by removing empty spaces. However, I see inconsistencies when applying a color and saving the post.

After applying a text color:
ℹ️ Note that the style doesn't have empty spaces.

<!-- wp:paragraph -->
<p><mark style="background-color:rgba(0, 0, 0, 0);color:#cf2e2e" class="has-inline-color has-vivid-red-color">Hello</mark></p>
<!-- /wp:paragraph -->

After saving post:
ℹ️ Note that the style has empty spaces.

<!-- wp:paragraph -->
<p><mark style="background-color: rgba(0, 0, 0, 0);color: #cf2e2e" class="has-inline-color has-vivid-red-color">Hello</mark></p>
<!-- /wp:paragraph -->

This behavior leads to an issue where the post is marked as modified every time you save it and select the text. I managed to reproduce this before these changes, so it's not a regression introduced in this PR. That said, seems this line can be removed.

cc @geriux as the original author of this file.

Copy link
Member Author

Choose a reason for hiding this comment

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

I also though it was needed to do some checks below, but there's nothing actually checking the style as a whole, only the color value, so we should preserve the original styles and not modify anything.

// 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
Loading