Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
32 changes: 14 additions & 18 deletions src/wp-includes/block-supports/background.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand Down
178 changes: 176 additions & 2 deletions tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ public function test_background_img_element_is_injected_with_no_repeat() {
$this->assertStringContainsString( '<img', $actual, 'no-repeat with contain should still inject an img element.' );
$this->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 );
}
Expand Down Expand Up @@ -632,8 +632,182 @@ public function test_background_img_element_is_injected() {
$this->assertStringContainsString( '<img', $actual, 'Output should contain an img element.' );
$this->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(
'<div>Content</div>',
'position: relative;',
),
'style without position' => array(
'<div style="color:red">Content</div>',
'color:red; position: relative;',
),
'position declaration' => array(
'<div style="color:red;position:absolute">Content</div>',
'color:red;position:absolute',
),
'position declaration in uppercase' => array(
'<div style="POSITION:absolute">Content</div>',
'POSITION:absolute',
),
'position declaration after a comment' => array(
'<div style="color:red;/* note */position:absolute">Content</div>',
'color:red;/* note */position:absolute',
),
'position inside a string' => array(
'<div style=\'font-family:"a;position:b"\'>Content</div>',
'font-family:"a;position:b"; position: relative;',
),
'position inside a comment' => array(
'<div style="color:red;/*;position:absolute*/">Content</div>',
'color:red;/*;position:absolute*/ position: relative;',
),
);
}

/**
* Tests that the background position becomes the IMG object-position when
* it is one CSS declaration value.
*
* @ticket 64725
*
* @dataProvider data_background_img_element_object_position
*
* @param string $position Background position attribute.
* @param string $expected_style Expected IMG style attribute.
* @param string|null $expected_object_position Expected IMG data-object-position attribute.
*/
public function test_background_img_element_object_position( $position, $expected_style, $expected_object_position ) {
$styles = $this->render_background_image_element(
'<div>Content</div>',
array(
'backgroundSize' => 'cover',
'backgroundPosition' => $position,
)
);
$this->assertSame( $expected_style, $styles['img_style'], 'IMG style should be correct.' );
$this->assertSame( $expected_object_position, $styles['img_object_position'], 'IMG data-object-position should be correct.' );
}

/**
* Data provider.
*
* @return array[]
*/
public static function data_background_img_element_object_position() {
$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: cover;';

return array(
'position' => array(
'right 10% bottom 20px',
"{$img_style} object-position: right 10% bottom 20px;",
'right 10% bottom 20px',
),
'position with a semicolon' => array(
'50% 50%; width: 1px',
$img_style,
null,
),
'position with an unclosed block' => array(
'calc(50% + 1px',
$img_style,
null,
),
);
}
}