Skip to content

Commit

Permalink
[BUGFIX] Add allow="autoplay; fullscreen" to YouTube/Vimeo iframes
Browse files Browse the repository at this point in the history
With the new chrome auto play policies we need to add a additional allow
attribute to the iframe to support autoplay.

https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

Resolves: #85233
Releases: master, 8.7
Change-Id: I19116a011b410933cf020b5b9606dc32d61e698d
Reviewed-on: https://review.typo3.org/57299
Reviewed-by: Sebastian Hofer <sebastian.hofer@marit.ag>
Tested-by: Sebastian Hofer <sebastian.hofer@marit.ag>
Reviewed-by: Robert Vierke <robert.vierke+typo3@gmail.com>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
  • Loading branch information
fsaris authored and neufeind committed Jun 22, 2018
1 parent d8d2a8d commit 2831b60
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public function render(FileInterface $file, $width, $height, array $options = nu
$src = sprintf('https://player.vimeo.com/video/%s?%s', $videoId, implode('&amp;', $urlParams));

$attributes = ['allowfullscreen'];
if (!isset($options['allow'])) {
$options['allow'] = 'fullscreen';
if (!empty($options['autoplay'])) {
$options['allow'] = 'autoplay; fullscreen';
}
}
if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
$attributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
}
Expand All @@ -137,7 +143,7 @@ public function render(FileInterface $file, $width, $height, array $options = nu
if (isset($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5') {
$attributes[] = 'frameborder="0"';
}
foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick'] as $key) {
foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'allow'] as $key) {
if (!empty($options[$key])) {
$attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public function render(FileInterface $file, $width, $height, array $options = nu
);

$attributes = ['allowfullscreen'];
if (!isset($options['allow'])) {
$options['allow'] = 'fullscreen';
if (!empty($options['autoplay'])) {
$options['allow'] = 'autoplay; fullscreen';
}
}
if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
$attributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
}
Expand All @@ -154,7 +160,7 @@ public function render(FileInterface $file, $width, $height, array $options = nu
if (isset($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5') {
$attributes[] = 'frameborder="0"';
}
foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload'] as $key) {
foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload', 'allow'] as $key) {
if (!empty($options[$key])) {
$attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function renderOutputIsCorrect()
$fileResourceMock = $this->createMock(File::class);

$this->assertSame(
'<iframe src="https://player.vimeo.com/video/7331?title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200"></iframe>',
'<iframe src="https://player.vimeo.com/video/7331?title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
$this->subject->render($fileResourceMock, '300m', '200')
);
}
Expand All @@ -106,7 +106,7 @@ public function renderOutputWithLoopIsCorrect()
$fileResourceMock = $this->createMock(File::class);

$this->assertSame(
'<iframe src="https://player.vimeo.com/video/7331?loop=1&amp;title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200"></iframe>',
'<iframe src="https://player.vimeo.com/video/7331?loop=1&amp;title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
$this->subject->render($fileResourceMock, '300m', '200', ['loop' => 1])
);
}
Expand All @@ -120,7 +120,7 @@ public function renderOutputWithAutoplayIsCorrect()
$fileResourceMock = $this->createMock(File::class);

$this->assertSame(
'<iframe src="https://player.vimeo.com/video/7331?autoplay=1&amp;title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200"></iframe>',
'<iframe src="https://player.vimeo.com/video/7331?autoplay=1&amp;title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
$this->subject->render($fileResourceMock, '300m', '200', ['autoplay' => 1])
);
}
Expand All @@ -139,7 +139,7 @@ public function renderOutputWithAutoplayFromReferenceIsCorrect()
$fileReferenceMock->expects($this->any())->method('getOriginalFile')->willReturn($fileResourceMock);

$this->assertSame(
'<iframe src="https://player.vimeo.com/video/7331?autoplay=1&amp;title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200"></iframe>',
'<iframe src="https://player.vimeo.com/video/7331?autoplay=1&amp;title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
$this->subject->render($fileReferenceMock, '300m', '200')
);
}
Expand All @@ -153,7 +153,7 @@ public function renderOutputWithAutoplayAndWithoutControllsIsCorrect()
$fileResourceMock = $this->createMock(File::class);

$this->assertSame(
'<iframe src="https://player.vimeo.com/video/7331?autoplay=1&amp;title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200"></iframe>',
'<iframe src="https://player.vimeo.com/video/7331?autoplay=1&amp;title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
$this->subject->render($fileResourceMock, '300m', '200', ['autoplay' => 1])
);
}
Expand All @@ -167,7 +167,7 @@ public function renderOutputWithAdditionalAttributes()
$fileResourceMock = $this->createMock(File::class);

$this->assertSame(
'<iframe src="https://player.vimeo.com/video/7331?title=0&amp;byline=0&amp;portrait=0" allowfullscreen foo="bar" custom-play="preload" width="300" height="200"></iframe>',
'<iframe src="https://player.vimeo.com/video/7331?title=0&amp;byline=0&amp;portrait=0" allowfullscreen foo="bar" custom-play="preload" width="300" height="200" allow="fullscreen"></iframe>',
$this->subject->render($fileResourceMock, '300m', '200', ['additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload']])
);
}
Expand All @@ -181,7 +181,7 @@ public function renderOutputWithDataAttributesForCustomization()
$fileResourceMock = $this->createMock(File::class);

$this->assertSame(
'<iframe src="https://player.vimeo.com/video/7331?title=0&amp;byline=0&amp;portrait=0" allowfullscreen data-player-handler="vimeo" data-custom-playerId="player-123" width="300" height="200"></iframe>',
'<iframe src="https://player.vimeo.com/video/7331?title=0&amp;byline=0&amp;portrait=0" allowfullscreen data-player-handler="vimeo" data-custom-playerId="player-123" width="300" height="200" allow="fullscreen"></iframe>',
$this->subject->render($fileResourceMock, '300m', '200', ['data' => ['player-handler' => 'vimeo', 'custom-playerId' => 'player-123']])
);
}
Expand All @@ -195,8 +195,36 @@ public function renderOutputWithCombinationOfDataAndAdditionalAttributes()
$fileResourceMock = $this->createMock(File::class);

$this->assertSame(
'<iframe src="https://player.vimeo.com/video/7331?title=0&amp;byline=0&amp;portrait=0" allowfullscreen foo="bar" custom-play="preload" data-player-handler="vimeo" data-custom-playerId="player-123" width="300" height="200"></iframe>',
'<iframe src="https://player.vimeo.com/video/7331?title=0&amp;byline=0&amp;portrait=0" allowfullscreen foo="bar" custom-play="preload" data-player-handler="vimeo" data-custom-playerId="player-123" width="300" height="200" allow="fullscreen"></iframe>',
$this->subject->render($fileResourceMock, '300m', '200', ['data' => ['player-handler' => 'vimeo', 'custom-playerId' => 'player-123'], 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload']])
);
}

/**
* @test
*/
public function renderOutputWithCustomAllowIsCorrect()
{
/** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
$fileResourceMock = $this->createMock(File::class);

$this->assertSame(
'<iframe src="https://player.vimeo.com/video/7331?title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200" allow="foo; bar"></iframe>',
$this->subject->render($fileResourceMock, '300m', '200', ['allow' => 'foo; bar'])
);
}

/**
* @test
*/
public function renderOutputWithCustomAllowAndAutoplayIsCorrect()
{
/** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
$fileResourceMock = $this->createMock(File::class);

$this->assertSame(
'<iframe src="https://player.vimeo.com/video/7331?autoplay=1&amp;title=0&amp;byline=0&amp;portrait=0" allowfullscreen width="300" height="200" allow="foo; bar"></iframe>',
$this->subject->render($fileResourceMock, '300m', '200', ['allow' => 'foo; bar', 'autoplay' => 1])
);
}
}
Loading

0 comments on commit 2831b60

Please sign in to comment.