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
|
|
|
*
|
2018-01-03 00:54:47 +00:00
|
|
|
* @copyright 2011-2018 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
|
|
|
|
2017-08-10 16:20:20 +00:00
|
|
|
// <span> is here for backwards compatibility with Parsoid content that may be
|
|
|
|
// stored in RESTBase. This is now generated as <figure-inline>. It should
|
|
|
|
// be safe to remove when verion 1.5 content is no longer acceptable.
|
|
|
|
ve.dm.MWInlineImageNode.static.matchTagNames = [ 'span', 'figure-inline' ];
|
2013-04-03 22:59:46 +00:00
|
|
|
|
2013-09-19 18:03:04 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.blacklistedAnnotationTypes = [ 'link' ];
|
|
|
|
|
2013-10-15 12:18:11 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements, converter ) {
|
2017-04-27 18:08:35 +00:00
|
|
|
var dataElement, attributes, types,
|
2018-06-05 11:41:09 +00:00
|
|
|
figureInline = domElements[ 0 ],
|
2018-08-02 19:25:08 +00:00
|
|
|
imgWrapper = figureInline.children[ 0 ], // <a> or <span>
|
|
|
|
img = imgWrapper.children[ 0 ], // <img>, <video> or <audio>
|
2018-10-02 20:48:25 +00:00
|
|
|
typeofAttrs = ( figureInline.getAttribute( 'typeof' ) || '' ).trim().split( /\s+/ ),
|
2018-06-26 12:02:19 +00:00
|
|
|
mwDataJSON = figureInline.getAttribute( 'data-mw' ),
|
|
|
|
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {},
|
2018-06-05 11:41:09 +00:00
|
|
|
classes = figureInline.getAttribute( 'class' ),
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses = [],
|
2015-05-24 15:21:04 +00:00
|
|
|
errorIndex = typeofAttrs.indexOf( 'mw:Error' ),
|
2018-06-05 11:41:09 +00:00
|
|
|
width = img.getAttribute( 'width' ),
|
|
|
|
height = img.getAttribute( 'height' );
|
2013-06-07 01:27:07 +00:00
|
|
|
|
2015-05-24 15:21:04 +00:00
|
|
|
if ( errorIndex !== -1 ) {
|
|
|
|
typeofAttrs.splice( errorIndex, 1 );
|
|
|
|
}
|
|
|
|
|
2017-04-29 11:36:17 +00:00
|
|
|
types = this.rdfaToTypes[ typeofAttrs[ 0 ] ];
|
2017-04-27 18:08:35 +00:00
|
|
|
|
2015-05-24 15:21:04 +00:00
|
|
|
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' ),
|
2018-06-05 11:41:09 +00:00
|
|
|
href: imgWrapper.getAttribute( 'href' ),
|
|
|
|
resource: img.getAttribute( 'resource' ),
|
|
|
|
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,
|
2018-06-05 11:41:09 +00:00
|
|
|
isError: errorIndex !== -1
|
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;
|
|
|
|
}
|
|
|
|
} );
|
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
|
|
|
|
2014-08-22 20:50:48 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-06-07 01:27:07 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.toDomElements = function ( data, doc ) {
|
2018-08-02 19:25:08 +00:00
|
|
|
var firstChild, srcAttr,
|
2017-04-27 18:08:35 +00:00
|
|
|
mediaClass = data.attributes.mediaClass,
|
2017-08-10 16:20:20 +00:00
|
|
|
figureInline = doc.createElement( 'figure-inline' ),
|
2018-08-02 19:25:08 +00:00
|
|
|
img = doc.createElement( this.typesToTags[ mediaClass ] ),
|
2013-06-28 00:24:05 +00:00
|
|
|
classes = [],
|
2017-04-27 18:08:35 +00:00
|
|
|
originalClasses = data.attributes.originalClasses;
|
2013-06-07 01:27:07 +00:00
|
|
|
|
2017-04-27 18:08:35 +00:00
|
|
|
ve.setDomAttributes( img, data.attributes, [ 'width', 'height', 'resource' ] );
|
2018-08-02 19:25:08 +00:00
|
|
|
srcAttr = this.typesToSrcAttrs[ mediaClass ];
|
|
|
|
if ( srcAttr ) {
|
|
|
|
img.setAttribute( srcAttr, data.attributes.src );
|
|
|
|
}
|
2013-06-07 01:27:07 +00:00
|
|
|
|
2017-04-27 18:08:35 +00:00
|
|
|
// RDFa type
|
2017-08-10 16:20:20 +00:00
|
|
|
figureInline.setAttribute( 'typeof', this.getRdfa( mediaClass, data.attributes.type ) );
|
2018-06-26 12:02:19 +00:00
|
|
|
if ( !ve.isEmptyObject( data.attributes.mw ) ) {
|
2018-07-18 11:16:07 +00:00
|
|
|
figureInline.setAttribute( 'data-mw', JSON.stringify( data.attributes.mw ) );
|
2018-06-26 12:02:19 +00:00
|
|
|
}
|
2013-09-09 19:42:20 +00:00
|
|
|
|
2013-06-07 01:27:07 +00:00
|
|
|
if ( data.attributes.defaultSize ) {
|
2013-06-28 00:24:05 +00:00
|
|
|
classes.push( 'mw-default-size' );
|
2013-06-07 01:27:07 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
if ( data.attributes.borderImage ) {
|
2013-06-28 00:24:05 +00:00
|
|
|
classes.push( 'mw-image-border' );
|
2013-06-07 01:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( data.attributes.valign && data.attributes.valign !== 'default' ) {
|
2013-06-28 00:24:05 +00:00
|
|
|
classes.push( 'mw-valign-' + data.attributes.valign );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( data.attributes.unrecognizedClasses ) {
|
2013-10-23 01:26:50 +00:00
|
|
|
classes = OO.simpleArrayUnion( classes, data.attributes.unrecognizedClasses );
|
2013-06-28 00:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
originalClasses &&
|
|
|
|
ve.compare( originalClasses.trim().split( /\s+/ ).sort(), classes.sort() )
|
|
|
|
) {
|
2017-08-10 16:20:20 +00:00
|
|
|
figureInline.className = originalClasses;
|
2013-06-28 00:24:05 +00:00
|
|
|
} else if ( classes.length > 0 ) {
|
2017-08-10 16:20:20 +00:00
|
|
|
figureInline.className = classes.join( ' ' );
|
2013-06-07 01:27:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 11:41:09 +00:00
|
|
|
if ( data.attributes.href ) {
|
2013-06-07 01:27:07 +00:00
|
|
|
firstChild = doc.createElement( 'a' );
|
|
|
|
firstChild.setAttribute( 'href', data.attributes.href );
|
|
|
|
} else {
|
|
|
|
firstChild = doc.createElement( 'span' );
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:20:20 +00:00
|
|
|
figureInline.appendChild( firstChild );
|
2013-06-07 01:27:07 +00:00
|
|
|
firstChild.appendChild( img );
|
|
|
|
|
2017-08-10 16:20:20 +00:00
|
|
|
return [ figureInline ];
|
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 );
|