Skip to content

Commit

Permalink
Opt-in to amp-img-auto-sizes experiment when there are converted images
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Apr 2, 2019
1 parent 65f1c0c commit 0180357
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions includes/sanitizers/class-amp-img-sanitizer.php
Expand Up @@ -107,6 +107,21 @@ public function sanitize() {

$this->determine_dimensions( $need_dimensions );
$this->adjust_and_replace_nodes_in_array_map( $need_dimensions );

/*
* Opt-in to amp-img-auto-sizes experiment.
* This is needed because the sizes attribute is removed from all img elements converted to amp-img
* in order to prevent the undesirable setting of the width. This $meta tag can be removed once the
* experiment ends (and the feature has been fully launched).
* See <https://github.com/ampproject/amphtml/issues/21371> and <https://github.com/ampproject/amp-wp/pull/2036>.
*/
$head = $this->dom->getElementsByTagName( 'head' )->item( 0 );
if ( $head ) {
$meta = $this->dom->createElement( 'meta' );
$meta->setAttribute( 'name', 'amp-experiments-opt-in' );
$meta->setAttribute( 'content', 'amp-img-auto-sizes' );
$head->insertBefore( $meta, $head->firstChild );
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/test-amp-img-sanitizer.php
Expand Up @@ -239,10 +239,14 @@ public function test_converter( $source, $expected = null ) {
$expected = $source;
}
$dom = AMP_DOM_Utils::get_dom_from_content( $source );
$img_count = $dom->getElementsByTagName( 'img' )->length;
$sanitizer = new AMP_Img_Sanitizer( $dom );
$sanitizer->sanitize();
$content = AMP_DOM_Utils::get_content_from_dom( $dom );
$this->assertEquals( $expected, $content );

$xpath = new DOMXPath( $dom );
$this->assertEquals( $img_count ? 1 : 0, $xpath->query( '/html/head/meta[ @name = "amp-experiments-opt-in" ][ @content = "amp-img-auto-sizes" ]' )->length );
}

/**
Expand Down

0 comments on commit 0180357

Please sign in to comment.