2013-04-25 00:47:17 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWBlockImageNode class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2013-04-25 00:47:17 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel MediaWiki image node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.dm.BranchNode
|
2013-10-15 12:18:11 +00:00
|
|
|
* @mixins ve.dm.MWImageNode
|
2014-10-28 11:46:40 +00:00
|
|
|
* @mixins ve.dm.ClassAttributeNode
|
2014-06-07 03:17:26 +00:00
|
|
|
*
|
2013-04-25 00:47:17 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Object} [element] Reference to element in linear model
|
2014-06-07 03:17:26 +00:00
|
|
|
* @param {ve.dm.Node[]} [children]
|
2013-04-25 00:47:17 +00:00
|
|
|
*/
|
2014-06-07 03:17:26 +00:00
|
|
|
ve.dm.MWBlockImageNode = function VeDmMWBlockImageNode() {
|
2013-10-15 12:18:11 +00:00
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.dm.MWBlockImageNode.super.apply( this, arguments );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.dm.MWImageNode.call( this );
|
2014-10-28 11:46:40 +00:00
|
|
|
ve.dm.ClassAttributeNode.call( this );
|
2013-04-25 00:47:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.dm.MWBlockImageNode, ve.dm.BranchNode );
|
2013-04-25 00:47:17 +00:00
|
|
|
|
2017-04-13 10:32:30 +00:00
|
|
|
// Need to mixin base class as well (T92540)
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.dm.MWBlockImageNode, ve.dm.GeneratedContentNode );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.dm.MWBlockImageNode, ve.dm.MWImageNode );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2014-10-28 11:46:40 +00:00
|
|
|
OO.mixinClass( ve.dm.MWBlockImageNode, ve.dm.ClassAttributeNode );
|
|
|
|
|
2013-04-25 00:47:17 +00:00
|
|
|
/* Static Properties */
|
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.name = 'mwBlockImage';
|
2013-04-25 00:47:17 +00:00
|
|
|
|
2015-02-18 10:29:14 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.preserveHtmlAttributes = function ( attribute ) {
|
2018-06-26 12:02:19 +00:00
|
|
|
var attributes = [ 'typeof', 'class', 'src', 'resource', 'width', 'height', 'href', 'rel', 'data-mw' ];
|
2015-03-10 12:50:42 +00:00
|
|
|
return attributes.indexOf( attribute ) === -1;
|
2013-06-07 23:04:45 +00:00
|
|
|
};
|
2013-05-20 23:54:03 +00:00
|
|
|
|
2013-04-25 00:47:17 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.handlesOwnChildren = true;
|
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.childNodeTypes = [ 'mwImageCaption' ];
|
2013-04-25 00:47:17 +00:00
|
|
|
|
2013-06-19 21:08:45 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.matchTagNames = [ 'figure' ];
|
2013-09-19 18:03:04 +00:00
|
|
|
|
2020-06-10 11:15:18 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.disallowedAnnotationTypes = [ 'link' ];
|
2013-06-19 21:08:45 +00:00
|
|
|
|
2014-10-28 11:46:40 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.classAttributes = {
|
|
|
|
'mw-image-border': { borderImage: true },
|
|
|
|
'mw-halign-left': { align: 'left' },
|
|
|
|
'mw-halign-right': { align: 'right' },
|
|
|
|
'mw-halign-center': { align: 'center' },
|
|
|
|
'mw-halign-none': { align: 'none' },
|
|
|
|
'mw-default-size': { defaultSize: true }
|
|
|
|
};
|
|
|
|
|
2013-04-25 00:47:17 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.toDataElement = function ( domElements, converter ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var figure = domElements[ 0 ];
|
|
|
|
var imgWrapper = figure.children[ 0 ]; // <a> or <span>
|
2022-03-21 21:03:00 +00:00
|
|
|
var img = imgWrapper.children[ 0 ]; // <img>, <video>, <audio>, or <span> if mw:Error
|
2021-10-13 12:57:45 +00:00
|
|
|
var captionNode = figure.children[ 1 ]; // <figcaption> or undefined
|
|
|
|
var classAttr = figure.getAttribute( 'class' );
|
|
|
|
var typeofAttrs = figure.getAttribute( 'typeof' ).trim().split( /\s+/ );
|
|
|
|
var mwDataJSON = figure.getAttribute( 'data-mw' );
|
|
|
|
var mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
|
|
|
|
var errorIndex = typeofAttrs.indexOf( 'mw:Error' );
|
2022-01-19 00:39:56 +00:00
|
|
|
var isError = errorIndex !== -1;
|
|
|
|
var width = img.getAttribute( isError ? 'data-width' : 'width' );
|
|
|
|
var height = img.getAttribute( isError ? 'data-height' : 'height' );
|
2021-10-13 12:57:45 +00:00
|
|
|
|
|
|
|
var href = imgWrapper.getAttribute( 'href' );
|
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
|
|
|
if ( href ) {
|
|
|
|
// Convert absolute URLs to relative if the href refers to a page on this wiki.
|
|
|
|
// Otherwise Parsoid generates |link= options for copy-pasted images (T193253).
|
2021-10-13 12:57:45 +00:00
|
|
|
var targetData = mw.libs.ve.getTargetDataFromHref( href, converter.getTargetHtmlDocument() );
|
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
|
|
|
if ( targetData.isInternal ) {
|
|
|
|
href = './' + targetData.rawTitle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 00:39:56 +00:00
|
|
|
if ( isError ) {
|
2015-05-12 22:48:20 +00:00
|
|
|
typeofAttrs.splice( errorIndex, 1 );
|
|
|
|
}
|
2017-04-27 18:08:35 +00:00
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var types = this.rdfaToTypes[ typeofAttrs[ 0 ] ];
|
2017-04-27 18:08:35 +00:00
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var attributes = {
|
2017-04-27 18:08:35 +00:00
|
|
|
mediaClass: types.mediaClass,
|
2022-05-30 23:17:46 +00:00
|
|
|
mediaTag: img.nodeName.toLowerCase(),
|
2017-04-27 18:08:35 +00:00
|
|
|
type: types.frameType,
|
2018-06-05 11:41:09 +00:00
|
|
|
src: img.getAttribute( 'src' ) || img.getAttribute( 'poster' ),
|
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
|
|
|
href: href,
|
2022-03-21 21:03:00 +00:00
|
|
|
imageClassAttr: img.getAttribute( 'class' ),
|
2022-03-09 19:33:42 +00:00
|
|
|
imgWrapperClassAttr: imgWrapper.getAttribute( 'class' ),
|
2019-10-31 21:24:13 +00:00
|
|
|
resource: img.getAttribute( 'resource' ),
|
2018-06-05 11:41:09 +00:00
|
|
|
width: width !== null && width !== '' ? +width : null,
|
|
|
|
height: height !== null && height !== '' ? +height : null,
|
|
|
|
alt: img.getAttribute( 'alt' ),
|
2018-06-26 12:02:19 +00:00
|
|
|
mw: mwData,
|
2022-01-19 00:39:56 +00:00
|
|
|
isError: isError
|
2015-05-12 22:48:20 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 11:46:40 +00:00
|
|
|
this.setClassAttributes( attributes, classAttr );
|
2013-05-15 23:23:42 +00:00
|
|
|
|
2014-10-28 11:46:40 +00:00
|
|
|
attributes.align = attributes.align || 'default';
|
2013-05-14 23:39:13 +00:00
|
|
|
|
|
|
|
// Default-size
|
2014-10-28 11:46:40 +00:00
|
|
|
if ( attributes.defaultSize ) {
|
2014-02-27 17:39:19 +00:00
|
|
|
// Force wiki-default size for thumb and frameless
|
|
|
|
if (
|
|
|
|
attributes.type === 'thumb' ||
|
|
|
|
attributes.type === 'frameless'
|
|
|
|
) {
|
2015-12-09 16:47:13 +00:00
|
|
|
// We're going to change .width and .height, store the original
|
2014-03-06 01:44:01 +00:00
|
|
|
// values so we can restore them later.
|
|
|
|
// FIXME "just" don't modify .width and .height instead
|
|
|
|
attributes.originalWidth = attributes.width;
|
|
|
|
attributes.originalHeight = attributes.height;
|
2014-02-27 17:39:19 +00:00
|
|
|
// Parsoid hands us images with default Wikipedia dimensions
|
|
|
|
// rather than default MediaWiki configuration dimensions.
|
|
|
|
// We must force local wiki default in edit mode for default
|
|
|
|
// size images.
|
2021-10-13 12:57:45 +00:00
|
|
|
var newDimensions = this.scaleToThumbnailSize( attributes );
|
2014-07-25 02:28:07 +00:00
|
|
|
if ( newDimensions ) {
|
|
|
|
attributes.width = newDimensions.width;
|
|
|
|
attributes.height = newDimensions.height;
|
2014-02-27 17:39:19 +00:00
|
|
|
}
|
2014-02-20 05:11:04 +00:00
|
|
|
}
|
2013-05-14 23:39:13 +00:00
|
|
|
}
|
|
|
|
|
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: 'mwImageCaption' } );
|
2015-01-26 17:46:55 +00:00
|
|
|
} else {
|
2018-08-01 02:35:49 +00:00
|
|
|
caption = [
|
2014-08-22 20:50:48 +00:00
|
|
|
{ type: 'mwImageCaption' },
|
2018-08-01 02:35:49 +00:00
|
|
|
{ type: 'paragraph', internal: { generated: 'wrapper' } },
|
|
|
|
{ type: '/paragraph' },
|
|
|
|
{ type: '/mwImageCaption' }
|
2013-05-23 05:47:11 +00:00
|
|
|
];
|
2013-05-14 23:39:13 +00:00
|
|
|
}
|
2018-08-01 02:35:49 +00:00
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var dataElement = { type: this.name, attributes: attributes };
|
2018-08-01 02:35:49 +00:00
|
|
|
|
|
|
|
this.storeGeneratedContents( dataElement, dataElement.attributes.src, converter.getStore() );
|
|
|
|
|
|
|
|
return [ dataElement ]
|
|
|
|
.concat( caption )
|
|
|
|
.concat( { type: '/' + this.name } );
|
2013-04-25 00:47:17 +00:00
|
|
|
};
|
|
|
|
|
2013-06-05 21:34:53 +00:00
|
|
|
// TODO: At this moment node is not resizable but when it will be then adding defaultSize class
|
|
|
|
// should be more conditional.
|
2013-04-25 00:47:17 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.toDomElements = function ( data, doc, converter ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var dataElement = data[ 0 ],
|
2022-01-19 00:43:38 +00:00
|
|
|
attributes = dataElement.attributes,
|
2013-04-25 00:47:17 +00:00
|
|
|
figure = doc.createElement( 'figure' ),
|
2022-01-19 00:43:38 +00:00
|
|
|
imgWrapper = doc.createElement( attributes.href ? 'a' : 'span' ),
|
2022-05-30 23:17:46 +00:00
|
|
|
img = doc.createElement( attributes.isError ? 'span' : attributes.mediaTag ),
|
2013-06-24 19:34:15 +00:00
|
|
|
wrapper = doc.createElement( 'div' ),
|
2022-01-19 00:43:38 +00:00
|
|
|
classAttr = this.getClassAttrFromAttributes( attributes ),
|
2014-03-06 01:44:01 +00:00
|
|
|
captionData = data.slice( 1, -1 );
|
2013-09-05 20:39:12 +00:00
|
|
|
|
2017-04-27 18:08:35 +00:00
|
|
|
// RDFa type
|
2022-05-30 23:17:46 +00:00
|
|
|
figure.setAttribute( 'typeof', this.getRdfa( attributes.mediaClass, attributes.type, attributes.isError ) );
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( !ve.isEmptyObject( attributes.mw ) ) {
|
|
|
|
figure.setAttribute( 'data-mw', JSON.stringify( attributes.mw ) );
|
2018-06-26 12:02:19 +00:00
|
|
|
}
|
2013-06-05 21:34:53 +00:00
|
|
|
|
2014-10-28 11:46:40 +00:00
|
|
|
if ( classAttr ) {
|
2020-04-09 13:33:54 +00:00
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
2014-10-28 11:46:40 +00:00
|
|
|
figure.className = classAttr;
|
2013-06-05 21:34:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( attributes.href ) {
|
|
|
|
imgWrapper.setAttribute( 'href', attributes.href );
|
2013-08-16 19:03:46 +00:00
|
|
|
}
|
2014-03-06 01:44:01 +00:00
|
|
|
|
2022-03-21 21:03:00 +00:00
|
|
|
// At the moment, preserving this is only relevant on mw:Error spans
|
|
|
|
if ( attributes.isError && attributes.imageClassAttr ) {
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
img.className = attributes.imageClassAttr;
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:33:42 +00:00
|
|
|
if ( attributes.imgWrapperClassAttr ) {
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
imgWrapper.className = attributes.imgWrapperClassAttr;
|
|
|
|
}
|
|
|
|
|
2022-01-19 00:43:38 +00:00
|
|
|
var width = attributes.width;
|
|
|
|
var height = attributes.height;
|
2014-03-06 01:44:01 +00:00
|
|
|
// If defaultSize is set, and was set on the way in, use the original width and height
|
|
|
|
// we got on the way in.
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( attributes.defaultSize ) {
|
|
|
|
if ( attributes.originalWidth !== undefined ) {
|
|
|
|
width = attributes.originalWidth;
|
2014-03-06 01:44:01 +00:00
|
|
|
}
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( attributes.originalHeight !== undefined ) {
|
|
|
|
height = attributes.originalHeight;
|
2014-03-06 01:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 23:17:46 +00:00
|
|
|
var srcAttr = this.tagsToSrcAttrs[ img.nodeName.toLowerCase() ];
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( srcAttr && !attributes.isError ) {
|
|
|
|
img.setAttribute( srcAttr, attributes.src );
|
2018-08-02 19:25:08 +00:00
|
|
|
}
|
2022-01-18 23:44:41 +00:00
|
|
|
if ( attributes.isError ) {
|
|
|
|
if ( converter.isForPreview() ) {
|
|
|
|
imgWrapper.classList.add( 'new' );
|
|
|
|
}
|
|
|
|
var filename = mw.libs.ve.normalizeParsoidResourceName( attributes.resource || '' );
|
|
|
|
img.appendChild( doc.createTextNode( filename ) );
|
|
|
|
}
|
|
|
|
|
2022-01-19 00:39:56 +00:00
|
|
|
if ( width !== null ) {
|
|
|
|
img.setAttribute( attributes.isError ? 'data-width' : 'width', width );
|
|
|
|
}
|
|
|
|
if ( height !== null ) {
|
2022-05-23 14:18:20 +00:00
|
|
|
img.setAttribute( attributes.isError ? 'data-height' : 'height', height );
|
2022-01-19 00:39:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 00:43:38 +00:00
|
|
|
img.setAttribute( 'resource', attributes.resource );
|
2021-01-27 11:56:17 +00:00
|
|
|
// TODO: This does not make sense for broken images (when img is a span node)
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( typeof attributes.alt === 'string' ) {
|
|
|
|
img.setAttribute( 'alt', attributes.alt );
|
2013-12-28 10:29:35 +00:00
|
|
|
}
|
2013-08-16 19:03:46 +00:00
|
|
|
figure.appendChild( imgWrapper );
|
|
|
|
imgWrapper.appendChild( img );
|
2013-04-25 00:47:17 +00:00
|
|
|
|
2013-06-25 00:22:03 +00:00
|
|
|
// If length of captionData is smaller or equal to 2 it means that there is no caption or that
|
|
|
|
// it is empty - in both cases we are going to skip appending <figcaption>.
|
|
|
|
if ( captionData.length > 2 ) {
|
|
|
|
converter.getDomSubtreeFromData( data.slice( 1, -1 ), wrapper );
|
|
|
|
while ( wrapper.firstChild ) {
|
|
|
|
figure.appendChild( wrapper.firstChild );
|
|
|
|
}
|
2013-04-25 00:47:17 +00:00
|
|
|
}
|
|
|
|
return [ figure ];
|
|
|
|
};
|
|
|
|
|
2013-06-18 22:58:10 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the caption node of the image.
|
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {ve.dm.MWImageCaptionNode|null} Caption node, if present
|
2013-06-18 22:58:10 +00:00
|
|
|
*/
|
2013-12-06 02:34:44 +00:00
|
|
|
ve.dm.MWBlockImageNode.prototype.getCaptionNode = function () {
|
2015-08-19 17:33:02 +00:00
|
|
|
var node = this.children[ 0 ];
|
2013-06-18 22:58:10 +00:00
|
|
|
return node instanceof ve.dm.MWImageCaptionNode ? node : null;
|
|
|
|
};
|
|
|
|
|
2018-12-10 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.dm.MWBlockImageNode.prototype.suppressSlugType = function () {
|
|
|
|
// TODO: Have alignment attribute changes trigger a parent branch node re-render
|
|
|
|
var align = this.getAttribute( 'align' );
|
|
|
|
return align !== 'none' && align !== 'center' ? 'float' : null;
|
|
|
|
};
|
|
|
|
|
2013-04-25 00:47:17 +00:00
|
|
|
/* Registration */
|
|
|
|
|
2015-05-16 17:02:33 +00:00
|
|
|
ve.dm.modelRegistry.unregister( ve.dm.BlockImageNode );
|
2013-06-06 20:49:29 +00:00
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWBlockImageNode );
|