Introduce HTML5 caption support.

When a theme supports HTML5 captions via add_theme_support( 'html5', 'caption' ), figure and figcaption will be used instead of div and p.

There's a bonus. But first, some history: Captions were introduced with an inline style set for the container. This remains, as it is there to force captions to wrap. But this inline style included an extra 10 pixels, which have vexxed theme developers for years. While these pixels were designed to ensure padding around floated images, modern themes handle this with grace. The additional pixels thus feel encumbering.

As the new HTML5 gallery support avoids outputting default gallery styles (again, irking theme developers for years; see #26697), the new HTML5 caption support will also ditch these 10 pixels of unwanted hand-holding. 

The 10 pixels are also removed entirely in the visual editor (and more styles may also disappear here; see #26642), giving themes the power necessary to match the frontend styles.

The filter img_caption_shortcode_width added in 3.7 to work around this madness (see #14380) is skipped entirely when the theme supports HTML5 captions.

props obenland, azaozz.
see #26642. also fixes #9066.

Built from https://develop.svn.wordpress.org/trunk@27668


git-svn-id: http://core.svn.wordpress.org/trunk@27511 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2014-03-24 02:05:14 +00:00
parent 155746131f
commit 60676ee282
6 changed files with 46 additions and 10 deletions

View File

@ -339,6 +339,7 @@ final class _WP_Editors {
'preview_styles' => 'font-family font-size font-weight font-style text-decoration text-transform', 'preview_styles' => 'font-family font-size font-weight font-style text-decoration text-transform',
'wpeditimage_disable_captions' => $no_captions, 'wpeditimage_disable_captions' => $no_captions,
'wpeditimage_html5_captions' => current_theme_supports( 'html5', 'caption' ),
'plugins' => implode( ',', $plugins ), 'plugins' => implode( ',', $plugins ),
); );

View File

@ -47,7 +47,10 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
return c; return c;
} }
width = parseInt( w, 10 ) + 10; width = parseInt( w, 10 );
if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
width += 10;
}
return '<div class="mceTemp"><dl id="'+ id +'" class="wp-caption '+ cls +'" style="width: '+ width +'px">' + return '<div class="mceTemp"><dl id="'+ id +'" class="wp-caption '+ cls +'" style="width: '+ width +'px">' +
'<dt class="wp-caption-dt">'+ img +'</dt><dd class="wp-caption-dd">'+ cap +'</dd></dl></div>'; '<dt class="wp-caption-dt">'+ img +'</dt><dd class="wp-caption-dd">'+ cap +'</dd></dl></div>';
@ -189,7 +192,12 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
html = createImageAndLink( imageData, 'html' ); html = createImageAndLink( imageData, 'html' );
width = imageData.width + 10; width = parseInt( imageData.width );
if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
width += 10;
}
className = 'align' + imageData.align; className = 'align' + imageData.align;
//TODO: shouldn't add the id attribute if it isn't an attachment //TODO: shouldn't add the id attribute if it isn't an attachment
@ -391,6 +399,10 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
editor.on( 'init', function() { editor.on( 'init', function() {
var dom = editor.dom; var dom = editor.dom;
if ( editor.getParam( 'wpeditimage_html5_captions' ) ) {
dom.addClass( editor.getBody(), 'html5-captions' );
}
// Add caption field to the default image dialog // Add caption field to the default image dialog
editor.on( 'wpLoadImageForm', function( event ) { editor.on( 'wpLoadImageForm', function( event ) {
if ( editor.getParam( 'wpeditimage_disable_captions' ) ) { if ( editor.getParam( 'wpeditimage_disable_captions' ) ) {
@ -475,8 +487,13 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
node = editor.selection.getNode(); node = editor.selection.getNode();
if ( data.width ) { if ( data.width ) {
captionWidth = parseInt( data.width, 10 ) + 10; captionWidth = parseInt( data.width, 10 );
captionWidth = ' style="width: '+ captionWidth +'px"';
if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
captionWidth += 10;
}
captionWidth = ' style="width: ' + captionWidth + 'px"';
} }
html = '<dl class="wp-caption alignnone"' + captionWidth + '>' + html = '<dl class="wp-caption alignnone"' + captionWidth + '>' +
@ -539,7 +556,12 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
captionWidth = data.width || imgNode.clientWidth; captionWidth = data.width || imgNode.clientWidth;
if ( captionWidth ) { if ( captionWidth ) {
captionWidth = parseInt( captionWidth, 10 ) + 10; captionWidth = parseInt( captionWidth, 10 );
if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
captionWidth += 10;
}
captionWidth = ' style="width: '+ captionWidth +'px"'; captionWidth = ' style="width: '+ captionWidth +'px"';
} }
@ -618,7 +640,6 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
// Remove toolbar to avoid an orphaned toolbar when dragging an image to a new location // Remove toolbar to avoid an orphaned toolbar when dragging an image to a new location
removeToolbar(); removeToolbar();
}); });
// Prevent IE11 from making dl.wp-caption resizable // Prevent IE11 from making dl.wp-caption resizable
@ -654,7 +675,12 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
width = event.width || editor.dom.getAttrib( node, 'width' ); width = event.width || editor.dom.getAttrib( node, 'width' );
if ( width ) { if ( width ) {
width = parseInt( width, 10 ) + 10; width = parseInt( width, 10 );
if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
width += 10;
}
editor.dom.setStyle( parent, 'width', width + 'px' ); editor.dom.setStyle( parent, 'width', width + 'px' );
} }
} }

File diff suppressed because one or more lines are too long

View File

@ -40,6 +40,10 @@ dl.aligncenter {
margin: 10px 0; margin: 10px 0;
} }
.html5-captions .wp-caption {
padding: 4px;
}
.mceIEcenter { .mceIEcenter {
text-align: center; text-align: center;
} }

View File

@ -765,6 +765,13 @@ function img_caption_shortcode( $attr, $content = null ) {
if ( ! empty( $atts['id'] ) ) if ( ! empty( $atts['id'] ) )
$atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" '; $atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" ';
$class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] );
if ( current_theme_supports( 'html5', 'caption' ) ) {
return '<figure ' . $atts['id'] . 'style="width: ' . (int) $atts['width'] . 'px;" class="' . esc_attr( $class ) . '">'
. do_shortcode( $content ) . '<figcaption class="wp-caption-text">' . $atts['caption'] . '</figcaption></figure>';
}
$caption_width = 10 + $atts['width']; $caption_width = 10 + $atts['width'];
/** /**
@ -788,8 +795,6 @@ function img_caption_shortcode( $attr, $content = null ) {
if ( $caption_width ) if ( $caption_width )
$style = 'style="width: ' . (int) $caption_width . 'px" '; $style = 'style="width: ' . (int) $caption_width . 'px" ';
$class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] );
return '<div ' . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">' return '<div ' . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $atts['caption'] . '</p></div>'; . do_shortcode( $content ) . '<p class="wp-caption-text">' . $atts['caption'] . '</p></div>';
} }