gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Creating a new color in multibyte character, the style in editor will be broken.

Open iidastudio opened this issue 4 years ago • 4 comments

Description

When creating a new color in Editor>Style>Pallet>Custom, the editor style is broken if a color name is created using multibyte characters such as Japanese or Emoji.

Especially when the language setting is Japanese, the default name is "color1", which is broken as it is. Japanese default value should be "color1" to disable input of multibyte characters or not break when typed.

Also, the css custom properties created in theme.json (--wp--preset--xxxx,--wp--custom-xxxx) are not output in the above action, which seems to break the style.

Step-by-step reproduction instructions

  1. Editor>Style>Pallet>Custom
  2. Create a new color name with multibyte characters
  3. Save and Reload
  4. Check the editor's style
  5. Try renaming the colors you create. Try also if you set the language to Japanese.

Screenshots, screen recording, code snippet

https://user-images.githubusercontent.com/43173384/156702716-a4ff7148-230b-4ce1-8e20-64651636c1e1.mov

Environment info

WordPress 5.9.1 Gutenberg 12.6.1 Chrome 98.0.4758.109

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

iidastudio avatar Mar 04 '22 05:03 iidastudio

Actually, this bug comes from the CSS parser, here allowed characters for the CSS properties are /^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/; (But in reality, CSS custom properties support emojis and other language characters too)

Parser prop matcher: https://github.com/WordPress/gutenberg/blob/d5915916abc45e6682f4bdb70888aa41e98aa395/packages/block-editor/src/utils/transform-styles/ast/parse.js#L244

I think the easiest solution will be allowing only these /^(\*?[-#\/\*\\\w]+(\[[0-9a-z_-]+\])?)\s*/ characters in the color name field as its being used as CSS custom properties. Thoughts?

delowardev avatar Mar 29 '22 12:03 delowardev

This issue may also be related to #49711.

t-hamano avatar Aug 17 '23 14:08 t-hamano

The root cause of this problem is probably the use of the kebabCase() function in many places where color slugs are generated.

This function is a paramCase in the change-case library and replaces or removes non-Latin characters with hyphens. If all characters are multibyte characters, all characters will be deleted and become empty.

runkit

Here are some of the codes that are causing the problem.

In custom palettes, multibyte characters are deleted when palette labels are changed:

https://github.com/WordPress/gutenberg/blob/ab97d28a55b028139b66b8199d795445d830cad8/packages/components/src/palette-edit/index.tsx#L229

Multibyte characters are removed when generating CSS variables with --wp-preset-- prefix:

https://github.com/WordPress/gutenberg/blob/ab97d28a55b028139b66b8199d795445d830cad8/packages/block-editor/src/components/global-styles/use-global-styles-output.js#L81

I think the easiest solution will be allowing only these /^(*?[-#/*\\w]+([[0-9a-z_-]+])?)\s*/ characters in the color name field as its being used as CSS custom properties. Thoughts?

I hesitate to take the approach of limiting input characters. Users will want to name their custom palettes based on their language. Ideally, I think it should generate a unique and appropriate slug for whatever characters are entered.

t-hamano avatar Sep 10 '23 07:09 t-hamano

Currently custom palettes do not generate a slug from the label, but generate a unified slug called --wp--preset--color--custom-{position}.

japanese-color

This issue is no longer occurring, so I'm closing this issue.

However, if we add a palette via theme.json, we can define any slug and will experience similar issues. This issue is tracked as #49711.

t-hamano avatar Aug 27 '24 12:08 t-hamano