All files / src/dm ve.dm.TableCellableNode.js

100% Statements 55/55
98.11% Branches 52/53
100% Functions 11/11
100% Lines 54/54

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                            1x   1546x             1x       1x       1x     96x 96x 97x                                                 1x 817x     9x 9x 10x 10x 10x   10x   9x 3x   9x 1x     808x 808x 808x   808x   808x 50x 50x 49x       808x 54x 54x 53x                   1x 255x           255x 7x     255x 7x       255x 248x     255x 248x     255x                   1x 3415x               1x 3415x               1x 31x                     1x 61x                   1x 34x         30x      
/*!
 * VisualEditor DataModel TableCellableNode class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * DataModel node which can behave as a table cell
 *
 * @class
 *
 * @abstract
 * @constructor
 */
ve.dm.TableCellableNode = function VeDmTableCellableNode() {
	// Events
	this.connect( this, {
		attributeChange: 'onCellableAttributeChange'
	} );
};
 
/* Inheritance */
 
OO.initClass( ve.dm.TableCellableNode );
 
/* Static Properties */
 
ve.dm.TableCellableNode.static.isCellable = true;
 
/* Static Methods */
 
ve.dm.TableCellableNode.static.areNodesCellable = function ( domNodes ) {
	// We can handle a DM node consisting of multiple table cell DOM elements with identical rowspan
	// as a table element. Give up on anything else.
	const cellElems = new Set( [ 'td', 'th' ] );
	return domNodes.every(
		( node ) => cellElems.has( node.nodeName.toLowerCase() ) &&
			node.rowspan === domNodes[ 0 ].rowspan
	);
};
 
/**
 * @typedef {Object} ve.dm.TableCellableNode.TableAttributes
 * @property {string} style 'header' or 'data'
 * @property {number} [rowspan]
 * @property {string} [originalRowspan]
 * @property {number} [colspan]
 * @property {string} [originalColspan]
 * @property {string} [align] DOM attribute align (deprecated)
 * @property {string} [valign] DOM attribute valign (deprecated)
 * @property {string} [textAlign] CSS property text-align
 * @property {string} [verticalAlign] CSS property vertical-align
 * @property {string} [originalTextAlign] CSS property text-align (original value)
 * @property {string} [originalVerticalAlign] CSS property vertical-align (original value)
 */
 
/**
 * @param {ve.dm.TableCellableNode.TableAttributes} attributes
 * @param {HTMLElement[]} domElements
 * @param {boolean} isAlien
 */
ve.dm.TableCellableNode.static.setAttributes = function ( attributes, domElements, isAlien ) {
	if ( isAlien ) {
		// For alienTableCells, we only need the colspan and rowspan, which
		// may need to be summed over an about-group (T366984).
		let colspan = 0, rowspan = 0;
		Array.prototype.forEach.call( domElements, ( node ) => {
			const attrs = {};
			this.setAttributes( attrs, [ node ], false );
			colspan += attrs.colspan || 1;
			// TODO: Support a non-rectangular alien group
			rowspan = Math.max( rowspan, attrs.rowspan || 1 );
		} );
		if ( colspan !== 1 ) {
			attributes.colspan = colspan;
		}
		if ( rowspan !== 1 ) {
			attributes.rowspan = rowspan;
		}
	} else {
		const style = domElements[ 0 ].nodeName.toLowerCase() === 'th' ? 'header' : 'data';
		const colspan = domElements[ 0 ].getAttribute( 'colspan' );
		const rowspan = domElements[ 0 ].getAttribute( 'rowspan' );
 
		attributes.style = style;
 
		if ( colspan !== null ) {
			attributes.originalColspan = colspan;
			if ( colspan !== '' && !isNaN( Number( colspan ) ) ) {
				attributes.colspan = Number( colspan );
			}
		}
 
		if ( rowspan !== null ) {
			attributes.originalRowspan = rowspan;
			if ( rowspan !== '' && !isNaN( Number( rowspan ) ) ) {
				attributes.rowspan = Number( rowspan );
			}
		}
	}
};
 
/**
 * @param {ve.dm.TableCellableNode.TableAttributes} attributes
 * @param {HTMLElement} domElement
 */
ve.dm.TableCellableNode.static.applyAttributes = function ( attributes, domElement ) {
	const spans = {
		colspan: attributes.colspan,
		rowspan: attributes.rowspan
	};
 
	// Ignore spans of 1 unless they were in the original HTML
	if ( attributes.colspan === 1 && Number( attributes.originalColspan ) !== 1 ) {
		spans.colspan = null;
	}
 
	if ( attributes.rowspan === 1 && Number( attributes.originalRowspan ) !== 1 ) {
		spans.rowspan = null;
	}
 
	// Use original value if the numerical value didn't change, or if we didn't set one
	if ( attributes.colspan === undefined || attributes.colspan === Number( attributes.originalColspan ) ) {
		spans.colspan = attributes.originalColspan;
	}
 
	if ( attributes.rowspan === undefined || attributes.rowspan === Number( attributes.originalRowspan ) ) {
		spans.rowspan = attributes.originalRowspan;
	}
 
	ve.setDomAttributes( domElement, spans );
};
 
/* Methods */
 
/**
 * Get the number of rows the cell spans
 *
 * @return {number} Rows spanned
 */
ve.dm.TableCellableNode.prototype.getRowspan = function () {
	return this.getAttribute( 'rowspan' ) || 1;
};
 
/**
 * Get the number of columns the cell spans
 *
 * @return {number} Columns spanned
 */
ve.dm.TableCellableNode.prototype.getColspan = function () {
	return this.getAttribute( 'colspan' ) || 1;
};
 
/**
 * Get number of columns and rows the cell spans
 *
 * @return {Object} Object containing 'col' and 'row'
 */
ve.dm.TableCellableNode.prototype.getSpans = function () {
	return {
		col: this.getColspan(),
		row: this.getRowspan()
	};
};
 
/**
 * Get the style of the cell
 *
 * @return {string} Style, 'header' or 'data'
 */
ve.dm.TableCellableNode.prototype.getStyle = function () {
	return this.getAttribute( 'style' ) || 'data';
};
 
/**
 * Handle attributes changes
 *
 * @param {string} key Attribute key
 * @param {string} from Old value
 * @param {string} to New value
 */
ve.dm.TableCellableNode.prototype.onCellableAttributeChange = function ( key ) {
	if ( this.getParent() && ( key === 'colspan' || key === 'rowspan' ) ) {
		// In practice the matrix should already be invalidated as you
		// shouldn't change a span without adding/removing other cells,
		// but it is possible to just change spans if you don't mind a
		// non-rectangular table.
		this.findParent( ve.dm.TableNode ).getMatrix().invalidate();
	}
};