Repository navigation
Use more robust HTML Tag Processor for Image Placeholders - #1477
westonruter merged 9 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Output for Image with picture element turned off: <img
data-dominant-color="788d7e"
data-has-transparency="false"
style="--dominant-color: #788d7e;"
fetchpriority="high"
decoding="async"
width="1024"
height="711"
src="http://localhost:8888/wp-content/uploads/2024/08/IMG-1-jpeg.webp"
alt=""
class="wp-image-13 not-transparent"
srcset="
http://localhost:8888/wp-content/uploads/2024/08/IMG-1-jpeg.webp 1024w,
http://localhost:8888/wp-content/uploads/2024/08/IMG-1-300x208-jpeg.webp 300w,
http://localhost:8888/wp-content/uploads/2024/08/IMG-1-768x533-jpeg.webp 768w"
sizes="(max-width: 620px) 100vw, 620px"
>With the picture element turned on: <picture
class="wp-picture-13"
style="display: contents;">
<source
type="image/webp"
srcset="
http://localhost:8888/wp-content/uploads/2024/08/IMG-1-jpeg.webp 1024w,
http://localhost:8888/wp-content/uploads/2024/08/IMG-1-300x208-jpeg.webp 300w,
http://localhost:8888/wp-content/uploads/2024/08/IMG-1-768x533-jpeg.webp 768w"
sizes="(max-width: 620px) 100vw, 620px">
<img
data-dominant-color="788d7e"
data-has-transparency="false"
style="--dominant-color: #788d7e;"
fetchpriority="high"
decoding="async"
width="1024"
height="711"
src="http://localhost:8888/wp-content/uploads/2024/08/IMG-1.jpeg"
alt=""
class="wp-image-13 not-transparent"
srcset="
http://localhost:8888/wp-content/uploads/2024/08/IMG-1.jpeg 1024w,
http://localhost:8888/wp-content/uploads/2024/08/IMG-1-300x208.jpeg 300w,
http://localhost:8888/wp-content/uploads/2024/08/IMG-1-768x533.jpeg 768w"
sizes="(max-width: 620px) 100vw, 620px"
></picture>After these changes style with |
| $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 ); | ||
| $style_attribute = '--dominant-color: #' . esc_attr( $image_meta['dominant_color'] ) . '; '; |
There was a problem hiding this comment.
No need to use esc_attr() with tag processor as this is handled for you.
There was a problem hiding this comment.
| $style_attribute = '--dominant-color: #' . esc_attr( $image_meta['dominant_color'] ) . '; '; | |
| $style_attribute = "--dominant-color: #{$image_meta['dominant_color']}; "; |
|
|
||
| if ( ! empty( $image_meta['dominant_color'] ) ) { | ||
| $data .= sprintf( 'data-dominant-color="%s" ', esc_attr( $image_meta['dominant_color'] ) ); | ||
| $processor->set_attribute( 'data-dominant-color', esc_attr( $image_meta['dominant_color'] ) ); |
There was a problem hiding this comment.
| $processor->set_attribute( 'data-dominant-color', esc_attr( $image_meta['dominant_color'] ) ); | |
| $processor->set_attribute( 'data-dominant-color', $image_meta['dominant_color'] ); |
There was a problem hiding this comment.
Using an HTML Tag Processor allows including single quotes and escaped double quotes for the image placeholder.
| 'escaped double quotes' => array( | ||
| 'image' => '<img src=\"%s\">', | ||
| 'expected' => false, | ||
| 'expected' => true, | ||
| ), |
There was a problem hiding this comment.
This is a strange test case to begin with. If the string is <img src=\"http://example.com/foo.jpg\">, the HTML Tag Processor would parse the src as having the value of \"http://example.com/foo.jpg\". I guess this doesn't even matter because we're not doing anything with the src anyway. I just wonder what value this test case has in the first place.
There was a problem hiding this comment.
I think we should delete this test case, I agree it doesn't make sense.
… fix/1475-image-paceholder-use-html-tag-processor
… fix/1475-image-paceholder-use-html-tag-processor
… fix/1475-image-paceholder-use-html-tag-processor
felixarntz
left a comment
There was a problem hiding this comment.
LGTM, just I think it's worth deleting the one pointless test.
| 'escaped double quotes' => array( | ||
| 'image' => '<img src=\"%s\">', | ||
| 'expected' => false, | ||
| 'expected' => true, | ||
| ), |
There was a problem hiding this comment.
I think we should delete this test case, I agree it doesn't make sense.
… fix/1475-image-paceholder-use-html-tag-processor
|
@felixarntz I have removed the test case. |
Summary
Fixes #1475