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
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @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-03-27 22:56:54 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {number} [length] Length of content data in document
|
|
|
|
* @param {Object} [element] Reference to element in linear model
|
|
|
|
*/
|
2013-04-24 23:46:34 +00:00
|
|
|
ve.dm.MWInlineImageNode = function VeDmMWInlineImageNode( length, element ) {
|
2013-06-07 01:27:07 +00:00
|
|
|
ve.dm.LeafNode.call( this, 0, element );
|
2013-03-27 22:56:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-06-07 01:27:07 +00:00
|
|
|
ve.inheritClass( ve.dm.MWInlineImageNode, ve.dm.LeafNode );
|
2013-03-27 22:56:54 +00:00
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
2013-09-09 19:42:20 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.rdfaToType = {
|
|
|
|
'mw:Image': 'inline',
|
|
|
|
'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
|
|
|
|
2013-06-07 01:27:07 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.storeHtmlAttributes = {
|
|
|
|
'blacklist': [ 'typeof', 'class', 'src', 'resource', 'width', 'height', 'href' ]
|
|
|
|
};
|
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 () {
|
|
|
|
return Object.keys( this.rdfaToType );
|
|
|
|
};
|
2013-03-27 22:56:54 +00:00
|
|
|
|
2013-04-24 23:46:34 +00:00
|
|
|
ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements ) {
|
2013-06-07 01:27:07 +00:00
|
|
|
var $span = $( domElements[0] ),
|
|
|
|
$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
|
|
|
|
|
|
|
// 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 ) {
|
|
|
|
attributes.border = 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
|
|
|
|
attributes.unrecognizedClasses = ve.simpleArrayDifference( classes, recognizedClasses );
|
|
|
|
|
2013-09-09 19:42:20 +00:00
|
|
|
return { 'type': this.name, 'attributes': attributes };
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if ( data.attributes.border ) {
|
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 ) {
|
|
|
|
classes = ve.simpleArrayUnion( classes, data.attributes.unrecognizedClasses );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|