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

94.87% Statements 37/39
82.93% Branches 34/41
100% Functions 8/8
94.87% Lines 37/39

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                            1x   1158x             1x       1x       1x 583x 583x 583x   583x   583x 44x 44x 43x       583x 44x 44x 43x         1x 122x           122x       122x         122x 122x     122x 122x     122x                   1x 3167x               1x 3167x               1x 28x                     1x 50x                   1x 28x         24x      
/*!
 * VisualEditor DataModel TableCellableNode class.
 *
 * @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * 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.setAttributes = function ( attributes, domElements ) {
	var style = domElements[ 0 ].nodeName.toLowerCase() === 'th' ? 'header' : 'data',
		colspan = domElements[ 0 ].getAttribute( 'colspan' ),
		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 );
		}
	}
};
 
ve.dm.TableCellableNode.static.applyAttributes = function ( attributes, domElement ) {
	var spans = {
		colspan: attributes.colspan,
		rowspan: attributes.rowspan
	};
 
	// Ignore spans of 1 unless they were in the original HTML
	Iif ( attributes.colspan === 1 && Number( attributes.originalColspan ) !== 1 ) {
		spans.colspan = null;
	}
 
	Iif ( 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
	Eif ( attributes.colspan === undefined || attributes.colspan === Number( attributes.originalColspan ) ) {
		spans.colspan = attributes.originalColspan;
	}
 
	Eif ( 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.element.attributes.rowspan || 1;
};
 
/**
 * Get the number of columns the cell spans
 *
 * @return {number} Columns spanned
 */
ve.dm.TableCellableNode.prototype.getColspan = function () {
	return this.element.attributes.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.element.attributes.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();
	}
};