utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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', 'tableCell' );
  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. /**
  63. * Formats model or view table - useful for chai assertions debuging.
  64. *
  65. * @param {String} tableString
  66. * @returns {String}
  67. */
  68. export function formatTable( tableString ) {
  69. return tableString
  70. .replace( /<tableRow>/g, '\n<tableRow>\n ' )
  71. .replace( /<thead>/g, '\n<thead>\n ' )
  72. .replace( /<tbody>/g, '\n<tbody>\n ' )
  73. .replace( /<tr>/g, '\n<tr>\n ' )
  74. .replace( /<\/tableRow>/g, '\n</tableRow>' )
  75. .replace( /<\/thead>/g, '\n</thead>' )
  76. .replace( /<\/tbody>/g, '\n</tbody>' )
  77. .replace( /<\/tr>/g, '\n</tr>' )
  78. .replace( /<\/table>/g, '\n</table>' );
  79. }
  80. /**
  81. * Returns formatted model table string.
  82. *
  83. * @param {Array.<String>} tableData
  84. * @param {Object} [attributes]
  85. * @returns {String}
  86. */
  87. export function formattedModelTable( tableData, attributes ) {
  88. const tableString = modelTable( tableData, attributes );
  89. return formatTable( tableString );
  90. }
  91. /**
  92. * Returns formatted view table string.
  93. *
  94. * @param {Array.<String>} tableData
  95. * @param {Object} [attributes]
  96. * @returns {String}
  97. */
  98. export function formattedViewTable( tableData, attributes ) {
  99. return formatTable( viewTable( tableData, attributes ) );
  100. }