utils.js 8.6 KB

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