Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'trunk' into 577-revise-dominant-color-styling-and-inlin…
…e-styling
  • Loading branch information
Felix Arntz
Felix Arntz committed Nov 16, 2022
commit c829b21ccfc36af1061038b9309c2cd9d7c77357
17 changes: 8 additions & 9 deletions modules/images/dominant-color/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ function dominant_color_img_tag_add_dominant_color( $filtered_image, $context, $

Comment thread
felixarntz marked this conversation as resolved.
if ( ! empty( $image_meta['dominant_color'] ) ) {
$data .= sprintf( 'data-dominant-color="%s" ', esc_attr( $image_meta['dominant_color'] ) );
$style = '--dominant-color: #' . esc_attr( $image_meta['dominant_color'] ) . '; ';

if ( str_contains( $filtered_image, ' style="' ) ) {
$filtered_image = str_replace( ' style="', ' style="--dominant-color: #' . esc_attr( $image_meta['dominant_color'] ) . '; ', $filtered_image );
} else {
$filtered_image = str_replace( '<img ', '<img style="--dominant-color: #' . esc_attr( $image_meta['dominant_color'] ) . ';" ', $filtered_image );
}
}

if ( isset( $image_meta['has_transparency'] ) ) {
Expand All @@ -131,14 +136,8 @@ function dominant_color_img_tag_add_dominant_color( $filtered_image, $context, $
$extra_class = $image_meta['has_transparency'] ? 'has-transparency' : 'not-transparent';
}

if ( ! empty( $data ) || ! empty( $style ) || ! str_contains( $data, $filtered_image ) ) {
if ( str_contains( $filtered_image, ' style=' ) ) {
$filtered_image = str_replace( ' style="', ' ' . $data . 'style="' . $style, $filtered_image );
} elseif ( ! empty( $style ) ) {
$filtered_image = str_replace( '<img ', '<img ' . $data . 'style="' . $style . '" ', $filtered_image );
} else {
$filtered_image = str_replace( '<img ', '<img ' . $data, $filtered_image );
}
if ( ! empty( $data ) ) {
$filtered_image = str_replace( '<img ', '<img ' . $data, $filtered_image );
}

if ( ! empty( $extra_class ) ) {
Expand Down
42 changes: 42 additions & 0 deletions tests/modules/images/dominant-color/dominant-color-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,48 @@ public function test_dominant_color_img_tag_add_dominant_color_requires_proper_q
$this->assertStringNotContainsString( ' data-dominant-color=', dominant_color_img_tag_add_dominant_color( $img_with_escaped_quotes ) );
}

/**
* Tests that dominant_color_img_tag_add_dominant_color() does not replace existing inline styles.
*
* @dataProvider data_provider_dominant_color_check_inline_style
*
* @covers ::dominant_color_img_tag_add_dominant_color
*
* @param string $filtered_image The filtered image markup.
* Must include `src="%s" width="%d" height="%d"`.
* @param string $expected The expected style attribute and value.
*/
public function test_dominant_color_img_tag_add_dominant_color_should_add_dominant_color_inline_style( $filtered_image, $expected ) {
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/dominant-color/red.jpg' );
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );

list( $src, $width, $height ) = wp_get_attachment_image_src( $attachment_id );

$filtered_image = sprintf( $filtered_image, $src, $width, $height );

$this->assertStringContainsString(
$expected,
dominant_color_img_tag_add_dominant_color( $filtered_image, 'the_content', $attachment_id )
);
}

/**
* Data provider for test_dominant_color_img_tag_add_dominant_color_should_add_dominant_color_inline_style().
*
* @return array[]
*/
public function data_provider_dominant_color_check_inline_style() {
return array(
'no existing inline styles' => array(
'filtered_image' => '<img src="%s" width="%d" height="%d" />',
'expected' => 'style="--dominant-color: #fe0000;"',
),
'existing inline styles' => array(
'filtered_image' => '<img style="color: #ffffff;" src="%s" width="%d" height="%d" />',
'expected' => 'style="--dominant-color: #fe0000; color: #ffffff;"',
),
);
}

/**
* Tests dominant_color_set_image_editors().
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.