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

Adapt heights, sizes and media attributes for SSR #4482

Merged
merged 41 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c026485
Update test suite and local metadata fallback
schlessera Mar 27, 2020
f248341
Add logic to SSR to convert sizes and heights attributes
schlessera Mar 30, 2020
f1f5fb3
Add basic media attribute handling
schlessera Mar 30, 2020
272862e
Exclude amp-audio from applying layout at the server
schlessera Mar 30, 2020
6c589a5
Add context info for media attribute
schlessera Mar 30, 2020
fa9d521
Skip spec tests for newly supported attributes
schlessera Mar 30, 2020
3b46da7
Fix PHPStan issue
schlessera Mar 30, 2020
8668a85
Merge branch 'develop' into add/4439-transform-media-sizes-heights
schlessera Mar 30, 2020
f1102c6
Adapt tests for getElementId()
schlessera Mar 31, 2020
e04f9cf
Add another getElementId() test
schlessera Mar 31, 2020
6efc85a
Adapt media query to use for replacing media attribute
schlessera Mar 31, 2020
330fd94
Fix negated media syntax
schlessera Mar 31, 2020
e24422e
Another stab at fixing media query generation
schlessera Mar 31, 2020
48bfebf
Merge branch 'develop' into add/4439-transform-media-sizes-heights
schlessera Apr 7, 2020
dd9e71b
Add size check for adding CSS
schlessera Apr 7, 2020
5b514b0
Merge branch 'develop' into add/4439-transform-media-sizes-heights
schlessera Apr 29, 2020
1891b37
Just strip sizes when srcset is missing
schlessera Apr 30, 2020
a666012
Use neutral formatting for CSS size number
schlessera May 5, 2020
f5f99b4
Merge branch 'develop' into add/4439-transform-media-sizes-heights
schlessera May 5, 2020
0b677f4
Don't remove sizes when disable-inline-width is set
schlessera May 5, 2020
b2172e6
Remove trailing commas in method calls
schlessera May 5, 2020
2d978c6
Skip currently broken tests in Optimizer
schlessera May 5, 2020
4b2781a
Merge branch 'develop' into add/4439-transform-media-sizes-heights
schlessera May 7, 2020
9731fe3
Remove test exception for #4654
schlessera May 7, 2020
58b550f
Omit unneeded semicolons
schlessera May 7, 2020
4842fc8
Remove left-over attribute removal
schlessera May 7, 2020
facc016
Align equal sign
schlessera May 7, 2020
bf6da0a
Adapt tests for removed semi-colons
schlessera May 7, 2020
11ff49c
Fix attribute name in error messages
schlessera May 7, 2020
c470761
Test precise error messages
schlessera May 7, 2020
0906a39
Provide more precise error for thrown exception
schlessera May 7, 2020
69f0b36
Fix error type for bad sizes test
schlessera May 7, 2020
2f423ca
Use :first-child to reference sizer in CSS
schlessera May 7, 2020
f877435
Update php-parallel-lint dependency to new name
schlessera May 7, 2020
f4d37fa
Use padding-top and override regular inline style for heights
schlessera May 7, 2020
a36dc0f
Pass DOMAttr instances rather than attribute name and value as separa…
westonruter May 7, 2020
84a4464
Ensure that source map comment is preserved at the end of amp-custom.css
westonruter May 7, 2020
8767882
Defer removal of attributes until after layout application
schlessera May 8, 2020
a7de3cd
Avoid method map and replace with a switch
schlessera May 8, 2020
3960f9c
Use explicit string type instead of generic array in return
westonruter May 8, 2020
5807091
Remove unused RuntimeException
westonruter May 8, 2020
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
4 changes: 2 additions & 2 deletions lib/common/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"ext-dom": "*",
"ext-iconv": "*",
"ext-json": "*",
"ext-libxml": "*"
"ext-libxml": "*",
"php-parallel-lint/php-parallel-lint": "^1.2"
},
"require-dev": {
"civicrm/composer-downloads-plugin": "^2.1",
"jakub-onderka/php-parallel-lint": "^1.0",
"roave/security-advisories": "dev-master",
"squizlabs/php_codesniffer": "^3"
},
Expand Down
4 changes: 2 additions & 2 deletions lib/optimizer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"ext-dom": "*",
"ext-iconv": "*",
"ext-libxml": "*",
"ampproject/common": "^1"
"ampproject/common": "^1",
"php-parallel-lint/php-parallel-lint": "^1.2"
},
"require-dev": {
"ext-zip": "*",
"civicrm/composer-downloads-plugin": "^2.1",
"jakub-onderka/php-parallel-lint": "^1.0",
"roave/security-advisories": "dev-master",
"squizlabs/php_codesniffer": "^3"
},
Expand Down
2 changes: 1 addition & 1 deletion lib/optimizer/resources/local_fallback/rtv/metadata
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"ampRuntimeVersion":"012004252135000","ampCssUrl":"https://cdn.ampproject.org/rtv/012004252135000/v0.css","canaryPercentage":"0.005","diversions":["002004252135000","002005050322000","022004252135000","032004252135000","032005050322000","042005051629000","052004252135000","102004252135000"],"ltsRuntimeVersion":"012004030010070","ltsCssUrl":"https://cdn.ampproject.org/rtv/012004030010070/v0.css"}
{"ampRuntimeVersion":"012004252135000","ampCssUrl":"https://cdn.ampproject.org/rtv/012004252135000/v0.css","canaryPercentage":"0.005","diversions":["002005050322000","022004252135000","032005050322000","042005062312000","052004252135000","102005050322000"],"ltsRuntimeVersion":"012004030010070","ltsCssUrl":"https://cdn.ampproject.org/rtv/012004030010070/v0.css"}
2 changes: 1 addition & 1 deletion lib/optimizer/resources/local_fallback/v0.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 39 additions & 18 deletions lib/optimizer/src/Transformer/ServerSideRendering.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ final class ServerSideRendering implements Transformer
*/
private $ampCustomCssByteCount = 0;

/**
* Associative array of custom sizer styles where the key is the ID of the associated element.
*
* @var string[]
*/
private $customSizerStyles = [];

/**
* Apply transformations to the provided DOM document.
*
Expand Down Expand Up @@ -557,7 +564,12 @@ private function maybeAddSizerInto(
$sizer = null;

if ($layout === Layout::RESPONSIVE) {
$sizer = $this->createResponsiveSizer($document, $width, $height);
$elementId = $element->getAttribute(Attribute::ID);
if (!empty($elementId) && array_key_exists($elementId, $this->customSizerStyles)) {
$sizer = $this->createResponsiveSizer($document, $width, $height, $this->customSizerStyles[$elementId]);
} else {
$sizer = $this->createResponsiveSizer($document, $width, $height);
}
} elseif ($layout === Layout::INTRINSIC) {
$sizer = $this->createIntrinsicSizer($document, $width, $height);
}
Expand All @@ -573,13 +585,15 @@ private function maybeAddSizerInto(
* @param Document $document DOM document to create the sizer for.
* @param CssLength $width Calculated width of the element.
* @param CssLength $height Calculated height of the element.
* @param string $style Style to use for the sizer. Defaults to padding-top in percentage.
* @return DOMElement
*/
private function createResponsiveSizer(Document $document, CssLength $width, CssLength $height)
private function createResponsiveSizer(Document $document, CssLength $width, CssLength $height, $style = 'padding-top:%1.4F%%;')
{
$padding = $height->getNumeral() / $width->getNumeral() * 100;
$sizer = $document->createElement(Amp::SIZER_ELEMENT);
$sizer->setAttribute(Tag::STYLE, sprintf('display:block;padding-top:%1.4F%%;', $padding));
$style = empty($style) ? 'display:block' : "display:block;{$style}";
$sizer->setAttribute(Tag::STYLE, sprintf($style, $padding));

return $sizer;
}
Expand Down Expand Up @@ -743,11 +757,10 @@ private function adaptBlockingAttributes(Document $document, DOMElement $ampElem
$method = self::EXTRACTION_METHOD_MAP[$normalizedAttributeName];

try {
$customCss .= $this->$method($document, $ampElement, $attribute->value);
$customCss .= $this->$method($document, $ampElement, $attribute->name, $attribute->value);
$attributesToRemove[] = $attribute->name;
$ampElement->removeAttribute($attribute->name);
} catch (Exception $exception) {
$errors->add(Error\CannotRemoveBoilerplate::fromAttributesRequiringBoilerplate($ampElement));
$errors->add(Error\CannotRemoveBoilerplate::fromAttributeThrowingException($exception));
return false;
}
}
Expand Down Expand Up @@ -814,10 +827,11 @@ private function addCustomCss(Document $document, $customCss)
*
* @param Document $document Document containing the element to adapt.
* @param DOMElement $element Element to adapt.
* @param string $attributeName Name of the attribute to be extracted.
* @param string $attributeValue Value of the attribute to extract the CSS styling from.
* @return string Extract custom CSS styling.
*/
private function extractSizesAttributeCss(Document $document, DOMElement $element, $attributeValue)
private function extractSizesAttributeCss(Document $document, DOMElement $element, $attributeName, $attributeValue)
schlessera marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$element->hasAttribute(Attribute::SRCSET) || empty($element->getAttribute(Attribute::SRCSET))) {
// According to the Mozilla docs, a sizes attribute without a valid srcset attribute should have no effect.
Expand All @@ -829,9 +843,10 @@ private function extractSizesAttributeCss(Document $document, DOMElement $elemen
return $this->extractAttributeCss(
$document,
$element,
$attributeName,
$attributeValue,
'#__ID__{width:%s};',
'@media %s{#__ID__{width:%s;}}'
'#__ID__{width:%s}',
'@media %s{#__ID__{width:%s}}'
);
}

Expand All @@ -849,17 +864,21 @@ private function extractSizesAttributeCss(Document $document, DOMElement $elemen
*
* @param Document $document Document containing the element to adapt.
* @param DOMElement $element Element to adapt.
* @param string $attributeName Name of the attribute to be extracted.
* @param string $attributeValue Value of the attribute to extract the CSS styling from.
* @return string Extract custom CSS styling.
*/
private function extractHeightsAttributeCss(Document $document, DOMElement $element, $attributeValue)
private function extractHeightsAttributeCss(Document $document, DOMElement $element, $attributeName, $attributeValue)
{
$this->customSizerStyles[$document->getElementId($element)] = '';

return $this->extractAttributeCss(
$document,
$element,
$attributeName,
$attributeValue,
'#__ID__>i-amphtml-sizer{height:%s};',
'@media %s{#__ID__>i-amphtml-sizer{height:%s;}}'
'#__ID__>:first-child{padding-top:%s}',
'@media %s{#__ID__>:first-child{padding-top:%s}}'
);
}

Expand All @@ -868,12 +887,13 @@ private function extractHeightsAttributeCss(Document $document, DOMElement $elem
*
* @param Document $document Document containing the element to adapt.
* @param DOMElement $element Element to adapt.
* @param string $attributeName Name of the attribute to be extracted.
* @param string $attributeValue Value of the attribute to extract the CSS styling from.
* @param string $mainStyle CSS template for the main style.
* @param string $mediaQueryStyle CSS template for a media query style.
* @return string Extract custom CSS styling.
*/
private function extractAttributeCss(Document $document, DOMElement $element, $attributeValue, $mainStyle, $mediaQueryStyle)
private function extractAttributeCss(Document $document, DOMElement $element, $attributeName, $attributeValue, $mainStyle, $mediaQueryStyle)
{
if (empty($attributeValue)) {
return '';
Expand All @@ -883,7 +903,7 @@ private function extractAttributeCss(Document $document, DOMElement $element, $a
$lastItem = trim(array_pop($sourceSizes), self::CSS_TRIM_CHARACTERS);

if (empty($lastItem)) {
throw InvalidHtmlAttribute::fromAttribute(Attribute::HEIGHTS, $element);
throw InvalidHtmlAttribute::fromAttribute($attributeName, $element);
}

$customCss = sprintf($mainStyle, $lastItem);
Expand All @@ -894,13 +914,13 @@ private function extractAttributeCss(Document $document, DOMElement $element, $a
$mediaCondition = trim($matches['media_condition'], self::CSS_TRIM_CHARACTERS);

if (empty($mediaCondition)) {
throw InvalidHtmlAttribute::fromAttribute(Attribute::HEIGHTS, $element);
throw InvalidHtmlAttribute::fromAttribute($attributeName, $element);
}

$dimension = trim($matches['dimension'], self::CSS_TRIM_CHARACTERS);

if (empty($dimension)) {
throw InvalidHtmlAttribute::fromAttribute(Attribute::HEIGHTS, $element);
throw InvalidHtmlAttribute::fromAttribute($attributeName, $element);
}

$customCss .= sprintf($mediaQueryStyle, $mediaCondition, $dimension);
Expand All @@ -923,10 +943,11 @@ private function extractAttributeCss(Document $document, DOMElement $element, $a
*
* @param Document $document Document containing the element to adapt.
* @param DOMElement $element Element to adapt.
* @param string $attributeName Name of the attribute to be extracted.
* @param string $attributeValue Value of the attribute to extract the CSS styling from.
* @return string Extract custom CSS styling.
*/
private function extractMediaAttributeCss(Document $document, DOMElement $element, $attributeValue)
private function extractMediaAttributeCss(Document $document, DOMElement $element, $attributeName, $attributeValue)
{
$attributeValue = trim($attributeValue, self::CSS_TRIM_CHARACTERS);

Expand All @@ -939,7 +960,7 @@ private function extractMediaAttributeCss(Document $document, DOMElement $elemen
}

return sprintf(
'@media not %s{#%s{display:none;}}',
'@media not %s{#%s{display:none}}',
schlessera marked this conversation as resolved.
Show resolved Hide resolved
$attributeValue,
$document->getElementId($element)
);
Expand Down
3 changes: 0 additions & 3 deletions lib/optimizer/tests/SpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ final class SpecTest extends TestCase

'ServerSideRendering - noscript_then_boilerplate_not_removed_due_to_attribute' => 'see https://github.com/ampproject/amp-wp/issues/4439',
'ServerSideRendering - boilerplate_then_noscript_not_removed_due_to_attribute' => 'see https://github.com/ampproject/amp-wp/issues/4439',

'AmpRuntimeCss - always_inlines_v0css' => 'see https://github.com/ampproject/amp-wp/issues/4654',
'AmpRuntimeCss - does_not_add_v0.css_if_style_amp-runtime_not_present' => 'see https://github.com/ampproject/amp-wp/issues/4654',
];

const CLASS_SKIP_TEST = '__SKIP__';
Expand Down
Loading