diff --git a/src/wp-includes/block-supports/background.php b/src/wp-includes/block-supports/background.php
index a11b9cccf65d5..ec98809387ea9 100644
--- a/src/wp-includes/block-supports/background.php
+++ b/src/wp-includes/block-supports/background.php
@@ -140,14 +140,17 @@ function wp_render_background_support( $block_content, $block ) {
$object_fit = $background_styles['backgroundSize'];
$object_position = $background_styles['backgroundPosition'] ?? null;
- $img_style = 'position:absolute;top:0;left:0;right:0;bottom:0;margin:0;padding:0;width:100%;height:100%;max-width:none;max-height:none;pointer-events:none;object-fit:' . esc_attr( $object_fit ) . ';';
- if ( $object_position ) {
- $img_style .= 'object-position:' . esc_attr( $object_position ) . ';';
+ $img_style = new WP_HTML_Style_Attribute_Processor( 'position:absolute;top:0;left:0;right:0;bottom:0;margin:0;padding:0;width:100%;height:100%;max-width:none;max-height:none;pointer-events:none;' );
+ $img_style->append_declaration( 'object-fit', $object_fit );
+
+ // A position that is not one CSS declaration value is left out.
+ if ( ! is_string( $object_position ) || ! $img_style->append_declaration( 'object-position', $object_position ) ) {
+ $object_position = null;
}
$img_attrs = array(
'class' => 'wp-block__background-image',
- 'style' => $img_style,
+ 'style' => $img_style->get_updated_style(),
'alt' => '',
'aria-hidden' => 'true',
'data-object-fit' => $object_fit,
@@ -163,22 +166,15 @@ function wp_render_background_support( $block_content, $block ) {
if ( $img_html ) {
$tags = new WP_HTML_Tag_Processor( $block_content );
if ( $tags->next_tag() ) {
- $tag_name = strtolower( $tags->get_tag() );
- $existing_style = $tags->get_attribute( 'style' );
-
- if ( is_string( $existing_style ) && '' !== $existing_style ) {
- $separator = str_ends_with( $existing_style, ';' ) ? '' : ';';
- // Only add position:relative when there is no existing position rule.
- if ( ! preg_match( '/(?:^|;)\s*position\s*:/', $existing_style ) ) {
- $wrapper_style = $existing_style . $separator . 'position:relative;';
- } else {
- $wrapper_style = rtrim( $existing_style, ';' ) . ';';
- }
- } else {
- $wrapper_style = 'position:relative;';
+ $tag_name = strtolower( $tags->get_tag() );
+ $wrapper_style = new WP_HTML_Style_Attribute_Processor( $tags->get_attribute( 'style' ) );
+
+ // An existing position declaration is kept.
+ if ( ! $wrapper_style->next_declaration( 'position' ) ) {
+ $wrapper_style->append_declaration( 'position', 'relative' );
}
- $tags->set_attribute( 'style', $wrapper_style );
+ $tags->set_attribute( 'style', $wrapper_style->get_updated_style() );
$tags->add_class( 'has-background' );
}
$modified_content = $tags->get_updated_html();
diff --git a/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php b/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php
index bf054bf0fd57b..5e07ac5176a74 100644
--- a/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php
+++ b/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php
@@ -573,7 +573,7 @@ public function test_background_img_element_is_injected_with_no_repeat() {
$this->assertStringContainsString( 'assertStringNotContainsString( 'background-image:', $actual, 'Output should not contain background-image CSS.' );
$this->assertStringContainsString( 'has-background', $actual, 'Wrapper should have has-background class.' );
- $this->assertStringContainsString( 'position:relative', $actual, 'Wrapper should have position:relative style.' );
+ $this->assertStringContainsString( 'position: relative', $actual, 'Wrapper should have position:relative style.' );
wp_delete_attachment( $attachment_id, true );
}
@@ -632,8 +632,182 @@ public function test_background_img_element_is_injected() {
$this->assertStringContainsString( '
assertStringNotContainsString( 'background-image:', $actual, 'Output should not contain background-image CSS.' );
$this->assertStringContainsString( 'has-background', $actual, 'Wrapper should have has-background class.' );
- $this->assertStringContainsString( 'position:relative', $actual, 'Wrapper should have position:relative style.' );
+ $this->assertStringContainsString( 'position: relative', $actual, 'Wrapper should have position:relative style.' );
wp_delete_attachment( $attachment_id, true );
}
+
+ /**
+ * Renders a block with a media library background image as an IMG element.
+ *
+ * @param string $wrapper Block wrapper markup.
+ * @param array $background Background style attributes, without `backgroundImage`.
+ * @return array {
+ * Styles of the rendered elements.
+ *
+ * @type string|true|null $wrapper_style Wrapper style attribute.
+ * @type string|true|null $img_style IMG style attribute.
+ * @type string|true|null $img_object_position IMG data-object-position attribute.
+ * }
+ */
+ private function render_background_image_element( $wrapper, $background ) {
+ switch_theme( 'block-theme-child-with-fluid-typography' );
+ $this->test_block_name = 'test/background-img-element-style';
+
+ $attachment_id = self::factory()->attachment->create_upload_object(
+ DIR_TESTDATA . '/images/canola.jpg',
+ 0,
+ array( 'post_mime_type' => 'image/jpeg' )
+ );
+
+ register_block_type(
+ $this->test_block_name,
+ array(
+ 'api_version' => 2,
+ 'attributes' => array(
+ 'style' => array(
+ 'type' => 'object',
+ ),
+ ),
+ 'supports' => array(
+ 'background' => array(
+ 'backgroundImage' => true,
+ ),
+ ),
+ )
+ );
+
+ $background['backgroundImage'] = array(
+ 'id' => $attachment_id,
+ 'url' => wp_get_attachment_url( $attachment_id ),
+ );
+
+ $block = array(
+ 'blockName' => $this->test_block_name,
+ 'attrs' => array(
+ 'style' => array(
+ 'background' => $background,
+ ),
+ ),
+ );
+
+ $actual = wp_render_background_support( $wrapper, $block );
+ wp_delete_attachment( $attachment_id, true );
+
+ $processor = new WP_HTML_Tag_Processor( $actual );
+ $processor->next_tag();
+ $wrapper_style = $processor->get_attribute( 'style' );
+ $processor->next_tag( 'IMG' );
+
+ return array(
+ 'wrapper_style' => $wrapper_style,
+ 'img_style' => $processor->get_attribute( 'style' ),
+ 'img_object_position' => $processor->get_attribute( 'data-object-position' ),
+ );
+ }
+
+ /**
+ * Tests that the wrapper gets `position: relative` unless a position
+ * declaration exists.
+ *
+ * @ticket 64725
+ *
+ * @dataProvider data_background_img_element_wrapper_position
+ *
+ * @param string $wrapper Block wrapper markup.
+ * @param string $expected Expected wrapper style attribute.
+ */
+ public function test_background_img_element_wrapper_position( $wrapper, $expected ) {
+ $styles = $this->render_background_image_element( $wrapper, array( 'backgroundSize' => 'cover' ) );
+ $this->assertSame( $expected, $styles['wrapper_style'] );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public static function data_background_img_element_wrapper_position() {
+ return array(
+ 'no style' => array(
+ '