|
|
@@ -3,51 +3,6 @@
|
|
|
* For licensing, see LICENSE.md.
|
|
|
*/
|
|
|
|
|
|
-function formatAttributes( attributes ) {
|
|
|
- let attributesString = '';
|
|
|
-
|
|
|
- if ( attributes ) {
|
|
|
- const entries = Object.entries( attributes );
|
|
|
-
|
|
|
- if ( entries.length ) {
|
|
|
- attributesString = ' ' + entries.map( entry => `${ entry[ 0 ] }="${ entry[ 1 ] }"` ).join( ' ' );
|
|
|
- }
|
|
|
- }
|
|
|
- return attributesString;
|
|
|
-}
|
|
|
-
|
|
|
-function makeRows( tableData, cellElement, rowElement, headingElement = 'th' ) {
|
|
|
- const tableRows = tableData
|
|
|
- .reduce( ( previousRowsString, tableRow ) => {
|
|
|
- const tableRowString = tableRow.reduce( ( tableRowString, tableCellData ) => {
|
|
|
- let tableCell = tableCellData;
|
|
|
-
|
|
|
- const isObject = typeof tableCellData === 'object';
|
|
|
-
|
|
|
- let resultingCellElement = cellElement;
|
|
|
-
|
|
|
- if ( isObject ) {
|
|
|
- tableCell = tableCellData.contents;
|
|
|
-
|
|
|
- if ( tableCellData.isHeading ) {
|
|
|
- resultingCellElement = headingElement;
|
|
|
- }
|
|
|
-
|
|
|
- delete tableCellData.contents;
|
|
|
- delete tableCellData.isHeading;
|
|
|
- }
|
|
|
-
|
|
|
- const formattedAttributes = formatAttributes( isObject ? tableCellData : '' );
|
|
|
- tableRowString += `<${ resultingCellElement }${ formattedAttributes }>${ tableCell }</${ resultingCellElement }>`;
|
|
|
-
|
|
|
- return tableRowString;
|
|
|
- }, '' );
|
|
|
-
|
|
|
- return `${ previousRowsString }<${ rowElement }>${ tableRowString }</${ rowElement }>`;
|
|
|
- }, '' );
|
|
|
- return tableRows;
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* @param {Number} columns
|
|
|
* @param {Array.<String>} tableData
|
|
|
@@ -74,7 +29,7 @@ export function viewTable( tableData, attributes = {} ) {
|
|
|
const thead = headingRows > 0 ? `<thead>${ makeRows( tableData.slice( 0, headingRows ), 'th', 'tr' ) }</thead>` : '';
|
|
|
const tbody = tableData.length > headingRows ? `<tbody>${ makeRows( tableData.slice( headingRows ), 'td', 'tr' ) }</tbody>` : '';
|
|
|
|
|
|
- return `<table>${ thead }${ tbody }</table>`;
|
|
|
+ return `<figure class="table"><table>${ thead }${ tbody }</table></figure>`;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -93,7 +48,8 @@ export function formatTable( tableString ) {
|
|
|
.replace( /<\/thead>/g, '\n</thead>' )
|
|
|
.replace( /<\/tbody>/g, '\n</tbody>' )
|
|
|
.replace( /<\/tr>/g, '\n</tr>' )
|
|
|
- .replace( /<\/table>/g, '\n</table>' );
|
|
|
+ .replace( /<\/table>/g, '\n</table>' )
|
|
|
+ .replace( /<\/figure>/g, '\n</table>' );
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -119,3 +75,48 @@ export function formattedModelTable( tableData, attributes ) {
|
|
|
export function formattedViewTable( tableData, attributes ) {
|
|
|
return formatTable( viewTable( tableData, attributes ) );
|
|
|
}
|
|
|
+
|
|
|
+function formatAttributes( attributes ) {
|
|
|
+ let attributesString = '';
|
|
|
+
|
|
|
+ if ( attributes ) {
|
|
|
+ const entries = Object.entries( attributes );
|
|
|
+
|
|
|
+ if ( entries.length ) {
|
|
|
+ attributesString = ' ' + entries.map( entry => `${ entry[ 0 ] }="${ entry[ 1 ] }"` ).join( ' ' );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return attributesString;
|
|
|
+}
|
|
|
+
|
|
|
+function makeRows( tableData, cellElement, rowElement, headingElement = 'th' ) {
|
|
|
+ const tableRows = tableData
|
|
|
+ .reduce( ( previousRowsString, tableRow ) => {
|
|
|
+ const tableRowString = tableRow.reduce( ( tableRowString, tableCellData ) => {
|
|
|
+ let tableCell = tableCellData;
|
|
|
+
|
|
|
+ const isObject = typeof tableCellData === 'object';
|
|
|
+
|
|
|
+ let resultingCellElement = cellElement;
|
|
|
+
|
|
|
+ if ( isObject ) {
|
|
|
+ tableCell = tableCellData.contents;
|
|
|
+
|
|
|
+ if ( tableCellData.isHeading ) {
|
|
|
+ resultingCellElement = headingElement;
|
|
|
+ }
|
|
|
+
|
|
|
+ delete tableCellData.contents;
|
|
|
+ delete tableCellData.isHeading;
|
|
|
+ }
|
|
|
+
|
|
|
+ const formattedAttributes = formatAttributes( isObject ? tableCellData : '' );
|
|
|
+ tableRowString += `<${ resultingCellElement }${ formattedAttributes }>${ tableCell }</${ resultingCellElement }>`;
|
|
|
+
|
|
|
+ return tableRowString;
|
|
|
+ }, '' );
|
|
|
+
|
|
|
+ return `${ previousRowsString }<${ rowElement }>${ tableRowString }</${ rowElement }>`;
|
|
|
+ }, '' );
|
|
|
+ return tableRows;
|
|
|
+}
|