Ticket #53232: 53232.10.diff
File 53232.10.diff, 24.9 KB (added by , 3 years ago) |
---|
-
src/wp-includes/media.php
diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 2b46167afca..1169bcdfbb8 100644
a b function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon 1011 1011 * @param string|array $attr { 1012 1012 * Optional. Attributes for the image markup. 1013 1013 * 1014 * @type string $src Image attachment URL. 1015 * @type string $class CSS class name or space-separated list of classes. 1016 * Default `attachment-$size_class size-$size_class`, 1017 * where `$size_class` is the image size being requested. 1018 * @type string $alt Image description for the alt attribute. 1019 * @type string $srcset The 'srcset' attribute value. 1020 * @type string $sizes The 'sizes' attribute value. 1021 * @type string|false $loading The 'loading' attribute value. Passing a value of false 1022 * will result in the attribute being omitted for the image. 1023 * Defaults to 'lazy', depending on wp_lazy_loading_enabled(). 1014 * @type string $src Image attachment URL. 1015 * @type string $class CSS class name or space-separated list of classes. 1016 * Default `attachment-$size_class size-$size_class`, 1017 * where `$size_class` is the image size being requested. 1018 * @type string $alt Image description for the alt attribute. 1019 * @type string $srcset The 'srcset' attribute value. 1020 * @type string $sizes The 'sizes' attribute value. 1021 * @type string|false $loading The 'loading' attribute value. Passing a value of false 1022 * will result in the attribute being omitted for the image. 1023 * Defaults to 'lazy', depending on wp_lazy_loading_enabled(). 1024 * @type string $decoding The 'decoding' attribute value. Possible values are 1025 * 'async' (default), 'sync', or 'auto'. 1024 1026 * } 1025 1027 * @return string HTML img element or empty string on failure. 1026 1028 */ … … function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f 1040 1042 } 1041 1043 1042 1044 $default_attr = array( 1043 'src' => $src, 1044 'class' => "attachment-$size_class size-$size_class", 1045 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), 1045 'src' => $src, 1046 'class' => "attachment-$size_class size-$size_class", 1047 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), 1048 'decoding' => 'async', 1046 1049 ); 1047 1050 1048 1051 // Add `loading` attribute. … … function wp_filter_content_tags( $content, $context = null ) { 1843 1846 $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context ); 1844 1847 } 1845 1848 1849 // Add 'decoding=async' attribute unless a 'decoding' attribute is already present. 1850 if ( ! str_contains( $filtered_image, ' decoding=' ) ) { 1851 $filtered_image = wp_img_tag_add_decoding_attr( $filtered_image, $context ); 1852 } 1853 1846 1854 /** 1847 1855 * Filters an img tag within the content for a given context. 1848 1856 * … … function wp_img_tag_add_loading_attr( $image, $context ) { 1934 1942 return $image; 1935 1943 } 1936 1944 1945 /** 1946 * Add `decoding` attribute to an `img` HTML tag. 1947 * 1948 * The `decoding` attribute allows developers to indicate whether the 1949 * browser can decode the image off the main thread (`async`), on the 1950 * main thread (`sync`) or as determined by the browser (`auto`). 1951 * 1952 * By default WordPress adds `decoding="async"` to images but developers 1953 * can use the {@see 'wp_img_tag_add_decoding_attr'} filter to modify this 1954 * to remove the attribute or set it to another accepted value. 1955 * 1956 * @since 6.0.0 1957 * 1958 * @param string $image The HTML `img` tag where the attribute should be added. 1959 * @param string $context Additional context to pass to the filters. 1960 * 1961 * @return string Converted `img` tag with `decoding` attribute added. 1962 */ 1963 function wp_img_tag_add_decoding_attr( $image, $context ) { 1964 /** 1965 * Filters the `decoding` attribute value to add to an image. Default `async`. 1966 * 1967 * Returning a falsey value will not add the attribute. 1968 * 1969 * @since 6.0.0 1970 * 1971 * @param string|false|null $value The `decoding` attribute value. Returning a falsey value will result in 1972 * the attribute being omitted for the image. Otherwise, it may be: 1973 * 'async' (default), 'sync', or 'auto'. 1974 * @param string $image The HTML `img` tag to be filtered. 1975 * @param string $context Additional context about how the function was called or where the img tag is. 1976 */ 1977 $value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context ); 1978 if ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) { 1979 $image = str_replace( '<img ', '<img decoding="' . esc_attr( $value ) . '" ', $image ); 1980 } 1981 1982 return $image; 1983 } 1984 1937 1985 /** 1938 1986 * Adds `width` and `height` attributes to an `img` HTML tag. 1939 1987 * -
src/wp-includes/pluggable.php
diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index fdbfdea2506..71e3079cc71 100644
a b function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = 2694 2694 'force_display' => false, 2695 2695 'loading' => null, 2696 2696 'extra_attr' => '', 2697 'decoding' => 'async', 2697 2698 ); 2698 2699 2699 2700 if ( wp_lazy_loading_enabled( 'img', 'get_avatar' ) ) { … … function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = 2781 2782 $extra_attr .= "loading='{$loading}'"; 2782 2783 } 2783 2784 2785 if ( in_array( $args['decoding'], array( 'async', 'sync', 'auto' ) ) && ! preg_match( '/\bdecoding\s*=/', $extra_attr ) ) { 2786 if ( ! empty( $extra_attr ) ) { 2787 $extra_attr .= ' '; 2788 } 2789 $extra_attr .= "decoding='{$args['decoding']}'"; 2790 } 2791 2784 2792 $avatar = sprintf( 2785 2793 "<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>", 2786 2794 esc_attr( $args['alt'] ), -
tests/phpunit/tests/avatar.php
diff --git a/tests/phpunit/tests/avatar.php b/tests/phpunit/tests/avatar.php index 336ab611e26..026809b4a1b 100644
a b public function get_avatar_comment_types_filter( $comment_types ) { 169 169 170 170 public function test_get_avatar() { 171 171 $img = get_avatar( 1 ); 172 $this->assertSame( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' loading='lazy' />$|", $img ), 1 );172 $this->assertSame( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' loading='lazy' decoding='async'/>$|", $img ), 1 ); 173 173 } 174 174 175 175 public function test_get_avatar_size() { -
tests/phpunit/tests/media.php
diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 8f3340aaf78..0f3f581196d 100644
a b public function test_oembed_explicit_media_link() { 1474 1474 public function test_wp_get_attachment_image_defaults() { 1475 1475 $image = image_downsize( self::$large_id, 'thumbnail' ); 1476 1476 $expected = sprintf( 1477 '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" />',1477 '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" />', 1478 1478 $image[1], 1479 1479 $image[2], 1480 1480 $image[0] … … public function test_wp_get_attachment_image_with_alt() { 1512 1512 1513 1513 $image = image_downsize( self::$large_id, 'thumbnail' ); 1514 1514 $expected = sprintf( 1515 '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" />',1515 '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" decoding="async" loading="lazy" />', 1516 1516 $image[1], 1517 1517 $image[2], 1518 1518 $image[0] … … public function test_wp_filter_content_tags_srcset_sizes() { 2248 2248 $respimg_xhtml, 2249 2249 $respimg_html5 2250 2250 ); 2251 $content_filtered = wp_img_tag_add_decoding_attr( $content_filtered, 'the_content' ); 2251 2252 2252 2253 // Do not add width, height, and loading. 2253 2254 add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); … … public function test_wp_filter_content_tags_srcset_sizes() { 2273 2274 public function test_wp_filter_content_tags_srcset_sizes_wrong() { 2274 2275 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2275 2276 $img = wp_img_tag_add_loading_attr( $img, 'test' ); 2277 $img = wp_img_tag_add_decoding_attr( $img, 'the_content' ); 2276 2278 2277 2279 // Replace the src URL. 2278 2280 $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $img ); … … public function test_wp_filter_content_tags_srcset_sizes_with_preexisting_srcset 2287 2289 // Generate HTML and add a dummy srcset attribute. 2288 2290 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2289 2291 $img = wp_img_tag_add_loading_attr( $img, 'test' ); 2292 $img = wp_img_tag_add_decoding_attr( $img, 'the_content' ); 2290 2293 $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img ); 2291 2294 2292 2295 // The content filter should return the image unchanged. … … public function test_wp_filter_content_tags_handles_duplicate_img_and_iframe_tag 2334 2337 */ 2335 2338 public function test_wp_filter_content_tags_filter_with_identical_image_tags_custom_attributes() { 2336 2339 $img = get_image_tag( self::$large_id, '', '', '', 'large' ); 2337 $img = str_replace( '<img ', '<img srcset="custom" sizes="custom" loading="custom" ', $img );2340 $img = str_replace( '<img ', '<img srcset="custom" sizes="custom" loading="custom" decoding="custom"', $img ); 2338 2341 $content = "$img\n$img"; 2339 2342 2340 2343 add_filter( … … public function test_wp_filter_content_tags_filter_with_identical_image_tags_dis 2359 2362 add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2360 2363 add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2361 2364 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2365 add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 2362 2366 2363 2367 add_filter( 2364 2368 'wp_content_img_tag', … … public function test_wp_filter_content_tags_schemes() { 2461 2465 $respimg_https, 2462 2466 $respimg_relative 2463 2467 ); 2468 $expected = wp_img_tag_add_decoding_attr( $expected, 'the_content' ); 2464 2469 2465 2470 $actual = wp_filter_content_tags( $unfiltered ); 2466 2471 … … public function test_wp_get_attachment_image_should_use_wp_get_attachment_metada 2603 2608 2604 2609 $expected = '<img width="999" height="999" ' . 2605 2610 'src="' . $uploads_url . 'test-image-testsize-999x999.jpg" ' . 2606 'class="attachment-testsize size-testsize" alt="" loading="lazy" ' .2611 'class="attachment-testsize size-testsize" alt="" decoding="async" loading="lazy" ' . 2607 2612 'srcset="' . $uploads_url . 'test-image-testsize-999x999.jpg 999w, ' . $uploads_url . $basename . '-150x150.jpg 150w" ' . 2608 2613 'sizes="(max-width: 999px) 100vw, 999px" />'; 2609 2614 … … public function test_wp_filter_content_tags_width_height() { 2910 2915 %4$s'; 2911 2916 2912 2917 $content_unfiltered = sprintf( $content, $img, $img_no_width_height, $img_no_width, $img_no_height ); 2913 $content_filtered = sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height);2918 $content_filtered = wp_img_tag_add_decoding_attr( sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height ), 'the_content' ); 2914 2919 2915 2920 // Do not add loading, srcset, and sizes. 2916 2921 add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); … … public function test_wp_filter_content_tags_loading_lazy() { 2968 2973 %8$s'; 2969 2974 2970 2975 $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe, $iframe_eager, $iframe_no_width_height ); 2971 $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $lazy_iframe, $iframe_eager, $iframe_no_width_height);2976 $content_filtered = wp_img_tag_add_decoding_attr( sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $lazy_iframe, $iframe_eager, $iframe_no_width_height ), 'the_content' ); 2972 2977 2973 2978 // Do not add width, height, srcset, and sizes. 2974 2979 add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); … … public function test_wp_filter_content_tags_loading_lazy_opted_in() { 2997 3002 %2$s'; 2998 3003 2999 3004 $content_unfiltered = sprintf( $content, $img, $iframe ); 3000 $content_filtered = sprintf( $content, $lazy_img, $lazy_iframe);3005 $content_filtered = sprintf( $content, wp_img_tag_add_decoding_attr( $lazy_img, 'the_content' ), $lazy_iframe, 'the_content' ); 3001 3006 3002 3007 // Do not add srcset and sizes while testing. 3003 3008 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); … … public function test_wp_filter_content_tags_loading_lazy_opted_in() { 3015 3020 * @ticket 50756 3016 3021 */ 3017 3022 public function test_wp_filter_content_tags_loading_lazy_opted_out() { 3018 $img = get_image_tag( self::$large_id, '', '', '', 'medium' );3023 $img = wp_img_tag_add_decoding_attr( get_image_tag( self::$large_id, '', '', '', 'medium' ), 'the_content' ); 3019 3024 $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; 3020 3025 3021 3026 $content = ' … … function() { 3479 3484 3480 3485 // Following the threshold of 2, the first two content media elements should not be lazy-loaded. 3481 3486 $content_unfiltered = $img1 . $iframe1 . $img2 . $img3 . $iframe2; 3482 $content_expected = $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2;3487 $content_expected = wp_img_tag_add_decoding_attr( $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2, 'the_content' ); 3483 3488 3484 3489 $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) ); 3485 3490 $wp_the_query = $wp_query; -
tests/phpunit/tests/media/getAdjacentImageLink.php
diff --git a/tests/phpunit/tests/media/getAdjacentImageLink.php b/tests/phpunit/tests/media/getAdjacentImageLink.php index 735c0ff15cf..7f413cab2ed 100644
a b public function data_get_adjacent_image_link() { 32 32 'when has previous link' => array( 33 33 'current_attachment_index' => 3, 34 34 'expected_attachment_index' => 2, 35 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src=" http://example.org/wp-content/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',35 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>', 36 36 ), 37 37 'with text when has previous link' => array( 38 38 'current_attachment_index' => 3, … … public function data_get_adjacent_image_link() { 43 43 'when has next link' => array( 44 44 'current_attachment_index' => 4, 45 45 'expected_attachment_index' => 5, 46 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src=" http://example.org/wp-content/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',46 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>', 47 47 'args' => array( 'prev' => false ), 48 48 ), 49 49 'with text when has next link' => array( -
tests/phpunit/tests/media/getNextImageLink.php
diff --git a/tests/phpunit/tests/media/getNextImageLink.php b/tests/phpunit/tests/media/getNextImageLink.php index 86a843cbfcd..b26f1fcd69e 100644
a b public function data_get_next_image_link() { 31 31 'when has next link' => array( 32 32 'current_attachment_index' => 4, 33 33 'expected_attachment_index' => 5, 34 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src=" http://example.org/wp-content/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',34 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>', 35 35 ), 36 36 'with text when has next link' => array( 37 37 'current_attachment_index' => 4, -
tests/phpunit/tests/media/getPreviousImageLink.php
diff --git a/tests/phpunit/tests/media/getPreviousImageLink.php b/tests/phpunit/tests/media/getPreviousImageLink.php index 2d2d511e4fd..36652e75ad6 100644
a b public function data_get_previous_image_link() { 31 31 'when has previous link' => array( 32 32 'current_attachment_index' => 3, 33 33 'expected_attachment_index' => 2, 34 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src=" http://example.org/wp-content/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',34 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>', 35 35 ), 36 36 'with text when has previous link' => array( 37 37 'current_attachment_index' => 3, -
tests/phpunit/tests/media/nextImageLink.php
diff --git a/tests/phpunit/tests/media/nextImageLink.php b/tests/phpunit/tests/media/nextImageLink.php index 7799779fa80..569e1900ce6 100644
a b public function data_next_image_link() { 30 30 'when has next link' => array( 31 31 'current_attachment_index' => 4, 32 32 'expected_attachment_index' => 5, 33 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src=" http://example.org/wp-content/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',33 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>', 34 34 ), 35 35 'with text when has next link' => array( 36 36 'current_attachment_index' => 4, -
tests/phpunit/tests/media/previousImageLink.php
diff --git a/tests/phpunit/tests/media/previousImageLink.php b/tests/phpunit/tests/media/previousImageLink.php index 11d6583d6a2..c75e0835379 100644
a b public function data_previous_image_link() { 30 30 'when has previous link' => array( 31 31 'current_attachment_index' => 3, 32 32 'expected_attachment_index' => 2, 33 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src=" http://example.org/wp-content/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',33 'expected' => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>', 34 34 ), 35 35 'with text when has previous link' => array( 36 36 'current_attachment_index' => 3, -
new file tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php
diff --git a/tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php b/tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php new file mode 100644 index 00000000000..76fa203b7e7
- + 1 <?php 2 3 /** 4 * Tests the `wp_img_tag_add_decoding_attr()` function. 5 * 6 * @group media 7 * @covers ::wp_img_tag_add_decoding_attr 8 */ 9 class Tests_Media_Wp_Img_Tag_Add_Decoding_Attr extends WP_UnitTestCase { 10 /** 11 * Tests that the `wp_img_tag_add_decoding_attr()` function should add 12 * the 'decoding' attribute. 13 * 14 * @ticket 53232 15 * 16 * @dataProvider data_should_add_decoding_attr 17 * 18 * @param string $image The HTML `img` tag where the attribute should be added. 19 * @param string $context Additional context to pass to the filters. 20 * @param string $decoding The value for the 'decoding' attribute. 'no value' for default. 21 * @param string $expected The expected `img` tag. 22 */ 23 public function test_should_add_decoding_attr( $image, $context, $decoding, $expected ) { 24 // Falsey values are allowed in the filter, cannot use `null` or `false` here. 25 if ( 'no value' !== $decoding ) { 26 add_filter( 27 'wp_img_tag_add_decoding_attr', 28 static function( $value ) use ( $decoding ) { 29 return $decoding; 30 } 31 ); 32 } 33 34 $this->assertSame( $expected, wp_img_tag_add_decoding_attr( $image, $context ) ); 35 } 36 37 /** 38 * Data provider. 39 * 40 * @return array 41 */ 42 public function data_should_add_decoding_attr() { 43 return array( 44 'default' => array( 45 'image' => '<img src="my-image.png">', 46 'context' => '', 47 'decoding' => 'no value', 48 'expected' => '<img decoding="async" src="my-image.png">', 49 ), 50 'async' => array( 51 'image' => '<img src="my-image.png">', 52 'context' => '', 53 'decoding' => 'async', 54 'expected' => '<img decoding="async" src="my-image.png">', 55 ), 56 'sync' => array( 57 'image' => '<img src="my-image.png">', 58 'context' => '', 59 'decoding' => 'sync', 60 'expected' => '<img decoding="sync" src="my-image.png">', 61 ), 62 'auto' => array( 63 'image' => '<img src="my-image.png">', 64 'context' => '', 65 'decoding' => 'auto', 66 'expected' => '<img decoding="auto" src="my-image.png">', 67 ), 68 ); 69 } 70 71 /** 72 * Tests that the `wp_img_tag_add_decoding_attr()` function should not add 73 * the 'decoding' attribute. 74 * 75 * @ticket 53232 76 * 77 * @dataProvider data_should_not_add_decoding_attr 78 * 79 * @param string $image The HTML `img` tag where the attribute should be added. 80 * @param string $context Additional context to pass to the filters. 81 * @param mixed $decoding The value for the 'decoding' attribute. 'no value' for default. 82 * @param string $expected The expected `img` tag. 83 */ 84 public function test_should_not_add_decoding_attr( $image, $context, $decoding, $expected ) { 85 // Falsey values are allowed in the filter, cannot use `null` or `false` here. 86 if ( 'no value' !== $decoding ) { 87 add_filter( 88 'wp_img_tag_add_decoding_attr', 89 static function( $value ) use ( $decoding ) { 90 return $decoding; 91 } 92 ); 93 } 94 95 $this->assertSame( $expected, wp_img_tag_add_decoding_attr( $image, $context, $expected ) ); 96 } 97 98 /** 99 * Data provider. 100 * 101 * @return array 102 */ 103 public function data_should_not_add_decoding_attr() { 104 return array( 105 // Unhappy paths. 106 'lazy (unaccepted value)' => array( 107 'image' => '<img src="my-image.png">', 108 'context' => '', 109 'decoding' => 'lazy', 110 'expected' => '<img src="my-image.png">', 111 ), 112 'a non-string value' => array( 113 'image' => '<img src="my-image.png">', 114 'context' => '', 115 'decoding' => array( 'sync' ), 116 'expected' => '<img src="my-image.png">', 117 ), 118 119 // Falsey values. 120 'false' => array( 121 'image' => '<img src="my-image.png">', 122 'context' => '', 123 'decoding' => false, 124 'expected' => '<img src="my-image.png">', 125 ), 126 'null' => array( 127 'image' => '<img src="my-image.png">', 128 'context' => '', 129 'decoding' => null, 130 'expected' => '<img src="my-image.png">', 131 ), 132 'empty string' => array( 133 'image' => '<img src="my-image.png">', 134 'context' => '', 135 'decoding' => '', 136 'expected' => '<img src="my-image.png">', 137 ), 138 'empty array' => array( 139 'image' => '<img src="my-image.png">', 140 'context' => '', 141 'decoding' => array(), 142 'expected' => '<img src="my-image.png">', 143 ), 144 '0 int' => array( 145 'image' => '<img src="my-image.png">', 146 'context' => '', 147 'decoding' => 0, 148 'expected' => '<img src="my-image.png">', 149 ), 150 '0 string' => array( 151 'image' => '<img src="my-image.png">', 152 'context' => '', 153 'decoding' => '0', 154 'expected' => '<img src="my-image.png">', 155 ), 156 '0.0 float' => array( 157 'image' => '<img src="my-image.png">', 158 'context' => '', 159 'decoding' => 0.0, 160 'expected' => '<img src="my-image.png">', 161 ), 162 ); 163 } 164 }