utils.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. /**
  15. * Returns a model representation of a table shorthand notation:
  16. *
  17. * modelTable( [
  18. * [ '00' ] // first row
  19. * [ '10' ] // second row
  20. * ] );
  21. *
  22. * will output:
  23. *
  24. * '<table><tableRow><tableCell>00</tableCell></tableRow><tableRow><tableCell>10</tableCell></tableRow></table>'
  25. *
  26. * Each table row passed in `tableData` array is represented as an array of strings or objects. A string defines text contents of a cell.
  27. *
  28. * Passing an object allows to pass additional table cell attributes:
  29. *
  30. * const tableCellData = {
  31. * colspan: 2,
  32. * rowspan: 4,
  33. * contents: 'foo' // text contents of a cell
  34. * };
  35. *
  36. * @param {Array.<String>} tableData
  37. * @param {Object} [attributes] Optional table attributes: `headingRows` and `headingColumns`.
  38. *
  39. * @returns {String}
  40. */
  41. export function modelTable( tableData, attributes ) {
  42. const tableRows = makeRows( tableData, {
  43. cellElement: 'tableCell',
  44. rowElement: 'tableRow',
  45. headingElement: 'tableCell',
  46. wrappingElement: 'paragraph',
  47. enforceWrapping: true
  48. } );
  49. return `<table${ formatAttributes( attributes ) }>${ tableRows }</table>`;
  50. }
  51. /**
  52. * Returns a view representation of a table shorthand notation:
  53. *
  54. * viewTable( [
  55. * [ '00', '01' ] // first row
  56. * [ '10', '11' ] // second row
  57. * ] );
  58. *
  59. * will output:
  60. *
  61. * '<table><tbody><tr><td>00</td><td>01<td></tr><tr><td>10</td><td>11<td></tr></tbody></table>'
  62. *
  63. * Each table row passed in `tableData` array is represented as an array of strings or objects. A string defines text contents of a cell.
  64. *
  65. * Passing an object allows to pass additional table cell attributes:
  66. *
  67. * const tableCellData = {
  68. * colspan: 2,
  69. * rowspan: 4,
  70. * isHeading: true, // will render table cell as `<th>` element
  71. * contents: 'foo' // text contents of a cell
  72. * };
  73. *
  74. * @param {Array.<Array.<String|Object>>} tableData The table data array.
  75. * @param {Object} [attributes] Optional table attributes: `headingRows` and `headingColumns` - passing them will properly render rows
  76. * in `<tbody>` or `<thead>` sections.
  77. *
  78. * @returns {String}
  79. */
  80. export function viewTable( tableData, attributes = {} ) {
  81. const headingRows = attributes.headingRows || 0;
  82. const asWidget = !!attributes.asWidget;
  83. const thead = headingRows > 0 ? `<thead>${ makeRows( tableData.slice( 0, headingRows ), {
  84. cellElement: 'th',
  85. rowElement: 'tr',
  86. headingElement: 'th',
  87. wrappingElement: asWidget ? 'span' : 'p',
  88. enforceWrapping: asWidget,
  89. asWidget
  90. } ) }</thead>` : '';
  91. const tbody = tableData.length > headingRows ?
  92. `<tbody>${ makeRows( tableData.slice( headingRows ), {
  93. cellElement: 'td',
  94. rowElement: 'tr',
  95. headingElement: 'th',
  96. wrappingElement: asWidget ? 'span' : 'p',
  97. enforceWrapping: asWidget,
  98. asWidget
  99. } ) }</tbody>` : '';
  100. const figureAttributes = asWidget ? 'class="ck-widget ck-widget_selectable table" contenteditable="false"' : 'class="table"';
  101. const widgetHandler = '<div class="ck ck-widget__selection-handler"></div>';
  102. return `<figure ${ figureAttributes }>${ asWidget ? widgetHandler : '' }<table>${ thead }${ tbody }</table></figure>`;
  103. }
  104. /**
  105. * Formats model or view table - useful for chai assertions debugging.
  106. *
  107. * @param {String} tableString
  108. * @returns {String}
  109. */
  110. export function formatTable( tableString ) {
  111. return tableString
  112. .replace( /<tableRow>/g, '\n<tableRow>\n ' )
  113. .replace( /<thead>/g, '\n<thead>\n ' )
  114. .replace( /<tbody>/g, '\n<tbody>\n ' )
  115. .replace( /<tr>/g, '\n<tr>\n ' )
  116. .replace( /<\/tableRow>/g, '\n</tableRow>' )
  117. .replace( /<\/thead>/g, '\n</thead>' )
  118. .replace( /<\/tbody>/g, '\n</tbody>' )
  119. .replace( /<\/tr>/g, '\n</tr>' )
  120. .replace( /<\/table>/g, '\n</table>' )
  121. .replace( /<\/figure>/g, '\n</figure>' );
  122. }
  123. /**
  124. * Returns formatted model table string.
  125. *
  126. * @param {Array.<String>} tableData
  127. * @param {Object} [attributes]
  128. * @returns {String}
  129. */
  130. export function formattedModelTable( tableData, attributes ) {
  131. const tableString = modelTable( tableData, attributes );
  132. return formatTable( tableString );
  133. }
  134. /**
  135. * Returns formatted view table string.
  136. *
  137. * @param {Array.<String>} tableData
  138. * @param {Object} [attributes]
  139. * @returns {String}
  140. */
  141. export function formattedViewTable( tableData, attributes ) {
  142. return formatTable( viewTable( tableData, attributes ) );
  143. }
  144. export function defaultSchema( schema ) {
  145. schema.register( 'table', {
  146. allowWhere: '$block',
  147. allowAttributes: [ 'headingRows', 'headingColumns' ],
  148. isObject: true
  149. } );
  150. schema.register( 'tableRow', { allowIn: 'table' } );
  151. schema.register( 'tableCell', {
  152. allowIn: 'tableRow',
  153. allowContentOf: '$block',
  154. allowAttributes: [ 'colspan', 'rowspan' ],
  155. isLimit: true
  156. } );
  157. schema.extend( '$block', { allowIn: 'tableCell' } );
  158. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  159. }
  160. export function defaultConversion( conversion, asWidget = false ) {
  161. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  162. // Table conversion.
  163. conversion.for( 'upcast' ).add( upcastTable() );
  164. conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget } ) );
  165. // Table row conversion.
  166. conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget } ) );
  167. conversion.for( 'downcast' ).add( downcastRemoveRow( { asWidget } ) );
  168. // Table cell conversion.
  169. conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget } ) );
  170. conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
  171. conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
  172. // Table attributes conversion.
  173. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  174. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  175. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange( { asWidget } ) );
  176. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange( { asWidget } ) );
  177. }
  178. // Formats table cell attributes
  179. //
  180. // @param {Object} attributes Attributes of a cell.
  181. function formatAttributes( attributes ) {
  182. let attributesString = '';
  183. if ( attributes ) {
  184. const entries = Object.entries( attributes );
  185. if ( entries.length ) {
  186. attributesString = ' ' + entries.map( entry => `${ entry[ 0 ] }="${ entry[ 1 ] }"` ).join( ' ' );
  187. }
  188. }
  189. return attributesString;
  190. }
  191. // Formats passed table data to a set of table rows.
  192. function makeRows( tableData, options ) {
  193. const { cellElement, rowElement, headingElement, wrappingElement, enforceWrapping, asWidget } = options;
  194. return tableData
  195. .reduce( ( previousRowsString, tableRow ) => {
  196. const tableRowString = tableRow.reduce( ( tableRowString, tableCellData ) => {
  197. const isObject = typeof tableCellData === 'object';
  198. let contents = isObject ? tableCellData.contents : tableCellData;
  199. let resultingCellElement = cellElement;
  200. if ( isObject ) {
  201. // TODO: check...
  202. if ( tableCellData.isHeading ) {
  203. resultingCellElement = headingElement;
  204. }
  205. delete tableCellData.contents;
  206. delete tableCellData.isHeading;
  207. }
  208. const attributes = isObject ? tableCellData : {};
  209. if ( asWidget ) {
  210. attributes.class = 'ck-editor__editable ck-editor__nested-editable';
  211. attributes.contenteditable = 'true';
  212. }
  213. if ( !( contents.replace( '[', '' ).replace( ']', '' ).startsWith( '<' ) ) && enforceWrapping ) {
  214. contents = `<${ wrappingElement }>${ contents }</${ wrappingElement }>`;
  215. }
  216. const formattedAttributes = formatAttributes( attributes );
  217. tableRowString += `<${ resultingCellElement }${ formattedAttributes }>${ contents }</${ resultingCellElement }>`;
  218. return tableRowString;
  219. }, '' );
  220. return `${ previousRowsString }<${ rowElement }>${ tableRowString }</${ rowElement }>`;
  221. }, '' );
  222. }