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

95.08% Statements 116/122
83.33% Branches 30/36
100% Functions 20/20
94.82% Lines 110/116

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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375                                                                      1x   450x   450x     450x 450x         1x                         1x 699x 699x 699x           1x 286x 286x 286x 286x 286x     286x 921x 921x   921x   921x           286x 2704x   2704x 2143x   2704x 80x 80x   2624x   2624x   2624x 2624x   2624x 2171x     453x 1265x 2897x 453x   2444x 2444x   2444x 2444x       286x 286x                   1x 2911x 2911x                 1x 34x 34x 34x 124x   34x                 1x 40x 40x                             1x 48x 48x                     1x 6203x 286x   6203x                     1x 554x     554x               1x 638x                     1x 2540x 2540x                   1x 528x   528x 1944x   528x                 1x 6x 6x   6x 6x     6x 6x 10x 6x                       1x 34x   34x   34x 54x 30x       4x         4x                                 1x 5068x 5068x 5068x 5068x 5068x   5068x         1x                     1x 16x 16x                       1x 2840x               1x 208x                     1x 104x    
/**
 * A helper class that allows random access to the table cells
 * and introduces place-holders for fields occupied by spanning cells,
 * making it a non-sparse representation of the sparse HTML model.
 * This is essential for the implementation of table manipulations, such as row insertions or deletions.
 *
 * Example:
 *
 *     <table>
 *       <tr><td rowspan=2>1</td><td colspan=2>2</td><td rowspan=2 colspan=2>3</td></tr>
 *       <tr><td>4</td><td>5</td></tr>
 *     </table>
 *
 * Visually this table would look like:
 *
 *      -------------------
 *     | 1 | 2     | 3     |
 *     |   |-------|       |
 *     |   | 4 | 5 |       |
 *      -------------------
 *
 * The HTML model is sparse which makes it hard to read but also difficult to work with programmatically.
 * The corresponding TableCellMatrix would look like:
 *
 *     | C[1] | C[2] | P[2] | C[3] | P[3] |
 *     | P[1] | C[4] | C[5] | P[3] | P[3] |
 *
 * Where C[1] represents a Cell instance wrapping cell 1,
 * and P[1] a PlaceHolder instance owned by that cell.
 *
 * @class
 * @mixins OO.EventEmitter
 * @constructor
 * @param {ve.dm.TableNode} tableNode Reference to a table instance
 */
ve.dm.TableMatrix = function VeDmTableMatrix( tableNode ) {
	// Mixin constructors
	OO.EventEmitter.call( this );
 
	this.tableNode = tableNode;
	// Do not access these directly as they get invalidated on structural changes
	// Use the accessor methods instead.
	this.matrix = null;
	this.rowNodes = null;
};
 
/* Inheritance */
 
OO.mixinClass( ve.dm.TableMatrix, OO.EventEmitter );
 
/**
 * @event structureChange
 */
 
/**
 * Invalidates the matrix structure.
 *
 * This is called by ve.dm.TableNode on structural changes.
 *
 * @fires structureChange
 */
ve.dm.TableMatrix.prototype.invalidate = function () {
	this.matrix = null;
	this.rowNodes = null;
	this.emit( 'structureChange' );
};
 
/**
 * Recreates the matrix structure.
 */
ve.dm.TableMatrix.prototype.update = function () {
	var matrix = [],
		rowNodes = [],
		iterator = this.tableNode.getIterator(),
		row = -1,
		col = -1;
 
	// Handle row transitions
	iterator.on( 'newRow', function ( rowNode ) {
		row++;
		col = -1;
		// Initialize a matrix row
		matrix[ row ] = matrix[ row ] || [];
		// Store the row node
		rowNodes.push( rowNode );
	} );
 
	// Iterates through all cells and stores the cells as well as
	// so called placeholders into the matrix.
	var cellNode;
	while ( ( cellNode = iterator.next() ) !== undefined ) {
		col++;
		// Skip placeholders
		while ( matrix[ row ][ col ] ) {
			col++;
		}
		if ( !cellNode ) {
			matrix[ row ][ col ] = null;
			continue;
		}
		var cell = new ve.dm.TableMatrixCell( cellNode, row, col );
		// Store the cell in the matrix
		matrix[ row ][ col ] = cell;
		// Add place holders for spanned cells
		var rowSpan = cellNode.getRowspan();
		var colSpan = cellNode.getColspan();
 
		if ( rowSpan === 1 && colSpan === 1 ) {
			continue;
		}
 
		for ( var i = 0; i < rowSpan; i++ ) {
			for ( var j = 0; j < colSpan; j++ ) {
				if ( i === 0 && j === 0 ) {
					continue;
				}
				var r = row + i;
				var c = col + j;
				// Initialize the cell matrix row if not yet present
				matrix[ r ] = matrix[ r ] || [];
				matrix[ r ][ c ] = new ve.dm.TableMatrixCell( cellNode, r, c, cell );
			}
		}
	}
	this.matrix = matrix;
	this.rowNodes = rowNodes;
};
 
/**
 * Retrieves a single cell.
 *
 * @param {number} row Row index
 * @param {number} col Column index
 * @return {ve.dm.TableMatrixCell|undefined} Cell, or undefined if out of bounds
 */
ve.dm.TableMatrix.prototype.getCell = function ( row, col ) {
	var matrix = this.getMatrix();
	return matrix[ row ] ? matrix[ row ][ col ] : undefined;
};
 
/**
 * Retrieves all cells of a column with given index.
 *
 * @param {number} col Column index
 * @return {ve.dm.TableMatrixCell[]} The cells of a column
 */
ve.dm.TableMatrix.prototype.getColumn = function ( col ) {
	var matrix = this.getMatrix();
	var cells = [];
	for ( var row = 0; row < matrix.length; row++ ) {
		cells.push( matrix[ row ][ col ] );
	}
	return cells;
};
 
/**
 * Retrieves all cells of a row with given index.
 *
 * @param {number} row Row index
 * @return {ve.dm.TableMatrixCell[]} The cells of a row
 */
ve.dm.TableMatrix.prototype.getRow = function ( row ) {
	var matrix = this.getMatrix();
	return matrix[ row ];
};
 
/**
 * Retrieves the row node of a row with given index.
 *
 * It is possible for bad tables to be constructed with rowspans
 * that exceed the last actual row node. In these cases, the number
 * of rows in the table matrix will exceed the number of row
 * nodes, so users should expected getRowNode to sometimes
 * return undefined. (T191858)
 *
 * @param {number} row Row index
 * @return {ve.dm.TableRowNode|undefined} Node at given index, if found
 */
ve.dm.TableMatrix.prototype.getRowNode = function ( row ) {
	var rowNodes = this.getRowNodes();
	return rowNodes[ row ];
};
 
/**
 * Provides a reference to the internal cell matrix.
 *
 * Note: this is primarily for internal use. Do not change the delivered matrix
 * and do not store as it may be invalidated.
 *
 * @return {ve.dm.TableMatrixCell[][]} Table matrix
 */
ve.dm.TableMatrix.prototype.getMatrix = function () {
	if ( !this.matrix ) {
		this.update();
	}
	return this.matrix;
};
 
/**
 * Provides a reference to the internal array of row nodes.
 *
 * Note: this is primarily for internal use. Do not change the delivered array
 * and do not store it as it may be invalidated.
 *
 * @return {ve.dm.TableRowNode[]} Table row nodes
 */
ve.dm.TableMatrix.prototype.getRowNodes = function () {
	Iif ( !this.rowNodes ) {
		this.update();
	}
	return this.rowNodes;
};
 
/**
 * Get number of rows in the table
 *
 * @return {number} Number of rows
 */
ve.dm.TableMatrix.prototype.getRowCount = function () {
	return this.getMatrix().length;
};
 
/**
 * Get number of columns in a row
 *
 * To get the number of columns in a table use #getMaxColCount
 *
 * @param {number} row Row to count columns in
 * @return {number} Number of columns
 */
ve.dm.TableMatrix.prototype.getColCount = function ( row ) {
	var matrix = this.getMatrix();
	return matrix.length ? matrix[ row ].length : 0;
};
 
/**
 * Get the maximum number of columns in a table
 *
 * This is required because in sparse tables the column count is variable.
 *
 * @return {number} Number of columns
 */
ve.dm.TableMatrix.prototype.getMaxColCount = function () {
	var colCount = 0;
 
	for ( var row = this.getRowCount() - 1; row >= 0; row-- ) {
		colCount = Math.max( colCount, this.getColCount( row ) );
	}
	return colCount;
};
 
/**
 * Look up the matrix cell for a given cell node.
 *
 * @param {ve.dm.TableCellNode} cellNode Cell node
 * @return {ve.dm.TableMatrixCell|null} The cell or null if not found
 */
ve.dm.TableMatrix.prototype.lookupCell = function ( cellNode ) {
	var matrix = this.getMatrix(),
		rowNodes = this.getRowNodes();
 
	var row = rowNodes.indexOf( cellNode.getParent() );
	Iif ( row < 0 ) {
		return null;
	}
	var rowCells = matrix[ row ];
	for ( var col = 0, cols = rowCells.length; col < cols; col++ ) {
		if ( rowCells[ col ] && rowCells[ col ].node === cellNode ) {
			return rowCells[ col ];
		}
	}
	return null;
};
 
/**
 * Finds the closest cell not being a placeholder for a given cell.
 *
 * @param {ve.dm.TableMatrixCell} cell Table cell
 * @return {ve.dm.TableMatrixCell|null} Closest cell
 */
ve.dm.TableMatrix.prototype.findClosestCell = function ( cell ) {
	var matrix = this.getMatrix();
 
	var rowCells = matrix[ cell.row ];
	var col;
	for ( col = cell.col; col >= 0; col-- ) {
		if ( !rowCells[ col ].isPlaceholder() ) {
			return rowCells[ col ];
		}
	}
	var cols;
	for ( col = cell.col + 1, cols = rowCells.length; col < cols; col++ ) {
		if ( !rowCells[ col ].isPlaceholder() ) {
			return rowCells[ col ];
		}
	}
	return null;
};
 
/**
 * An object wrapping a table cell node, augmenting it with row and column indexes.
 *
 * Cells which are occupied by another cell's with 'rowspan' or 'colspan' attributes are
 * placeholders and have an owner property other than themselves.
 * Placeholders are used to create a dense representation of the sparse HTML table model.
 *
 * @class
 * @constructor
 * @param {ve.dm.TableCellNode} node DM Node
 * @param {number} row Row index
 * @param {number} col Column index
 * @param {ve.dm.TableMatrixCell} [owner] Owner cell if this is a placeholder
 */
ve.dm.TableMatrixCell = function VeDmTableMatrixCell( node, row, col, owner ) {
	this.node = node;
	this.row = row;
	this.col = col;
	this.key = row + '_' + col;
	this.owner = owner || this;
	// Used when moving cells
	this.data = null;
};
 
/* Inheritance */
 
OO.initClass( ve.dm.TableMatrixCell );
 
/* Static Methods */
 
/**
 * Comparison function for sorting cells in text flow order
 *
 * @param {ve.dm.TableMatrixCell} a First cell
 * @param {ve.dm.TableMatrixCell} b Second cell
 * @return {number} Positive, negative or zero, depending on relative position
 */
ve.dm.TableMatrixCell.static.sortDescending = function ( a, b ) {
	Eif ( a.row !== b.row ) {
		return b.row - a.row;
	}
	return b.col - a.col;
};
 
/* Methods */
 
/**
 * Check if this cell is a placeholder
 *
 * @return {boolean} This cell is a placeholder
 */
ve.dm.TableMatrixCell.prototype.isPlaceholder = function () {
	return this.owner !== this;
};
 
/**
 * Get owner matrix cell
 *
 * @return {ve.dm.TableMatrixCell} Owner cell
 */
ve.dm.TableMatrixCell.prototype.getOwner = function () {
	return this.owner;
};
 
/**
 * Compare to another cell
 *
 * Cells are considered equal to their placeholders
 *
 * @param {ve.dm.TableMatrixCell} other Cell to compare
 * @return {boolean} Cells are equal
 */
ve.dm.TableMatrixCell.prototype.equals = function ( other ) {
	return this.getOwner().key === other.getOwner().key;
};