/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* Returns a model representation of a table shorthand notation:
*
* modelTable( [
* [ '00' ] // first row
* [ '10' ] // second row
* ] );
*
* will output:
*
* '
0010
'
*
* Each table row passed in `tableData` array is represented as an array of strings or objects. A string defines text contents of a cell.
*
* Passing an object allows to pass additional table cell attributes:
*
* const tableCellData = {
* colspan: 2,
* rowspan: 4,
* contents: 'foo' // text contents of a cell
* };
*
* @param {Array.|Object>} tableData
* @param {Object} [attributes] Optional table attributes: `headingRows` and `headingColumns`.
*
* @returns {String}
*/
export function modelTable( tableData, attributes ) {
const tableRows = makeRows( tableData, 'tableCell', 'tableRow', 'tableCell' );
return `
${ tableRows }
`;
}
/**
* Returns a view representation of a table shorthand notation:
*
* viewTable( [
* [ '00', '01' ] // first row
* [ '10', '11' ] // second row
* ] );
*
* will output:
*
* '
00
01
10
11
'
*
* Each table row passed in `tableData` array is represented as an array of strings or objects. A string defines text contents of a cell.
*
* Passing an object allows to pass additional table cell attributes:
*
* const tableCellData = {
* colspan: 2,
* rowspan: 4,
* isHeading: true, // will render table cell as `
` element
* contents: 'foo' // text contents of a cell
* };
*
* @param {Array.>} tableData The table data array.
* @param {Object} [attributes] Optional table attributes: `headingRows` and `headingColumns` - passing them will properly render rows
* in `` or `` sections.
*
* @returns {String}
*/
export function viewTable( tableData, attributes = {} ) {
const headingRows = attributes.headingRows || 0;
const thead = headingRows > 0 ? `${ makeRows( tableData.slice( 0, headingRows ), 'th', 'tr' ) }` : '';
const tbody = tableData.length > headingRows ? `${ makeRows( tableData.slice( headingRows ), 'td', 'tr' ) }` : '';
return `