Skip to content

Commit

Permalink
Add tests and fix legacy code path
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Oct 20, 2021
1 parent 240504a commit 19dbeee
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/block-supports/typography.php
Expand Up @@ -115,12 +115,11 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
// We don't need this code path when it lands in core.
$font_family_custom = $block_attributes['style']['typography']['fontFamily'];
if ( strpos( $font_family_custom, 'var:preset|font-family' ) !== false ) {
$index_to_splice = strrpos( $font_family_custom, '|' ) + 1;
$font_family_slug = substr( $font_family_custom, $index_to_splice );
$classes[] = sprintf( 'has-%s-font-family', $font_family_slug );
} else {
$styles[] = sprintf( 'font-family: %s;', $font_family_custom );
$index_to_splice = strrpos( $font_family_custom, '|' ) + 1;
$font_family_slug = gutenberg_experimental_to_kebab_case( substr( $font_family_custom, $index_to_splice ) );
$font_family_custom = sprintf( 'var(--wp--preset--font-family--%s)', $font_family_slug );
}
$styles[] = sprintf( 'font-family: %s;', $font_family_custom );
}
}

Expand Down
88 changes: 88 additions & 0 deletions phpunit/block-supports/typography-test.php
Expand Up @@ -36,4 +36,92 @@ function test_font_size_slug_with_numbers_is_kebab_cased_properly() {
$this->assertSame( $expected, $actual );
unregister_block_type( 'test/font-size-slug-with-numbers' );
}

function test_font_family_with_legacy_inline_styles_using_a_value() {
$block_name = 'test/font-family-with-inline-styles-using-value';
register_block_type(
$block_name,
array(
'api_version' => 2,
'attributes' => array(
'style' => array(
'type' => 'object',
),
),
'supports' => array(
'typography' => array(
'__experimentalFontFamily' => true,
),
),
)
);
$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( $block_name );
$block_atts = array( 'style' => array( 'typography' => array( 'fontFamily' => 'serif' ) ) );

$actual = gutenberg_apply_typography_support( $block_type, $block_atts );
$expected = array( 'style' => 'font-family: serif;' );

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

function test_font_family_with_legacy_inline_styles_using_a_css_var() {
$block_name = 'test/font-family-with-inline-styles-using-css-var';
register_block_type(
$block_name,
array(
'api_version' => 2,
'attributes' => array(
'style' => array(
'type' => 'object',
),
),
'supports' => array(
'typography' => array(
'__experimentalFontFamily' => true,
),
),
)
);
$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( $block_name );
$block_atts = array( 'style' => array( 'typography' => array( 'fontFamily' => 'var:preset|font-family|h1' ) ) );

$actual = gutenberg_apply_typography_support( $block_type, $block_atts );
$expected = array( 'style' => 'font-family: var(--wp--preset--font-family--h-1);' );

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

function test_font_family_with_class() {
$block_name = 'test/font-family-with-class';
register_block_type(
$block_name,
array(
'api_version' => 2,
'attributes' => array(
'style' => array(
'type' => 'object',
),
),
'supports' => array(
'typography' => array(
'__experimentalFontFamily' => true,
),
),
)
);
$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( $block_name );
$block_atts = array( 'fontFamily' => 'h1' );

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

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

}

0 comments on commit 19dbeee

Please sign in to comment.