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

93.67% Statements 74/79
89.47% Branches 51/57
100% Functions 9/9
93.67% Lines 74/79

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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                                  1x   1573x     1573x         1x   1x       1x   1x   1x   1x   1x   1x     1x 9x         1x 824x   824x 824x   824x     1x 255x 255x 255x   255x 255x   255x                 1x 1207x 1207x 1207x 1207x 2x   1207x 1x   1207x 9x   1207x 2x                   1x 370x     370x     370x     370x                     1x 36x 36x 36x 36x 36x 2x   36x 1x   36x 5x   36x 1x                             1x 67x               67x 28x   67x       67x     1x 18x               18x   9x 1x   9x 3x     9x 4x   9x 9x       5x         1x  
/*!
 * VisualEditor DataModel TableCellNode class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * DataModel table cell node.
 *
 * @class
 * @extends ve.dm.BranchNode
 * @mixes 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 ) {
	const attributes = Object.create( null );
 
	ve.dm.TableCellableNode.static.setAttributes( attributes, domElements );
	ve.dm.TableCellNode.static.setAlignmentAttributes( attributes, domElements );
 
	return { type: this.name, attributes };
};
 
ve.dm.TableCellNode.static.toDomElements = function ( dataElement, doc ) {
	const tag = dataElement.attributes && dataElement.attributes.style === 'header' ? 'th' : 'td',
		domElement = doc.createElement( tag ),
		attributes = dataElement.attributes;
 
	ve.dm.TableCellableNode.static.applyAttributes( attributes, domElement );
	ve.dm.TableCellNode.static.applyAlignmentAttributes( attributes, domElement );
 
	return [ domElement ];
};
 
/**
 * Sets align and textAlign from the DOM element onto model data (via toDataElement).
 *
 * @param {ve.dm.TableCellableNode.TableAttributes} attributes Attributes of model data
 * @param {HTMLElement[]} domElements DOM elements
 */
ve.dm.TableCellNode.static.setAlignmentAttributes = function ( attributes, domElements ) {
	const align = domElements[ 0 ].getAttribute( 'align' );
	const valign = domElements[ 0 ].getAttribute( 'valign' );
	const { textAlign, verticalAlign } = domElements[ 0 ].style;
	if ( align ) {
		attributes.align = align;
	}
	if ( valign ) {
		attributes.valign = valign;
	}
	if ( textAlign ) {
		attributes.textAlign = attributes.originalTextAlign = textAlign;
	}
	if ( verticalAlign ) {
		attributes.verticalAlign = attributes.originalVerticalAlign = verticalAlign;
	}
};
 
/**
 * Apply alignment attributes from model data onto the DOM element (via toDomElements).
 *
 * @param {ve.dm.TableCellableNode.TableAttributes} attributes Attributes of model data
 * @param {HTMLElement} domElement DOM element
 */
ve.dm.TableCellNode.static.applyAlignmentAttributes = function ( attributes, domElement ) {
	Iif ( attributes.align ) {
		domElement.setAttribute( 'align', attributes.align );
	}
	Iif ( attributes.valign ) {
		domElement.setAttribute( 'valign', attributes.valign );
	}
	Iif ( attributes.textAlign !== attributes.originalTextAlign ) {
		domElement.style.textAlign = attributes.textAlign;
	}
	Iif ( attributes.verticalAlign !== attributes.originalVerticalAlign ) {
		domElement.style.verticalAlign = attributes.verticalAlign;
	}
};
 
/**
 * Copies align and textAlign from one model data onto another model data (via createData).
 *
 * @param {ve.dm.TableCellNode|ve.dm.TableRowNode} clonedCell Copy certain attributes from this cell
 * @param {ve.dm.TableCellableNode.TableAttributes} attributes Attributes of model data
 */
ve.dm.TableCellNode.static.copyAlignmentData = function ( clonedCell, attributes ) {
	const align = clonedCell.getAttribute( 'align' );
	const valign = clonedCell.getAttribute( 'valign' );
	const textAlign = clonedCell.getAttribute( 'textAlign' );
	const verticalAlign = clonedCell.getAttribute( 'verticalAlign' );
	if ( align ) {
		attributes.align = align;
	}
	if ( valign ) {
		attributes.valign = valign;
	}
	if ( textAlign ) {
		attributes.textAlign = textAlign;
	}
	if ( verticalAlign ) {
		attributes.verticalAlign = verticalAlign;
	}
};
 
/**
 * 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 {ve.dm.TableCellNode} [options.clonedCell] Copy certain attributes from this cell
 * @param {Array} [options.content] Linear model data, defaults to empty wrapper paragraph
 * @return {ve.dm.LinearData.Item[]} Model data for a new table cell
 */
ve.dm.TableCellNode.static.createData = function ( options = {} ) {
	const opening = {
		type: 'tableCell',
		attributes: {
			style: options.style || 'data',
			rowspan: options.rowspan || 1,
			colspan: options.colspan || 1
		}
	};
	if ( options.clonedCell ) {
		this.copyAlignmentData( options.clonedCell, opening.attributes );
	}
	const content = options.content || [
		{ type: 'paragraph', internal: { generated: 'wrapper' } },
		{ type: '/paragraph' }
	];
	return [ opening ].concat( content, { type: '/tableCell' } );
};
 
ve.dm.TableCellNode.static.describeChange = function ( key, change ) {
	Iif ( 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 ) )
		);
	} else if ( key === 'colspan' || key === 'rowspan' ) {
		// colspan/rowspan of '1' is the same as not setting it
		if ( change.from === 1 ) {
			change.from = undefined;
		}
		if ( change.to === 1 ) {
			change.to = undefined;
		}
		// These might be the same now
		if ( change.from === change.to ) {
			return null;
		}
	} else Eif ( key === 'originalColspan' || key === 'originalRowspan' ) {
		return null;
	}
 
	// Parent method
	return ve.dm.TableCellNode.super.static.describeChange.call( this, key, change );
};
 
/* Registration */
 
ve.dm.modelRegistry.register( ve.dm.TableCellNode );