/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import TableWalker from '../../src/tablewalker';
const WIDGET_TABLE_CELL_CLASS = 'ck-editor__editable ck-editor__nested-editable';
/**
* 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, {
cellElement: 'tableCell',
rowElement: 'tableRow',
headingElement: 'tableCell',
wrappingElement: 'paragraph',
enforceWrapping: true
} );
return `
${ tableRows }
`;
}
/**
* A helper method for creating a test table with a single table cell of which attributes are defined as objects.
*
* setTableCellWithObjectAttributes(
* model,
* {
* margin: { top: '1px', left: '2px' },
* borderColor: { top: '#f00', left: '#ba2' }
* backgroundColor: '#f00'
* },
* 'fo[o]'
* );
*
* This will create a model table with one table cell with a "foo" text.
* The selection will be set on the last "o" and a table cell will have three attributes.
*
* @param {module:engine/model/model~Model} model
* @param {Object} attributes
* @param {String} cellContent
*/
export function setTableCellWithObjectAttributes( model, attributes, cellContent ) {
setData( model, modelTable( [ [ { contents: cellContent } ] ] ) );
const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
model.change( writer => {
for ( const [ key, value ] of Object.entries( attributes ) ) {
writer.setAttribute( key, value, tableCell );
}
} );
}
/**
* A helper method for creating a test table, with a single table cell. Table attributes are defined as objects.
*
* setTableWithObjectAttributes(
* model,
* {
* borderColor: { top: '#f00', left: '#ba2' }
* backgroundColor: '#f00'
* },
* 'fo[o]'
* );
*
* This will create a model table with one table cell with a "foo" text.
* The selection will be set on last "o" and a table will have three attributes.
*
* @param {module:engine/model/model~Model} model
* @param {Object} attributes
* @param {String} cellContent
*/
export function setTableWithObjectAttributes( model, attributes, cellContent ) {
setData( model, modelTable( [ [ { contents: cellContent } ] ] ) );
const table = model.document.getRoot().getChild( 0 );
model.change( writer => {
for ( const [ key, value ] of Object.entries( attributes ) ) {
writer.setAttribute( key, value, table );
}
} );
}
/**
* 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 `
`;
}
/**
* An assertion helper for top-right-bottom-left attribute object.
*
* @param {module:engine/model/node~Node} element
* @param {String} key Attribute key
* @param {String} top Top value. Pass `null` to omit the value in the attributes object.
* @param {String} [right=top] Right value - defaults to top if not provided.
* Pass `null` to omit the value in the attributes object.
* @param {String} [bottom=top] Bottom value - defaults to top (right value must be defined).
* Pass `null` to omit the value in the attributes object.
* @param {String} [left=right] Left value - defaults to right (bottom and right values must be defined).
* Pass `null` to omit the value in the attributes object.
*/
export function assertTRBLAttribute( element, key, top, right = top, bottom = top, left = right ) {
const styleObject = {};
if ( top ) {
styleObject.top = top;
}
if ( right ) {
styleObject.right = right;
}
if ( bottom ) {
styleObject.bottom = bottom;
}
if ( left ) {
styleObject.left = left;
}
expect( element.getAttribute( key ) ).to.deep.equal( styleObject );
}
/**
* An assertion helper for testing the `
` style attribute.
*
* @param {module:core/editor/editor~Editor} editor
* @param {String} [tableStyle=''] A style to assert on
.
* @param {String} [figureStyle=''] A style to assert on