Add |alt= option for galleries ( Bug 18682 ). Recommit of r86749, with nested |alt= now fixed. Patch by Jan Paul Posma

This commit is contained in:
Leo Koppelkamm 2011-04-25 13:51:54 +00:00
parent 301e940435
commit a4296effc7
4 changed files with 89 additions and 58 deletions

View file

@ -136,14 +136,15 @@ class ImageGallery
* Add an image to the gallery.
*
* @param $title Title object of the image that is added to the gallery
* @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
* @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
* @param $alt String: Alt text for the image
*/
function add( $title, $html='' ) {
function add( $title, $html = '', $alt = '' ) {
if ( $title instanceof File ) {
// Old calling convention
$title = $title->getTitle();
}
$this->mImages[] = array( $title, $html );
$this->mImages[] = array( $title, $html, $alt );
wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
}
@ -151,14 +152,15 @@ class ImageGallery
* Add an image at the beginning of the gallery.
*
* @param $title Title object of the image that is added to the gallery
* @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
* @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
* @param $alt String: Alt text for the image
*/
function insert( $title, $html='' ) {
function insert( $title, $html='', $alt='' ) {
if ( $title instanceof File ) {
// Old calling convention
$title = $title->getTitle();
}
array_unshift( $this->mImages, array( &$title, $html ) );
array_unshift( $this->mImages, array( &$title, $html, $alt ) );
}
@ -218,15 +220,16 @@ class ImageGallery
if ( $this->mPerRow > 0 ) {
$maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS );
$oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : "";
# _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
$this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
}
$attribs = Sanitizer::mergeAttributes(
array( 'class' => 'gallery' ), $this->mAttribs );
$s = Xml::openElement( 'ul', $attribs );
$output = Xml::openElement( 'ul', $attribs );
if ( $this->mCaption ) {
$s .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
$output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
}
$params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
@ -234,6 +237,7 @@ class ImageGallery
foreach ( $this->mImages as $pair ) {
$nt = $pair[0];
$text = $pair[1]; # "text" means "caption" here
$alt = $pair[2];
$descQuery = false;
if ( $nt->getNamespace() == NS_FILE ) {
@ -272,18 +276,19 @@ class ImageGallery
$thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">'
. htmlspecialchars( $img->getLastError() ) . '</div>';
} else {
//We get layout problems with the margin, if the image is smaller
//than the line-height, so we less margin in these cases.
# We get layout problems with the margin, if the image is smaller
# than the line-height (17), so we add less margin in these cases.
$minThumbHeight = $thumb->height > 17 ? $thumb->height : 17;
$vpad = floor(( self::THUMB_PADDING + $this->mHeights - $minThumbHeight ) /2);
$imageParameters = array(
'desc-link' => true,
'desc-query' => $descQuery
'desc-query' => $descQuery,
'alt' => $alt,
);
# In the absence of a caption, fall back on providing screen readers with the filename as alt text
if ( $text == '' ) {
# In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
if ( $alt == '' && $text == '' ) {
$imageParameters['alt'] = $nt->getText();
}
@ -308,14 +313,14 @@ class ImageGallery
if( $this->mShowBytes ) {
if( $img ) {
$nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
$fileSize = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
$wgLang->formatNum( $img->getSize() ) );
} else {
$nb = wfMsgHtml( 'filemissing' );
$fileSize = wfMsgHtml( 'filemissing' );
}
$nb = "$nb<br />\n";
$fileSize = "$fileSize<br />\n";
} else {
$nb = '';
$fileSize = '';
}
$textlink = $this->mShowFilename ?
@ -332,20 +337,20 @@ class ImageGallery
# in version 4.8.6 generated crackpot html in its absence, see:
# http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
# Weird double wrapping in div needed due to FF2 bug
# Weird double wrapping (the extra div inside the li) needed due to FF2 bug
# Can be safely removed if FF2 falls completely out of existance
$s .=
$output .=
"\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
. '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
. $thumbhtml
. "\n\t\t\t" . '<div class="gallerytext">' . "\n"
. $textlink . $text . $nb
. $textlink . $text . $fileSize
. "\n\t\t\t</div>"
. "\n\t\t</div></li>";
}
$s .= "\n</ul>";
$output .= "\n</ul>";
return $s;
return $output;
}
/**

View file

@ -35,6 +35,7 @@ class MagicWord {
var $mRegexStart = '';
var $mBaseRegex = '';
var $mVariableRegex = '';
var $mVariableStartToEndRegex = '';
var $mModified = false;
var $mFound = false;

View file

@ -4733,21 +4733,38 @@ class Parser {
if ( strpos( $matches[0], '%' ) !== false ) {
$matches[1] = rawurldecode( $matches[1] );
}
$tp = Title::newFromText( $matches[1], NS_FILE );
$nt =& $tp;
if ( is_null( $nt ) ) {
$title = Title::newFromText( $matches[1], NS_FILE );
if ( is_null( $title ) ) {
# Bogus title. Ignore these so we don't bomb out later.
continue;
}
$label = '';
$alt = '';
if ( isset( $matches[3] ) ) {
$label = $matches[3];
} else {
$label = '';
// look for an |alt= definition while trying not to break existing
// captions with multiple pipes (|) in it, until a more sensible grammar
// is defined for images in galleries
$matches[3] = $this->recursiveTagParse( trim( $matches[3] ) );
$altmatches = StringUtils::explode('|', $matches[3]);
$magicWordAlt = MagicWord::get( 'img_alt' );
foreach ( $altmatches as $altmatch ) {
$match = $magicWordAlt->matchVariableStartToEnd( $altmatch );
if ( $match ) {
$alt = $this->stripAltText( $match, false );
}
else {
// concatenate all other pipes
$label .= '|' . $altmatch;
}
}
// remove the first pipe
$label = substr( $label, 1 );
}
$html = $this->recursiveTagParse( trim( $label ) );
$ig->add( $nt, $html );
$ig->add( $title, $label, $alt );
}
return $ig->toHTML();
}

View file

@ -7312,6 +7312,7 @@ File:Nonexistant.jpg|caption
File:Nonexistant.jpg
image:foobar.jpg|some '''caption''' [[Main Page]]
image:foobar.jpg
image:foobar.jpg|Blabla|alt=This is a foo-bar.|blabla.
</gallery>
!! result
<ul class="gallery" style="max-width: 202px;_width: 202px;">
@ -7340,6 +7341,40 @@ image:foobar.jpg
<div class="gallerytext">
</div>
</div></li>
<li class="gallerybox" style="width: 95px"><div style="width: 95px">
<div class="thumb" style="width: 90px;"><div style="margin:26px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="This is a foo-bar." src="http://example.com/images/3/3a/Foobar.jpg" width="60" height="7" /></a></div></div>
<div class="gallerytext">
<p>Blabla|blabla.
</p>
</div>
</div></li>
</ul>
!! end
!! test
Gallery with wikitext inside caption
!! input
<gallery>
File:foobar.jpg|[[File:foobar.jpg|20px|desc|alt=inneralt]]|alt=galleryalt
File:foobar.jpg|{{Test|unamedParam|alt=param}}|alt=galleryalt
</gallery>
!! result
<ul class="gallery">
<li class="gallerybox" style="width: 155px"><div style="width: 155px">
<div class="thumb" style="width: 150px;"><div style="margin:66px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="galleryalt" src="http://example.com/images/3/3a/Foobar.jpg" width="120" height="14" /></a></div></div>
<div class="gallerytext">
<p><a href="/wiki/File:Foobar.jpg" class="image" title="desc"><img alt="inneralt" src="http://example.com/images/3/3a/Foobar.jpg" width="20" height="2" /></a>
</p>
</div>
</div></li>
<li class="gallerybox" style="width: 155px"><div style="width: 155px">
<div class="thumb" style="width: 150px;"><div style="margin:66px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="galleryalt" src="http://example.com/images/3/3a/Foobar.jpg" width="120" height="14" /></a></div></div>
<div class="gallerytext">
<p>This is a test template
</p>
</div>
</div></li>
</ul>
!! end
@ -7424,33 +7459,6 @@ foobar.jpg
!! end
!! test
Gallery with wikitext inside caption
!! input
<gallery>
File:Wiki.png|[[File:Wiki.png|20px|desc|alt=inneralt]]|alt=galleryalt
File:Wiki.png|{{tl|test|alt=param}}|alt=galleryalt
</gallery>
!! result
<ul class="gallery">
<li class="gallerybox" style="width: 155px"><div style="width: 155px">
<div style="height: 150px;">Wiki.png</div>
<div class="gallerytext">
<p><a href="/index.php?title=Special:Upload&amp;wpDestFile=Wiki.png" class="new" title="File:Wiki.png">desc</a>|alt=galleryalt
</p>
</div>
</div></li>
<li class="gallerybox" style="width: 155px"><div style="width: 155px">
<div style="height: 150px;">Wiki.png</div>
<div class="gallerytext">
<p><a href="/index.php?title=Template:Tl&amp;action=edit&amp;redlink=1" class="new" title="Template:Tl (page does not exist)">Template:Tl</a>|alt=galleryalt
</p>
</div>
</div></li>
</ul>
!! end
!! test
HTML Hex character encoding (spells the word "JavaScript")
!! input