8
0

utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @param {Number} columns
  7. * @param {Array.<String>} tableData
  8. * @param {Object} [attributes]
  9. *
  10. * @returns {String}
  11. */
  12. export function modelTable( tableData, attributes ) {
  13. const tableRows = makeRows( tableData, 'tableCell', 'tableRow', 'tableCell' );
  14. return `<table${ formatAttributes( attributes ) }>${ tableRows }</table>`;
  15. }
  16. /**
  17. * @param {Number} columns
  18. * @param {Array.<String>} tableData
  19. * @param {Object} [attributes]
  20. *
  21. * @returns {String}
  22. */
  23. export function viewTable( tableData, attributes = {} ) {
  24. const headingRows = attributes.headingRows || 0;
  25. const thead = headingRows > 0 ? `<thead>${ makeRows( tableData.slice( 0, headingRows ), 'th', 'tr' ) }</thead>` : '';
  26. const tbody = tableData.length > headingRows ? `<tbody>${ makeRows( tableData.slice( headingRows ), 'td', 'tr' ) }</tbody>` : '';
  27. return `<figure class="table"><table>${ thead }${ tbody }</table></figure>`;
  28. }
  29. /**
  30. * Formats model or view table - useful for chai assertions debuging.
  31. *
  32. * @param {String} tableString
  33. * @returns {String}
  34. */
  35. export function formatTable( tableString ) {
  36. return tableString
  37. .replace( /<tableRow>/g, '\n<tableRow>\n ' )
  38. .replace( /<thead>/g, '\n<thead>\n ' )
  39. .replace( /<tbody>/g, '\n<tbody>\n ' )
  40. .replace( /<tr>/g, '\n<tr>\n ' )
  41. .replace( /<\/tableRow>/g, '\n</tableRow>' )
  42. .replace( /<\/thead>/g, '\n</thead>' )
  43. .replace( /<\/tbody>/g, '\n</tbody>' )
  44. .replace( /<\/tr>/g, '\n</tr>' )
  45. .replace( /<\/table>/g, '\n</table>' )
  46. .replace( /<\/figure>/g, '\n</table>' );
  47. }
  48. /**
  49. * Returns formatted model table string.
  50. *
  51. * @param {Array.<String>} tableData
  52. * @param {Object} [attributes]
  53. * @returns {String}
  54. */
  55. export function formattedModelTable( tableData, attributes ) {
  56. const tableString = modelTable( tableData, attributes );
  57. return formatTable( tableString );
  58. }
  59. /**
  60. * Returns formatted view table string.
  61. *
  62. * @param {Array.<String>} tableData
  63. * @param {Object} [attributes]
  64. * @returns {String}
  65. */
  66. export function formattedViewTable( tableData, attributes ) {
  67. return formatTable( viewTable( tableData, attributes ) );
  68. }
  69. function formatAttributes( attributes ) {
  70. let attributesString = '';
  71. if ( attributes ) {
  72. const entries = Object.entries( attributes );
  73. if ( entries.length ) {
  74. attributesString = ' ' + entries.map( entry => `${ entry[ 0 ] }="${ entry[ 1 ] }"` ).join( ' ' );
  75. }
  76. }
  77. return attributesString;
  78. }
  79. function makeRows( tableData, cellElement, rowElement, headingElement = 'th' ) {
  80. const tableRows = tableData
  81. .reduce( ( previousRowsString, tableRow ) => {
  82. const tableRowString = tableRow.reduce( ( tableRowString, tableCellData ) => {
  83. let tableCell = tableCellData;
  84. const isObject = typeof tableCellData === 'object';
  85. let resultingCellElement = cellElement;
  86. if ( isObject ) {
  87. tableCell = tableCellData.contents;
  88. if ( tableCellData.isHeading ) {
  89. resultingCellElement = headingElement;
  90. }
  91. delete tableCellData.contents;
  92. delete tableCellData.isHeading;
  93. }
  94. const formattedAttributes = formatAttributes( isObject ? tableCellData : '' );
  95. tableRowString += `<${ resultingCellElement }${ formattedAttributes }>${ tableCell }</${ resultingCellElement }>`;
  96. return tableRowString;
  97. }, '' );
  98. return `${ previousRowsString }<${ rowElement }>${ tableRowString }</${ rowElement }>`;
  99. }, '' );
  100. return tableRows;
  101. }