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

Use responsive layout for blocks with alignwide class #4693

Merged
merged 5 commits into from
May 22, 2020
Merged

Use responsive layout for blocks with alignwide class #4693

merged 5 commits into from
May 22, 2020

Conversation

deepaklalwani97
Copy link
Contributor

@deepaklalwani97 deepaklalwani97 commented May 13, 2020

Summary

  • Use the responsive layout for amp-iframe if the Gutenberg block has wide alignment.

Fixes #4692

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@googlebot googlebot added the cla: yes Signed the Google CLA label May 13, 2020
@deepaklalwani97 deepaklalwani97 marked this pull request as ready for review May 13, 2020 13:17
Copy link
Contributor

@pierlon pierlon left a comment

Choose a reason for hiding this comment

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

Minor suggestion, but otherwise from that it looks good!

Comment on lines 140 to 148

$figure_node_classes = explode( ' ', $figure_node->getAttribute( 'class' ) );

// Changes layout to responsive for videos which have wide or full width (contains alignwide or alignfull class).
$show_responsive = ( in_array( 'alignfull', $figure_node_classes, true ) || in_array( 'alignwide', $figure_node_classes, true ) );

if ( true === $show_responsive ) {
$normalized_attributes['layout'] = 'responsive';
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor formatting suggestion:

Suggested change
$figure_node_classes = explode( ' ', $figure_node->getAttribute( 'class' ) );
// Changes layout to responsive for videos which have wide or full width (contains alignwide or alignfull class).
$show_responsive = ( in_array( 'alignfull', $figure_node_classes, true ) || in_array( 'alignwide', $figure_node_classes, true ) );
if ( true === $show_responsive ) {
$normalized_attributes['layout'] = 'responsive';
}
$figure_node_classes = explode( ' ', $figure_node->getAttribute( 'class' ) );
// If the alignment was set to 'wide width' or 'full width', set the layout to responsive.
$show_responsive = (
in_array( 'alignfull', $figure_node_classes, true ) ||
in_array( 'alignwide', $figure_node_classes, true )
);
if ( true === $show_responsive ) {
$normalized_attributes['layout'] = 'responsive';
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I have formatted the code as per the suggestion.

@pierlon pierlon requested a review from westonruter May 13, 2020 14:06
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Just a couple other things:


if ( $figure_node && $figure_node->hasAttribute( 'class' ) ) {

$figure_node_classes = explode( ' ', $figure_node->getAttribute( 'class' ) );
Copy link
Member

Choose a reason for hiding this comment

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

It will be more robust to split by any whitespace because there could be newlines or other characters than spaces.

Suggested change
$figure_node_classes = explode( ' ', $figure_node->getAttribute( 'class' ) );
$figure_node_classes = preg_split( '/\s+/', trim( $figure_node->getAttribute( 'class' ) ) );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback. I have applied the suggested changes.

Comment on lines 144 to 149
$show_responsive = (
in_array( 'alignfull', $figure_node_classes, true ) ||
in_array( 'alignwide', $figure_node_classes, true )
);

if ( true === $show_responsive ) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this can be simplified a bit:

Suggested change
$show_responsive = (
in_array( 'alignfull', $figure_node_classes, true ) ||
in_array( 'alignwide', $figure_node_classes, true )
);
if ( true === $show_responsive ) {
if ( in_array( 'alignfull', $figure_node_classes, true ) || in_array( 'alignwide', $figure_node_classes, true ) ) {

@deepaklalwani97
Copy link
Contributor Author

@westonruter I have applied the suggested changes. This PR can be reviewed again let me know if any feedback.

@westonruter
Copy link
Member

I just realized that there is similar logic in the Image sanitizer:

// Use responsive images when a theme supports wide and full-bleed images.
if (
! empty( $this->args['align_wide_support'] )
&& $node->parentNode instanceof DOMElement
&& 'figure' === $node->parentNode->nodeName
&& preg_match( '/(^|\s)(alignwide|alignfull)(\s|$)/', $node->parentNode->getAttribute( Attribute::CLASS_ ) )
) {
$new_attributes[ Attribute::LAYOUT ] = Layout::RESPONSIVE;
} else {
$new_attributes[ Attribute::LAYOUT ] = Layout::INTRINSIC;
}

And see:

'AMP_Img_Sanitizer' => [
'align_wide_support' => current_theme_supports( 'align-wide' ),
],

I think what is needed is to harmonize the logic being introduced in this PR with the pre-existing logic in the image sanitizer to apply to all other sanitizers as well, where there is the possibility for the sanitized element to be a block that has the alignfull or alignwide block classes.

For example, the logic probably needs to be accounted for in AMP_Embed_Sanitizer and AMP_Video_Sanitizer at least.

@westonruter
Copy link
Member

I noticed that gfycat embeds weren't getting sized responsively, but I believe they'll be accounted for in #4650.

@westonruter westonruter merged commit 710f0f7 into ampproject:develop May 22, 2020
@westonruter
Copy link
Member

I also squashed the commits together to cherry-pick onto the 1.5 branch: 1ec497a.

@westonruter westonruter added this to the v1.5.4 milestone Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wide width alignment of videopress block is not respected in AMP mode.
4 participants