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

Embed fallbacks #493

Closed
wants to merge 9 commits into from
Closed

Embed fallbacks #493

wants to merge 9 commits into from

Conversation

coreymckrill
Copy link
Contributor

@coreymckrill coreymckrill commented Sep 29, 2016

This is a response to #354. Other related issues are #130, #288, #316, and #350.

The goal of this is to provide a consistent user experience when embedded media can't load because it's not from an https source. A few different options were discussed to make this happen, but this is what we settled on:

Remove the (embedded) element completely, and replace it with a blockquote or some other block-level element containing the source URL and perhaps a message like “Could not load.”

Here are the types of embeds that should be covered:

  • Audio
  • Video
  • iFrame

Both the audio and video sanitizers use the same routine to iterate
through the source elements within the main node and filter/sanitize
their attributes. However, the filtered attributes are not added back to
the source element before it is appended to the new amp-audio or
amp-video element. This is problematic when the attribute filtering
method converts URLs from http to https, but the converted URL does not
make it back into the source element.

This commit addresses the issue by updating each source element's
attributes before it is appended.
If an audio or video element, or its source child element, has a src
URL, but it is invalid (eg it uses the http protocol instead of https),
this creates a fallback blockquote element to replace the original that
explains that the media could not be loaded.
@coreymckrill
Copy link
Contributor Author

coreymckrill commented Sep 29, 2016

So far I've added fallbacks for audio and video. iFrame is still in the works. iFrame also has a fallback now.

Creates a fallback blockquote element to replace an iframe that doesn't
have a valid src attribute.
For a URL with a relative protocol, such as

//www.youtube.com/watch?v=dQw4w9WgXcQ

...the assumption is that either http or https will work with it. Since
AMP requires https, we should go ahead and add the https scheme to these
during filtering, if the sanitizer is configured to require or force
the protocol.
@coreymckrill coreymckrill changed the title [WIP] Embed fallbacks Embed fallbacks Oct 5, 2016

if ( $protocol === $src && ( $https_required || $force_https ) ) {
// src has relative protocol, ie //example.com/asdf, so add https.
$src = set_url_scheme( $src, 'https' );
Copy link
Contributor

Choose a reason for hiding this comment

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

What would happen with relative URLs, like /path/to/file.mp3?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Perhaps we could detect a double forward slash // at the beginning of the string to differentiate relative path from relative protocol?

Should we also try to handle relative path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I took a stab at handling relative paths along with the relative protocol scenario: f7dafe5

$node->parentNode->replaceChild( $fallback_node, $node );
} else {
$node->parentNode->removeChild( $node );
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This block of code (and the similar ones across the other sanitizers) could probably be abstracted to a new method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. 2365364


$fallback_content = $this->dom->createDocumentFragment();
$fallback_content->appendXML( sprintf(
wp_kses( __( 'Could not load <a href="%s">video</a>.', 'amp' ), array( 'a' => array( 'href' => true ) ) ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we switch the statement from an error ("Could not load") to more of a action ("Watch video"). It's not entirely accurate to say that we couldn't load the video. (Note: this applies to all the sanitizers)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could use "Watch video" and "Listen to audio", but I'm not sure what we'd do for the iframe one...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, I'm not sure "Could not load" is really misleading, since the AMP code is preventing non-https sources from loading.

In the previous commit I removed the embed-specific class name from the
abstracted `create_fallback_node` method, but forgot to update the tests
@westonruter
Copy link
Member

Fallbacks for iframes and media elements have been implemented in #1913 and #1861.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants