mediawiki-extensions-Visual.../modules/ve/dm/nodes/ve.dm.ImageNode.js
Ed Sanders dd73b873a8 Add alt attribute to core image nodes
Also update and refactor tests.

Change-Id: I96d08faed42118b713af8011c5d6befb760e65c9
2013-09-07 12:56:17 -07:00

60 lines
1.5 KiB
JavaScript

/*!
* VisualEditor DataModel ImageNode class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* DataModel image node.
*
* @class
* @extends ve.dm.LeafNode
* @constructor
* @param {number} [length] Length of content data in document
* @param {Object} [element] Reference to element in linear model
*/
ve.dm.ImageNode = function VeDmImageNode( length, element ) {
// Parent constructor
ve.dm.LeafNode.call( this, 0, element );
};
/* Inheritance */
ve.inheritClass( ve.dm.ImageNode, ve.dm.LeafNode );
/* Static Properties */
ve.dm.ImageNode.static.name = 'image';
ve.dm.ImageNode.static.isContent = true;
ve.dm.ImageNode.static.matchTagNames = [ 'img' ];
ve.dm.ImageNode.static.toDataElement = function ( domElements ) {
var $node = $( domElements[0] ),
alt = $node.attr( 'alt' ),
width = $node.attr( 'width' ),
height = $node.attr( 'height' );
return {
'type': 'image',
'attributes': {
'src': $node.attr( 'src' ),
'alt': alt !== undefined ? alt : null,
'width': width !== undefined && width !== '' ? Number( width ) : null,
'height': height !== undefined && height !== '' ? Number( height ) : null
}
};
};
ve.dm.ImageNode.static.toDomElements = function ( dataElement, doc ) {
var domElement = doc.createElement( 'img' );
ve.setDomAttributes( domElement, dataElement.attributes, [ 'alt', 'src', 'width', 'height' ] );
return [ domElement ];
};
/* Registration */
ve.dm.modelRegistry.register( ve.dm.ImageNode );