utils.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import {
  6. downcastInsertCell,
  7. downcastInsertRow,
  8. downcastInsertTable,
  9. downcastRemoveRow,
  10. downcastTableHeadingColumnsChange,
  11. downcastTableHeadingRowsChange
  12. } from '../../src/converters/downcast';
  13. import upcastTable, { upcastTableCell } from '../../src/converters/upcasttable';
  14. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  15. const WIDGET_TABLE_CELL_CLASS = 'ck-editor__editable ck-editor__nested-editable';
  16. /**
  17. * Returns a model representation of a table shorthand notation:
  18. *
  19. * modelTable( [
  20. * [ '00' ] // first row
  21. * [ '10' ] // second row
  22. * ] );
  23. *
  24. * will output:
  25. *
  26. * '<table><tableRow><tableCell>00</tableCell></tableRow><tableRow><tableCell>10</tableCell></tableRow></table>'
  27. *
  28. * Each table row passed in `tableData` array is represented as an array of strings or objects. A string defines text contents of a cell.
  29. *
  30. * Passing an object allows to pass additional table cell attributes:
  31. *
  32. * const tableCellData = {
  33. * colspan: 2,
  34. * rowspan: 4,
  35. * contents: 'foo' // text contents of a cell
  36. * };
  37. *
  38. * @param {Array.<Array.<String>|Object>} tableData
  39. * @param {Object} [attributes] Optional table attributes: `headingRows` and `headingColumns`.
  40. *
  41. * @returns {String}
  42. */
  43. export function modelTable( tableData, attributes ) {
  44. const tableRows = makeRows( tableData, {
  45. cellElement: 'tableCell',
  46. rowElement: 'tableRow',
  47. headingElement: 'tableCell',
  48. wrappingElement: 'paragraph',
  49. enforceWrapping: true
  50. } );
  51. return `<table${ formatAttributes( attributes ) }>${ tableRows }</table>`;
  52. }
  53. /**
  54. * Returns a view representation of a table shorthand notation:
  55. *
  56. * viewTable( [
  57. * [ '00', '01' ] // first row
  58. * [ '10', '11' ] // second row
  59. * ] );
  60. *
  61. * will output:
  62. *
  63. * '<table><tbody><tr><td>00</td><td>01<td></tr><tr><td>10</td><td>11<td></tr></tbody></table>'
  64. *
  65. * Each table row passed in `tableData` array is represented as an array of strings or objects. A string defines text contents of a cell.
  66. *
  67. * Passing an object allows to pass additional table cell attributes:
  68. *
  69. * const tableCellData = {
  70. * colspan: 2,
  71. * rowspan: 4,
  72. * isHeading: true, // will render table cell as `<th>` element
  73. * contents: 'foo' // text contents of a cell
  74. * };
  75. *
  76. * @param {Array.<Array.<String|Object>>} tableData The table data array.
  77. * @param {Object} [attributes] Optional table attributes: `headingRows` and `headingColumns` - passing them will properly render rows
  78. * in `<tbody>` or `<thead>` sections.
  79. *
  80. * @returns {String}
  81. */
  82. export function viewTable( tableData, attributes = {} ) {
  83. const headingRows = attributes.headingRows || 0;
  84. const asWidget = !!attributes.asWidget;
  85. const thead = headingRows > 0 ? `<thead>${ makeRows( tableData.slice( 0, headingRows ), {
  86. cellElement: 'th',
  87. rowElement: 'tr',
  88. headingElement: 'th',
  89. wrappingElement: asWidget ? 'span' : 'p',
  90. enforceWrapping: asWidget,
  91. asWidget
  92. } ) }</thead>` : '';
  93. const tbody = tableData.length > headingRows ?
  94. `<tbody>${ makeRows( tableData.slice( headingRows ), {
  95. cellElement: 'td',
  96. rowElement: 'tr',
  97. headingElement: 'th',
  98. wrappingElement: asWidget ? 'span' : 'p',
  99. enforceWrapping: asWidget,
  100. asWidget
  101. } ) }</tbody>` : '';
  102. const figureAttributes = asWidget ?
  103. 'class="ck-widget ck-widget_with-selection-handle table" contenteditable="false"' : 'class="table"';
  104. const widgetHandler = '<div class="ck ck-widget__selection-handle"></div>';
  105. return `<figure ${ figureAttributes }>${ asWidget ? widgetHandler : '' }<table>${ thead }${ tbody }</table></figure>`;
  106. }
  107. export function defaultSchema( schema, registerParagraph = true ) {
  108. schema.register( 'table', {
  109. allowWhere: '$block',
  110. allowAttributes: [ 'headingRows', 'headingColumns' ],
  111. isLimit: true,
  112. isObject: true,
  113. isBlock: true
  114. } );
  115. schema.register( 'tableRow', {
  116. allowIn: 'table',
  117. isLimit: true
  118. } );
  119. schema.register( 'tableCell', {
  120. allowIn: 'tableRow',
  121. allowAttributes: [ 'colspan', 'rowspan' ],
  122. isLimit: true
  123. } );
  124. // Allow all $block content inside table cell.
  125. schema.extend( '$block', { allowIn: 'tableCell' } );
  126. // Disallow table in table.
  127. schema.addChildCheck( ( context, childDefinition ) => {
  128. if ( childDefinition.name == 'table' && Array.from( context.getNames() ).includes( 'table' ) ) {
  129. return false;
  130. }
  131. } );
  132. if ( registerParagraph ) {
  133. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  134. }
  135. // Styles
  136. schema.extend( 'tableCell', {
  137. allowAttributes: [ 'border' ]
  138. } );
  139. }
  140. export function defaultConversion( conversion, asWidget = false ) {
  141. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  142. // Table conversion.
  143. conversion.for( 'upcast' ).add( upcastTable() );
  144. conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget } ) );
  145. // Table row conversion.
  146. conversion.for( 'upcast' ).elementToElement( { model: 'tableRow', view: 'tr' } );
  147. conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget } ) );
  148. conversion.for( 'downcast' ).add( downcastRemoveRow( { asWidget } ) );
  149. // Table cell conversion.
  150. conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
  151. conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
  152. conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget } ) );
  153. // Table attributes conversion.
  154. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  155. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  156. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange( { asWidget } ) );
  157. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange( { asWidget } ) );
  158. // Styles
  159. conversion.for( 'upcast' ).attributeToAttribute( {
  160. view: {
  161. name: 'td',
  162. styles: {
  163. border: /[\s\S]+/
  164. }
  165. },
  166. model: {
  167. key: 'border',
  168. value: viewElement => viewElement.getStyle( 'border' )
  169. }
  170. } );
  171. }
  172. /**
  173. * Assertion helper for top-right-bottom-left attribute object.
  174. *
  175. * @param {module:engine/model/node~Node} element
  176. * @param {String} key Attribute key
  177. * @param {String} top Top value. Pass null to omit value in attributes object.
  178. * @param {String} [right=top] Right value - defaults to top if not provided.
  179. * Pass null to omit value in attributes object.
  180. * @param {String} [bottom=top] Bottom value - defaults to top (right value must be defined).
  181. * Pass null to omit value in attributes object.
  182. * @param {String} [left=right] Left value - defaults to right (bottom and right values must be defined).
  183. * Pass null to omit value in attributes object.
  184. */
  185. export function assertTRBLAttribute( element, key, top, right = top, bottom = top, left = right ) {
  186. const styleObject = {};
  187. if ( top ) {
  188. styleObject.top = top;
  189. }
  190. if ( right ) {
  191. styleObject.right = right;
  192. }
  193. if ( bottom ) {
  194. styleObject.bottom = bottom;
  195. }
  196. if ( left ) {
  197. styleObject.left = left;
  198. }
  199. expect( element.getAttribute( key ) ).to.deep.equal( styleObject );
  200. }
  201. /**
  202. * Assertion helper for testing <table> style attribute.
  203. *
  204. * @param {module:core/editor/editor~Editor} editor
  205. * @param {String} tableStyle A style to assert on table.
  206. */
  207. export function assertTableStyle( editor, tableStyle ) {
  208. const styleEntry = tableStyle ? ` style="${ tableStyle }"` : '';
  209. assertEqualMarkup( editor.getData(),
  210. `<figure class="table"><table${ styleEntry }><tbody><tr><td>foo</td></tr></tbody></table></figure>`
  211. );
  212. }
  213. /**
  214. * Assertion helper for testing <td> style attribute.
  215. *
  216. * @param {module:core/editor/editor~Editor} editor
  217. * @param {String} tableCellStyle A style to assert on td.
  218. */
  219. export function assertTableCellStyle( editor, tableCellStyle ) {
  220. assertEqualMarkup( editor.getData(),
  221. '<figure class="table"><table><tbody><tr>' +
  222. `<td${ tableCellStyle ? ` style="${ tableCellStyle }"` : '' }>foo</td>` +
  223. '</tr></tbody></table></figure>'
  224. );
  225. }
  226. // Formats table cell attributes
  227. //
  228. // @param {Object} attributes Attributes of a cell.
  229. function formatAttributes( attributes ) {
  230. let attributesString = '';
  231. if ( attributes ) {
  232. const entries = Object.entries( attributes );
  233. if ( entries.length ) {
  234. attributesString = ' ' + entries.map( entry => `${ entry[ 0 ] }="${ entry[ 1 ] }"` ).join( ' ' );
  235. }
  236. }
  237. return attributesString;
  238. }
  239. // Formats passed table data to a set of table rows.
  240. function makeRows( tableData, options ) {
  241. const { cellElement, rowElement, headingElement, wrappingElement, enforceWrapping, asWidget } = options;
  242. return tableData
  243. .reduce( ( previousRowsString, tableRow ) => {
  244. const tableRowString = tableRow.reduce( ( tableRowString, tableCellData ) => {
  245. const isObject = typeof tableCellData === 'object';
  246. let contents = isObject ? tableCellData.contents : tableCellData;
  247. let resultingCellElement = cellElement;
  248. if ( isObject ) {
  249. if ( tableCellData.isHeading ) {
  250. resultingCellElement = headingElement;
  251. }
  252. delete tableCellData.contents;
  253. delete tableCellData.isHeading;
  254. }
  255. const attributes = isObject ? tableCellData : {};
  256. if ( asWidget ) {
  257. attributes.class = WIDGET_TABLE_CELL_CLASS + ( attributes.class ? ` ${ attributes.class }` : '' );
  258. attributes.contenteditable = 'true';
  259. }
  260. if ( !( contents.replace( '[', '' ).replace( ']', '' ).startsWith( '<' ) ) && enforceWrapping ) {
  261. contents = `<${ wrappingElement }>${ contents }</${ wrappingElement }>`;
  262. }
  263. const formattedAttributes = formatAttributes( attributes );
  264. tableRowString += `<${ resultingCellElement }${ formattedAttributes }>${ contents }</${ resultingCellElement }>`;
  265. return tableRowString;
  266. }, '' );
  267. return `${ previousRowsString }<${ rowElement }>${ tableRowString }</${ rowElement }>`;
  268. }, '' );
  269. }