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 class serialization of font-size and colors in dynamic blocks that use block supports #35751

Merged
merged 6 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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/block-supports/colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
}
// Apply color class or inline style.
if ( $has_named_text_color ) {
$classes[] = sprintf( 'has-%s-color', $block_attributes['textColor'] );
$classes[] = sprintf( 'has-%s-color', gutenberg_experimental_to_kebab_case( $block_attributes['textColor'] ) );
} elseif ( $has_custom_text_color ) {
$styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
}
Expand All @@ -109,7 +109,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
}
// Apply background color classes or styles.
if ( $has_named_background_color ) {
$classes[] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] );
$classes[] = sprintf( 'has-%s-background-color', gutenberg_experimental_to_kebab_case( $block_attributes['backgroundColor'] ) );
} elseif ( $has_custom_background_color ) {
$styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
}
Expand All @@ -125,7 +125,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
}
// Apply required background class.
if ( $has_named_gradient ) {
$classes[] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] );
$classes[] = sprintf( 'has-%s-gradient-background', gutenberg_experimental_to_kebab_case( $block_attributes['gradient'] ) );
} elseif ( $has_custom_gradient ) {
$styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
}
Expand Down
2 changes: 1 addition & 1 deletion lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
$has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] );

if ( $has_named_font_size ) {
$classes[] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] );
$classes[] = sprintf( 'has-%s-font-size', gutenberg_experimental_to_kebab_case( $block_attributes['fontSize'] ) );
} elseif ( $has_custom_font_size ) {
$styles[] = sprintf( 'font-size: %s;', $block_attributes['style']['typography']['fontSize'] );
}
Expand Down
50 changes: 50 additions & 0 deletions phpunit/block-supports/colors-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* Test the typography block supports.
*
* @package Gutenberg
*/

class WP_Block_Supports_Colors_Test extends WP_UnitTestCase {

function test_color_slugs_with_numbers_are_kebab_cased_properly() {
register_block_type(
oandregal marked this conversation as resolved.
Show resolved Hide resolved
'test/color-slug-with-numbers',
array(
'api_version' => 2,
'attributes' => array(
'textColor' => array(
'type' => 'string',
),
'backgroundColor' => array(
'type' => 'string',
),
'gradient' => array(
'type' => 'string',
),
),
'supports' => array(
'color' => array(
'text' => true,
'background' => true,
'gradients' => true,
),
),
)
);
$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( 'test/color-slug-with-numbers' );

$block_atts = array(
'textColor' => 'fg1',
'backgroundColor' => 'bg2',
'gradient' => 'gr3',
);

$actual = gutenberg_apply_colors_support( $block_type, $block_atts );
$expected = array( 'class' => 'has-text-color has-fg-1-color has-background has-bg-2-background-color has-background has-gr-3-gradient-background' );

$this->assertSame( $expected, $actual );
}
}
38 changes: 38 additions & 0 deletions phpunit/block-supports/typography-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* Test the typography block supports.
*
* @package Gutenberg
*/

class WP_Block_Supports_Typography_Test extends WP_UnitTestCase {

function test_font_size_slug_with_numbers_is_kebab_cased_properly() {
register_block_type(
oandregal marked this conversation as resolved.
Show resolved Hide resolved
'test/font-size-slug-with-numbers',
array(
'api_version' => 2,
'attributes' => array(
'fontSize' => array(
'type' => 'string',
),
),
'supports' => array(
'typography' => array(
'fontSize' => true,
),
),
)
);
$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( 'test/font-size-slug-with-numbers' );

$block_atts = array( 'fontSize' => 'h1' );

$actual = gutenberg_apply_typography_support( $block_type, $block_atts );
$expected = array( 'class' => 'has-h-1-font-size' );

$this->assertSame( $expected, $actual );
}
}