Make WordPress Core

Changeset 41724


Ignore:
Timestamp:
10/04/2017 02:49:19 AM (9 years ago)
Author:
joemcgill
Message:

Media: Use max-width for default captions.

This alters the HTML output of the image caption shortcode to use
max-width instead of width to improve compatibility with
flexible layouts.

Props aaronrutley, desrosj.
Fixes #33981.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r41383 r41724  
    15791579        $style = '';
    15801580        if ( $caption_width ) {
    1581                 $style = 'style="width: ' . (int) $caption_width . 'px" ';
     1581                $style = 'style="max-width: ' . (int) $caption_width . 'px" ';
    15821582        }
    15831583
  • trunk/tests/phpunit/tests/media.php

    r41693 r41724  
    2929                parent::setUp();
    3030                $this->caption = 'A simple caption.';
     31                $this->alternate_caption = 'Alternate caption.';
    3132                $this->html_content = <<<CAP
    3233A <strong class='classy'>bolded</strong> <em>caption</em> with a <a href="#">link</a>.
     
    5253        }
    5354
    54         function test_img_caption_shortcode_with_bad_attr() {
    55                 $result = img_caption_shortcode( array(), 'content' );
    56                 $this->assertEquals( 'content', 'content' );
     55        /**
     56         * @ticket 33981
     57         */
     58        function test_img_caption_shortcode_with_empty_params_but_content() {
     59                $result = img_caption_shortcode( array(), $this->caption );
     60                $this->assertEquals( $this->caption, $result );
     61        }
     62
     63        /**
     64         * @ticket 33981
     65         */
     66        function test_img_caption_shortcode_short_circuit_filter() {
     67                add_filter( 'img_caption_shortcode', array( $this, '_return_alt_caption' ) );
     68
     69                $result = img_caption_shortcode( array(), $this->caption );
     70                $this->assertEquals( $this->alternate_caption, $result );
     71        }
     72
     73        /**
     74         * Filter used in test_img_caption_shortcode_short_circuit_filter()
     75         */
     76        function _return_alt_caption() {
     77                return $this->alternate_caption;
     78        }
     79
     80        /**
     81         * @ticket 33981
     82         */
     83        function test_img_caption_shortcode_empty_width() {
     84                $result = img_caption_shortcode(
     85                        array(
     86                                'width' => 0,
     87                        ),
     88                        $this->caption
     89                );
     90                $this->assertEquals( $this->caption, $result );
     91        }
     92
     93        /**
     94         * @ticket 33981
     95         */
     96        function test_img_caption_shortcode_empty_caption() {
     97                $result = img_caption_shortcode(
     98                        array(
     99                                'caption' => '',
     100                        )
     101                );
     102                $this->assertNull( $result );
     103        }
     104
     105        /**
     106         * @ticket 33981
     107         */
     108        function test_img_caption_shortcode_empty_caption_and_content() {
     109                $result = img_caption_shortcode(
     110                        array(
     111                                'caption' => '',
     112                        ),
     113                        $this->caption
     114                );
     115                $this->assertEquals( $this->caption, $result );
    57116        }
    58117
     
    67126
    68127                if ( current_theme_supports( 'html5', 'caption' ) ) {
    69                         $this->assertEquals( 1, preg_match_all( "/width: 20/", $result, $_r ) );
     128                        $this->assertEquals( 1, preg_match_all( "/max-width: 20/", $result, $_r ) );
    70129                } else {
    71                         $this->assertEquals( 1, preg_match_all( "/width: 30/", $result, $_r ) );
     130                        $this->assertEquals( 1, preg_match_all( "/max-width: 30/", $result, $_r ) );
    72131                }
    73132        }
     
    85144                $this->assertEquals( 1, preg_match_all( '/id="myId"/', $result, $_r ) );
    86145                $this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
     146        }
     147
     148        function test_img_caption_shortcode_with_old_format_and_class() {
     149                $result = img_caption_shortcode(
     150                        array(
     151                                'width' => 20,
     152                                'class' => 'some-class another-class',
     153                                'caption' => $this->caption,
     154                        )
     155                );
     156                $this->assertEquals( 1, preg_match_all( '/wp-caption alignnone some-class another-class/', $result, $_r ) );
     157
    87158        }
    88159
  • trunk/tests/phpunit/tests/shortcode.php

    r41026 r41724  
    478478                        array(
    479479                                '[caption caption="test" width="2"]<div>hello</div>[/caption]',
    480                                 '<div style="width: 12px" class="wp-caption alignnone"><div>hello</div><p class="wp-caption-text">test</p></div>',
     480                                '<div style="max-width: 12px" class="wp-caption alignnone"><div>hello</div><p class="wp-caption-text">test</p></div>',
    481481                        ),
    482482                        array(
Note: See TracChangeset for help on using the changeset viewer.