/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import {
downcastInsertCell,
downcastInsertRow,
downcastInsertTable,
downcastRemoveRow,
downcastTableHeadingColumnsChange,
downcastTableHeadingRowsChange
} from '../../src/converters/downcast';
import upcastTable, { upcastTableCell } from '../../src/converters/upcasttable';
/**
* 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.} tableData
* @param {Object} [attributes] Optional table attributes: `headingRows` and `headingColumns`.
*
* @returns {String}
*/
export function modelTable( tableData, attributes ) {
const tableRows = makeRows( tableData, {
cellElement: 'tableCell',
rowElement: 'tableRow',
headingElement: 'tableCell',
wrappingElement: 'paragraph',
enforceWrapping: true
} );
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 ), {
cellElement: 'th',
rowElement: 'tr',
headingElement: 'th',
wrappingElement: 'p'
} ) }` : '';
const tbody = tableData.length > headingRows ?
`${ makeRows( tableData.slice( headingRows ), {
cellElement: 'td',
rowElement: 'tr',
headingElement: 'th',
wrappingElement: 'p'
} ) }` : '';
return `