Skip to content

Commit

Permalink
Merge pull request #6581 from ampproject/bug/4635-disable-mute-when-a…
Browse files Browse the repository at this point in the history
…utoplay-subset

Remove `muted` attribute from `amp-video` and its `video` fallback when `autoplay` is present
  • Loading branch information
westonruter authored Aug 30, 2021
2 parents 96e76b1 + 8e93bd3 commit a7e7992
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions includes/sanitizers/class-amp-video-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public function sanitize() {

if ( $this->args['add_noscript_fallback'] ) {
$this->initialize_noscript_allowed_attributes( self::$tag );

// Omit muted from noscript > video since it causes deprecation warnings in validator.
unset( $this->noscript_fallback_allowed_attributes['muted'] );
}

for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
Expand Down Expand Up @@ -312,6 +315,14 @@ private function filter_attributes( $attributes ) {
}
}

/*
* The amp-video will forcibly be muted whenever it is set to autoplay.
* So omit the `muted` attribute if it exists.
*/
if ( isset( $out['autoplay'], $out['muted'] ) ) {
unset( $out['muted'] );
}

return $out;
}
}
9 changes: 7 additions & 2 deletions tests/php/test-amp-video-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function get_data() {
'<amp-video src="https://example.com/file.mp4" class="amp-wp-unknown-size" height="400" layout="fixed-height" width="auto"><a href="https://example.com/file.mp4" fallback="">https://example.com/file.mp4</a><noscript><video src="https://example.com/file.mp4"></video></noscript></amp-video>',
],

'video_with_autoplay' => [
'<video src="https://example.com/file.mp4" autoplay muted width="160" height="80"></video>',
'<amp-video src="https://example.com/file.mp4" autoplay width="160" height="80" layout="responsive"><a href="https://example.com/file.mp4" fallback="">https://example.com/file.mp4</a><noscript><video src="https://example.com/file.mp4" autoplay width="160" height="80"></video></noscript></amp-video>',
],

'local_video_without_dimensions' => [
sprintf( '<video src="%s"></video>', '{{video_url}}' ),
sprintf( '<amp-video src="%1$s" width="560" height="320" layout="responsive"><a href="%1$s" fallback="">%1$s</a><noscript><video src="%1$s"></video></noscript></amp-video>', '{{video_url}}' ),
Expand Down Expand Up @@ -109,12 +114,12 @@ public function get_data() {

'video_with_allowlisted_attributes__enabled' => [
'<video width="300" height="300" src="https://example.com/video.mp4" controls loop="true" muted="muted"></video>',
'<amp-video width="300" height="300" src="https://example.com/video.mp4" controls="" loop="" muted="" layout="responsive"><a href="https://example.com/video.mp4" fallback="">https://example.com/video.mp4</a><noscript><video width="300" height="300" src="https://example.com/video.mp4" controls loop="true" muted="muted"></video></noscript></amp-video>',
'<amp-video width="300" height="300" src="https://example.com/video.mp4" controls="" loop="" muted="" layout="responsive"><a href="https://example.com/video.mp4" fallback="">https://example.com/video.mp4</a><noscript><video width="300" height="300" src="https://example.com/video.mp4" controls loop="true"></video></noscript></amp-video>',
],

'video_with_allowlisted_attributes__disabled' => [
'<video width="300" height="300" src="https://example.com/video.mp4" controls="false" loop="false" muted="false"></video>',
'<amp-video width="300" height="300" src="https://example.com/video.mp4" layout="responsive"><a href="https://example.com/video.mp4" fallback="">https://example.com/video.mp4</a><noscript><video width="300" height="300" src="https://example.com/video.mp4" controls="false" loop="false" muted="false"></video></noscript></amp-video>',
'<amp-video width="300" height="300" src="https://example.com/video.mp4" layout="responsive"><a href="https://example.com/video.mp4" fallback="">https://example.com/video.mp4</a><noscript><video width="300" height="300" src="https://example.com/video.mp4" controls="false" loop="false"></video></noscript></amp-video>',
],

'video_with_custom_attribute' => [
Expand Down

0 comments on commit a7e7992

Please sign in to comment.