-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Fix site editor broken when fontWeight is not defined or is an integer in theme.json or theme styles #64953
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
Fix site editor broken when fontWeight is not defined or is an integer in theme.json or theme styles #64953
Changes from all commits
dbe7731
b9dd96c
ca110e5
47a9f00
3e7955c
c3f423d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,7 +79,10 @@ export function getFontStylesAndWeights( fontFamilyFaces ) { | |
|
|
||
| fontFamilyFaces?.forEach( ( face ) => { | ||
| // Check for variable font by looking for a space in the font weight value. e.g. "100 900" | ||
| if ( /\s/.test( face.fontWeight.trim() ) ) { | ||
| if ( | ||
| 'string' === typeof face.fontWeight && | ||
| /\s/.test( face.fontWeight.trim() ) | ||
| ) { | ||
| isVariableFont = true; | ||
|
|
||
| // Find font weight start and end values. | ||
|
|
@@ -105,11 +108,15 @@ export function getFontStylesAndWeights( fontFamilyFaces ) { | |
| } | ||
|
|
||
| // Format font style and weight values. | ||
| const fontWeight = formatFontWeight( face.fontWeight ); | ||
| const fontWeight = formatFontWeight( | ||
| 'number' === typeof face.fontWeight | ||
| ? face.fontWeight.toString() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we coerce all values to string at the top? I'm thinking, if we make sure all items in the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very good idea @ramonjd ! I actually tried to implement it. But later I stopped, because I was afraid of adding an regression because we're modifying the value fetched from the JSON. So for now, I'm just avoiding doing it in this PR. But WDYT about creating an issue where we properly test all implications of and modify the values at the top? Thank you!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough - probably a premature optimization. Good to have the bug fixed first 😄 |
||
| : face.fontWeight | ||
| ); | ||
| const fontStyle = formatFontStyle( face.fontStyle ); | ||
|
|
||
| // Create font style and font weight lists without duplicates. | ||
| if ( fontStyle ) { | ||
| if ( fontStyle && Object.keys( fontStyle ).length ) { | ||
| if ( | ||
| ! fontStyles.some( | ||
| ( style ) => style.value === fontStyle.value | ||
|
|
@@ -118,7 +125,8 @@ export function getFontStylesAndWeights( fontFamilyFaces ) { | |
| fontStyles.push( fontStyle ); | ||
| } | ||
| } | ||
| if ( fontWeight ) { | ||
|
|
||
| if ( fontWeight && Object.keys( fontWeight ).length ) { | ||
| if ( | ||
| ! fontWeights.some( | ||
| ( weight ) => weight.value === fontWeight.value | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're guarding against extra whitespace for space-separated values, I'm wondering if we should trim all
face.fontWeightstring values, e.g.,There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for checking this and the suggestions @ramonjd ! I thought about writing tests for it right after adding the solution but then decided to put in the effort after we agreed on the solution's approach. As I'm very new to this area, there could be other ways to solve it which could be way better :p So wanted to make sure I'm doing it right.
If we're guarding against extra whitespace for space-separated values, I'm wondering if we should trim all face.fontWeightThis is a very nice idea. My only concern about this is that we'll probably be doing the regex test even on numeric values in the right next line.
Also, the regex test will return true if the value of fontweight variable is an object. It should probably never be an object, but this line of here makes me wonder if it sometimes is an object for some reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, true. Thanks for the explanation 👍🏻
Maybe tests will reveal some optimizations too. For now, it LGTM to me since it fixes a pretty major bug 🚀