Skip to content

Commit ba42713

Browse files
Editor: fix fluid font division by zero error when min and max viewport widths are equal.
Fixes a division error by returning null when `minViewportWidth` - `maxViewportWidth` is zero in `wp_get_computed_fluid_typography_value`. Props ramonopoly, mukesh27, andrewserong, audrasjb. Fixes #60263. git-svn-id: https://develop.svn.wordpress.org/trunk@57329 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c9f04d4 commit ba42713

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/wp-includes/block-supports/typography.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ function wp_get_typography_value_and_unit( $raw_value, $options = array() ) {
398398
*
399399
* @since 6.1.0
400400
* @since 6.3.0 Checks for unsupported min/max viewport values that cause invalid clamp values.
401+
* @since 6.5.0 Returns early when min and max viewport subtraction is zero to avoid division by zero.
401402
* @access private
402403
*
403404
* @param array $args {
@@ -468,12 +469,18 @@ function wp_get_computed_fluid_typography_value( $args = array() ) {
468469
return null;
469470
}
470471

472+
// Calculates the linear factor denominator. If it's 0, we cannot calculate a fluid value.
473+
$linear_factor_denominator = $maximum_viewport_width['value'] - $minimum_viewport_width['value'];
474+
if ( empty( $linear_factor_denominator ) ) {
475+
return null;
476+
}
477+
471478
/*
472479
* Build CSS rule.
473480
* Borrowed from https://websemantics.uk/tools/responsive-font-calculator/.
474481
*/
475482
$view_port_width_offset = round( $minimum_viewport_width['value'] / 100, 3 ) . $font_size_unit;
476-
$linear_factor = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $maximum_viewport_width['value'] - $minimum_viewport_width['value'] ) );
483+
$linear_factor = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $linear_factor_denominator ) );
477484
$linear_factor_scaled = round( $linear_factor * $scale_factor, 3 );
478485
$linear_factor_scaled = empty( $linear_factor_scaled ) ? 1 : $linear_factor_scaled;
479486
$fluid_target_font_size = implode( '', $minimum_font_size_rem ) . " + ((1vw - $view_port_width_offset) * $linear_factor_scaled)";

tests/phpunit/tests/block-supports/typography.php

+11
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ public function data_invalid_size_wp_get_typography_value_and_unit() {
913913
* Tests computed font size values.
914914
*
915915
* @ticket 58522
916+
* @ticket 60263
916917
*
917918
* @covers ::wp_get_computed_fluid_typography_value
918919
*
@@ -951,6 +952,16 @@ public function data_wp_get_computed_fluid_typography_value() {
951952
),
952953
'expected_output' => 'clamp(50px, 3.125rem + ((1vw - 3.2px) * 7.353), 100px)',
953954
),
955+
'returns `null` when maximum and minimum viewport width are equal' => array(
956+
'args' => array(
957+
'minimum_viewport_width' => '800px',
958+
'maximum_viewport_width' => '800px',
959+
'minimum_font_size' => '50px',
960+
'maximum_font_size' => '100px',
961+
'scale_factor' => 1,
962+
),
963+
'expected_output' => null,
964+
),
954965
'returns `null` when `maximum_viewport_width` is an unsupported unit' => array(
955966
'args' => array(
956967
'minimum_viewport_width' => '320px',

0 commit comments

Comments
 (0)