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

Unable to register format type to handle bare tag name #46795

Closed
davilera opened this issue Dec 27, 2022 · 2 comments · Fixed by #46798
Closed

Unable to register format type to handle bare tag name #46795

davilera opened this issue Dec 27, 2022 · 2 comments · Fixed by #46798
Assignees
Labels
[Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed [Type] Bug An existing feature does not function as intended

Comments

@davilera
Copy link
Contributor

Description

Users are unable to register a new format type to handle a bare tag name (i.e. when className is set to null), as registerFormatType logs the following error:

Format "core/unknown" is already registered to handle bare tag name "xxx".

This occurs because registerFormatType has the following validation snippet in line 80:

if ( settings.className === null ) {
  const formatTypeForBareElement = select(
    richTextStore
 ).getFormatTypeForBareElement( settings.tagName );

  if ( formatTypeForBareElement ) {
    window.console.error(
      `Format "${ formatTypeForBareElement.name }" is already registered to handle bare tag name "${ settings.tagName }".`
    );
    return;
 }
}

that checks if the given tagName is already registered. The current implementation of getFormatTypeForBareElement:

export function getFormatTypeForBareElement( state, bareElementTagName ) {
  const formatTypes = getFormatTypes( state );
  return (
    formatTypes.find( ( { className, tagName } ) => {
      return className === null && bareElementTagName === tagName;
    } ) ||
    formatTypes.find( ( { className, tagName } ) => {
      return className === null && '*' === tagName;
    } )
  );
}

always returns a valid format type for any bare element, because of the second part of the return statement, thus making it impossible to register new formats.

Possible Solution

In registerFormatType, change line 85 from:

if ( formatTypeForBareElement ) {

to

if (
  formatTypeForBareElement &&
  formatTypeForBareElement.name !== 'core/unknown'
) {

Step-by-step reproduction instructions

  1. Go to the post editor
  2. Open your browser's developer tools
  3. In the JavaScript console, type the following:
wp.richText.registerFormatType( 'name/format', {
  title: 'Test bare tag format',
  tagName: 'xxx',
  className: null,
  edit: () => null,
} );

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 6.1.1
  • Gutenberg 14.8.1

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@t-hamano
Copy link
Contributor

Thanks for the report.
I think this issue is the same as #46777.

@t-hamano t-hamano added the [Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable label Dec 27, 2022
@kathrynwp kathrynwp added the [Type] Bug An existing feature does not function as intended label Dec 27, 2022
@kathrynwp
Copy link

I'm going to go ahead and close this out, so that efforts to fix the issue can be concentrated over in the other ticket. Thank you again for reporting!

@kathrynwp kathrynwp closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2022
@kathrynwp kathrynwp added the [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed label Dec 27, 2022
@priethor priethor removed the [Status] In Progress Tracking issues with work in progress label Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants