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

Implement SSR for amp-audio #523

Merged
merged 6 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Optimizer/Transformer/ServerSideRendering.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public function transform(Document $document, ErrorCollection $errors)
if ($ampElement->tagName === Extension::AUDIO) {
$errors->add(Error\CannotRemoveBoilerplate::fromAmpAudio($ampElement));
$canRemoveBoilerplate = false;
$this->ssrAmpAudio($document, $ampElement);
}

/*
Expand Down Expand Up @@ -1060,4 +1061,28 @@ private function isZero($number)

return abs($number) < self::FLOATING_POINT_EPSILON;
}

/**
* Server-side rendering of an amp-audio element.
*
* @param Document $document DOM document to apply the transformations to.
* @param Element $element Element to adapt.
*/
private function ssrAmpAudio(Document $document, Element $element)
{
// Check if we already have a SSR-ed audio element.
if ($element->hasChildNodes()) {
foreach ($element->childNodes as $childNode) {
if ($childNode instanceof Element && $childNode->tagName === Tag::AUDIO) {
return;
}
}
}

$audio = $document->createElement(Tag::AUDIO);
$audio->setAttribute(Attribute::CONTROLS, '');
$audio->addInlineStyle('width:100%');

$element->appendChild($audio);
}
}
2 changes: 2 additions & 0 deletions tests/Optimizer/SpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ final class SpecTest extends TestCase
'ReorderHead - preserves_amp_custom_style_order' => 'see https://github.com/ampproject/amp-toolbox/issues/604',

'MinifyHtml - minifies_inline_amp-script' => 'see https://github.com/ampproject/amp-toolbox-php/issues/260',

'ServerSideRendering - does_not_transform_amp_audio' => 'The amp-toolbox needs to implement SSR amp-audio',
];

const CLASS_SKIP_TEST = '__SKIP__';
Expand Down
62 changes: 61 additions & 1 deletion tests/Optimizer/Transformer/ServerSideRenderingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function dataTransform()

'amp-audio' => [
$input('<amp-audio></amp-audio>'),
$expectWithBoilerplate('<amp-audio></amp-audio>'),
$expectWithBoilerplate('<amp-audio><audio controls style="width:100%;"></audio></amp-audio>'),
[
Error\CannotRemoveBoilerplate::fromAmpAudio(
Document::fromHtmlFragment(
Expand Down Expand Up @@ -326,6 +326,66 @@ public function dataTransform()
$input('<amp-ad type="doubleclick" data-slot="/6355419/Travel" layout="fluid" height="fluid" width="300"></amp-ad>'),
$expectWithoutBoilerplate('<amp-ad class="i-amphtml-layout-fluid i-amphtml-layout-awaiting-size" data-slot="/6355419/Travel" height="fluid" width="300" i-amphtml-layout="fluid" layout="fluid" style="width:100%;height:0;" type="doubleclick"></amp-ad>'),
],

'server side render amp-audio' => [
$input('<amp-audio src="http://example.com/audio.mp3" width="300"></amp-audio>'),
$expectWithBoilerplate('<amp-audio src="http://example.com/audio.mp3" width="300"><audio controls style="width:100%;"></audio></amp-audio>'),
[
Error\CannotRemoveBoilerplate::fromAmpAudio(
Document::fromHtmlFragment(
'<amp-audio src="http://example.com/audio.mp3" width="300"></amp-audio>'
)->body->firstChild
),
],
],

'ssr amp-audio appends audio element' => [
$input(
'<amp-audio src="http://example.com/audio.mp3" width="300">'
. '<div fallback="">Your browser doesn’t support HTML5 audio</div>'
. '</amp-audio>'
),
$expectWithBoilerplate(
'<amp-audio src="http://example.com/audio.mp3" width="300">'
. '<div fallback="">Your browser doesn’t support HTML5 audio</div>'
. '<audio controls style="width:100%;"></audio>'
. '</amp-audio>'
),
[
Error\CannotRemoveBoilerplate::fromAmpAudio(
Document::fromHtmlFragment(
'<amp-audio src="http://example.com/audio.mp3" width="300">'
. '<div fallback="">Your browser doesn’t support HTML5 audio</div>'
. '</amp-audio>'
)->body->firstChild
),
],
],

'skip ssr amp-audio if audio child node is present' => [
$input(
'<amp-audio src="http://example.com/audio.mp3" width="300">'
. '<div fallback="">Your browser doesn’t support HTML5 audio</div>'
. '<audio controls="" style="width: 100%" src="http://example.com/audio.mp3"></audio>'
. '</amp-audio>'
),
$expectWithBoilerplate(
'<amp-audio src="http://example.com/audio.mp3" width="300">'
. '<div fallback="">Your browser doesn’t support HTML5 audio</div>'
. '<audio controls="" style="width: 100%" src="http://example.com/audio.mp3"></audio>'
. '</amp-audio>'
),
[
Error\CannotRemoveBoilerplate::fromAmpAudio(
Document::fromHtmlFragment(
'<amp-audio src="http://example.com/audio.mp3" width="300">'
. '<div fallback="">Your browser doesn’t support HTML5 audio</div>'
. '<audio controls="" style="width: 100%" src="http://example.com/audio.mp3"></audio>'
. '</amp-audio>'
)->body->firstChild
),
],
]
];
}

Expand Down