Skip to content

Commit

Permalink
Merge branch 'release'
Browse files Browse the repository at this point in the history
  • Loading branch information
fnoks committed Sep 25, 2024
2 parents 3ba8d0d + a729f98 commit 6c7a399
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 4 deletions.
8 changes: 6 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
"contentType": "Static content",
"description": "Simple library that displays table.",
"majorVersion": 1,
"minorVersion": 1,
"patchVersion": 19,
"minorVersion": 2,
"patchVersion": 2,
"runnable": 0,
"machineName": "H5P.Table",
"author": "Joubel",
"coreApi": {
"majorVersion": 1,
"minorVersion": 27
},
"preloadedJs": [
{
"path": "scripts/table.js"
Expand Down
22 changes: 21 additions & 1 deletion scripts/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,27 @@ var H5P = H5P || {};
* @param {int} id Content identifier
*/
H5P.Table = function (params, id) {
this.text = params.text === undefined ? '<table class="h5p-table"><thead><tr><th scope="col">Heading Column 1</th><th scope="col">Heading Column 2</th></tr></thead><tbody><tr><td>Row 1 Col 1</td><td>Row 1 Col 2</td></tr><tr><td>Row 2 Col 1</td><td>Row 2 Col 2</td></tr></tbody></table>' : params.text;
const defaultTable = '<figure class="table">' +
'<table class="h5p-table">' +
'<thead>' +
'<tr>' +
'<th>Heading Column 1</th>' +
'<th>Heading Column 2</th>'+
'</tr>' +
'</thead>' +
'<tbody>' +
'<tr>' +
'<td>Row 1 Col 1</td>' +
'<td>Row 1 Col 2</td>' +
'</tr>' +
'<tr>' +
'<td>Row 2 Col 1</td>' +
'<td>Row 2 Col 2</td>' +
'</tr>' +
'</tbody>' +
'</table>' +
'</figure>'
this.text = params.text === undefined ? defaultTable : params.text;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion semantics.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"widget": "html",
"label": "Table",
"importance": "high",
"default": "<table class=\"h5p-table\"><thead><tr><th scope=\"col\">Heading Column 1</th><th scope=\"col\">Heading Column 2</th></tr></thead><tbody><tr><td>Row 1 Col 1</td><td>Row 1 Col 2</td></tr><tr><td>Row 2 Col 1</td><td>Row 2 Col 2</td></tr></tbody></table>",
"default": "<figure class=\"table\"><table class=\"h5p-table\"><thead><tr><th>Heading Column 1</th><th>Heading Column 2</th></tr></thead><tbody><tr><td>Row 1 Col 1</td><td>Row 1 Col 2</td></tr><tr><td>Row 2 Col 1</td><td>Row 2 Col 2</td></tr></tbody></table></figure>",
"tags": [
"strong",
"em",
Expand Down
105 changes: 105 additions & 0 deletions upgrades.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/** @namespace H5PUpgrades */
var H5PUpgrades = H5PUpgrades || {};

H5PUpgrades['H5P.Table'] = (function () {
return {
1: {
2: function (parameters, finished, extras) {
const tables = parameters.text.split('<table');
let newParams = tables[0];

for (let i = 1; i < tables.length; i++) {
let tableChanges = false;
let cellChanges = false;
let style = 'style="';
let cellStyle = 'style="';

// Find and replace border width and style
if (tables[i].includes('border')) {
const needle = 'border="';
const needleStart = tables[i].indexOf(needle);
const needleEnd = tables[i].indexOf('"', needleStart + needle.length);
const borderWidth = parseInt(tables[i].substring(needleStart + needle.length, needleEnd));

style += 'border-style:solid;border-collapse:collapse;' + 'border-width:' + borderWidth + 'px;';
cellStyle += tables[i].includes('h5p-table') ?
'border-style:solid;' :
'border-style:double;border-collapse:collapse;border-width:0.15em;';

tableChanges = true;
cellChanges = true;
}

// Find and replace cell padding
if (tables[i].includes('cellpadding')) {
const needle = 'cellpadding="';
const needleStart = tables[i].indexOf(needle);
const needleEnd = tables[i].indexOf('"', needleStart + needle.length);
const cellPadding = parseInt(tables[i].substring(needleStart + needle.length, needleEnd));

cellStyle += 'padding:' + cellPadding + 'px;';
tables[i] = tables[i].replace(/cellpadding="[0-9]*"/, '');

cellChanges = true;
}

if (tableChanges) {
const tagEnd = tables[i].indexOf('>');
let substring = tables[i].substring(0, tagEnd);

// Set border style on the table
if (substring.includes('style="')) {
tables[i] = substring.replace('style="', style) + tables[i].substring(tagEnd);
}
else {
tables[i] = ' ' + style + '"' + tables[i];
}
}

if (cellChanges) {
// Set border style on header cells
let headers = tables[i].split(/<th(?!ead)/);
tables[i] = headers[0];

for (let j = 1; j < headers.length; j++) {
tables[i] += '<th';

const tagEnd = headers[j].indexOf('>');
let substring = headers[j].substring(0, tagEnd);

if (substring.includes('style="')) {
tables[i] += substring.replace('style="', cellStyle) + headers[j].substring(tagEnd);
}
else {
tables[i] += ' ' + cellStyle + '"' + headers[j];
}
}

// Set border style on cells
let cells = tables[i].split('<td');
tables[i] = cells[0];

for (let j = 1; j < cells.length; j++) {
tables[i] += '<td';

const tagEnd = cells[j].indexOf('>');
let substring = cells[j].substring(0, tagEnd);

if (substring.includes('style="')) {
tables[i] += substring.replace('style="', cellStyle) + cells[j].substring(tagEnd);
}
else {
tables[i] += ' ' + cellStyle + '"' + cells[j];
}
}
}

newParams += '<table' + tables[i];
}

parameters.text = newParams;
finished(null, parameters, extras);
}
}
};
})();

0 comments on commit 6c7a399

Please sign in to comment.