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: Update the regex to allow underscores in Format Type class name format #56599

Merged

Conversation

alexandrebuffet
Copy link
Contributor

What?

This allows underscores in Format Type class name format.

Why?

Currently, Format Types that apply a class containing underscores are not registered correctly and cause the following error in the console: A class name must begin with a letter, followed by any number of hyphens, letters, or numbers.

How?

This PR fixes #56571 by adding _ (underscore) character to the regular expression used when checking the className format in the registerFormatType function.

Testing Instructions

  • Declare a new "My Format" Format Type via registerFormatType in a JS file a enqueue it in editor like so:
/**
 * WordPress dependencies
 */
import { __, _x } from "@wordpress/i18n";
import { toggleFormat, registerFormatType } from "@wordpress/rich-text";
import { RichTextToolbarButton } from "@wordpress/block-editor";
import { wordpress } from "@wordpress/icons";

const name = "test-format-type/my-format";
const title = __("My Format");

registerFormatType(name, {
    title: title,
    icon: wordpress,
    tagName: "span",
    className: "my-format-class__with-underscore",
    attributes: {
        class: "class"
    },
    edit({ isActive, value, onChange, onFocus }) {
        function onClick() {
            onChange(toggleFormat(value, { type: name }));
            onFocus();
        }

        return (
            <>
                <RichTextToolbarButton icon={wordpress} title={title} onClick={onClick} isActive={isActive} />
            </>
        );
    }
});
  • In admin, open editor adding a page, apply "My Format" format to a text selection in a Paragraph for example
  • Open the browser console and check for error
  • Save the page and check for the <span class="my-format-class__with-underscore"> in the paragraph markup
@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 28, 2023
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @alexandrebuffet! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@Mamaduka Mamaduka added the [Type] Enhancement A suggestion for improvement. label Nov 28, 2023
@@ -71,9 +71,9 @@ export function registerFormatType( name, settings ) {
return;
}

if ( ! /^[_a-zA-Z]+[a-zA-Z0-9-]*$/.test( settings.className ) ) {
if ( ! /^[_a-zA-Z]+[a-zA-Z0-9_-]*$/.test( settings.className ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Could we follow the spec here? We should allow any class name that is allowed.

Copy link
Member

Choose a reason for hiding this comment

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

Actually this pretty much follows it https://www.w3.org/TR/CSS21/syndata.html#characters

@ellatrix ellatrix merged commit c6be294 into WordPress:trunk Nov 28, 2023
52 of 55 checks passed
derekblank pushed a commit that referenced this pull request Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Enhancement A suggestion for improvement.
3 participants