utils.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. function formatAttributes( attributes ) {
  6. let attributesString = '';
  7. if ( attributes ) {
  8. const entries = Object.entries( attributes );
  9. if ( entries.length ) {
  10. attributesString = ' ' + entries.map( entry => `${ entry[ 0 ] }="${ entry[ 1 ] }"` ).join( ' ' );
  11. }
  12. }
  13. return attributesString;
  14. }
  15. function makeRows( tableData, cellElement, rowElement, headingElement = 'th' ) {
  16. const tableRows = tableData
  17. .reduce( ( previousRowsString, tableRow ) => {
  18. const tableRowString = tableRow.reduce( ( tableRowString, tableCellData ) => {
  19. let tableCell = tableCellData;
  20. const isObject = typeof tableCellData === 'object';
  21. let resultingCellElement = cellElement;
  22. if ( isObject ) {
  23. tableCell = tableCellData.contents;
  24. if ( tableCellData.isHeading ) {
  25. resultingCellElement = headingElement;
  26. }
  27. delete tableCellData.contents;
  28. delete tableCellData.isHeading;
  29. }
  30. const formattedAttributes = formatAttributes( isObject ? tableCellData : '' );
  31. tableRowString += `<${ resultingCellElement }${ formattedAttributes }>${ tableCell }</${ resultingCellElement }>`;
  32. return tableRowString;
  33. }, '' );
  34. return `${ previousRowsString }<${ rowElement }>${ tableRowString }</${ rowElement }>`;
  35. }, '' );
  36. return tableRows;
  37. }
  38. /**
  39. * @param {Number} columns
  40. * @param {Array.<String>} tableData
  41. * @param {Object} [attributes]
  42. *
  43. * @returns {String}
  44. */
  45. export function modelTable( tableData, attributes ) {
  46. const tableRows = makeRows( tableData, 'tableCell', 'tableRow' );
  47. return `<table${ formatAttributes( attributes ) }>${ tableRows }</table>`;
  48. }
  49. /**
  50. * @param {Number} columns
  51. * @param {Array.<String>} tableData
  52. * @param {Object} [attributes]
  53. *
  54. * @returns {String}
  55. */
  56. export function viewTable( tableData, attributes = {} ) {
  57. const headingRows = attributes.headingRows || 0;
  58. const thead = headingRows > 0 ? `<thead>${ makeRows( tableData.slice( 0, headingRows ), 'th', 'tr' ) }</thead>` : '';
  59. const tbody = tableData.length > headingRows ? `<tbody>${ makeRows( tableData.slice( headingRows ), 'td', 'tr' ) }</tbody>` : '';
  60. return `<table>${ thead }${ tbody }</table>`;
  61. }
  62. export function formatModelTable( tableString ) {
  63. return tableString
  64. .replace( /<tableRow>/g, '\n<tableRow>\n ' )
  65. .replace( /<\/tableRow>/g, '\n</tableRow>' )
  66. .replace( /<\/table>/g, '\n</table>' );
  67. }
  68. export function formattedModelTable( tableData, attributes ) {
  69. const tableString = modelTable( tableData, attributes );
  70. return formatModelTable( tableString );
  71. }