Skip to content

Commit

Permalink
Accommodate for a 100% width or height being defined as style descrip…
Browse files Browse the repository at this point in the history
…tor or via its attribute
  • Loading branch information
pierlon committed Dec 6, 2019
1 parent bb9c7cd commit e3efc98
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
23 changes: 20 additions & 3 deletions includes/sanitizers/class-amp-layout-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,29 @@ public function sanitize() {
if ( ! $this->attribute_empty( $style ) ) {
$styles = $this->parse_style_string( $style );

// If both height & width descriptors are 100%, apply fill layout.
/*
* If both height & width descriptors are 100%, or
* width attribute is 100% and height style descriptor is 100%, or
* height attribute is 100% and width style descriptor is 100%
* then apply fill layout.
*/
if (
isset( $styles['width'], $styles['height'] ) &&
( '100%' === $styles['width'] && '100%' === $styles['height'] )
(
isset( $styles['width'], $styles['height'] ) &&
( '100%' === $styles['width'] && '100%' === $styles['height'] )
) ||
(
( ! $this->attribute_empty( $width ) && '100%' === $width ) &&
( isset( $styles['height'] ) && '100%' === $styles['height'] )
) ||
(
( ! $this->attribute_empty( $height ) && '100%' === $height ) &&
( isset( $styles['width'] ) && '100%' === $styles['width'] )
)
) {
unset( $styles['width'], $styles['height'] );
$node->removeAttribute( 'width' );
$node->removeAttribute( 'height' );

if ( empty( $styles ) ) {
$node->removeAttribute( 'style' );
Expand Down
10 changes: 10 additions & 0 deletions tests/php/test-amp-layout-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public function get_body_data() {
'<amp-img src="foo.jpg" style="width:100%; height:100%; color:#000"></amp-img>',
'<amp-img src="foo.jpg" style="color:#000" layout="fill"></amp-img>',
],

'fill_height_attribute_and_width_style' => [
'<amp-img src="foo.jpg" style="width:100%;" height="100%"></amp-img>',
'<amp-img src="foo.jpg" layout="fill"></amp-img>',
],

'fill_width_attribute_and_height_style' => [
'<amp-img src="foo.jpg" style="height:100%;" width="100%"></amp-img>',
'<amp-img src="foo.jpg" layout="fill"></amp-img>',
],
];
}

Expand Down

0 comments on commit e3efc98

Please sign in to comment.