<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[48237] trunk: Media: Introduce `wp_img_tag_add_width_and_height_attr()` to add dimension attributes to images.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/48237">48237</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/48237","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-06-30 19:28:07 +0000 (Tue, 30 Jun 2020)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Media: Introduce `wp_img_tag_add_width_and_height_attr()` to add dimension attributes to images.

Following up on <a href="https://core.trac.wordpress.org/changeset/48170">[48170]</a>, this changeset moves the new logic to add missing `img` dimension attributes into a separate function that is run first within `wp_filter_content_tags()`. It also adds a utility function `wp_image_src_get_dimensions()` with logic reused from `wp_image_add_srcset_and_sizes()`, and it ensures that `width` and `height` attributes only get added if both of the attributes are missing on the original `img` tag.

This changeset furthermore improves test coverage and separates tests for the different aspects of `img` tag modification.

Props azaozz.
Fixes <a href="https://core.trac.wordpress.org/ticket/50367">#50367</a>. See <a href="https://core.trac.wordpress.org/ticket/44427">#44427</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmediaphp">trunk/src/wp-includes/media.php</a></li>
<li><a href="#trunktestsphpunittestsmediaphp">trunk/tests/phpunit/tests/media.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/media.php   2020-06-30 14:20:37 UTC (rev 48236)
+++ trunk/src/wp-includes/media.php     2020-06-30 19:28:07 UTC (rev 48237)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1492,10 +1492,41 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Determines an image's width and height dimensions based on the source file.
+ *
+ * @since 5.5.0
+ *
+ * @param string $image_src  The image source file.
+ * @param array  $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
+ * @return array|false Array with first element being the width and second element being the height,
+ *                     or false if dimensions cannot be determined.
+ */
+function wp_image_src_get_dimensions( $image_src, $image_meta ) {
+       $image_filename = wp_basename( $image_src );
+
+       if ( wp_basename( $image_meta['file'] ) === $image_filename ) {
+               return array(
+                       (int) $image_meta['width'],
+                       (int) $image_meta['height'],
+               );
+       }
+
+       foreach ( $image_meta['sizes'] as $image_size_data ) {
+               if ( $image_filename === $image_size_data['file'] ) {
+                       return array(
+                               (int) $image_size_data['width'],
+                               (int) $image_size_data['height'],
+                       );
+               }
+       }
+
+       return false;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.4.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 5.5.0 `width` and `height` are now added if not already present.
</del><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @see wp_calculate_image_srcset()
</span><span class="cx" style="display: block; padding: 0 10px">  * @see wp_calculate_image_sizes()
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1526,41 +1557,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $image;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $attr = '';
-
</del><span class="cx" style="display: block; padding: 0 10px">         $width  = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0;
</span><span class="cx" style="display: block; padding: 0 10px">        $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! $width || ! $height ) {
-               /*
-                * If attempts to parse the size value failed, attempt to use the image meta data to match
-                * the image file name from 'src' against the available sizes for an attachment.
-                */
-               $image_filename = wp_basename( $image_src );
-
-               if ( wp_basename( $image_meta['file'] ) === $image_filename ) {
-                       $width  = (int) $image_meta['width'];
-                       $height = (int) $image_meta['height'];
-               } else {
-                       foreach ( $image_meta['sizes'] as $image_size_data ) {
-                               if ( $image_filename === $image_size_data['file'] ) {
-                                       $width  = (int) $image_size_data['width'];
-                                       $height = (int) $image_size_data['height'];
-                                       break;
-                               }
-                       }
-               }
-
-               if ( ! $width || ! $height ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $width && $height ) {
+               $size_array = array( $width, $height );
+       } else {
+               $size_array = wp_image_src_get_dimensions( $image_src, $image_meta );
+               if ( ! $size_array ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return $image;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               // Add width and height if not present.
-               $attr .= ' ' . trim( image_hwstring( $width, $height ) );
</del><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $size_array = array( $width, $height );
-       $srcset     = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $srcset ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Check if there is already a 'sizes' attribute.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1573,19 +1582,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $srcset && $sizes ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Format the 'srcset' and 'sizes' string and escape attributes.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $attr .= sprintf( ' srcset="%s"', esc_attr( $srcset ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_string( $sizes ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( empty( $attr ) ) {
-               return $image;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Add extra attributes to the image markup.
+               return preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // Add extra attributes to the image markup.
-       return preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $image;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1621,8 +1628,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.5.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @see wp_img_tag_add_width_and_height_attr()
+ * @see wp_img_tag_add_srcset_and_sizes_attr()
</ins><span class="cx" style="display: block; padding: 0 10px">  * @see wp_img_tag_add_loading_attr()
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @see wp_img_tag_add_srcset_and_sizes_attr()
</del><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $content The HTML content to be filtered.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $context Optional. Additional context to pass to the filters.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1676,6 +1684,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">        foreach ( $images as $image => $attachment_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $filtered_image = $image;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Add 'width' and 'height' attributes if applicable.
+               if ( $attachment_id > 0 && false === strpos( $filtered_image, ' width=' ) && false === strpos( $filtered_image, ' height=' ) ) {
+                       $filtered_image = wp_img_tag_add_width_and_height_attr( $filtered_image, $context, $attachment_id );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Add 'srcset' and 'sizes' attributes if applicable.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $attachment_id > 0 && false === strpos( $filtered_image, ' srcset=' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1723,25 +1736,57 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $value = 'lazy';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Images should have dimension attributes for the `loading` attribute to be added.
-               if ( false === strpos( $image, ' width=' ) || false === strpos( $image, ' height=' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Images should have source and dimension attributes for the `loading` attribute to be added.
+               if ( false === strpos( $image, ' src=' ) || false === strpos( $image, ' width=' ) || false === strpos( $image, ' height=' ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return $image;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $quote = null;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Check if the img tag is valid (has `src` attribute) and get the quote character.
-               // In almost all cases it will have src and a double quote.
-               if ( false !== strpos( $image, ' src="' ) ) {
-                       $quote = '"';
-               } elseif ( preg_match( '/\ssrc\s*=(["\'])/', $image, $matches ) ) {
-                       $quote = $matches[1];
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $image;
+}
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $quote ) {
-                       $loading = "loading={$quote}{$value}{$quote}";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Adds `width` and `height` attributes to an `img` HTML tag.
+ *
+ * @since 5.5.0
+ *
+ * @param string $image         The HTML `img` tag where the attribute should be added.
+ * @param string $context       Additional context to pass to the filters.
+ * @param int    $attachment_id Image attachment ID.
+ * @return string Converted 'img' element with 'width' and 'height' attributes added.
+ */
+function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id ) {
+       $image_src         = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
+       list( $image_src ) = explode( '?', $image_src );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return str_replace( '<img', "<img {$loading}", $image );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Return early if we couldn't get the image source.
+       if ( ! $image_src ) {
+               return $image;
+       }
+
+       /**
+        * Filters whether to add the missing `width` and `height` HTML attributes to the img tag. Default `true`.
+        *
+        * Returning anything else than `true` will not add the attributes.
+        *
+        * @since 5.5.0
+        *
+        * @param bool   $value         The filtered value, defaults to `true`.
+        * @param string $image         The HTML `img` tag where the attribute should be added.
+        * @param string $context       Additional context about how the function was called or where the img tag is.
+        * @param int    $attachment_id The image attachment ID.
+        */
+       $add = apply_filters( 'wp_img_tag_add_width_and_height_attr', true, $image, $context, $attachment_id );
+
+       if ( true === $add ) {
+               $image_meta = wp_get_attachment_metadata( $attachment_id );
+               $size_array = wp_image_src_get_dimensions( $image_src, $image_meta );
+
+               if ( $size_array ) {
+                       $hw = trim( image_hwstring( $size_array[0], $size_array[1] ) );
+                       return str_replace( '<img', "<img {$hw}", $image );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestsmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media.php       2020-06-30 14:20:37 UTC (rev 48236)
+++ trunk/tests/phpunit/tests/media.php 2020-06-30 19:28:07 UTC (rev 48237)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1963,9 +1963,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 33641
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @ticket 50367
</del><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function test_wp_filter_content_tags() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_filter_content_tags_srcset_sizes() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $image_meta = wp_get_attachment_metadata( self::$large_id );
</span><span class="cx" style="display: block; padding: 0 10px">                $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1974,7 +1973,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Function used to build HTML for the editor.
</span><span class="cx" style="display: block; padding: 0 10px">                $img                  = get_image_tag( self::$large_id, '', '', '', 'medium' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $img                  = wp_img_tag_add_loading_attr( $img, 'test' );
</del><span class="cx" style="display: block; padding: 0 10px">                 $img_no_size_in_class = str_replace( 'size-', '', $img );
</span><span class="cx" style="display: block; padding: 0 10px">                $img_no_width_height  = str_replace( ' width="' . $size_array[0] . '"', '', $img );
</span><span class="cx" style="display: block; padding: 0 10px">                $img_no_width_height  = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1983,12 +1981,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $img_xhtml            = str_replace( ' />', '/>', $img );
</span><span class="cx" style="display: block; padding: 0 10px">                $img_html5            = str_replace( ' />', '>', $img );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $hwstring = image_hwstring( $size_array[0], $size_array[1] );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Manually add srcset and sizes to the markup from get_image_tag().
</span><span class="cx" style="display: block; padding: 0 10px">                $respimg                  = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
</span><span class="cx" style="display: block; padding: 0 10px">                $respimg_no_size_in_class = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_in_class );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $respimg_no_width_height  = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $hwstring . $srcset . ' ' . $sizes . ' />', $img_no_width_height );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $respimg_no_width_height  = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_width_height );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $respimg_with_sizes_attr  = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' />', $img_with_sizes_attr );
</span><span class="cx" style="display: block; padding: 0 10px">                $respimg_xhtml            = preg_replace( '|<img ([^>]+)/>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_xhtml );
</span><span class="cx" style="display: block; padding: 0 10px">                $respimg_html5            = preg_replace( '|<img ([^>]+)>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_html5 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2000,7 +1996,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        <p>Image, no size class. Should have srcset and sizes.</p>
</span><span class="cx" style="display: block; padding: 0 10px">                        %2$s
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        <p>Image, no width and height attributes. Should have width, height, srcset and sizes (from matching the file name).</p>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 <p>Image, no width and height attributes. Should have srcset and sizes (from matching the file name).</p>
</ins><span class="cx" style="display: block; padding: 0 10px">                         %3$s
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        <p>Image, no attachment ID class. Should NOT have srcset and sizes.</p>
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2018,7 +2014,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $content_unfiltered = sprintf( $content, $img, $img_no_size_in_class, $img_no_width_height, $img_no_size_id, $img_with_sizes_attr, $img_xhtml, $img_html5 );
</span><span class="cx" style="display: block; padding: 0 10px">                $content_filtered   = sprintf( $content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id, $respimg_with_sizes_attr, $respimg_xhtml, $respimg_html5 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Do not add width, height, and loading.
+               add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
+               add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
+               remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2032,7 +2035,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 34898
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 33641
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function test_wp_filter_content_tags_wrong() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_filter_content_tags_srcset_sizes_wrong() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
</span><span class="cx" style="display: block; padding: 0 10px">                $img = wp_img_tag_add_loading_attr( $img, 'test' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2045,7 +2048,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 33641
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function test_wp_filter_content_tags_with_preexisting_srcset() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_filter_content_tags_srcset_sizes_with_preexisting_srcset() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Generate HTML and add a dummy srcset attribute.
</span><span class="cx" style="display: block; padding: 0 10px">                $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
</span><span class="cx" style="display: block; padding: 0 10px">                $img = wp_img_tag_add_loading_attr( $img, 'test' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2532,10 +2535,54 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 50367
+        */
+       function test_wp_filter_content_tags_width_height() {
+               $image_meta = wp_get_attachment_metadata( self::$large_id );
+               $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
+
+               $img                 = get_image_tag( self::$large_id, '', '', '', 'medium' );
+               $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
+               $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
+               $img_no_width        = str_replace( ' width="' . $size_array[0] . '"', '', $img );
+               $img_no_height       = str_replace( ' height="' . $size_array[1] . '"', '', $img );
+
+               $hwstring = image_hwstring( $size_array[0], $size_array[1] );
+
+               // Manually add width and height to the markup from get_image_tag().
+               $respimg_no_width_height = str_replace( '<img ', '<img ' . $hwstring, $img_no_width_height );
+
+               $content = '
+                       <p>Image, with width and height. Should NOT be modified.</p>
+                       %1$s
+
+                       <p>Image, no width and height attributes. Should have width, height, srcset and sizes (from matching the file name).</p>
+                       %2$s
+
+                       <p>Image, no width but height attribute. Should NOT be modified.</p>
+                       %3$s
+
+                       <p>Image, no height but width attribute. Should NOT be modified.</p>
+                       %4$s';
+
+               $content_unfiltered = sprintf( $content, $img, $img_no_width_height, $img_no_width, $img_no_height );
+               $content_filtered   = sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height );
+
+               // Do not add loading, srcset, and sizes.
+               add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
+               add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
+
+               $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
+
+               remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
+               remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 44427
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 50367
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function test_wp_lazy_load_content_media() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_filter_content_tags_loading_lazy() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $image_meta = wp_get_attachment_metadata( self::$large_id );
</span><span class="cx" style="display: block; padding: 0 10px">                $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2570,11 +2617,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe );
</span><span class="cx" style="display: block; padding: 0 10px">                $content_filtered   = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $iframe );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Do not add srcset and sizes.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Do not add width, height, srcset, and sizes.
+               add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2581,7 +2630,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 44427
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function test_wp_lazy_load_content_media_opted_in() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_filter_content_tags_loading_lazy_opted_in() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $img      = get_image_tag( self::$large_id, '', '', '', 'medium' );
</span><span class="cx" style="display: block; padding: 0 10px">                $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2606,7 +2655,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 44427
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function test_wp_lazy_load_content_media_opted_out() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_filter_content_tags_loading_lazy_opted_out() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $content = '
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2626,13 +2675,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @ticket 44427
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 50367
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function test_wp_img_tag_add_loading_attr_single_quote() {
-               $img = "<img src='example.png' alt='' width='300' height='225' />";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_img_tag_add_loading_attr_without_src() {
+               $img = '<img alt=" width="300" height="225" />';
</ins><span class="cx" style="display: block; padding: 0 10px">                 $img = wp_img_tag_add_loading_attr( $img, 'test' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertContains( " loading='lazy'", $img );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertNotContains( ' loading="lazy"', $img );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre>
</div>
</div>

</body>
</html>