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

Rich text: formats: allow format to filter value when changing tag name #35516

Merged
merged 11 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 7 additions & 0 deletions packages/format-library/src/text-color/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,12 @@ export const textColor = {
style: 'style',
class: 'class',
},
__unstableFilterAttributeValue( key, value ) {
if ( key !== 'style' ) return value;
// We should not add a background-color if it's already set
if ( value && value.includes( 'background-color' ) ) return value;
const addedCSS = [ 'background-color', 'rgba(0, 0, 0, 0)' ].join( ':' );
Copy link
Member

@fullofcaffeine fullofcaffeine Oct 14, 2021

Choose a reason for hiding this comment

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

Nit and curious: why the join( ':' ) and not just a single string?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's probably keeping with the style used in ./inline.js. But personally I'd avoid it in such a situation, since I don't know how smart the VM is.

return value ? [ value, addedCSS ].join( ';' ) : addedCSS;
},
edit: TextColorEdit,
};
2 changes: 1 addition & 1 deletion packages/format-library/src/text-color/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { __ } from '@wordpress/i18n';
*/
import { textColor as settings } from './index';

function parseCSS( css = '' ) {
export function parseCSS( css = '' ) {
Copy link
Member

@fullofcaffeine fullofcaffeine Oct 14, 2021

Choose a reason for hiding this comment

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

Is this export really needed? I couldn't find usage of this function outside of this module.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a dev artefact? I would remove it too.

Copy link
Member

Choose a reason for hiding this comment

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

Done ✅

return css.split( ';' ).reduce( ( accumulator, rule ) => {
if ( rule ) {
const [ property, value ] = rule.split( ':' );
Expand Down
35 changes: 21 additions & 14 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ function createEmptyValue() {
};
}

function simpleFindKey( object, value ) {
for ( const key in object ) {
if ( object[ key ] === value ) {
return key;
}
}
}

function toFormat( { type, attributes } ) {
let formatType;

Expand Down Expand Up @@ -94,15 +86,30 @@ function toFormat( { type, attributes } ) {

const registeredAttributes = {};
const unregisteredAttributes = {};
const _attributes = { ...attributes };

for ( const name in attributes ) {
const key = simpleFindKey( formatType.attributes, name );
for ( const key in formatType.attributes ) {
const name = formatType.attributes[ key ];

if ( key ) {
registeredAttributes[ key ] = attributes[ name ];
} else {
unregisteredAttributes[ name ] = attributes[ name ];
if ( typeof _attributes[ name ] !== 'undefined' ) {
registeredAttributes[ key ] = _attributes[ name ];
// delete the attribute and what's left is considered
// to be unregistered.
delete _attributes[ name ];
}

if ( formatType.__unstableFilterAttributeValue ) {
registeredAttributes[
key
] = formatType.__unstableFilterAttributeValue(
key,
registeredAttributes[ key ]
);
}
}

for ( const name in _attributes ) {
unregisteredAttributes[ name ] = attributes[ name ];
}

return {
Expand Down