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

Add quotes to font family names #476

Merged
merged 9 commits into from
Nov 15, 2023
Merged

Add quotes to font family names #476

merged 9 commits into from
Nov 15, 2023

Conversation

vcanales
Copy link
Member

@vcanales vcanales commented Nov 15, 2023

Checks if the font family name of a given font, either added through Google Fonts or Locally, ends with a number and if so, it adds single quotes to its name. Add single quotes to font family names to avoid errors related to trying to parse font names with special characters, white spaces, or numbers.

Also, check if the font family name of a font being uploaded includes non-alphanumeric characters and invalidates the form if so.

Currently, no validation errors are shown in the UI; I'll be making a note of this to add it in as a follow-up change.

Fixes: #468

Moves the definition of onFormDataChange inside the effect where it's
used so that it won't be re defined on render.

Also, wraps isFormValid on useCallback for the same reason.
@vcanales
Copy link
Member Author

vcanales commented Nov 15, 2023

I've also added a couple of optimizations that I noticed while fixing this:

  1. Refactor promise calls to use async/await syntax, for consistency's sake.
  2. Avoid re renders by wrapping isFormValid with useCallback, and also move the definition of onFormDataChange inside the effect where it's used to avoid having to pass it as a dependency.

Copy link
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

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

This works in my testing for the font Exo 2, but I don't think it'll catch the names of the following fonts:

  • Libre Barcode 128 Text
  • Press Start 2P
  • Rock 3D
  • Rubik 80s Fade

I've left a longer comment about this below, but I'm wondering if it may be best practice to always wrap font names in quotes, as long as the name isn't a generic fallback name (which they're unlikely to be, as they're custom fonts).

Refactor promise calls to use async/await syntax
Avoid re renders by wrapping isFormValid with useCallback,

Nice!

Definitely not a blocker for this PR but it would be great if we could write some tests, especially for things like the utils functions which are hopefully easier to cover in isolation. I know we've not historically done this in this repo - the list of example fonts above made me think that they'd make great test cases.

src/utils.js Outdated Show resolved Hide resolved
src/utils.js Outdated Show resolved Hide resolved
@vcanales
Copy link
Member Author

vcanales commented Nov 15, 2023

@mikachan You're correct in that those cases should also be considered. Here's a few examples:

// Normal
> new FontFace('Example1', {});
< FontFace { family: "Example1", style: "normal", weight: "normal", stretch: "normal", unicodeRange: "U+0-10FFFF", variant: "normal", featureSettings: "normal", variationSettings: "normal", display: "auto", ascentOverride: "normal" }

// Notice the empty family name:
> new FontFace('Example 2', {});
< FontFace { family: "", style: "normal", weight: "normal", stretch: "normal", unicodeRange: "U+0-10FFFF", variant: "normal", featureSettings: "normal", variationSettings: "normal", display: "auto", ascentOverride: "normal" }

// Same, empty family name:
new FontFace('Rubik 80s Fade', {});
FontFace { family: "", style: "normal", weight: "normal", stretch: "normal", unicodeRange: "U+0-10FFFF", variant: "normal", featureSettings: "normal", variationSettings: "normal", display: "auto", ascentOverride: "normal" }

For some reason, fonts that have white spaces and numbers in their name didn't output errors, but they're just as wrong. I'll have addQuotesToName just add quotes and trim names without conditions. I can't seem to find any downsides to doing this.

@vcanales
Copy link
Member Author

Definitely not a blocker for this PR but it would be great if we could write some tests, especially for things like the utils functions which are hopefully easier to cover in isolation. I know we've not historically done this in this repo - the list of example fonts above made me think that they'd make great test cases.

I don't think we even have the tools for this right now, so I'll add it to my to-do for this week. I agree that this change in particular is very well fit for unit tests.

@vcanales vcanales changed the title Add quotes to font family names if they end in number. Add quotes to font family names Nov 15, 2023
Copy link
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

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

We should add the new addQuotesToName function to demoStyle too (around line 56 of local-fonts/index.js), something like this:

		const style = {
			fontFamily: addQuotesToName( formData.name ),
			fontWeight: formData.weight,
			fontStyle: formData.style,
		};

This allows the inline font demo on the local fonts page to apply the font family correctly.

src/local-fonts/index.js Show resolved Hide resolved
Add dependencies required for test and linting as well:

- @wordpress/scripts
- @wordpress/eslint-plugin
Out of scope for this PR; follow-up to come.
@vcanales
Copy link
Member Author

@mikachan I ended up adding tests here; it was simpler than expected. I added @wordpress/wp-scripts as a dependency now, which undoes #467.

@mikachan
Copy link
Member

I ended up adding tests here; it was simpler than expected.

Awesome! This is a great start. With this we can start adding tests in other areas too!

Copy link
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

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

Thanks for working on all of this! I think we should bring this in.

@vcanales vcanales merged commit 4514b96 into trunk Nov 15, 2023
1 check passed
@mikachan mikachan deleted the sanitize-font-family-name branch November 15, 2023 16:13
professionalRuker added a commit to professionalRuker/create-block-theme that referenced this pull request Mar 20, 2024
* Add quotes to families ending in number on gfonts

* Add quotes to uploaded font family name

* Check for special characters on form upload

* Trim font names

* Avoid re-rendering due to function definitions

Moves the definition of onFormDataChange inside the effect where it's
used so that it won't be re defined on render.

Also, wraps isFormValid on useCallback for the same reason.

* Add quotes to all font name strings

WordPress/create-block-theme#476 (comment)

* Add tests

Add dependencies required for test and linting as well:

- @wordpress/scripts
- @wordpress/eslint-plugin

* Temporarily disable eslint rule

Out of scope for this PR; follow-up to come.

* Add quotes to demo style font name
suptechie added a commit to suptechie/Wordpress-block-theme that referenced this pull request Oct 24, 2024
* Add quotes to families ending in number on gfonts

* Add quotes to uploaded font family name

* Check for special characters on form upload

* Trim font names

* Avoid re-rendering due to function definitions

Moves the definition of onFormDataChange inside the effect where it's
used so that it won't be re defined on render.

Also, wraps isFormValid on useCallback for the same reason.

* Add quotes to all font name strings

WordPress/create-block-theme#476 (comment)

* Add tests

Add dependencies required for test and linting as well:

- @wordpress/scripts
- @wordpress/eslint-plugin

* Temporarily disable eslint rule

Out of scope for this PR; follow-up to come.

* Add quotes to demo style font name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fonts with invalid names are not displayed or installed properly
2 participants