All files / src/dm/nodes ve.dm.BlockImageNode.js

90.57% Statements 48/53
75% Branches 15/20
71.43% Functions 5/7
90.57% Lines 48/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                                    1x   19x     19x 19x                   1x   1x     1x   1x       1x   1x         1x   1x   1x   1x   1x 12x     1x 12x 12x 12x 12x 12x 12x   12x             12x   12x         12x               12x                 1x 16x 16x 16x   16x   16x   16x 16x   12x     16x     16x 16x 16x 16x 16x       16x                   1x               1x   34x         1x  
/*!
 * VisualEditor DataModel BlockImageNode class.
 *
 * @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * DataModel block image node.
 *
 * @class
 * @extends ve.dm.BranchNode
 * @mixins ve.dm.ImageNode
 * @mixins ve.dm.AlignableNode
 *
 * @constructor
 * @param {Object} [element] Reference to element in linear model
 * @param {ve.dm.Node[]} [children]
 */
ve.dm.BlockImageNode = function VeDmBlockImageNode() {
	// Parent constructor
	ve.dm.BlockImageNode.super.apply( this, arguments );
 
	// Mixin constructors
	ve.dm.ImageNode.call( this );
	ve.dm.AlignableNode.call( this );
 
	// <figure>
	//   <img/>
	//   [<figcaption/>]
	// </figure>
};
 
/* Inheritance */
 
OO.inheritClass( ve.dm.BlockImageNode, ve.dm.BranchNode );
 
OO.mixinClass( ve.dm.BlockImageNode, ve.dm.ImageNode );
 
// Mixin Alignable's parent class
OO.mixinClass( ve.dm.BlockImageNode, ve.dm.ClassAttributeNode );
 
OO.mixinClass( ve.dm.BlockImageNode, ve.dm.AlignableNode );
 
/* Static Properties */
 
ve.dm.BlockImageNode.static.name = 'blockImage';
 
ve.dm.BlockImageNode.static.preserveHtmlAttributes = function ( attribute ) {
	var attributes = [ 'class', 'src', 'width', 'height' ];
	return attributes.indexOf( attribute ) === -1;
};
 
ve.dm.BlockImageNode.static.handlesOwnChildren = true;
 
ve.dm.BlockImageNode.static.ignoreChildren = true;
 
ve.dm.BlockImageNode.static.childNodeTypes = [ 'imageCaption' ];
 
ve.dm.BlockImageNode.static.matchTagNames = [ 'figure' ];
 
ve.dm.BlockImageNode.static.matchFunction = function ( element ) {
	return element.children[ 0 ] && element.children[ 0 ].nodeName === 'IMG';
};
 
ve.dm.BlockImageNode.static.toDataElement = function ( domElements, converter ) {
	var figure = domElements[ 0 ];
	var classAttr = figure.getAttribute( 'class' );
	var img = figure.children[ 0 ];
	var width = img.getAttribute( 'width' );
	var height = img.getAttribute( 'height' );
	var caption = figure.children[ 1 ];
 
	var attributes = {
		src: img.getAttribute( 'src' ),
		width: width !== null && width !== '' ? +width : null,
		height: height !== null && height !== '' ? +height : null,
		alt: img.getAttribute( 'alt' )
	};
 
	this.setClassAttributes( attributes, classAttr );
 
	var dataElement = {
		type: this.name,
		attributes: attributes
	};
 
	Iif ( !caption ) {
		return [
			dataElement,
			{ type: 'imageCaption' },
			{ type: 'imageCaption' },
			{ type: '/' + this.name }
		];
	} else {
		return [ dataElement ]
			.concat( converter.getDataFromDomClean( caption, { type: 'imageCaption' } ) )
			.concat( [ { type: '/' + this.name } ] );
	}
};
 
// TODO: Consider using jQuery instead of pure JS.
// TODO: At this moment node is not resizable but when it will be then adding defaultSize class
// should be more conditional.
ve.dm.BlockImageNode.static.toDomElements = function ( data, doc, converter ) {
	var dataElement = data[ 0 ],
		figure = doc.createElement( 'figure' ),
		img = doc.createElement( 'img' );
 
	ve.setDomAttributes( img, dataElement.attributes, [ 'alt', 'src', 'width', 'height' ] );
 
	figure.appendChild( img );
 
	var classAttr = this.getClassAttrFromAttributes( dataElement.attributes );
	if ( classAttr ) {
		// eslint-disable-next-line mediawiki/class-doc
		figure.className = classAttr;
	}
 
	var captionData = data.slice( 1, -1 );
	// If length of captionData is smaller or equal to 2 it means that there is no caption or that
	// it is empty - in both cases we are going to skip appending <figcaption>.
	Eif ( captionData.length > 2 ) {
		var wrapper = doc.createElement( 'div' );
		converter.getDomSubtreeFromData( data.slice( 1, -1 ), wrapper );
		while ( wrapper.firstChild ) {
			figure.appendChild( wrapper.firstChild );
		}
	}
 
	return [ figure ];
};
 
/* Methods */
 
/**
 * Get the caption node of the image.
 *
 * @return {ve.dm.BlockImageCaptionNode|null} Caption node, if present
 */
ve.dm.BlockImageNode.prototype.getCaptionNode = function () {
	var node = this.children[ 0 ];
	return node instanceof ve.dm.BlockImageCaptionNode ? node : null;
};
 
/**
 * @inheritdoc
 */
ve.dm.BlockImageNode.prototype.suppressSlugType = function () {
	// TODO: Have alignment attribute changes trigger a parent branch node re-render
	return this.getAttribute( 'align' ) !== 'center' ? 'float' : null;
};
 
/* Registration */
 
ve.dm.modelRegistry.register( ve.dm.BlockImageNode );