Skip to content

Commit

Permalink
Merge pull request #4111 from ampproject/fix/4036-invalid-layout
Browse files Browse the repository at this point in the history
Fix invalid layout on non-AMP elements
  • Loading branch information
westonruter authored Jan 28, 2020
2 parents c431687 + 7f577cc commit 6021df7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions includes/sanitizers/class-amp-layout-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ class AMP_Layout_Sanitizer extends AMP_Base_Sanitizer {
public function sanitize() {
$xpath = new DOMXPath( $this->dom );

// Elements with the `layout` attribute will be validated by `AMP_Tag_And_Attribute_Sanitizer`.
$nodes = $xpath->query( '//*[ not( @layout ) and ( @data-amp-layout or @width or @height or @style ) ]' );
/**
* Sanitize AMP nodes to be AMP compatible. Elements with the `layout` attribute will be validated by
* `AMP_Tag_And_Attribute_Sanitizer`.
*/
$nodes = $xpath->query( '//*[ starts-with( name(), "amp-" ) and not( @layout ) and ( @data-amp-layout or @width or @height or @style ) ]' );

foreach ( $nodes as $node ) {
/**
Expand Down
5 changes: 5 additions & 0 deletions tests/php/test-amp-layout-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class AMP_Layout_Sanitizer_Test extends WP_UnitTestCase {
*/
public function get_body_data() {
return [
'non_amp_component' => [
'<svg height="10%" width="10%"></svg>',
'<svg height="10%" width="10%"></svg>',
],

'no_width_or_height' => [
'<amp-img src="foo.jpg" data-amp-layout="fill"></amp-img>',
'<amp-img src="foo.jpg" layout="fill"></amp-img>',
Expand Down

0 comments on commit 6021df7

Please sign in to comment.