-
Notifications
You must be signed in to change notification settings - Fork 138
Use more robust HTML Tag Processor for Image Placeholders #1477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use more robust HTML Tag Processor for Image Placeholders #1477
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to use esc_attr() with tag processor as this is handled for you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
westonruter
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
… fix/1475-image-paceholder-use-html-tag-processor
felixarntz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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