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
|
|
|
*
|
2015-01-08 23:54:03 +00:00
|
|
|
* @copyright 2011-2015 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
|
2014-06-07 03:17:26 +00:00
|
|
|
ve.dm.LeafNode.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
|
|
|
|
2013-10-15 12:18:11 +00:00
|
|
|
// Need to mixin base class as well
|
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-09-09 19:42:20 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.rdfaToType = {
|
2014-03-25 16:01:04 +00:00
|
|
|
'mw:Image': 'none',
|
2013-09-09 19:42:20 +00:00
|
|
|
'mw:Image/Frameless': 'frameless'
|
|
|
|
};
|
|
|
|
|
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 ) {
|
|
|
|
return ve.indexOf(
|
|
|
|
attribute,
|
|
|
|
[ 'typeof', 'class', 'src', 'resource', 'width', 'height', 'href' ]
|
|
|
|
) === -1;
|
2013-06-07 01:27:07 +00:00
|
|
|
};
|
2013-03-27 22:56:54 +00:00
|
|
|
|
2013-06-19 21:08:45 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.matchTagNames = [ 'span' ];
|
2013-04-03 22:59:46 +00:00
|
|
|
|
2013-09-19 18:03:04 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.blacklistedAnnotationTypes = [ 'link' ];
|
|
|
|
|
2013-09-09 19:42:20 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.getMatchRdfaTypes = function () {
|
2014-02-20 21:30:29 +00:00
|
|
|
return ve.getObjectKeys( this.rdfaToType );
|
2013-09-09 19:42:20 +00:00
|
|
|
};
|
2013-03-27 22:56:54 +00:00
|
|
|
|
2013-10-15 12:18:11 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements, converter ) {
|
|
|
|
var dataElement,
|
|
|
|
$span = $( domElements[0] ),
|
2013-06-07 01:27:07 +00:00
|
|
|
$firstChild = $span.children().first(), // could be <span> or <a>
|
|
|
|
$img = $firstChild.children().first(),
|
|
|
|
typeofAttr = $span.attr( 'typeof' ),
|
|
|
|
classes = $span.attr( 'class' ),
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses = [],
|
2013-06-07 01:27:07 +00:00
|
|
|
attributes = {
|
2013-09-09 19:42:20 +00:00
|
|
|
type: this.rdfaToType[typeofAttr],
|
2013-06-07 01:27:07 +00:00
|
|
|
src: $img.attr( 'src' ),
|
2013-06-28 00:24:05 +00:00
|
|
|
resource: $img.attr( 'resource' ),
|
|
|
|
originalClasses: classes
|
2013-06-07 01:27:07 +00:00
|
|
|
},
|
|
|
|
width = $img.attr( 'width' ),
|
|
|
|
height = $img.attr( 'height' );
|
|
|
|
|
|
|
|
attributes.width = width !== undefined && width !== '' ? Number( width ) : null;
|
|
|
|
attributes.height = height !== undefined && height !== '' ? Number( height ) : null;
|
|
|
|
|
|
|
|
attributes.isLinked = $firstChild.is( 'a' );
|
|
|
|
if ( attributes.isLinked ) {
|
|
|
|
attributes.href = $firstChild.attr( 'href' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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' );
|
|
|
|
}
|
2013-06-07 01:27:07 +00:00
|
|
|
// Vertical alignment
|
|
|
|
if ( classes.indexOf( 'mw-valign-middle' ) !== -1 ) {
|
|
|
|
attributes.valign = 'middle';
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses.push( 'mw-valign-middle' );
|
2013-06-07 01:27:07 +00:00
|
|
|
} else if ( classes.indexOf( 'mw-valign-baseline' ) !== -1 ) {
|
|
|
|
attributes.valign = 'baseline';
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses.push( 'mw-valign-baseline' );
|
2013-06-07 01:27:07 +00:00
|
|
|
} else if ( classes.indexOf( 'mw-valign-sub' ) !== -1 ) {
|
|
|
|
attributes.valign = 'sub';
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses.push( 'mw-valign-sub' );
|
2013-06-07 01:27:07 +00:00
|
|
|
} else if ( classes.indexOf( 'mw-valign-super' ) !== -1 ) {
|
|
|
|
attributes.valign = 'super';
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses.push( 'mw-valign-super' );
|
2013-06-07 01:27:07 +00:00
|
|
|
} else if ( classes.indexOf( 'mw-valign-top' ) !== -1 ) {
|
|
|
|
attributes.valign = 'top';
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses.push( 'mw-valign-top' );
|
2013-06-07 01:27:07 +00:00
|
|
|
} else if ( classes.indexOf( 'mw-valign-text-top' ) !== -1 ) {
|
|
|
|
attributes.valign = 'text-top';
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses.push( 'mw-valign-text-top' );
|
2013-06-07 01:27:07 +00:00
|
|
|
} else if ( classes.indexOf( 'mw-valign-bottom' ) !== -1 ) {
|
|
|
|
attributes.valign = 'bottom';
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses.push( 'mw-valign-bottom' );
|
2013-06-07 01:27:07 +00:00
|
|
|
} else if ( classes.indexOf( 'mw-valign-text-bottom' ) !== -1 ) {
|
|
|
|
attributes.valign = 'text-bottom';
|
2013-06-28 00:24:05 +00:00
|
|
|
recognizedClasses.push( 'mw-valign-text-bottom' );
|
2013-06-07 01:27:07 +00:00
|
|
|
} else {
|
|
|
|
attributes.valign = 'default';
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 ) {
|
2013-06-28 00:24:05 +00:00
|
|
|
var firstChild,
|
|
|
|
span = doc.createElement( 'span' ),
|
2013-06-07 01:27:07 +00:00
|
|
|
img = doc.createElement( 'img' ),
|
2013-06-28 00:24:05 +00:00
|
|
|
classes = [],
|
2013-09-09 19:42:20 +00:00
|
|
|
originalClasses = data.attributes.originalClasses,
|
|
|
|
rdfa;
|
2013-06-07 01:27:07 +00:00
|
|
|
|
|
|
|
ve.setDomAttributes( img, data.attributes, [ 'src', 'width', 'height', 'resource' ] );
|
|
|
|
|
2013-09-09 19:42:20 +00:00
|
|
|
if ( !this.typeToRdfa ) {
|
|
|
|
this.typeToRdfa = {};
|
|
|
|
for ( rdfa in this.rdfaToType ) {
|
|
|
|
this.typeToRdfa[this.rdfaToType[rdfa]] = rdfa;
|
|
|
|
}
|
2013-06-07 01:27:07 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 19:42:20 +00:00
|
|
|
span.setAttribute( 'typeof', this.typeToRdfa[data.attributes.type] );
|
|
|
|
|
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() )
|
|
|
|
) {
|
|
|
|
span.className = originalClasses;
|
|
|
|
} else if ( classes.length > 0 ) {
|
|
|
|
span.className = classes.join( ' ' );
|
2013-06-07 01:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( data.attributes.isLinked ) {
|
|
|
|
firstChild = doc.createElement( 'a' );
|
|
|
|
firstChild.setAttribute( 'href', data.attributes.href );
|
|
|
|
} else {
|
|
|
|
firstChild = doc.createElement( 'span' );
|
|
|
|
}
|
|
|
|
|
|
|
|
span.appendChild( firstChild );
|
|
|
|
firstChild.appendChild( img );
|
|
|
|
|
|
|
|
return [ span ];
|
2013-03-29 11:57:42 +00:00
|
|
|
};
|
|
|
|
|
2013-03-27 22:56:54 +00:00
|
|
|
/* Registration */
|
|
|
|
|
2013-04-24 23:46:34 +00:00
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWInlineImageNode );
|