Make WordPress Core

Ticket #26642: 26642.7.diff

File 26642.7.diff, 5.0 KB (added by jond3r, 11 years ago)

Code and test for the option "don't add 10px"

  • src/wp-includes/class-wp-editor.php

     
    333333                                        'preview_styles' => 'font-family font-size font-weight font-style text-decoration text-transform',
    334334
    335335                                        'wpeditimage_disable_captions' => $no_captions,
     336                                        'wpeditimage_no_add_10px_captions' => current_theme_supports( 'no-add-10px-captions' ),
    336337                                        'plugins' => implode( ',', $plugins ),
    337338                                );
    338339
  • src/wp-includes/media.php

     
    765765        if ( ! empty( $atts['id'] ) )
    766766                $atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" ';
    767767
    768         $caption_width = 10 + $atts['width'];
     768        $caption_width = $atts['width'];
    769769
     770        if ( ! current_theme_supports( 'no-add-10px-captions' ) ) {
     771                $caption_width += 10;
     772        }
     773
    770774        /**
    771775         * Filter the width of an image's caption.
    772776         *
  • src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js

     
    22tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
    33        function parseShortcode( content ) {
    44                return content.replace( /(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) {
    5                         var id, cls, w, cap, img, width,
     5                        var id, cls, w, cap, img, width, style,
    66                                trim = tinymce.trim;
    77
    88                        id = b.match( /id=['"]([^'"]*)['"] ?/ );
     
    4747                                return c;
    4848                        }
    4949
    50                         width = parseInt( w, 10 ) + 10;
     50                        width = parseInt( w, 10 );
    5151
    52                         return '<div class="mceTemp"><dl id="'+ id +'" class="wp-caption '+ cls +'" style="width: '+ width +'px">' +
     52                        if ( ! editor.getParam( 'wpeditimage_no_add_10px_captions' ) ) {
     53                                width += 10;
     54                        }
     55                        style = ' style="width: '+ width +'px"';
     56
     57                        return '<div class="mceTemp"><dl id="'+ id +'" class="wp-caption '+ cls +'"'+ style +'>' +
    5358                                '<dt class="wp-caption-dt">'+ img +'</dt><dd class="wp-caption-dd">'+ cap +'</dd></dl></div>';
    5459                });
    5560        }
     
    189194
    190195                        html = createImageAndLink( imageData, 'html' );
    191196
    192                         width = imageData.width + 10;
    193197                        className = 'align' + imageData.align;
    194198
     199                        width = imageData.width;
     200
     201                        if ( ! editor.getParam( 'wpeditimage_no_add_10px_captions' ) ) {
     202                                width += 10;
     203                        }
     204                        style = ' style="width: '+ width +'px"';
     205
    195206                        //TODO: shouldn't add the id attribute if it isn't an attachment
    196207
    197208                        // should create a new function for generating the caption markup
    198                         html =  '<dl id="'+ imageData.attachment_id +'" class="wp-caption '+ className +'" style="width: '+ width +'px">' +
     209                        html =  '<dl id="'+ imageData.attachment_id +'" class="wp-caption '+ className +'"'+ style +'>' +
    199210                                '<dt class="wp-caption-dt">'+ html + '</dt><dd class="wp-caption-dd">'+ imageData.caption +'</dd></dl>';
    200211
    201212                        node = editor.dom.create( 'div', { 'class': 'mceTemp' }, html );
     
    476487                                        node = editor.selection.getNode();
    477488
    478489                                        if ( data.width ) {
    479                                                 captionWidth = parseInt( data.width, 10 ) + 10;
     490                                                captionWidth = parseInt( data.width, 10 );
     491
     492                                                if ( ! editor.getParam( 'wpeditimage_no_add_10px_captions' ) ) {
     493                                                        captionWidth += 10;
     494                                                }
    480495                                                captionWidth = ' style="width: '+ captionWidth +'px"';
    481496                                        }
    482497
     
    540555                                                captionWidth = data.width || imgNode.clientWidth;
    541556
    542557                                                if ( captionWidth ) {
    543                                                         captionWidth = parseInt( captionWidth, 10 ) + 10;
     558                                                        captionWidth = parseInt( captionWidth, 10 );
     559
     560                                                        if ( ! editor.getParam( 'wpeditimage_no_add_10px_captions' ) ) {
     561                                                                captionWidth += 10;
     562                                                        }
    544563                                                        captionWidth = ' style="width: '+ captionWidth +'px"';
    545564                                                }
    546565
  • tests/phpunit/tests/media.php

     
    108108                $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
    109109        }
    110110
     111        /**
     112         * @ticket 26642
     113         */
     114        function test_new_img_caption_shortcode_new_format_no_inline() {
     115                add_theme_support( 'no-add-10px-captions' );
     116                $result = img_caption_shortcode(
     117                        array( 'width' => 20 ),
     118                        $this->img_content . $this->html_content
     119                );
     120                $expected = '<div style="width: 20px" class="wp-caption alignnone">' . $this->img_content . '<p class="wp-caption-text">' . $this->html_content . '</p></div>';
     121                $this->assertEquals( $expected, $result );
     122                remove_theme_support( 'no-add-10px-captions' );
     123        }
     124
    111125        function test_add_remove_oembed_provider() {
    112126                wp_oembed_add_provider( 'http://foo.bar/*', 'http://foo.bar/oembed' );
    113127                $this->assertTrue( wp_oembed_remove_provider( 'http://foo.bar/*' ) );