Skip to content

Commit

Permalink
fix(next/font): update axes error logic (#72442)
Browse files Browse the repository at this point in the history
## Why?

The current error message when you have have a set `weight: ['400', '700']` and set `axes: ['wdth']` for a variable font is confusing—it should mention to correct your weight property (e.g., `weight: 'variable'` or nothing at all for `weight`).

- Fixes #72413
  • Loading branch information
samcx authored Nov 7, 2024
1 parent 49ac101 commit a422054
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 28 deletions.
101 changes: 76 additions & 25 deletions crates/next-core/src/next_font/google/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ pub(super) fn options_from_request(
})
.unwrap_or_default();

let supports_variable_weight = font_data.weights.iter().any(|el| el == "variable");
let weights = if requested_weights.is_empty() {
if !font_data.weights.contains(&"variable".into()) {
if !supports_variable_weight {
return Err(anyhow!(
"Missing weight for {}. Available weights: {}",
font_family,
Expand Down Expand Up @@ -170,8 +171,17 @@ pub(super) fn options_from_request(
}

if let Some(axes) = argument.axes.as_ref() {
if !axes.is_empty() && !matches!(weights, FontWeights::Variable) {
return Err(anyhow!("Axes can only be defined for variable fonts"));
if !axes.is_empty() {
if !supports_variable_weight {
return Err(anyhow!("Axes can only be defined for variable fonts."));
}

if weights != FontWeights::Variable {
return Err(anyhow!(
"Axes can only be defined for variable fonts when the weight property is \
nonexistent or set to `variable`."
));
}
}
}

Expand Down Expand Up @@ -208,7 +218,7 @@ mod tests {
"styles": ["normal", "italic"]
}
}
"#,
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
Expand All @@ -219,7 +229,7 @@ mod tests {
"variableName": "inter",
"arguments": [{}]
}
"#,
"#,
)?;

match options_from_request(&request, &data) {
Expand All @@ -241,7 +251,7 @@ mod tests {
"styles": ["normal", "italic"]
}
}
"#,
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
Expand All @@ -252,7 +262,7 @@ mod tests {
"variableName": "abeezee",
"arguments": []
}
"#,
"#,
)?;

assert_eq!(
Expand Down Expand Up @@ -284,7 +294,7 @@ mod tests {
"styles": ["normal", "italic"]
}
}
"#,
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
Expand All @@ -295,7 +305,7 @@ mod tests {
"variableName": "abeezee",
"arguments": [{}]
}
"#,
"#,
)?;

match options_from_request(&request, &data) {
Expand All @@ -320,7 +330,7 @@ mod tests {
"styles": ["normal", "italic"]
}
}
"#,
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
Expand All @@ -333,7 +343,7 @@ mod tests {
"weight": ["400", "variable"]
}]
}
"#,
"#,
)?;

match options_from_request(&request, &data) {
Expand All @@ -359,7 +369,7 @@ mod tests {
"styles": ["normal", "italic"]
}
}
"#,
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
Expand All @@ -372,7 +382,7 @@ mod tests {
"weight": ["200"]
}]
}
"#,
"#,
)?;

match options_from_request(&request, &data) {
Expand All @@ -397,7 +407,7 @@ mod tests {
"styles": ["italic"]
}
}
"#,
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
Expand All @@ -410,7 +420,7 @@ mod tests {
"weight": ["400"]
}]
}
"#,
"#,
)?;

let options = options_from_request(&request, &data)?;
Expand All @@ -429,7 +439,7 @@ mod tests {
"styles": ["normal", "italic"]
}
}
"#,
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
Expand All @@ -442,7 +452,7 @@ mod tests {
"weight": ["400"]
}]
}
"#,
"#,
)?;

let options = options_from_request(&request, &data)?;
Expand All @@ -461,7 +471,7 @@ mod tests {
"styles": ["normal", "italic"]
}
}
"#,
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
Expand All @@ -475,7 +485,7 @@ mod tests {
"style": ["foo"]
}]
}
"#,
"#,
)?;

match options_from_request(&request, &data) {
Expand All @@ -501,7 +511,7 @@ mod tests {
"styles": ["normal", "italic"]
}
}
"#,
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
Expand All @@ -515,7 +525,7 @@ mod tests {
"display": "foo"
}]
}
"#,
"#,
)?;

match options_from_request(&request, &data) {
Expand All @@ -533,7 +543,7 @@ mod tests {
}

#[test]
fn test_errors_on_axes_without_variable() -> Result<()> {
fn test_errors_on_axes_without_variable_weight() -> Result<()> {
let data: FxIndexMap<RcStr, FontDataEntry> = parse_json_with_source_context(
r#"
{
Expand All @@ -542,7 +552,7 @@ mod tests {
"styles": ["normal", "italic"]
}
}
"#,
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
Expand All @@ -556,15 +566,56 @@ mod tests {
"axes": ["wght"]
}]
}
"#,
"#,
)?;

match options_from_request(&request, &data) {
Ok(_) => panic!(),
Err(err) => {
assert_eq!(
err.to_string(),
"Axes can only be defined for variable fonts when the weight property is \
nonexistent or set to `variable`."
)
}
}

Ok(())
}

#[test]
fn test_errors_on_axes_without_variable_font() -> Result<()> {
let data: FxIndexMap<RcStr, FontDataEntry> = parse_json_with_source_context(
r#"
{
"ABeeZee": {
"weights": ["400", "700"],
"styles": ["normal", "italic"]
}
}
"#,
)?;

let request: NextFontRequest = parse_json_with_source_context(
r#"
{
"import": "ABeeZee",
"path": "index.js",
"variableName": "abeezee",
"arguments": [{
"weight": ["400", "700"],
"axes": ["wght"]
}]
}
"#,
)?;

match options_from_request(&request, &data) {
Ok(_) => panic!(),
Err(err) => {
assert_eq!(
err.to_string(),
"Axes can only be defined for variable fonts"
"Axes can only be defined for variable fonts."
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,19 @@ describe('validateFontFunctionCall errors', () => {
subsets: ['latin'],
})
).toThrowErrorMatchingInlineSnapshot(
`"Axes can only be defined for variable fonts"`
`"Axes can only be defined for variable fonts."`
)
})

test('Setting axes on variable font with incorrect weight', async () => {
expect(() =>
validateGoogleFontFunctionCall('Roboto_Flex', {
weight: ['400', '700'],
axes: ['wght'],
subsets: ['latin'],
})
).toThrowErrorMatchingInlineSnapshot(
'"Axes can only be defined for variable fonts when the weight property is nonexistent or set to `variable`."'
)
})
})
12 changes: 10 additions & 2 deletions packages/font/src/google/validate-google-font-function-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,16 @@ export function validateGoogleFontFunctionCall(
)
}

if (weights[0] !== 'variable' && axes) {
nextFontError('Axes can only be defined for variable fonts')
if (axes) {
if (!fontWeights.includes('variable')) {
nextFontError('Axes can only be defined for variable fonts.')
}

if (weights[0] !== 'variable') {
nextFontError(
'Axes can only be defined for variable fonts when the weight property is nonexistent or set to `variable`.'
)
}
}

return {
Expand Down

0 comments on commit a422054

Please sign in to comment.