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

Fix error in with handling spacing preset slugs #43237

Merged
merged 5 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -806,7 +806,7 @@ function data_generate_spacing_scale_fixtures() {
'expected_output' => array(
array(
'name' => 'Medium',
'slug' => 50,
'slug' => '50',
'size' => '4rem',
),
),
Expand All @@ -823,12 +823,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 @@ -845,17 +845,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 @@ -872,22 +872,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 @@ -904,27 +904,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 @@ -941,27 +941,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 @@ -978,27 +978,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