Skip to content
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

Prevent distortion of images with a 'sizes' attribute #4622

Merged
merged 2 commits into from
Apr 28, 2020
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
18 changes: 10 additions & 8 deletions includes/sanitizers/class-amp-img-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use AmpProject\DevMode;
use AmpProject\Layout;

/**
* Class AMP_Img_Sanitizer
Expand Down Expand Up @@ -132,20 +133,20 @@ public function sanitize() {
} elseif ( $node->hasAttribute( 'layout' ) ) {
pierlon marked this conversation as resolved.
Show resolved Hide resolved
$layout = $node->getAttribute( 'layout' );
pierlon marked this conversation as resolved.
Show resolved Hide resolved
} else {
$layout = 'intrinsic';
$layout = Layout::INTRINSIC;
}

$has_width = is_numeric( $node->getAttribute( 'width' ) );
$has_height = is_numeric( $node->getAttribute( 'height' ) );

// Determine which images need their dimensions determined/extracted.
$missing_dimensions = (
( ! $has_height && 'fixed-height' === $layout )
( ! $has_height && Layout::FIXED_HEIGHT === $layout )
||
(
( ! $has_width || ! $has_height )
&&
in_array( $layout, [ 'fixed', 'responsive', 'intrinsic' ], true )
in_array( $layout, [ Layout::FIXED, Layout::RESPONSIVE, Layout::INTRINSIC ], true )
)
);
if ( $missing_dimensions ) {
Expand Down Expand Up @@ -324,14 +325,15 @@ private function adjust_and_replace_node( $node ) {
&& 'figure' === $node->parentNode->nodeName
&& preg_match( '/(^|\s)(alignwide|alignfull)(\s|$)/', $node->parentNode->getAttribute( 'class' ) )
) {
$new_attributes['layout'] = 'responsive';
$new_attributes['layout'] = Layout::RESPONSIVE;
pierlon marked this conversation as resolved.
Show resolved Hide resolved
} else {
$new_attributes['layout'] = 'intrinsic';
$new_attributes['layout'] = Layout::INTRINSIC;
pierlon marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Remove sizes attribute since it causes headaches in AMP and because AMP will generate it for us. See <https://github.com/ampproject/amphtml/issues/21371>.
unset( $new_attributes['sizes'] );
if ( isset( $new_attributes['sizes'] ) ) {
$new_attributes['disable-inline-width'] = '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use Attribute here.

Suggested change
if ( isset( $new_attributes['sizes'] ) ) {
$new_attributes['disable-inline-width'] = '';
if ( isset( $new_attributes[ Attribute::SIZES ] ) ) {
$new_attributes[ Attribute::DISABLE_INLINE_WIDTH'] = '';

Note: You'll have to add the DISABLE_INLINE_WIDTH constant to the Layout class as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constants everywhere 😄!

}

if ( $this->is_gif_url( $new_attributes['src'] ) ) {
$this->did_convert_elements = true;
Expand All @@ -353,7 +355,7 @@ private function adjust_and_replace_node( $node ) {
*/
if ( $img_node->hasAttribute( 'style' ) ) {
$layout = $img_node->getAttribute( 'layout' );
if ( in_array( $layout, [ 'fixed-height', 'responsive', 'fill', 'flex-item' ], true ) ) {
if ( in_array( $layout, [ Layout::FIXED_HEIGHT, Layout::RESPONSIVE, Layout::FILL, Layout::FLEX_ITEM ], true ) ) {
$required_display = 'block';
} elseif ( 'nodisplay' === $layout ) {
pierlon marked this conversation as resolved.
Show resolved Hide resolved
$required_display = 'none';
Expand Down
4 changes: 2 additions & 2 deletions tests/php/test-amp-img-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ public function get_data() {
null,
],

'img_with_sizes_attribute_removed' => [
'img_with_sizes_attribute_kept' => [
'<img width="825" height="510" src="https://placehold.it/825x510" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" sizes="(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw">',
'<amp-img width="825" height="510" src="https://placehold.it/825x510" class="attachment-post-thumbnail size-post-thumbnail wp-post-image amp-wp-enforced-sizes" alt="" layout="intrinsic"><noscript><img width="825" height="510" src="https://placehold.it/825x510" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" sizes="(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw"></noscript></amp-img>',
'<amp-img width="825" height="510" src="https://placehold.it/825x510" class="attachment-post-thumbnail size-post-thumbnail wp-post-image amp-wp-enforced-sizes" alt="" sizes="(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw" layout="intrinsic" disable-inline-width=""><noscript><img width="825" height="510" src="https://placehold.it/825x510" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" sizes="(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw"></noscript></amp-img>',
],

'amp_img_with_sizes_attribute_retained' => [
Expand Down