Skip to content

Commit a7e7992

Browse files
authored
Merge pull request #6581 from ampproject/bug/4635-disable-mute-when-autoplay-subset
Remove `muted` attribute from `amp-video` and its `video` fallback when `autoplay` is present
2 parents 96e76b1 + 8e93bd3 commit a7e7992

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

includes/sanitizers/class-amp-video-sanitizer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public function sanitize() {
6363

6464
if ( $this->args['add_noscript_fallback'] ) {
6565
$this->initialize_noscript_allowed_attributes( self::$tag );
66+
67+
// Omit muted from noscript > video since it causes deprecation warnings in validator.
68+
unset( $this->noscript_fallback_allowed_attributes['muted'] );
6669
}
6770

6871
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
@@ -312,6 +315,14 @@ private function filter_attributes( $attributes ) {
312315
}
313316
}
314317

318+
/*
319+
* The amp-video will forcibly be muted whenever it is set to autoplay.
320+
* So omit the `muted` attribute if it exists.
321+
*/
322+
if ( isset( $out['autoplay'], $out['muted'] ) ) {
323+
unset( $out['muted'] );
324+
}
325+
315326
return $out;
316327
}
317328
}

tests/php/test-amp-video-sanitizer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function get_data() {
6262
'<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>',
6363
],
6464

65+
'video_with_autoplay' => [
66+
'<video src="https://example.com/file.mp4" autoplay muted width="160" height="80"></video>',
67+
'<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>',
68+
],
69+
6570
'local_video_without_dimensions' => [
6671
sprintf( '<video src="%s"></video>', '{{video_url}}' ),
6772
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}}' ),
@@ -109,12 +114,12 @@ public function get_data() {
109114

110115
'video_with_allowlisted_attributes__enabled' => [
111116
'<video width="300" height="300" src="https://example.com/video.mp4" controls loop="true" muted="muted"></video>',
112-
'<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>',
117+
'<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>',
113118
],
114119

115120
'video_with_allowlisted_attributes__disabled' => [
116121
'<video width="300" height="300" src="https://example.com/video.mp4" controls="false" loop="false" muted="false"></video>',
117-
'<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>',
122+
'<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>',
118123
],
119124

120125
'video_with_custom_attribute' => [

0 commit comments

Comments
 (0)