Changeset 48170
- Timestamp:
- 06/25/2020 06:43:25 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/media.php
r48110 r48170 1496 1496 * 1497 1497 * @since 4.4.0 1498 * @since 5.5.0 `width` and `height` are now added if not already present. 1498 1499 * 1499 1500 * @see wp_calculate_image_srcset() … … 1525 1526 return $image; 1526 1527 } 1528 1529 $attr = ''; 1527 1530 1528 1531 $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; … … 1548 1551 } 1549 1552 } 1550 } 1551 1552 if ( ! $width || ! $height ) { 1553 return $image; 1553 1554 if ( ! $width || ! $height ) { 1555 return $image; 1556 } 1557 1558 // Add width and height if not present. 1559 $attr .= ' ' . trim( image_hwstring( $width, $height ) ); 1554 1560 } 1555 1561 … … 1568 1574 if ( $srcset && $sizes ) { 1569 1575 // Format the 'srcset' and 'sizes' string and escape attributes. 1570 $attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) );1576 $attr .= sprintf( ' srcset="%s"', esc_attr( $srcset ) ); 1571 1577 1572 1578 if ( is_string( $sizes ) ) { 1573 1579 $attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) ); 1574 1580 } 1575 1576 // Add 'srcset' and 'sizes' attributes to the image markup. 1577 $image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image ); 1578 } 1579 1580 return $image; 1581 } 1582 1583 if ( empty( $attr ) ) { 1584 return $image; 1585 } 1586 1587 // Add extra attributes to the image markup. 1588 return preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image ); 1581 1589 } 1582 1590 … … 1713 1721 if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { 1714 1722 $value = 'lazy'; 1723 } 1724 1725 // Images should have dimension attributes for the `loading` attribute 1726 // to be added. 1727 if ( false === strpos( $image, ' width=' ) || false === strpos( $image, ' height=' ) ) { 1728 return $image; 1715 1729 } 1716 1730 -
trunk/tests/phpunit/tests/media.php
r48121 r48170 1964 1964 /** 1965 1965 * @ticket 33641 1966 * @ticket 50367 1966 1967 */ 1967 1968 function test_wp_filter_content_tags() { … … 1983 1984 $img_html5 = str_replace( ' />', '>', $img ); 1984 1985 1986 $hwstring = image_hwstring( $size_array[0], $size_array[1] ); 1987 1985 1988 // Manually add srcset and sizes to the markup from get_image_tag(). 1986 1989 $respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img ); 1987 1990 $respimg_no_size_in_class = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_in_class ); 1988 $respimg_no_width_height = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $ srcset . ' ' . $sizes . ' />', $img_no_width_height );1991 $respimg_no_width_height = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $hwstring . $srcset . ' ' . $sizes . ' />', $img_no_width_height ); 1989 1992 $respimg_with_sizes_attr = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' />', $img_with_sizes_attr ); 1990 1993 $respimg_xhtml = preg_replace( '|<img ([^>]+)/>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_xhtml ); … … 1998 2001 %2$s 1999 2002 2000 <p>Image, no width and height attributes. Should have srcset and sizes (from matching the file name).</p>2003 <p>Image, no width and height attributes. Should have width, height, srcset and sizes (from matching the file name).</p> 2001 2004 %3$s 2002 2005 … … 2531 2534 /** 2532 2535 * @ticket 44427 2536 * @ticket 50367 2533 2537 */ 2534 2538 function test_wp_lazy_load_content_media() { 2535 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2536 $img_xhtml = str_replace( ' />', '/>', $img ); 2537 $img_html5 = str_replace( ' />', '>', $img ); 2538 $iframe = '<iframe src="https://www.example.com"></iframe>'; 2539 $image_meta = wp_get_attachment_metadata( self::$large_id ); 2540 $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' ); 2541 2542 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2543 $img_xhtml = str_replace( ' />', '/>', $img ); 2544 $img_html5 = str_replace( ' />', '>', $img ); 2545 $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img ); 2546 $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height ); 2547 $iframe = '<iframe src="https://www.example.com"></iframe>'; 2539 2548 2540 2549 $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' ); … … 2552 2561 <p>Image, HTML 5.0 style.</p> 2553 2562 %3$s 2554 <p>Image, with pre-existing "loading" attribute.</p> 2563 <p>Image, with pre-existing "loading" attribute. Should not be modified.</p> 2564 %4$s 2565 <p>Image, without dimension attributes. Should not be modified.</p> 2555 2566 %5$s 2556 2567 <p>Iframe, standard. Should not be modified.</p> 2557 % 4$s';2558 2559 $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $i frame, $img_eager);2560 $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $i frame, $img_eager);2568 %6$s'; 2569 2570 $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe ); 2571 $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $iframe ); 2561 2572 2562 2573 // Do not add srcset and sizes.
Note: See TracChangeset
for help on using the changeset viewer.