mediawiki-extensions-Visual.../modules/ve-mw/dm/nodes/ve.dm.MWGalleryImageNode.js
Bartosz Dziewoński 86f2057c81 Always generate empty wrapper paragraph inside image captions (slugs begone!)
While all of the following are valid in the model:

1. <mwBlockImage></mwBlockImage>
   Image with no caption. Must use the media dialog to insert one.
2. <mwBlockImage><mwImageCaption></mwImageCaption></mwBlockImage>
   Image with empty caption. There is a slug to insert a paragraph.
3. <mwBlockImage><mwImageCaption><paragraph></paragraph></mwImageCaption></mwBlockImage>
   Image with caption with empty paragraph. Nice and intuitive!

(Same for <mwGalleryImage> / <mwGalleryImageCaption>.)

The third option is the most convenient for the user. We should always
generate that when converting documents from HTML and from the editing
tools (MWGalleryDialog, MWMediaDialog/MWImageModel).

Previously, the editing tools generated option 2 if no caption text
was entered, and the converter generated option 2 if there was no
caption node or if it was empty. Curiously, option 1 was never used.

Wikitext for manual testing:

```
[[File:Foo.png|thumb]]
[[File:Foo.png|thumb|]]
[[File:Foo.png|thumb|Caption]]

<gallery mode="packed">
File:Foo.png
File:Foo.png|
File:Foo.png|Caption
</gallery>
```

Bug: T200387
Change-Id: Ie82fb339f6bd8ae1b289235bf5402490722d9a7c
2018-08-01 05:40:57 +02:00

151 lines
4.2 KiB
JavaScript

/*!
* VisualEditor DataModel MWGalleryImageNode class.
*
* @copyright 2016 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* DataModel MediaWiki gallery image node.
*
* @class
* @extends ve.dm.BranchNode
*
* @constructor
* @param {Object} [element] Reference to element in linear model
* @param {ve.dm.Node[]} [children]
*/
ve.dm.MWGalleryImageNode = function VeDmMWGalleryImageNode() {
// Parent constructor
ve.dm.MWGalleryImageNode.super.apply( this, arguments );
};
/* Inheritance */
OO.inheritClass( ve.dm.MWGalleryImageNode, ve.dm.BranchNode );
/* Static members */
ve.dm.MWGalleryImageNode.static.name = 'mwGalleryImage';
ve.dm.MWGalleryImageNode.static.matchTagNames = [ 'li' ];
ve.dm.MWGalleryImageNode.static.childNodeTypes = [ 'mwGalleryImageCaption' ];
ve.dm.MWGalleryImageNode.static.matchFunction = function ( element ) {
var parentTypeof = ( element.parentNode && element.parentNode.getAttribute( 'typeof' ) ) || '';
return element.getAttribute( 'class' ) === 'gallerybox' &&
parentTypeof.split( ' ' ).indexOf( 'mw:Extension/gallery' ) !== -1;
};
ve.dm.MWGalleryImageNode.static.parentNodeTypes = [ 'mwGallery' ];
ve.dm.MWGalleryImageNode.static.toDataElement = function ( domElements, converter ) {
var li, img, captionNode, caption, filename, dataElement;
// TODO: Improve handling of missing files. See 'isError' in MWBlockImageNode#toDataElement
li = domElements[ 0 ];
img = li.querySelector( 'img,audio,video' );
// Get caption (may be missing for mode="packed-hover" galleries)
captionNode = li.querySelector( '.gallerytext' );
if ( captionNode ) {
captionNode = captionNode.cloneNode( true );
// If showFilename is 'yes', the filename is also inside the caption, so throw this out
filename = captionNode.querySelector( '.galleryfilename' );
if ( filename ) {
filename.remove();
}
}
if ( captionNode ) {
caption = converter.getDataFromDomClean( captionNode, { type: 'mwGalleryImageCaption' } );
} else {
caption = [
{ type: 'mwGalleryImageCaption' },
{ type: 'paragraph', internal: { generated: 'wrapper' } },
{ type: '/paragraph' },
{ type: '/mwGalleryImageCaption' }
];
}
dataElement = {
type: this.name,
attributes: {
resource: ve.normalizeParsoidResourceName( img.getAttribute( 'resource' ) ),
altText: img.getAttribute( 'alt' ),
// 'src' for images, 'poster' for video/audio
src: img.getAttribute( 'src' ) || img.getAttribute( 'poster' ),
height: img.getAttribute( 'height' ),
width: img.getAttribute( 'width' )
}
};
return [ dataElement ]
.concat( caption )
.concat( { type: '/' + this.name } );
};
ve.dm.MWGalleryImageNode.static.toDomElements = function ( data, doc ) {
// ImageNode:
// <li> li (gallerybox)
// <div> outerDiv
// <div> thumbDiv
// <div> innerDiv
// <a> a
// <img> img
var model = data,
li = doc.createElement( 'li' ),
outerDiv = doc.createElement( 'div' ),
thumbDiv = doc.createElement( 'div' ),
innerDiv = doc.createElement( 'div' ),
a = doc.createElement( 'a' ),
img = doc.createElement( 'img' ),
alt = model.attributes.altText;
li.classList.add( 'gallerybox' );
thumbDiv.classList.add( 'thumb' );
// TODO: Support editing the link
// a.setAttribute( 'href', model.attributes.src );
img.setAttribute( 'resource', model.attributes.resource );
img.setAttribute( 'src', model.attributes.src );
if ( alt ) {
img.setAttribute( 'alt', alt );
}
a.appendChild( img );
innerDiv.appendChild( a );
thumbDiv.appendChild( innerDiv );
outerDiv.appendChild( thumbDiv );
li.appendChild( outerDiv );
return [ li ];
};
ve.dm.MWGalleryImageNode.static.describeChange = function ( key ) {
// These attributes are computed
if ( key === 'src' || key === 'width' || key === 'height' ) {
return null;
}
// Parent method
return ve.dm.MWGalleryImageNode.super.static.describeChange.apply( this, arguments );
};
/* Methods */
/**
* Get the image's caption node.
*
* @method
* @return {ve.dm.MWImageCaptionNode|null} Caption node, if present
*/
ve.dm.MWGalleryImageNode.prototype.getCaptionNode = function () {
return this.children.length > 0 ? this.children[ 0 ] : null;
};
/* Registration */
ve.dm.modelRegistry.register( ve.dm.MWGalleryImageNode );