-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, '' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
After saving post:
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
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.