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

Fix handling of gallery carousels in paragraphs and fix no-JS fallbacks #4009

Merged
merged 1 commit into from
Jan 6, 2020

Conversation

westonruter
Copy link
Member

Summary

I found that when a gallery carousel is displayed inside of a paragraph (that is, an <amp-carousel> inside of a <p>), it completely breaks because the use of <div> inside of the <amp-carousel> causes it to break out the <p> (since block level elements can't appear inside of paragraphs).

Given this gallery on a non-AMP page:

image

Where the underlying post_content is:

<p>Gallery: [gallery amp-carousel=true size="large" ids="2436,2435,2434"]</p>

The expected rendered gallery is:

image

But the actual gallery appears as:

image

By looking at the DOM tree, the issue of the slide divs breaking out of the amp-carousel can be clearly seen:

image

After the fix in this PR, the DOM appears properly nested:

image

No-JS Fallbacks

Additionally, I also found that when JS is turned off in the browser, the carousel images do not display at all. This is because the gallery embed handler was creating an amp-img, when it should have been creating an img so that the image sanitizer would create the necessary amp-img with the noscript > img fallback content.

Before:

image

After:

image

Amends #3659. See #3658.

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).

@westonruter westonruter added the Bug Something isn't working label Dec 24, 2019
@westonruter westonruter added this to the v1.5 milestone Dec 24, 2019
@googlebot googlebot added the cla: yes Signed the Google CLA label Dec 24, 2019
@westonruter
Copy link
Member Author

Don't worry about reviewing this until the new year!!

Copy link
Contributor

@kienstra kienstra left a comment

Choose a reason for hiding this comment

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

Approved

Hi @westonruter,
Sorry for the delay. This looks good for both the Gallery block and [gallery] shortcode.

I should have considered the <amp-carousel> being wrapped in a <p> when working on this.

Before: <amp-carousel> wrapped in <p>

before-gallery

After

after-carousel-wrapped

carousel-not-in-p

* @param DOMElement $element Element.
* @return bool If it is an image.
*/
private function is_image_element( DOMElement $element ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea to extract this into a helper method.

$slide_node->setAttribute( 'layout', 'fill' );
$slide_node->setAttribute( 'object-fit', 'cover' );
} elseif ( isset( $slide_node->firstChild->tagName ) && 'amp-img' === $slide_node->firstChild->tagName ) {
} elseif ( $slide_node->firstChild instanceof DOMElement && $this->is_image_element( $slide_node->firstChild ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea to change this to instanceof DOMElement

@westonruter westonruter merged commit 6b37ea5 into develop Jan 6, 2020
@westonruter westonruter deleted the fix/carousel-in-paragraph branch January 6, 2020 05:21
@kienstra
Copy link
Contributor

Testing Steps

  1. Create a new post
  2. Add a Custom HTML block
  3. Add: <p>Gallery: [gallery amp-carousel=true size="large" ids="5525,5527"]</p>
  4. Go to the front-end
  5. Expected: it should look like:

gallery-now

6. In the Chrome dev tools, click 'Settings':

js-console

7. Click 'Disable JavaScript':

disable-js

  1. Reload
  2. Expected: the images still display, but the styling might look bad:

images-visible

@csossi
Copy link

csossi commented Jan 27, 2020

Verified in QA

@csossi csossi added the QA passed Has passed QA and is done label Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working cla: yes Signed the Google CLA QA passed Has passed QA and is done
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants