Skip to content

Commit bf853ec

Browse files
Address review feedback
1 parent e2d29dc commit bf853ec

6 files changed

Lines changed: 59 additions & 19 deletions

File tree

‎lib/block-supports/background.php‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ function gutenberg_register_background_support( $block_type ) {
3131
}
3232

3333
/**
34-
* Gets the background classes and styles for a block's attributes.
34+
* Returns background classes and inline styles for block attributes, like the JS
35+
* `getBackgroundClassesAndStyles()`. Does not check block support or skipped
36+
* serialization.
3537
*
3638
* @since 7.2.0
3739
*
3840
* @param array $block_attributes Block attributes.
39-
* @return array Background `class` and `style` strings, each present only when non-empty.
41+
* @return array Array with `class` and `style` keys, each present only when non-empty.
4042
*/
4143
function gutenberg_get_background_classes_and_styles( $block_attributes ) {
4244
$background = $block_attributes['style']['background'] ?? null;
@@ -57,7 +59,6 @@ function gutenberg_get_background_classes_and_styles( $block_attributes ) {
5759

5860
if ( ! empty( $background_styles['backgroundImage'] ) ) {
5961
$background_styles['backgroundSize'] = $background_styles['backgroundSize'] ?? 'cover';
60-
// If the background size is set to `contain` and no position is set, set the position to `center`.
6162
if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
6263
$background_styles['backgroundPosition'] = '50% 50%';
6364
}
@@ -69,21 +70,18 @@ function gutenberg_get_background_classes_and_styles( $block_attributes ) {
6970
return array();
7071
}
7172

72-
$attributes = array();
73+
$classes_and_styles = array();
7374

74-
/*
75-
* A background clipped to the text paints the glyphs rather than the
76-
* block's box, so there is no block background to announce.
77-
*/
7875
$has_background = ! empty( $background_styles['backgroundImage'] ) || ! empty( $background_styles['gradient'] );
7976

77+
// A text clip paints the glyphs, not the box, so there is no block background.
8078
if ( $has_background && 'text' !== $background_styles['backgroundClip'] ) {
81-
$attributes['class'] = 'has-background';
79+
$classes_and_styles['class'] = 'has-background';
8280
}
8381

84-
$attributes['style'] = $styles['css'];
82+
$classes_and_styles['style'] = $styles['css'];
8583

86-
return $attributes;
84+
return $classes_and_styles;
8785
}
8886

8987
/**
@@ -136,7 +134,7 @@ function gutenberg_render_background_support( $block_content, $block ) {
136134
$background_styles['backgroundClip'] = $block_attributes['style']['background']['backgroundClip'] ?? null;
137135
}
138136

139-
// Pass only serialized values, so a skipped or unsupported clip cannot suppress `has-background`.
137+
// Serialized values only, so a skipped clip cannot drop `has-background`.
140138
$styles = gutenberg_get_background_classes_and_styles(
141139
array( 'style' => array( 'background' => $background_styles ) )
142140
);

‎packages/block-editor/src/hooks/use-background-props.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function getBackgroundClassesAndStyles( attributes ) {
2020
const { style } = attributes;
2121
const backgroundStyles = style?.background || {};
2222

23-
// Mirrors gutenberg_render_background_support() in lib/block-supports/background.php.
23+
// Mirrors gutenberg_get_background_classes_and_styles() in lib/block-supports/background.php.
2424
const className =
2525
'text' !== backgroundStyles.backgroundClip
2626
? getBackgroundImageClasses( style )

‎packages/block-library/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
"wp_get_typography_font_size_value",
7474
"wp_style_engine_get_styles",
7575
"wp_get_global_styles",
76-
"wp_get_global_settings",
77-
"wp_get_background_classes_and_styles"
76+
"wp_get_background_classes_and_styles",
77+
"wp_get_global_settings"
7878
],
7979
"suffixClasses": [
8080
"WP_Navigation_Block_Renderer"

‎packages/block-library/src/button/index.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function render_block_core_button( $attributes, $content ) {
6464
$background = $attributes['style']['background'] ?? null;
6565

6666
if ( is_array( $background ) ) {
67-
// Button does not support the clip.
67+
// Button has no clip support, so ignore a stale value.
6868
unset( $background['backgroundClip'] );
6969
$styles = wp_get_background_classes_and_styles( array( 'style' => array( 'background' => $background ) ) );
7070

‎phpunit/block-supports/background-test.php‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,23 @@ public function data_background_block_support() {
271271
'expected_wrapper' => '<div class="has-background" style="background-image:linear-gradient(135deg,hsl(0,100%,50%) 0%,hsl(240,100%,50%) 100%), url(' . $apos . 'https://example.com/image.jpg' . $apos . ');background-size:cover;">Content</div>',
272272
'wrapper' => '<div>Content</div>',
273273
),
274+
'background gradient is applied without the image when the image skips serialization' => array(
275+
'theme_name' => 'block-theme-child-with-fluid-typography',
276+
'block_name' => 'test/background-image-skipped-gradient-applied',
277+
'background_settings' => array(
278+
'backgroundImage' => true,
279+
'gradient' => true,
280+
'__experimentalSkipSerialization' => array( 'backgroundImage' ),
281+
),
282+
'background_style' => array(
283+
'backgroundImage' => array(
284+
'url' => 'https://example.com/image.jpg',
285+
),
286+
'gradient' => 'linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%)',
287+
),
288+
'expected_wrapper' => '<div class="has-background" style="background-image:linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%);">Content</div>',
289+
'wrapper' => '<div>Content</div>',
290+
),
274291
'background image style is not applied if the block does not support background image' => array(
275292
'theme_name' => 'block-theme-child-with-fluid-typography',
276293
'block_name' => 'test/background-rules-are-not-output',
@@ -478,6 +495,32 @@ public function data_get_background_classes_and_styles() {
478495
'style' => 'background-clip:padding-box;-webkit-text-fill-color:currentColor;',
479496
),
480497
),
498+
'image clipped to text has no class' => array(
499+
'block_attributes' => array(
500+
'style' => array(
501+
'background' => array(
502+
'backgroundImage' => array( 'url' => 'https://example.com/image.jpg' ),
503+
'backgroundClip' => 'text',
504+
),
505+
),
506+
),
507+
'expected' => array(
508+
'style' => "background-image:url('https://example.com/image.jpg');background-size:cover;background-clip:text;-webkit-background-clip:text;-webkit-text-fill-color:transparent;",
509+
),
510+
),
511+
'image without url gets default size' => array(
512+
'block_attributes' => array(
513+
'style' => array(
514+
'background' => array(
515+
'backgroundImage' => array( 'id' => 1 ),
516+
),
517+
),
518+
),
519+
'expected' => array(
520+
'class' => 'has-background',
521+
'style' => 'background-size:cover;',
522+
),
523+
),
481524
'numeric position is not output' => array(
482525
'block_attributes' => array(
483526
'style' => array(

‎phpunit/blocks/render-block-button-test.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function test_background_gradient_preset_applies_to_link() {
203203
/**
204204
* @covers ::gutenberg_render_block_core_button
205205
*/
206-
public function test_background_image_and_gradient_full_markup() {
206+
public function test_background_ignores_clip_and_applies_contain_defaults() {
207207
$attributes = array(
208208
'style' => array(
209209
'background' => array(
@@ -219,8 +219,7 @@ public function test_background_image_and_gradient_full_markup() {
219219
),
220220
);
221221

222-
$result = gutenberg_render_block_core_button( $attributes, self::$button_content );
223-
// WP 6.9+ encodes the apostrophe as &apos;, earlier versions as &#039;.
222+
$result = gutenberg_render_block_core_button( $attributes, self::$button_content );
224223
$apostrophe = function_exists( 'get_block_bindings_supported_attributes' ) ? '&apos;' : '&#039;';
225224

226225
$this->assertSame(

0 commit comments

Comments
 (0)