diff --git a/includes/sanitizers/class-amp-img-sanitizer.php b/includes/sanitizers/class-amp-img-sanitizer.php index c4ce753e768..4673ec6fe9b 100644 --- a/includes/sanitizers/class-amp-img-sanitizer.php +++ b/includes/sanitizers/class-amp-img-sanitizer.php @@ -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 and . + */ + $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 ); + } } /** diff --git a/tests/test-amp-img-sanitizer.php b/tests/test-amp-img-sanitizer.php index d8d103a4b82..4455a1d7792 100644 --- a/tests/test-amp-img-sanitizer.php +++ b/tests/test-amp-img-sanitizer.php @@ -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 ); } /**