-
Notifications
You must be signed in to change notification settings - Fork 384
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
Closed
Embed fallbacks #493
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2c11b6a
Sanitize audio/video: update source attributes
coreymckrill 696d1d0
Fallbacks: Replace invalid audio/video element with fallback element
coreymckrill 3832b59
Fallbacks: update audio/video tests to reflect https is now required
coreymckrill 9e02d56
Fallbacks: Replace invalid iframe element with fallback element
coreymckrill e5a6253
Maybe set https scheme on URLs with relative protocols
coreymckrill a689a2c
Fallbacks: update iframe tests to reflect that https is now required
coreymckrill 2365364
Fallbacks: abstract fallback node creation to a base class method
coreymckrill b1077d6
Fallbacks: update tests
coreymckrill f7dafe5
Detect and handle protocol- and path-relative URLs in base sanitizer
coreymckrill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,10 @@ class AMP_Video_Sanitizer extends AMP_Base_Sanitizer { | |
|
||
public static $tag = 'video'; | ||
|
||
protected $DEFAULT_ARGS = array( | ||
'require_https_src' => true, | ||
); | ||
|
||
public function sanitize() { | ||
$nodes = $this->dom->getElementsByTagName( self::$tag ); | ||
$num_nodes = $nodes->length; | ||
|
@@ -18,9 +22,15 @@ public function sanitize() { | |
} | ||
|
||
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { | ||
$orig_src = array(); | ||
|
||
$node = $nodes->item( $i ); | ||
$old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ); | ||
|
||
if ( isset( $old_attributes['src'] ) ) { | ||
$orig_src[] = $old_attributes['src']; | ||
} | ||
|
||
$new_attributes = $this->filter_attributes( $old_attributes ); | ||
|
||
$new_attributes = $this->enforce_fixed_height( $new_attributes ); | ||
|
@@ -32,21 +42,36 @@ public function sanitize() { | |
foreach ( $node->childNodes as $child_node ) { | ||
$new_child_node = $child_node->cloneNode( true ); | ||
$old_child_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $new_child_node ); | ||
|
||
if ( isset( $old_child_attributes['src'] ) ) { | ||
$orig_src[] = $old_child_attributes['src']; | ||
} | ||
|
||
$new_child_attributes = $this->filter_attributes( $old_child_attributes ); | ||
|
||
// Only append source tags with a valid src attribute | ||
if ( ! empty( $new_child_attributes['src'] ) && 'source' === $new_child_node->tagName ) { | ||
AMP_DOM_Utils::add_attributes_to_node( $new_child_node, $new_child_attributes ); | ||
$new_node->appendChild( $new_child_node ); | ||
} | ||
} | ||
|
||
// If the node has at least one valid source, replace the old node with it. | ||
// If the node has no valid sources, but at least one invalid (http) one, add a fallback element. | ||
// Otherwise, just remove the node. | ||
// | ||
// TODO: Add a fallback handler. | ||
// See: https://github.com/ampproject/amphtml/issues/2261 | ||
if ( 0 === $new_node->childNodes->length && empty( $new_attributes['src'] ) ) { | ||
$node->parentNode->removeChild( $node ); | ||
if ( ! empty( $orig_src ) ) { | ||
$fallback_node = $this->create_fallback_node( | ||
sprintf( | ||
wp_kses( __( 'Could not load <a href="%s">video</a>.', 'amp' ), array( 'a' => array( 'href' => true ) ) ), | ||
esc_url( array_shift( $orig_src ) ) | ||
) | ||
); | ||
|
||
$node->parentNode->replaceChild( $fallback_node, $node ); | ||
} else { | ||
$node->parentNode->removeChild( $node ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. 2365364 |
||
} else { | ||
$node->parentNode->replaceChild( $new_node, $node ); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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)
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.
We could use "Watch video" and "Listen to audio", but I'm not sure what we'd do for the iframe one...
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.
Also, I'm not sure "Could not load" is really misleading, since the AMP code is preventing non-https sources from loading.