2013-04-25 00:47:17 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWBlockImageNode class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel MediaWiki image node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.dm.BranchNode
|
|
|
|
* @constructor
|
|
|
|
* @param {number} [length] Length of content data in document
|
|
|
|
* @param {Object} [element] Reference to element in linear model
|
|
|
|
*/
|
|
|
|
ve.dm.MWBlockImageNode = function VeDmMWBlockImageNode( length, element ) {
|
|
|
|
ve.dm.BranchNode.call( this, 0, element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.inheritClass( ve.dm.MWBlockImageNode, ve.dm.BranchNode );
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.name = 'mwBlockImage';
|
2013-04-25 00:47:17 +00:00
|
|
|
|
2013-05-20 23:54:03 +00:00
|
|
|
ve.dm.MWBlockImageNode.static.storeHtmlAttributes = [ 'data-parsoid' ];
|
|
|
|
|
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-05-14 23:39:13 +00:00
|
|
|
// Match typeof="mw:Image/Thumb" and typeof="mw:Image/Frame"
|
|
|
|
ve.dm.MWBlockImageNode.static.matchRdfaTypes = [ /mw:Image\/(Thumb|Frame)/ ];
|
2013-04-25 00:47:17 +00:00
|
|
|
|
|
|
|
ve.dm.MWBlockImageNode.static.toDataElement = function ( domElements, converter ) {
|
|
|
|
var $figure = $( domElements[0] ),
|
|
|
|
$a = $figure.children( 'a' ).eq( 0 ),
|
|
|
|
$img = $a.children( 'img' ).eq( 0 ),
|
|
|
|
$caption = $figure.children( 'figcaption' ).eq( 0 ),
|
2013-05-14 23:39:13 +00:00
|
|
|
typeofAttr = $figure.attr( 'typeof' ),
|
2013-05-15 23:23:42 +00:00
|
|
|
classes = $figure.attr( 'class' ),
|
2013-05-14 23:39:13 +00:00
|
|
|
attributes = {
|
|
|
|
href: $a.attr( 'href' ),
|
|
|
|
src: $img.attr( 'src' ),
|
|
|
|
width: $img.attr( 'width' ),
|
|
|
|
height: $img.attr( 'height' ),
|
|
|
|
resource: $img.attr( 'resource' )
|
|
|
|
};
|
|
|
|
|
2013-05-15 23:23:42 +00:00
|
|
|
// Extract individual classes
|
|
|
|
classes = typeof classes === 'string' ?
|
|
|
|
classes.replace( /\s{2,}/g, ' ' ).split( ' ' ) : [];
|
|
|
|
|
2013-05-14 23:39:13 +00:00
|
|
|
// Type
|
|
|
|
switch ( typeofAttr ) {
|
|
|
|
case 'mw:Image/Thumb':
|
|
|
|
attributes.type = 'thumb';
|
|
|
|
break;
|
|
|
|
case 'mw:Image/Frame':
|
|
|
|
attributes.type = 'frame';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Horizontal alignment
|
|
|
|
if ( classes.indexOf( 'mw-halign-left' ) !== -1 ) {
|
|
|
|
attributes.align = 'left';
|
|
|
|
} else if ( classes.indexOf( 'mw-halign-right' ) !== -1 ) {
|
|
|
|
attributes.align = 'right';
|
|
|
|
} else if ( classes.indexOf( 'mw-halign-center' ) !== -1 ) {
|
|
|
|
attributes.align = 'center';
|
|
|
|
} else if ( classes.indexOf( 'mw-halign-none' ) !== -1 ) {
|
|
|
|
attributes.align = 'none';
|
|
|
|
} else {
|
|
|
|
attributes.align = 'right';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default-size
|
|
|
|
if ( $figure.hasClass( 'mw-default-size' ) ) {
|
|
|
|
attributes.defaultSize = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $caption.length === 0 ) {
|
2013-05-23 05:47:11 +00:00
|
|
|
return [
|
2013-05-28 11:31:41 +00:00
|
|
|
{ 'type': 'mwBlockImage', 'attributes': attributes },
|
|
|
|
{ 'type': 'mwImageCaption' },
|
|
|
|
{ 'type': '/mwImageCaption' },
|
|
|
|
{ 'type': '/mwBlockImage' }
|
2013-05-23 05:47:11 +00:00
|
|
|
];
|
2013-05-14 23:39:13 +00:00
|
|
|
} else {
|
2013-05-28 11:31:41 +00:00
|
|
|
return [ { 'type': 'mwBlockImage', 'attributes': attributes } ].
|
|
|
|
concat( converter.getDataFromDomRecursionClean( $caption[0], { 'type': 'mwImageCaption' } ) ).
|
|
|
|
concat( [ { 'type': '/mwBlockImage' } ] );
|
2013-05-14 23:39:13 +00:00
|
|
|
}
|
2013-04-25 00:47:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWBlockImageNode.static.toDomElements = function ( data, doc, converter ) {
|
|
|
|
var dataElement = data[0],
|
|
|
|
figure = doc.createElement( 'figure' ),
|
|
|
|
a = doc.createElement( 'a' ),
|
|
|
|
img = doc.createElement( 'img' ),
|
|
|
|
wrapper = doc.createElement( 'div' );
|
|
|
|
figure.setAttribute( 'typeof', 'mw:Image/Thumb' );
|
|
|
|
a.setAttribute( 'rel', 'mw:thumb' );
|
|
|
|
a.setAttribute( 'href', dataElement.attributes.href );
|
|
|
|
img.setAttribute( 'src', dataElement.attributes.src );
|
|
|
|
img.setAttribute( 'width', dataElement.attributes.width );
|
|
|
|
img.setAttribute( 'height', dataElement.attributes.height );
|
|
|
|
img.setAttribute( 'resource', dataElement.attributes.resource );
|
|
|
|
figure.appendChild( a );
|
|
|
|
a.appendChild( img );
|
|
|
|
|
2013-05-28 22:35:23 +00:00
|
|
|
converter.getDomSubtreeFromData( data.slice( 1, -1 ), wrapper );
|
2013-04-25 00:47:17 +00:00
|
|
|
while ( wrapper.firstChild ) {
|
|
|
|
figure.appendChild( wrapper.firstChild );
|
|
|
|
}
|
|
|
|
return [ figure ];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWBlockImageNode );
|