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

87.88% Statements 29/33
77.78% Branches 14/18
66.67% Functions 4/6
87.88% Lines 29/33

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                                  1x   1195x     1195x         1x   1x       1x   1x   1x   1x   1x   1x     1x           1x 614x   614x   614x           1x 134x 134x 134x   134x   134x                         1x 61x 61x               61x       61x     1x                               1x  
/*!
 * VisualEditor DataModel TableCellNode class.
 *
 * @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * DataModel table cell node.
 *
 * @class
 * @extends ve.dm.BranchNode
 * @mixins ve.dm.TableCellableNode
 *
 * @constructor
 * @param {Object} [element] Reference to element in linear model
 * @param {ve.dm.Node[]} [children]
 */
ve.dm.TableCellNode = function VeDmTableCellNode() {
	// Parent constructor
	ve.dm.TableCellNode.super.apply( this, arguments );
 
	// Mixin constructor
	ve.dm.TableCellableNode.call( this );
};
 
/* Inheritance */
 
OO.inheritClass( ve.dm.TableCellNode, ve.dm.BranchNode );
 
OO.mixinClass( ve.dm.TableCellNode, ve.dm.TableCellableNode );
 
/* Static Properties */
 
ve.dm.TableCellNode.static.name = 'tableCell';
 
ve.dm.TableCellNode.static.isUnwrappable = false;
 
ve.dm.TableCellNode.static.parentNodeTypes = [ 'tableRow' ];
 
ve.dm.TableCellNode.static.defaultAttributes = { style: 'data' };
 
ve.dm.TableCellNode.static.matchTagNames = [ 'td', 'th' ];
 
ve.dm.TableCellNode.static.isCellEditable = true;
 
// Exclude 'colspan' and 'rowspan' as they are managed explicitly
ve.dm.TableCellNode.static.preserveHtmlAttributes = function ( attribute ) {
	return attribute !== 'colspan' && attribute !== 'rowspan';
};
 
/* Static Methods */
 
ve.dm.TableCellNode.static.toDataElement = function ( domElements ) {
	var attributes = {};
 
	ve.dm.TableCellableNode.static.setAttributes( attributes, domElements );
 
	return {
		type: this.name,
		attributes: attributes
	};
};
 
ve.dm.TableCellNode.static.toDomElements = function ( dataElement, doc ) {
	var tag = dataElement.attributes && dataElement.attributes.style === 'header' ? 'th' : 'td',
		domElement = doc.createElement( tag ),
		attributes = dataElement.attributes;
 
	ve.dm.TableCellableNode.static.applyAttributes( attributes, domElement );
 
	return [ domElement ];
};
 
/**
 * Creates data that can be inserted into the model to create a new table cell.
 *
 * @param {Object} [options]
 * @param {string} [options.style='data'] Either 'header' or 'data'
 * @param {number} [options.rowspan=1] Number of rows the cell spans
 * @param {number} [options.colspan=1] Number of columns the cell spans
 * @param {Array} [options.content] Linear model data, defaults to empty wrapper paragraph
 * @return {Array} Model data for a new table cell
 */
ve.dm.TableCellNode.static.createData = function ( options ) {
	options = options || {};
	var opening = {
		type: 'tableCell',
		attributes: {
			style: options.style || 'data',
			rowspan: options.rowspan || 1,
			colspan: options.colspan || 1
		}
	};
	var content = options.content || [
		{ type: 'paragraph', internal: { generated: 'wrapper' } },
		{ type: '/paragraph' }
	];
	return [ opening ].concat( content ).concat( [ { type: '/tableCell' } ] );
};
 
ve.dm.TableCellNode.static.describeChange = function ( key, change ) {
	if ( key === 'style' ) {
		return ve.htmlMsg( 'visualeditor-changedesc-no-key',
			// The following messages are used here:
			// * visualeditor-table-format-data
			// * visualeditor-table-format-header
			this.wrapText( 'del', ve.msg( 'visualeditor-table-format-' + change.from ) ),
			this.wrapText( 'ins', ve.msg( 'visualeditor-table-format-' + change.to ) )
		);
	}
	// Parent method
	return ve.dm.TableCellNode.parent.static.describeChange.apply( this, arguments );
};
 
/* Registration */
 
ve.dm.modelRegistry.register( ve.dm.TableCellNode );