diff --git a/lib/block-supports/border.php b/lib/block-supports/border.php index 1c09361ba08a40..3646d44ed0df3c 100644 --- a/lib/block-supports/border.php +++ b/lib/block-supports/border.php @@ -44,76 +44,111 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { return array(); } - $border_block_styles = array(); - $has_border_color_support = gutenberg_has_border_feature_support( $block_type, 'color' ); - $has_border_width_support = gutenberg_has_border_feature_support( $block_type, 'width' ); - - // Border radius. - if ( - gutenberg_has_border_feature_support( $block_type, 'radius' ) && - isset( $block_attributes['style']['border']['radius'] ) && - ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' ) - ) { - $border_radius = $block_attributes['style']['border']['radius']; - - if ( is_numeric( $border_radius ) ) { - $border_radius .= 'px'; - } + $has_border_color_support = gutenberg_has_border_feature_support( $block_type, 'color' ); + $has_border_radius_support = gutenberg_has_border_feature_support( $block_type, 'radius' ); + $has_border_style_support = gutenberg_has_border_feature_support( $block_type, 'style' ); + $has_border_width_support = gutenberg_has_border_feature_support( $block_type, 'width' ); + $skip_color = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ); + $skip_radius = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' ); + $skip_style = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ); + $skip_width = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ); + + // The unset() calls below fail on a non-array style or border. + if ( ! isset( $block_attributes['style']['border'] ) || ! is_array( $block_attributes['style']['border'] ) ) { + unset( $block_attributes['style'] ); + } + + if ( ! $has_border_radius_support || $skip_radius ) { + unset( $block_attributes['style']['border']['radius'] ); + } - $border_block_styles['radius'] = $border_radius; + if ( ! $has_border_style_support || $skip_style ) { + unset( $block_attributes['style']['border']['style'] ); } - // Border style. - if ( - gutenberg_has_border_feature_support( $block_type, 'style' ) && - isset( $block_attributes['style']['border']['style'] ) && - ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) - ) { - $border_block_styles['style'] = $block_attributes['style']['border']['style']; + if ( ! $has_border_width_support || $skip_width ) { + unset( $block_attributes['style']['border']['width'] ); } - // Border width. - if ( - $has_border_width_support && - isset( $block_attributes['style']['border']['width'] ) && - ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) - ) { - $border_width = $block_attributes['style']['border']['width']; + if ( ! $has_border_color_support || $skip_color ) { + unset( $block_attributes['style']['border']['color'], $block_attributes['borderColor'] ); + } + + // Side values follow skipped serialization only, as on trunk. + foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { + $has_side = isset( $block_attributes['style']['border'][ $side ] ) && is_array( $block_attributes['style']['border'][ $side ] ); + + if ( ! $has_side || ( ! $has_border_color_support && ! $has_border_width_support ) ) { + unset( $block_attributes['style']['border'][ $side ] ); + continue; + } + + if ( $skip_width ) { + unset( $block_attributes['style']['border'][ $side ]['width'] ); + } - // This check handles original unitless implementation. - if ( is_numeric( $border_width ) ) { - $border_width .= 'px'; + if ( $skip_color ) { + unset( $block_attributes['style']['border'][ $side ]['color'] ); } - $border_block_styles['width'] = $border_width; - } - - // Border color. - if ( - $has_border_color_support && - ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) - ) { - $preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null; - $custom_border_color = $block_attributes['style']['border']['color'] ?? null; - $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color; - } - - // Generate styles for individual border sides. - if ( $has_border_color_support || $has_border_width_support ) { - foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { - $border = $block_attributes['style']['border'][ $side ] ?? null; - $border_side_values = array( - 'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null, - 'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null, - 'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null, - ); - $border_block_styles[ $side ] = $border_side_values; + if ( $skip_style ) { + unset( $block_attributes['style']['border'][ $side ]['style'] ); } } - // Collect classes and styles. + return gutenberg_get_border_classes_and_styles( $block_attributes ); +} + +/** + * Returns border classes and inline styles for block attributes, like the JS + * `getBorderClassesAndStyles()`. Does not check block support or skipped + * serialization. + * + * @since 7.3.0 + * + * @param array $block_attributes Block attributes. + * @return array Array with `class` and `style` keys, each present only when non-empty. + */ +function gutenberg_get_border_classes_and_styles( $block_attributes ) { + if ( ! is_array( $block_attributes ) ) { + return array(); + } + + $border = isset( $block_attributes['style']['border'] ) && is_array( $block_attributes['style']['border'] ) + ? $block_attributes['style']['border'] + : array(); + $border_styles = array(); + + // Unitless radius and width are from the original implementation. + if ( isset( $border['radius'] ) ) { + $border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius']; + } + + if ( isset( $border['style'] ) ) { + $border_styles['style'] = $border['style']; + } + + if ( isset( $border['width'] ) ) { + $border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width']; + } + + // Preset slugs are strings in theme.json and the editor. + $preset_slug = $block_attributes['borderColor'] ?? null; + $border_styles['color'] = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|color|{$preset_slug}" : ( $border['color'] ?? null ); + + foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { + if ( ! isset( $border[ $side ] ) || ! is_array( $border[ $side ] ) ) { + continue; + } + $border_styles[ $side ] = array( + 'width' => $border[ $side ]['width'] ?? null, + 'color' => $border[ $side ]['color'] ?? null, + 'style' => $border[ $side ]['style'] ?? null, + ); + } + $attributes = array(); - $styles = gutenberg_style_engine_get_styles( array( 'border' => $border_block_styles ) ); + $styles = gutenberg_style_engine_get_styles( array( 'border' => $border_styles ) ); if ( ! empty( $styles['classnames'] ) ) { $attributes['class'] = $styles['classnames']; diff --git a/packages/block-library/package.json b/packages/block-library/package.json index f41612153347f3..1a386bceb00bc6 100644 --- a/packages/block-library/package.json +++ b/packages/block-library/package.json @@ -70,6 +70,7 @@ "prefixFunctions": [ "wp_apply_colors_support", "wp_get_color_classes_and_styles", + "wp_get_border_classes_and_styles", "wp_enqueue_block_support_styles", "wp_get_typography_font_size_value", "wp_get_shadow_classes_and_styles", diff --git a/packages/block-library/src/avatar/index.php b/packages/block-library/src/avatar/index.php index 0f56ce461a08f6..f7206c14119ec9 100644 --- a/packages/block-library/src/avatar/index.php +++ b/packages/block-library/src/avatar/index.php @@ -108,48 +108,7 @@ function render_block_core_avatar( $attributes, $content, $block ) { * @return array The border-related classnames and styles for the block. */ function get_block_core_avatar_border_attributes( $attributes ) { - $border_styles = array(); - $sides = array( 'top', 'right', 'bottom', 'left' ); - - // Border radius. - if ( isset( $attributes['style']['border']['radius'] ) ) { - $border_styles['radius'] = $attributes['style']['border']['radius']; - } - - // Border style. - if ( isset( $attributes['style']['border']['style'] ) ) { - $border_styles['style'] = $attributes['style']['border']['style']; - } - - // Border width. - if ( isset( $attributes['style']['border']['width'] ) ) { - $border_styles['width'] = $attributes['style']['border']['width']; - } - - // Border color. - $preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null; - $custom_color = $attributes['style']['border']['color'] ?? null; - $border_styles['color'] = $preset_color ? $preset_color : $custom_color; - - // Individual border styles e.g. top, left etc. - foreach ( $sides as $side ) { - $border = $attributes['style']['border'][ $side ] ?? null; - $border_styles[ $side ] = array( - 'color' => $border['color'] ?? null, - 'style' => $border['style'] ?? null, - 'width' => $border['width'] ?? null, - ); - } - - $styles = wp_style_engine_get_styles( array( 'border' => $border_styles ) ); - $attributes = array(); - if ( ! empty( $styles['classnames'] ) ) { - $attributes['class'] = $styles['classnames']; - } - if ( ! empty( $styles['css'] ) ) { - $attributes['style'] = $styles['css']; - } - return $attributes; + return wp_get_border_classes_and_styles( $attributes ); } /** diff --git a/packages/block-library/src/icon/index.php b/packages/block-library/src/icon/index.php index 017d3d90bbec9e..f06330bfd72257 100644 --- a/packages/block-library/src/icon/index.php +++ b/packages/block-library/src/icon/index.php @@ -46,33 +46,6 @@ function render_block_core_icon( $attributes ) { } $background_styles = wp_get_background_classes_and_styles( array( 'style' => array( 'background' => $background ) ) ); - // Border. - $border_styles = array(); - $sides = array( 'top', 'right', 'bottom', 'left' ); - - if ( isset( $attributes['style']['border']['radius'] ) ) { - $border_styles['radius'] = $attributes['style']['border']['radius']; - } - if ( isset( $attributes['style']['border']['style'] ) ) { - $border_styles['style'] = $attributes['style']['border']['style']; - } - if ( isset( $attributes['style']['border']['width'] ) ) { - $border_styles['width'] = $attributes['style']['border']['width']; - } - - $preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null; - $custom_color = $attributes['style']['border']['color'] ?? null; - $border_styles['color'] = $preset_color ? $preset_color : $custom_color; - - foreach ( $sides as $side ) { - $border = $attributes['style']['border'][ $side ] ?? null; - $border_styles[ $side ] = array( - 'color' => $border['color'] ?? null, - 'style' => $border['style'] ?? null, - 'width' => $border['width'] ?? null, - ); - } - // Shadow. $shadow_styles = $attributes['style']['shadow'] ?? null; @@ -84,16 +57,16 @@ function render_block_core_icon( $attributes ) { } } - // Generate styles and classes. - $styles = wp_style_engine_get_styles( + // Border sits between color and shadow in the style engine's order. + $styles_before_border = wp_style_engine_get_styles( array( 'color' => $color_styles ) ); + $border_styles = wp_get_border_classes_and_styles( $attributes ); + $styles_after_border = wp_style_engine_get_styles( array( - 'color' => $color_styles, - 'border' => $border_styles, 'shadow' => $shadow_styles, 'dimensions' => $dimensions_styles, - ), + ) ); - $spacing_styles = wp_get_spacing_classes_and_styles( + $spacing_styles = wp_get_spacing_classes_and_styles( array( 'style' => array( 'spacing' => array( @@ -102,17 +75,20 @@ function render_block_core_icon( $attributes ) { ), ) ); - $styles['css'] = ( $styles['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' ); + $styles = array( + 'classnames' => implode( ' ', array_filter( array( $styles_before_border['classnames'] ?? '', $border_styles['class'] ?? '', $styles_after_border['classnames'] ?? '' ) ) ), + 'css' => ( $styles_before_border['css'] ?? '' ) . ( $border_styles['style'] ?? '' ) . ( $styles_after_border['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' ), + ); // The style engine outputs background before the other groups, so it leads here too. - $css = ( $background_styles['style'] ?? '' ) . ( $styles['css'] ?? '' ); + $css = ( $background_styles['style'] ?? '' ) . $styles['css']; $svg = wp_get_icon( $attributes['icon'], array( // Width is applied via the dimensions block support styles below. 'size' => null, - 'class' => $styles['classnames'] ?? '', + 'class' => $styles['classnames'], 'label' => $attributes['ariaLabel'] ?? '', ) ); diff --git a/packages/block-library/src/post-featured-image/index.php b/packages/block-library/src/post-featured-image/index.php index 08ac73adff0705..7b4c568f4cb0a1 100644 --- a/packages/block-library/src/post-featured-image/index.php +++ b/packages/block-library/src/post-featured-image/index.php @@ -209,48 +209,7 @@ function get_block_core_post_featured_image_overlay_element_markup( $attributes * @return array The border-related classnames and styles for the block. */ function get_block_core_post_featured_image_border_attributes( $attributes ) { - $border_styles = array(); - $sides = array( 'top', 'right', 'bottom', 'left' ); - - // Border radius. - if ( isset( $attributes['style']['border']['radius'] ) ) { - $border_styles['radius'] = $attributes['style']['border']['radius']; - } - - // Border style. - if ( isset( $attributes['style']['border']['style'] ) ) { - $border_styles['style'] = $attributes['style']['border']['style']; - } - - // Border width. - if ( isset( $attributes['style']['border']['width'] ) ) { - $border_styles['width'] = $attributes['style']['border']['width']; - } - - // Border color. - $preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null; - $custom_color = $attributes['style']['border']['color'] ?? null; - $border_styles['color'] = $preset_color ? $preset_color : $custom_color; - - // Individual border styles e.g. top, left etc. - foreach ( $sides as $side ) { - $border = $attributes['style']['border'][ $side ] ?? null; - $border_styles[ $side ] = array( - 'color' => $border['color'] ?? null, - 'style' => $border['style'] ?? null, - 'width' => $border['width'] ?? null, - ); - } - - $styles = wp_style_engine_get_styles( array( 'border' => $border_styles ) ); - $attributes = array(); - if ( ! empty( $styles['classnames'] ) ) { - $attributes['class'] = $styles['classnames']; - } - if ( ! empty( $styles['css'] ) ) { - $attributes['style'] = $styles['css']; - } - return $attributes; + return wp_get_border_classes_and_styles( $attributes ); } /** diff --git a/packages/block-library/src/post-navigation-link/index.php b/packages/block-library/src/post-navigation-link/index.php index 8e61b4fbbd4850..79ebc4c16025d7 100644 --- a/packages/block-library/src/post-navigation-link/index.php +++ b/packages/block-library/src/post-navigation-link/index.php @@ -166,38 +166,8 @@ function render_block_core_post_navigation_link( $attributes, $content ) { * } */ function block_core_post_navigation_link_get_support_styles( $attributes ) { - $block_styles = $attributes['style'] ?? array(); - $border = $block_styles['border'] ?? array(); - $border_styles = array(); - - // Border radius and width accept unitless numbers from the original implementation. - if ( isset( $border['radius'] ) ) { - $border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius']; - } - - if ( isset( $border['width'] ) ) { - $border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width']; - } - - if ( isset( $border['style'] ) ) { - $border_styles['style'] = $border['style']; - } - - // A preset border color is stored in its own attribute rather than under `style`. - $border_styles['color'] = isset( $attributes['borderColor'] ) - ? "var:preset|color|{$attributes['borderColor']}" - : ( $border['color'] ?? null ); - - // Individual border sides e.g. top, left etc. - foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { - $border_styles[ $side ] = array( - 'width' => $border[ $side ]['width'] ?? null, - 'color' => $border[ $side ]['color'] ?? null, - 'style' => $border[ $side ]['style'] ?? null, - ); - } - - $background = $block_styles['background'] ?? null; + $block_styles = $attributes['style'] ?? array(); + $background = $block_styles['background'] ?? null; // Post Navigation Link has no clip support, so ignore a stale value. if ( is_array( $background ) ) { @@ -205,10 +175,10 @@ function block_core_post_navigation_link_get_support_styles( $attributes ) { } $background_styles = wp_get_background_classes_and_styles( array( 'style' => array( 'background' => $background ) ) ); + $border_styles = wp_get_border_classes_and_styles( $attributes ); $styles = wp_style_engine_get_styles( array( - 'border' => $border_styles, 'dimensions' => array( 'minHeight' => $block_styles['dimensions']['minHeight'] ?? null, 'minWidth' => $block_styles['dimensions']['minWidth'] ?? null, @@ -220,8 +190,8 @@ function block_core_post_navigation_link_get_support_styles( $attributes ) { // The style engine outputs background before the other groups, so it leads here too. return array( - 'class' => trim( ( $background_styles['class'] ?? '' ) . ' ' . ( $styles['classnames'] ?? '' ) ), - 'style' => ( $background_styles['style'] ?? '' ) . ( $styles['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' ), + 'class' => implode( ' ', array_filter( array( $background_styles['class'] ?? '', $border_styles['class'] ?? '', $styles['classnames'] ?? '' ) ) ), + 'style' => ( $background_styles['style'] ?? '' ) . ( $border_styles['style'] ?? '' ) . ( $styles['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' ), ); } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index cb73cab91d85b5..9114e6ce8309ab 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -50,6 +50,12 @@ parameters: message: '#^Function wp_get_background_classes_and_styles not found\.$#' identifier: function.notFound + # wp_get_border_classes_and_styles() is new in WordPress Core: https://github.com/WordPress/wordpress-develop/pull/14094 + # Remove this entry once the WordPress stubs are generated from a Core revision that includes it. + - + message: '#^Function wp_get_border_classes_and_styles not found\.$#' + identifier: function.notFound + # Level 3: # WP_REST_Request implements ArrayAccess, and the WordPress stubs declare its offsetSet() with a method-level # template (`@template TOffset of key-of`, `@param T[TOffset] $value`). PHPStan does not resolve that diff --git a/phpunit/block-supports/border-test.php b/phpunit/block-supports/border-test.php index d6e98314effc60..cc86e8940a0d0c 100644 --- a/phpunit/block-supports/border-test.php +++ b/phpunit/block-supports/border-test.php @@ -184,6 +184,28 @@ public function test_flat_border_with_individual_skipped_serialization() { $this->assertSame( $expected, $actual ); } + public function test_border_color_preset_with_skipped_color_serialization() { + $block_type = self::register_bordered_block_with_support( + 'test/border-color-preset-with-skipped-color-serialization', + array( + '__experimentalBorder' => array( + 'color' => true, + 'width' => true, + '__experimentalSkipSerialization' => array( 'color' ), + ), + ) + ); + $block_attrs = array( + 'borderColor' => 'red', + 'style' => array( 'border' => array( 'width' => '1px' ) ), + ); + + $this->assertSame( + array( 'style' => 'border-width:1px;' ), + gutenberg_apply_border_support( $block_type, $block_attrs ) + ); + } + public function test_split_border_radius() { $block_type = self::register_bordered_block_with_support( 'test/split-border-radius', @@ -458,4 +480,164 @@ public function test_split_borders_with_named_colors() { $this->assertSame( $expected, $actual ); } + + public function test_split_borders_without_color_or_width_support_are_dropped() { + $block_type = self::register_bordered_block_with_support( + 'test/split-borders-without-color-or-width-support', + array( + '__experimentalBorder' => array( + 'radius' => true, + 'style' => true, + ), + ) + ); + $block_attrs = array( + 'style' => array( + 'border' => array( + 'radius' => '5px', + 'top' => array( + 'style' => 'dashed', + 'width' => '2px', + ), + ), + ), + ); + $actual = gutenberg_apply_border_support( $block_type, $block_attrs ); + $expected = array( + 'style' => 'border-radius:5px;', + ); + + $this->assertSame( $expected, $actual ); + } + + public function test_non_array_border_and_sides_are_ignored() { + $block_type = self::register_bordered_block_with_support( + 'test/non-array-border-and-sides', + array( + '__experimentalBorder' => true, + ) + ); + + $this->assertSame( array(), gutenberg_apply_border_support( $block_type, array( 'style' => 'invalid' ) ) ); + $this->assertSame( array(), gutenberg_apply_border_support( $block_type, array( 'style' => array( 'border' => 'invalid' ) ) ) ); + $this->assertSame( + array( 'style' => 'border-bottom-width:1px;' ), + gutenberg_apply_border_support( + $block_type, + array( + 'style' => array( + 'border' => array( + 'top' => 'invalid', + 'bottom' => array( 'width' => '1px' ), + ), + ), + ) + ) + ); + } + + /** + * @covers ::gutenberg_get_border_classes_and_styles + * + * @dataProvider data_get_border_classes_and_styles + * + * @param mixed $block_attributes Block attributes. + * @param array $expected Expected classes and styles. + */ + public function test_get_border_classes_and_styles( $block_attributes, $expected ) { + $this->assertSame( $expected, gutenberg_get_border_classes_and_styles( $block_attributes ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_get_border_classes_and_styles() { + return array( + 'custom' => array( + 'block_attributes' => array( + 'style' => array( + 'border' => array( + 'radius' => '10px', + 'style' => 'solid', + 'width' => '2px', + 'color' => '#ff0000', + 'top' => array( + 'style' => 'dashed', + 'color' => '#00ff00', + 'width' => '3px', + ), + ), + ), + ), + 'expected' => array( + 'class' => 'has-border-color', + 'style' => 'border-color:#ff0000;border-radius:10px;border-style:solid;border-width:2px;border-top-width:3px;border-top-color:#00ff00;border-top-style:dashed;', + ), + ), + 'preset wins over custom' => array( + 'block_attributes' => array( + 'borderColor' => 'red', + 'style' => array( + 'border' => array( + 'color' => '#ff0000', + ), + ), + ), + 'expected' => array( + 'class' => 'has-border-color has-red-border-color', + ), + ), + 'empty preset keeps custom' => array( + 'block_attributes' => array( + 'borderColor' => '', + 'style' => array( + 'border' => array( + 'color' => '#ff0000', + ), + ), + ), + 'expected' => array( + 'class' => 'has-border-color', + 'style' => 'border-color:#ff0000;', + ), + ), + 'non-string preset' => array( + 'block_attributes' => array( + 'borderColor' => 1, + 'style' => array( + 'border' => array( + 'color' => '#ff0000', + ), + ), + ), + 'expected' => array( + 'class' => 'has-border-color', + 'style' => 'border-color:#ff0000;', + ), + ), + 'numeric' => array( + 'block_attributes' => array( + 'style' => array( + 'border' => array( + 'radius' => 5, + 'width' => 2, + ), + ), + ), + 'expected' => array( + 'style' => 'border-radius:5px;border-width:2px;', + ), + ), + 'empty' => array( + 'block_attributes' => array(), + 'expected' => array(), + ), + 'malformed non-array' => array( + 'block_attributes' => 'border', + 'expected' => array(), + ), + ); + } } diff --git a/phpunit/blocks/render-block-avatar-test.php b/phpunit/blocks/render-block-avatar-test.php index 02bf59ae98a297..63289924df169a 100644 --- a/phpunit/blocks/render-block-avatar-test.php +++ b/phpunit/blocks/render-block-avatar-test.php @@ -64,4 +64,34 @@ public function test_shadow_follows_border_on_the_image() { $this->assertSame( 'avatar avatar-96 photo wp-block-avatar__image has-border-color', $actual['class'] ); $this->assertSame( 'border-color:#ff0000;border-radius:10px;box-shadow:1px 1px 1px #000;', $actual['style'] ); } + + public function test_image_border_and_shadow_styles() { + $attributes = array( + 'userId' => self::$user_id, + 'borderColor' => 'accent-2', + 'style' => array( + 'border' => array( + 'radius' => 5, + 'top' => array( + 'style' => 'dashed', + 'width' => '3px', + ), + ), + 'shadow' => '10px 10px 5px #000000', + ), + ); + + $processor = new WP_HTML_Tag_Processor( do_blocks( '' ) ); + $this->assertTrue( $processor->next_tag( 'img' ) ); + $this->assertSame( + array( + 'class' => 'avatar avatar-96 photo wp-block-avatar__image has-border-color has-accent-2-border-color', + 'style' => 'border-radius:5px;border-top-width:3px;border-top-style:dashed;box-shadow:10px 10px 5px #000000;', + ), + array( + 'class' => $processor->get_attribute( 'class' ), + 'style' => $processor->get_attribute( 'style' ), + ) + ); + } } diff --git a/phpunit/blocks/render-block-icon-test.php b/phpunit/blocks/render-block-icon-test.php index b49d25ece90812..bc54e04777a060 100644 --- a/phpunit/blocks/render-block-icon-test.php +++ b/phpunit/blocks/render-block-icon-test.php @@ -193,6 +193,40 @@ public function test_background_ignores_clip() { $this->assertTrue( $processor->has_class( 'has-background' ) ); } + public function test_border_styles_sit_between_color_and_dimensions() { + $attributes = array( + 'icon' => 'core/caution', + 'textColor' => 'vivid-red', + 'borderColor' => 'accent-2', + 'style' => array( + 'color' => array( 'background' => '#eeeeee' ), + 'border' => array( + 'radius' => 5, + 'top' => array( + 'style' => 'dashed', + 'width' => '3px', + ), + ), + 'shadow' => '10px 10px 5px #000000', + 'dimensions' => array( 'width' => '48px' ), + 'spacing' => array( 'padding' => array( 'top' => '4px' ) ), + ), + ); + + $processor = new WP_HTML_Tag_Processor( gutenberg_render_block_core_icon( $attributes ) ); + $this->assertTrue( $processor->next_tag( 'svg' ) ); + $this->assertSame( + array( + 'class' => 'has-text-color has-vivid-red-color has-background has-border-color has-accent-2-border-color', + 'style' => 'fill: none; color:var(--wp--preset--color--vivid-red);background-color:#eeeeee;border-radius:5px;border-top-width:3px;border-top-style:dashed;box-shadow:10px 10px 5px #000000;width:48px;padding-top:4px;', + ), + array( + 'class' => $processor->get_attribute( 'class' ), + 'style' => $processor->get_attribute( 'style' ), + ) + ); + } + public function test_renders_only_icons_in_public_collections() { // Renders public core icon. $processor = new WP_HTML_Tag_Processor( gutenberg_render_block_core_icon( array( 'icon' => 'core/caution' ) ) ); diff --git a/phpunit/blocks/render-block-post-featured-image-test.php b/phpunit/blocks/render-block-post-featured-image-test.php index f24209acc2d340..7e266b627c7a5f 100644 --- a/phpunit/blocks/render-block-post-featured-image-test.php +++ b/phpunit/blocks/render-block-post-featured-image-test.php @@ -75,4 +75,54 @@ public function test_shadow_follows_border_on_the_image() { $this->assertSame( 'has-border-color wp-post-image', $actual['class'] ); $this->assertSame( 'border-color:#ff0000;border-radius:10px;object-fit:cover;box-shadow:1px 1px 1px #000;', $actual['style'] ); } + + public function test_image_and_overlay_border_styles() { + $block = new WP_Block( + array( + 'blockName' => 'core/post-featured-image', + 'attrs' => array( + 'dimRatio' => 50, + 'borderColor' => 'accent-2', + 'style' => array( + 'border' => array( + 'radius' => 5, + 'top' => array( + 'style' => 'dashed', + 'width' => '3px', + ), + ), + 'shadow' => '10px 10px 5px #000000', + ), + ), + ), + array( 'postId' => self::$post_id ) + ); + + $processor = new WP_HTML_Tag_Processor( $block->render() ); + $this->assertTrue( $processor->next_tag( 'img' ) ); + $image = array( + 'class' => $processor->get_attribute( 'class' ), + 'style' => $processor->get_attribute( 'style' ), + ); + $this->assertTrue( $processor->next_tag( 'span' ) ); + $overlay = array( + 'class' => $processor->get_attribute( 'class' ), + 'style' => $processor->get_attribute( 'style' ), + ); + + $this->assertSame( + array( + 'class' => 'has-border-color has-accent-2-border-color wp-post-image', + 'style' => 'border-radius:5px;border-top-width:3px;border-top-style:dashed;object-fit:cover;box-shadow:10px 10px 5px #000000;', + ), + $image + ); + $this->assertSame( + array( + 'class' => 'wp-block-post-featured-image__overlay has-border-color has-accent-2-border-color has-background-dim has-background-dim-50', + 'style' => 'border-radius:5px;border-top-width:3px;border-top-style:dashed', + ), + $overlay + ); + } } diff --git a/phpunit/blocks/render-block-post-navigation-link-test.php b/phpunit/blocks/render-block-post-navigation-link-test.php index 5626e23b9a4437..a7a394e18a6702 100644 --- a/phpunit/blocks/render-block-post-navigation-link-test.php +++ b/phpunit/blocks/render-block-post-navigation-link-test.php @@ -129,6 +129,30 @@ public function test_rendered_link_includes_border_dimensions_shadow_and_spacing $this->assertStringContainsString( 'has-accent-2-border-color', $output ); } + public function test_rendered_link_support_styles_full_markup() { + $attributes = $this->styled_attributes(); + $attributes['style']['border']['radius'] = 5; + $attributes['style']['border']['top'] = array( + 'width' => '4px', + 'color' => '#ff0000', + 'style' => 'solid', + ); + $attributes['style']['background'] = array( 'backgroundImage' => array( 'url' => 'https://example.com/image.jpg' ) ); + + $processor = new WP_HTML_Tag_Processor( $this->render_block( self::$second_post_id, $attributes ) ); + $this->assertTrue( $processor->next_tag( 'div' ) ); + $this->assertSame( + array( + 'class' => 'post-navigation-link-previous has-background has-border-color has-accent-2-border-color wp-block-post-navigation-link', + 'style' => 'background-image:url(\'https://example.com/image.jpg\');background-size:cover;border-radius:5px;border-style:dashed;border-width:3px;border-top-width:4px;border-top-color:#ff0000;border-top-style:solid;box-shadow:10px 10px 5px #000000;min-height:100px;min-width:200px;padding-top:20px;margin-top:30px', + ), + array( + 'class' => $processor->get_attribute( 'class' ), + 'style' => $processor->get_attribute( 'style' ), + ) + ); + } + public function test_individual_border_sides_match_the_core_declaration_order() { $attributes = array( 'type' => 'previous',