Skip to content

Commit

Permalink
Fix error in with handling spacing preset slugs (#43237)
Browse files Browse the repository at this point in the history
  • Loading branch information
glendaviesnz authored Aug 18, 2022
1 parent 1ec3395 commit a9ae1b7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 33 deletions.
6 changes: 3 additions & 3 deletions lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ public function set_spacing_sizes() {
$below_sizes[] = array(
/* translators: %s: Multiple of t-shirt sizing, eg. 2X-Small */
'name' => $x === $steps_mid_point - 1 ? __( 'Small', 'gutenberg' ) : sprintf( __( '%sX-Small', 'gutenberg' ), strval( $x_small_count ) ),
'slug' => $slug,
'slug' => (string) $slug,
'size' => round( $current_step, 2 ) . $unit,
);

Expand All @@ -1231,7 +1231,7 @@ public function set_spacing_sizes() {

$below_sizes[] = array(
'name' => __( 'Medium', 'gutenberg' ),
'slug' => 50,
'slug' => '50',
'size' => $spacing_scale['mediumStep'] . $unit,
);

Expand All @@ -1249,7 +1249,7 @@ public function set_spacing_sizes() {
$above_sizes[] = array(
/* translators: %s: Multiple of t-shirt sizing, eg. 2X-Large */
'name' => 0 === $x ? __( 'Large', 'gutenberg' ) : sprintf( __( '%sX-Large', 'gutenberg' ), strval( $x_large_count ) ),
'slug' => $slug,
'slug' => (string) $slug,
'size' => round( $current_step, 2 ) . $unit,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe( 'getSpacingPresetSlug', () => {
expect( getSpacingPresetSlug( 'default' ) ).toBe( 'default' );
} );
it( 'should return the int value of the slug portion of a valid preset var', () => {
expect( getSpacingPresetSlug( 'var:preset|spacing|20' ) ).toBe( 20 );
expect( getSpacingPresetSlug( 'var:preset|spacing|20' ) ).toBe( '20' );
} );
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export function getCustomValueFromPreset( value, spacingSizes ) {
}

const slug = getSpacingPresetSlug( value );
const spacingSize = spacingSizes.find( ( size ) => size.slug === slug );
const spacingSize = spacingSizes.find(
( size ) => String( size.slug ) === slug
);

return spacingSize?.size;
}
Expand Down Expand Up @@ -80,7 +82,7 @@ export function getSpacingPresetSlug( value ) {

const slug = value.match( /var:preset\|spacing\|(.+)/ );

return slug ? parseInt( slug[ 1 ], 10 ) : undefined;
return slug ? slug[ 1 ] : undefined;
}

/**
Expand All @@ -100,7 +102,7 @@ export function getSliderValueFromPreset( presetValue, spacingSizes ) {
? '0'
: getSpacingPresetSlug( presetValue );
const sliderValue = spacingSizes.findIndex( ( spacingSize ) => {
return spacingSize.slug === slug;
return String( spacingSize.slug ) === slug;
} );

// Returning NaN rather than undefined as undefined makes range control thumb sit in center
Expand Down
50 changes: 25 additions & 25 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ function data_generate_spacing_scale_fixtures() {
'expected_output' => array(
array(
'name' => 'Medium',
'slug' => 50,
'slug' => '50',
'size' => '4rem',
),
),
Expand All @@ -797,12 +797,12 @@ function data_generate_spacing_scale_fixtures() {
'expected_output' => array(
array(
'name' => 'Medium',
'slug' => 50,
'slug' => '50',
'size' => '4rem',
),
array(
'name' => 'Large',
'slug' => 60,
'slug' => '60',
'size' => '5.5rem',
),
),
Expand All @@ -819,17 +819,17 @@ function data_generate_spacing_scale_fixtures() {
'expected_output' => array(
array(
'name' => 'Small',
'slug' => 40,
'slug' => '40',
'size' => '2.5rem',
),
array(
'name' => 'Medium',
'slug' => 50,
'slug' => '50',
'size' => '4rem',
),
array(
'name' => 'Large',
'slug' => 60,
'slug' => '60',
'size' => '5.5rem',
),
),
Expand All @@ -846,22 +846,22 @@ function data_generate_spacing_scale_fixtures() {
'expected_output' => array(
array(
'name' => 'Small',
'slug' => 40,
'slug' => '40',
'size' => '2.5rem',
),
array(
'name' => 'Medium',
'slug' => 50,
'slug' => '50',
'size' => '4rem',
),
array(
'name' => 'Large',
'slug' => 60,
'slug' => '60',
'size' => '5.5rem',
),
array(
'name' => 'X-Large',
'slug' => 70,
'slug' => '70',
'size' => '7rem',
),
),
Expand All @@ -878,27 +878,27 @@ function data_generate_spacing_scale_fixtures() {
'expected_output' => array(
array(
'name' => 'Small',
'slug' => 40,
'slug' => '40',
'size' => '2.5rem',
),
array(
'name' => 'Medium',
'slug' => 50,
'slug' => '50',
'size' => '5rem',
),
array(
'name' => 'Large',
'slug' => 60,
'slug' => '60',
'size' => '7.5rem',
),
array(
'name' => 'X-Large',
'slug' => 70,
'slug' => '70',
'size' => '10rem',
),
array(
'name' => '2X-Large',
'slug' => 80,
'slug' => '80',
'size' => '12.5rem',
),
),
Expand All @@ -915,27 +915,27 @@ function data_generate_spacing_scale_fixtures() {
'expected_output' => array(
array(
'name' => 'X-Small',
'slug' => 30,
'slug' => '30',
'size' => '0.67rem',
),
array(
'name' => 'Small',
'slug' => 40,
'slug' => '40',
'size' => '1rem',
),
array(
'name' => 'Medium',
'slug' => 50,
'slug' => '50',
'size' => '1.5rem',
),
array(
'name' => 'Large',
'slug' => 60,
'slug' => '60',
'size' => '2.25rem',
),
array(
'name' => 'X-Large',
'slug' => 70,
'slug' => '70',
'size' => '3.38rem',
),
),
Expand All @@ -952,27 +952,27 @@ function data_generate_spacing_scale_fixtures() {
'expected_output' => array(
array(
'name' => 'X-Small',
'slug' => 30,
'slug' => '30',
'size' => '0.09rem',
),
array(
'name' => 'Small',
'slug' => 40,
'slug' => '40',
'size' => '0.38rem',
),
array(
'name' => 'Medium',
'slug' => 50,
'slug' => '50',
'size' => '1.5rem',
),
array(
'name' => 'Large',
'slug' => 60,
'slug' => '60',
'size' => '6rem',
),
array(
'name' => 'X-Large',
'slug' => 70,
'slug' => '70',
'size' => '24rem',
),
),
Expand Down
2 changes: 1 addition & 1 deletion schemas/json/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
"type": "string"
},
"slug": {
"description": "Kebab-case unique identifier for the space size preset.",
"description": "Unique identifier for the space size preset. For best cross theme compatibility these should be in the form '10','20','30','40','50','60', etc. with '50' representing the 'Medium' size step.",
"type": "string"
},
"size": {
Expand Down

0 comments on commit a9ae1b7

Please sign in to comment.