Skip to content

Commit 89cd1f6

Browse files
authored
Merge pull request #521 from ampproject/fix/511-i-amphtml-sizer-responsiveness-issue
Fix SSR sizer responsiveness issue
2 parents a315295 + 560cc4d commit 89cd1f6

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/Optimizer/Transformer/ServerSideRendering.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,15 @@ private function maybeAddSizerInto(
590590
if ($layout === Layout::RESPONSIVE) {
591591
$elementId = $element->getAttribute(Attribute::ID);
592592
if (!empty($elementId) && array_key_exists($elementId, $this->customSizerStyles)) {
593-
$sizer = $this->createResponsiveSizer($document, $width, $height, $this->customSizerStyles[$elementId]);
593+
$sizer = $this->createResponsiveSizer(
594+
$document,
595+
$element,
596+
$width,
597+
$height,
598+
$this->customSizerStyles[$elementId]
599+
);
594600
} else {
595-
$sizer = $this->createResponsiveSizer($document, $width, $height);
601+
$sizer = $this->createResponsiveSizer($document, $element, $width, $height);
596602
}
597603
} elseif ($layout === Layout::INTRINSIC) {
598604
$sizer = $this->createIntrinsicSizer($document, $width, $height);
@@ -607,25 +613,30 @@ private function maybeAddSizerInto(
607613
* Create a sizer element for a responsive layout.
608614
*
609615
* @param Document $document DOM document to create the sizer for.
616+
* @param Element $element Element to add a sizer to.
610617
* @param CssLength $width Calculated width of the element.
611618
* @param CssLength $height Calculated height of the element.
612619
* @param string $style Style to use for the sizer. Defaults to padding-top in percentage.
613620
* @return Element
614621
*/
615622
private function createResponsiveSizer(
616623
Document $document,
624+
Element $element,
617625
CssLength $width,
618626
CssLength $height,
619-
$style = 'padding-top:%s%%'
627+
$style = ''
620628
) {
621629
$padding = $height->getNumeral() / $width->getNumeral() * 100;
622630
$paddingString = rtrim(rtrim(sprintf('%.4F', round($padding, 4)), '0'), '.');
631+
$paddingStyle = ! $element->hasAttribute(Attribute::HEIGHTS)
632+
? sprintf('padding-top:%s%%', $paddingString)
633+
: '';
623634

624-
$style = empty($style) ? 'display:block' : "display:block;{$style}";
635+
$style = "display:block;{$paddingStyle};{$style}";
625636

626637
$sizer = $document->createElement(Amp::SIZER_ELEMENT);
627638
$sizer->setAttribute(Attribute::SLOT, Amp::SERVICE_SLOT);
628-
$sizer->addInlineStyle(sprintf($style, $paddingString));
639+
$sizer->addInlineStyle($style);
629640

630641
return $sizer;
631642
}

tests/Optimizer/Transformer/ServerSideRenderingTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function dataTransform()
253253
'heights attribute without amp-custom' => [
254254
$input('<amp-img height="256" heights="(min-width: 500px) 200px, 80%" layout="responsive" width="320"></amp-img>'),
255255
$expectWithoutBoilerplate(
256-
'<amp-img height="256" layout="responsive" width="320" id="i-amp-0" class="i-amphtml-layout-responsive i-amphtml-layout-size-defined" i-amphtml-layout="responsive"><i-amphtml-sizer slot="i-amphtml-svc" style="display:block;padding-top:80%"></i-amphtml-sizer></amp-img>',
256+
'<amp-img height="256" layout="responsive" width="320" id="i-amp-0" class="i-amphtml-layout-responsive i-amphtml-layout-size-defined" i-amphtml-layout="responsive"><i-amphtml-sizer slot="i-amphtml-svc" style="display:block"></i-amphtml-sizer></amp-img>',
257257
'<style amp-custom>#i-amp-0>:first-child{padding-top:80%}@media (min-width: 500px){#i-amp-0>:first-child{padding-top:200px}}</style>'
258258
),
259259
[],
@@ -265,7 +265,7 @@ public function dataTransform()
265265
'<style amp-custom>body h1{color:red;}</style>'
266266
),
267267
$expectWithoutBoilerplate(
268-
'<amp-img height="256" layout="responsive" width="320" id="i-amp-0" class="i-amphtml-layout-responsive i-amphtml-layout-size-defined" i-amphtml-layout="responsive"><i-amphtml-sizer slot="i-amphtml-svc" style="display:block;padding-top:80%"></i-amphtml-sizer></amp-img>',
268+
'<amp-img height="256" layout="responsive" width="320" id="i-amp-0" class="i-amphtml-layout-responsive i-amphtml-layout-size-defined" i-amphtml-layout="responsive"><i-amphtml-sizer slot="i-amphtml-svc" style="display:block"></i-amphtml-sizer></amp-img>',
269269
'<style amp-custom>body h1{color:red;}#i-amp-0>:first-child{padding-top:80%}@media (min-width: 500px){#i-amp-0>:first-child{padding-top:200px}}</style>'
270270
),
271271
[],
@@ -274,7 +274,7 @@ public function dataTransform()
274274
'bad heights attribute' => [
275275
$input('<amp-img height="256" heights=",,," layout="responsive" width="320"></amp-img>'),
276276
// This adds an ID as it stores the CSS to inline before the actual error is detected.
277-
$expectWithBoilerplate('<amp-img class="i-amphtml-layout-responsive i-amphtml-layout-size-defined" height="256" heights=",,," i-amphtml-layout="responsive" layout="responsive" width="320"><i-amphtml-sizer slot="i-amphtml-svc" style="display:block;padding-top:80%"></i-amphtml-sizer></amp-img>'),
277+
$expectWithBoilerplate('<amp-img class="i-amphtml-layout-responsive i-amphtml-layout-size-defined" height="256" heights=",,," i-amphtml-layout="responsive" layout="responsive" width="320"><i-amphtml-sizer slot="i-amphtml-svc" style="display:block"></i-amphtml-sizer></amp-img>'),
278278
[
279279
Error\CannotRemoveBoilerplate::fromAttributeThrowingException(
280280
InvalidHtmlAttribute::fromAttribute(

0 commit comments

Comments
 (0)