2013-03-27 22:56:54 +00:00
|
|
|
/*!
|
2013-07-16 14:06:47 +00:00
|
|
|
* VisualEditor DataModel MWInlineImage class.
|
2013-03-27 22:56:54 +00:00
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2013-03-27 22:56:54 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel MediaWiki image node.
|
|
|
|
*
|
|
|
|
* @class
|
2013-06-07 01:27:07 +00:00
|
|
|
* @extends ve.dm.LeafNode
|
2013-10-15 12:18:11 +00:00
|
|
|
* @mixins ve.dm.MWImageNode
|
2014-06-07 03:17:26 +00:00
|
|
|
*
|
2013-03-27 22:56:54 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Object} [element] Reference to element in linear model
|
|
|
|
*/
|
2014-06-07 03:17:26 +00:00
|
|
|
ve.dm.MWInlineImageNode = function VeDmMWInlineImageNode() {
|
2013-10-15 12:18:11 +00:00
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.dm.MWInlineImageNode.super.apply( this, arguments );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.dm.MWImageNode.call( this );
|
2013-03-27 22:56:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.dm.MWInlineImageNode, ve.dm.LeafNode );
|
2013-03-27 22:56:54 +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.MWInlineImageNode, ve.dm.GeneratedContentNode );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.dm.MWInlineImageNode, ve.dm.MWImageNode );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2013-03-27 22:56:54 +00:00
|
|
|
/* Static Properties */
|
|
|
|
|
2013-06-07 01:27:07 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.isContent = true;
|
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.name = 'mwInlineImage';
|
2013-03-27 22:56:54 +00:00
|
|
|
|
2015-02-18 10:29:14 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.preserveHtmlAttributes = function ( attribute ) {
|
2018-06-26 12:02:19 +00:00
|
|
|
var attributes = [ 'typeof', 'class', 'src', 'resource', 'width', 'height', 'href', 'data-mw' ];
|
2015-03-10 12:50:42 +00:00
|
|
|
return attributes.indexOf( attribute ) === -1;
|
2013-06-07 01:27:07 +00:00
|
|
|
};
|
2013-03-27 22:56:54 +00:00
|
|
|
|
2022-05-31 18:35:52 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.matchTagNames = [ 'span' ];
|
2013-04-03 22:59:46 +00:00
|
|
|
|
2020-06-10 11:15:18 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.disallowedAnnotationTypes = [ 'link' ];
|
2013-09-19 18:03:04 +00:00
|
|
|
|
2013-10-15 12:18:11 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements, converter ) {
|
2022-05-31 18:35:52 +00:00
|
|
|
var container = domElements[ 0 ]; // <span>
|
2021-10-13 12:57:45 +00:00
|
|
|
var imgWrapper = container.children[ 0 ]; // <a> or <span>
|
2020-11-05 20:57:40 +00:00
|
|
|
if ( !imgWrapper ) {
|
|
|
|
// Malformed figure, alienate (T267282)
|
|
|
|
return null;
|
|
|
|
}
|
2021-10-13 12:57:45 +00:00
|
|
|
var img = imgWrapper.children[ 0 ]; // <img>, <video> or <audio>
|
|
|
|
var typeofAttrs = ( container.getAttribute( 'typeof' ) || '' ).trim().split( /\s+/ );
|
|
|
|
var mwDataJSON = container.getAttribute( 'data-mw' );
|
|
|
|
var mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
|
|
|
|
var classes = container.getAttribute( 'class' );
|
|
|
|
var recognizedClasses = [];
|
|
|
|
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-24 15:21:04 +00:00
|
|
|
typeofAttrs.splice( errorIndex, 1 );
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
type: types.frameType,
|
2018-08-02 19:25:08 +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
|
|
|
originalClasses: classes,
|
|
|
|
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-05-31 18:35:52 +00:00
|
|
|
isError: isError
|
2015-05-24 15:21:04 +00:00
|
|
|
};
|
|
|
|
|
2013-06-07 01:27:07 +00:00
|
|
|
// Extract individual classes
|
2013-06-28 00:24:05 +00:00
|
|
|
classes = typeof classes === 'string' ? classes.trim().split( /\s+/ ) : [];
|
2013-06-07 01:27:07 +00:00
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
// Deal with border flag
|
|
|
|
if ( classes.indexOf( 'mw-image-border' ) !== -1 ) {
|
|
|
|
attributes.borderImage = true;
|
|
|
|
recognizedClasses.push( 'mw-image-border' );
|
|
|
|
}
|
2018-05-14 12:02:20 +00:00
|
|
|
|
2013-06-07 01:27:07 +00:00
|
|
|
// Vertical alignment
|
2018-05-14 12:02:20 +00:00
|
|
|
attributes.valign = 'default';
|
|
|
|
[ 'midde', 'baseline', 'sub', 'super', 'top', 'text-top', 'bottom', 'text-bottom' ].some( function ( valign ) {
|
|
|
|
var className = 'mw-valign-' + valign;
|
|
|
|
if ( classes.indexOf( className ) !== -1 ) {
|
|
|
|
attributes.valign = valign;
|
|
|
|
recognizedClasses.push( className );
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-02 20:30:00 +00:00
|
|
|
return false;
|
2018-05-14 12:02:20 +00:00
|
|
|
} );
|
2013-06-07 01:27:07 +00:00
|
|
|
|
|
|
|
// Border
|
|
|
|
if ( classes.indexOf( 'mw-image-border' ) !== -1 ) {
|
2014-03-25 16:01:04 +00:00
|
|
|
attributes.borderImage = true;
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses.push( 'mw-image-border' );
|
2013-06-07 01:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default-size
|
|
|
|
if ( classes.indexOf( 'mw-default-size' ) !== -1 ) {
|
|
|
|
attributes.defaultSize = true;
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses.push( 'mw-default-size' );
|
2013-06-07 01:27:07 +00:00
|
|
|
}
|
2013-06-28 00:24:05 +00:00
|
|
|
|
|
|
|
// Store unrecognized classes so we can restore them on the way out
|
2013-10-23 01:26:50 +00:00
|
|
|
attributes.unrecognizedClasses = OO.simpleArrayDifference( classes, recognizedClasses );
|
2013-06-28 00:24:05 +00:00
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var dataElement = { type: this.name, attributes: attributes };
|
2013-10-15 12:18:11 +00:00
|
|
|
|
|
|
|
this.storeGeneratedContents( dataElement, dataElement.attributes.src, converter.getStore() );
|
|
|
|
|
|
|
|
return dataElement;
|
2013-03-27 22:56:54 +00:00
|
|
|
};
|
|
|
|
|
2022-01-18 23:44:41 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.toDomElements = function ( dataElement, doc, converter ) {
|
2022-01-19 00:43:38 +00:00
|
|
|
var attributes = dataElement.attributes,
|
|
|
|
mediaClass = attributes.mediaClass,
|
2022-05-31 18:35:52 +00:00
|
|
|
container = doc.createElement( 'span' ),
|
2022-01-19 00:43:38 +00:00
|
|
|
img = doc.createElement( attributes.isError ? 'span' : this.typesToTags[ mediaClass ] ),
|
2013-06-28 00:24:05 +00:00
|
|
|
classes = [],
|
2022-01-19 00:43:38 +00:00
|
|
|
originalClasses = attributes.originalClasses;
|
2013-06-07 01:27:07 +00:00
|
|
|
|
2022-01-19 00:39:56 +00:00
|
|
|
ve.setDomAttributes( img, attributes, [ 'resource' ] );
|
|
|
|
var width = attributes.width;
|
|
|
|
var height = attributes.height;
|
|
|
|
if ( width !== null ) {
|
|
|
|
img.setAttribute( attributes.isError ? 'data-width' : 'width', width );
|
|
|
|
}
|
|
|
|
if ( height !== null ) {
|
|
|
|
img.setAttribute( attributes.isError ? 'data-width' : 'height', height );
|
|
|
|
}
|
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var srcAttr = this.typesToSrcAttrs[ mediaClass ];
|
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
|
|
|
}
|
2013-06-07 01:27:07 +00:00
|
|
|
|
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 );
|
2019-03-28 16:23:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 18:08:35 +00:00
|
|
|
// RDFa type
|
2022-01-19 00:39:56 +00:00
|
|
|
container.setAttribute( 'typeof', this.getRdfa( mediaClass, attributes.type, attributes.isError ) );
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( !ve.isEmptyObject( attributes.mw ) ) {
|
|
|
|
container.setAttribute( 'data-mw', JSON.stringify( attributes.mw ) );
|
2018-06-26 12:02:19 +00:00
|
|
|
}
|
2013-09-09 19:42:20 +00:00
|
|
|
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( attributes.defaultSize ) {
|
2013-06-28 00:24:05 +00:00
|
|
|
classes.push( 'mw-default-size' );
|
2013-06-07 01:27:07 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( attributes.borderImage ) {
|
2013-06-28 00:24:05 +00:00
|
|
|
classes.push( 'mw-image-border' );
|
2013-06-07 01:27:07 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( attributes.valign && attributes.valign !== 'default' ) {
|
|
|
|
classes.push( 'mw-valign-' + attributes.valign );
|
2013-06-28 00:24:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( attributes.unrecognizedClasses ) {
|
|
|
|
classes = OO.simpleArrayUnion( classes, attributes.unrecognizedClasses );
|
2013-06-28 00:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
originalClasses &&
|
|
|
|
ve.compare( originalClasses.trim().split( /\s+/ ).sort(), classes.sort() )
|
|
|
|
) {
|
2020-04-09 13:33:54 +00:00
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
2020-12-11 16:09:42 +00:00
|
|
|
container.className = originalClasses;
|
2013-06-28 00:24:05 +00:00
|
|
|
} else if ( classes.length > 0 ) {
|
2020-04-09 13:33:54 +00:00
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
2020-12-11 16:09:42 +00:00
|
|
|
container.className = classes.join( ' ' );
|
2013-06-07 01:27:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var firstChild;
|
2022-01-19 00:43:38 +00:00
|
|
|
if ( attributes.href ) {
|
2013-06-07 01:27:07 +00:00
|
|
|
firstChild = doc.createElement( 'a' );
|
2022-01-19 00:43:38 +00:00
|
|
|
firstChild.setAttribute( 'href', attributes.href );
|
2022-03-09 19:33:42 +00:00
|
|
|
if ( attributes.imgWrapperClassAttr ) {
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
firstChild.className = attributes.imgWrapperClassAttr;
|
|
|
|
}
|
2013-06-07 01:27:07 +00:00
|
|
|
} else {
|
|
|
|
firstChild = doc.createElement( 'span' );
|
|
|
|
}
|
|
|
|
|
2022-01-18 23:44:41 +00:00
|
|
|
if ( attributes.isError ) {
|
|
|
|
if ( converter.isForPreview() ) {
|
|
|
|
firstChild.classList.add( 'new' );
|
|
|
|
}
|
|
|
|
var filename = mw.libs.ve.normalizeParsoidResourceName( attributes.resource || '' );
|
|
|
|
img.appendChild( doc.createTextNode( filename ) );
|
2022-03-21 21:03:00 +00:00
|
|
|
// At the moment, preserving this is only relevant on mw:Error spans
|
|
|
|
if ( attributes.imageClassAttr ) {
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
img.className = attributes.imageClassAttr;
|
|
|
|
}
|
2022-01-18 23:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 16:09:42 +00:00
|
|
|
container.appendChild( firstChild );
|
2013-06-07 01:27:07 +00:00
|
|
|
firstChild.appendChild( img );
|
|
|
|
|
2020-12-11 16:09:42 +00:00
|
|
|
return [ container ];
|
2013-03-29 11:57:42 +00:00
|
|
|
};
|
|
|
|
|
2013-03-27 22:56:54 +00:00
|
|
|
/* Registration */
|
|
|
|
|
2015-05-16 17:02:33 +00:00
|
|
|
ve.dm.modelRegistry.unregister( ve.dm.InlineImageNode );
|
2013-04-24 23:46:34 +00:00
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWInlineImageNode );
|