2016-11-19 23:11:06 +00:00
|
|
|
/*!
|
|
|
|
* 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' ];
|
|
|
|
|
2018-04-03 18:33:00 +00:00
|
|
|
ve.dm.MWGalleryImageNode.static.childNodeTypes = [ 'mwGalleryImageCaption' ];
|
|
|
|
|
2016-11-19 23:11:06 +00:00
|
|
|
ve.dm.MWGalleryImageNode.static.matchFunction = function ( element ) {
|
|
|
|
var parentTypeof = ( element.parentNode && element.parentNode.getAttribute( 'typeof' ) ) || '';
|
|
|
|
return element.getAttribute( 'class' ) === 'gallerybox' &&
|
2018-10-02 20:48:25 +00:00
|
|
|
parentTypeof.trim().split( /\s+/ ).indexOf( 'mw:Extension/gallery' ) !== -1;
|
2016-11-19 23:11:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWGalleryImageNode.static.parentNodeTypes = [ 'mwGallery' ];
|
|
|
|
|
|
|
|
ve.dm.MWGalleryImageNode.static.toDataElement = function ( domElements, converter ) {
|
|
|
|
// TODO: Improve handling of missing files. See 'isError' in MWBlockImageNode#toDataElement
|
2021-10-13 12:57:45 +00:00
|
|
|
var li = domElements[ 0 ];
|
|
|
|
var img = li.querySelector( 'img,audio,video,span[resource]' );
|
|
|
|
var figureInline = img.parentNode.parentNode;
|
2016-11-19 23:11:06 +00:00
|
|
|
|
|
|
|
// Get caption (may be missing for mode="packed-hover" galleries)
|
2021-10-13 12:57:45 +00:00
|
|
|
var captionNode = li.querySelector( '.gallerytext' );
|
2018-08-01 02:35:49 +00:00
|
|
|
if ( captionNode ) {
|
|
|
|
captionNode = captionNode.cloneNode( true );
|
2016-11-19 23:11:06 +00:00
|
|
|
// If showFilename is 'yes', the filename is also inside the caption, so throw this out
|
2021-10-13 12:57:45 +00:00
|
|
|
var filename = captionNode.querySelector( '.galleryfilename' );
|
2016-11-19 23:11:06 +00:00
|
|
|
if ( filename ) {
|
|
|
|
filename.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var caption;
|
2018-08-01 02:35:49 +00:00
|
|
|
if ( captionNode ) {
|
|
|
|
caption = converter.getDataFromDomClean( captionNode, { type: 'mwGalleryImageCaption' } );
|
2016-11-19 23:11:06 +00:00
|
|
|
} else {
|
2018-08-01 02:35:49 +00:00
|
|
|
caption = [
|
|
|
|
{ type: 'mwGalleryImageCaption' },
|
|
|
|
{ type: 'paragraph', internal: { generated: 'wrapper' } },
|
|
|
|
{ type: '/paragraph' },
|
|
|
|
{ type: '/mwGalleryImageCaption' }
|
|
|
|
];
|
2016-11-19 23:11:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var dataElement = {
|
2016-11-19 23:11:06 +00:00
|
|
|
type: this.name,
|
|
|
|
attributes: {
|
Parse relative hrefs on image nodes like on regular links (try 2)
Previous, reverted attempt: da9b6fffbdba9007cfe893b1eafd1ccb42dbb537.
This attempt also includes 6037fefbe044fbf61e7734923f3b625f10e910a1,
and fixes minor conflicts with other changes.
* In normal images, parse relative 'href' attributes instead of
expanding them to absolute. This resolves Parsoid generating
|link= options for copy-pasted images (T193253).
Keep them in the underscore-form to avoid causing dirty diffs like
T237040 again. Unlike in the previous attempt, we don't need to be
super-careful about the 'resource' attribute, thanks to the Parsoid
changes in T108504.
* In gallery images stuff, prefix the 'resource' attribute with './',
same as normal images do. This causes no functional changes, but it
makes updating tests easier, and the consistency is probably good.
* Update test examples to also prefix 'resource' and relative 'href'
attributes with './', like the real Parsoid does.
Bug: T193253
Change-Id: I91131728a87c9406bf069d46d3c94c9a8905a003
2019-09-10 18:57:05 +00:00
|
|
|
resource: './' + mw.libs.ve.normalizeParsoidResourceName( img.getAttribute( 'resource' ) ),
|
2016-11-19 23:11:06 +00:00
|
|
|
altText: img.getAttribute( 'alt' ),
|
2018-04-05 21:14:16 +00:00
|
|
|
// 'src' for images, 'poster' for video/audio
|
|
|
|
src: img.getAttribute( 'src' ) || img.getAttribute( 'poster' ),
|
2016-11-19 23:11:06 +00:00
|
|
|
height: img.getAttribute( 'height' ),
|
2020-12-11 00:09:57 +00:00
|
|
|
width: img.getAttribute( 'width' ),
|
|
|
|
tagName: figureInline.nodeName.toLowerCase()
|
2016-11-19 23:11:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return [ dataElement ]
|
|
|
|
.concat( caption )
|
|
|
|
.concat( { type: '/' + this.name } );
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWGalleryImageNode.static.toDomElements = function ( data, doc ) {
|
|
|
|
// ImageNode:
|
2019-12-31 00:17:40 +00:00
|
|
|
// <li> li (gallerybox)
|
2018-11-21 12:33:32 +00:00
|
|
|
// <div> thumbDiv
|
2020-12-11 21:09:20 +00:00
|
|
|
// <span> innerDiv
|
2018-11-21 12:33:32 +00:00
|
|
|
// <a> a
|
|
|
|
// <img> img
|
2016-11-19 23:11:06 +00:00
|
|
|
var model = data,
|
|
|
|
li = doc.createElement( 'li' ),
|
|
|
|
thumbDiv = doc.createElement( 'div' ),
|
2020-12-11 21:09:20 +00:00
|
|
|
innerDiv = doc.createElement( model.attributes.tagName || 'span' ),
|
2016-11-19 23:11:06 +00:00
|
|
|
a = doc.createElement( 'a' ),
|
|
|
|
img = doc.createElement( 'img' ),
|
|
|
|
alt = model.attributes.altText;
|
|
|
|
|
|
|
|
li.classList.add( 'gallerybox' );
|
|
|
|
thumbDiv.classList.add( 'thumb' );
|
2019-12-31 00:17:40 +00:00
|
|
|
innerDiv.setAttribute( 'typeof', 'mw:Image' );
|
2016-11-19 23:11:06 +00:00
|
|
|
|
|
|
|
// TODO: Support editing the link
|
|
|
|
// a.setAttribute( 'href', model.attributes.src );
|
|
|
|
|
|
|
|
img.setAttribute( 'resource', model.attributes.resource );
|
2018-04-05 11:57:18 +00:00
|
|
|
img.setAttribute( 'src', model.attributes.src );
|
2016-11-19 23:11:06 +00:00
|
|
|
if ( alt ) {
|
|
|
|
img.setAttribute( 'alt', alt );
|
|
|
|
}
|
|
|
|
|
|
|
|
a.appendChild( img );
|
|
|
|
innerDiv.appendChild( a );
|
|
|
|
thumbDiv.appendChild( innerDiv );
|
2019-12-31 00:17:40 +00:00
|
|
|
li.appendChild( thumbDiv );
|
2016-11-19 23:11:06 +00:00
|
|
|
|
|
|
|
return [ li ];
|
|
|
|
};
|
|
|
|
|
2018-04-04 16:28:26 +00:00
|
|
|
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 );
|
|
|
|
};
|
|
|
|
|
2016-11-19 23:11:06 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the image's caption node.
|
|
|
|
*
|
|
|
|
* @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 );
|