fix: tables parse correctly with new version of jsdom

issues/907-white-space-in-bold-and-italic-causes-problems
Estevão Soares dos Santos 2022-03-26 04:34:50 +00:00
parent 5fc843e175
commit db571fbaac
2 changed files with 9949 additions and 63 deletions

9954
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -12,7 +12,6 @@ showdown.subParser('makeMarkdown.table',
var txt = '',
tableArray = [[], []],
headings,
hasHeadings = false,
rows = [],
colCount,
i,
@ -38,7 +37,7 @@ showdown.subParser('makeMarkdown.table',
// first lets look for <thead>
// we will ignore thead without <tr> children
// also, since markdown doesn't support tables with multiple heading rows, onyl the first one will be transformed
// also, since markdown doesn't support tables with multiple heading rows, only the first one will be transformed
// the rest will count as regular rows
if (node.querySelectorAll(':scope>thead').length !== 0 && node.querySelectorAll(':scope>thead>tr').length !== 0) {
var thead = node.querySelectorAll(':scope>thead>tr');
@ -47,7 +46,6 @@ showdown.subParser('makeMarkdown.table',
for (i = 0; i < thead.length; ++i) {
rows.push(iterateRow(thead[i]));
}
hasHeadings = true;
}
// now let's look for tbody
@ -71,16 +69,16 @@ showdown.subParser('makeMarkdown.table',
// lastly look for naked tr
if (node.querySelectorAll(':scope>tr').length !== 0) {
var tr = node.querySelectorAll(':scope>tr');
// tfoot>tr can have td and th children, although th are not very screen reader friendly
for (i = 0; i < tr.length; ++i) {
rows.push(iterateRow(tr[i]));
}
hasHeadings = true; // kinda, first row counts as heading
}
// TODO: implement <caption> in tables https://developer.mozilla.org/pt-BR/docs/Web/HTML/Element/caption
// note: colgroups are ignored, since they are basically styling
// note: <colgroup> is ignored, since they are basically styling
// we need now to account for cases of completely empty tables, like <table></table> or equivalent
if (rows.length === 0) {
@ -91,41 +89,31 @@ showdown.subParser('makeMarkdown.table',
// count the first row. We need it to trim the table (if table rows have inconsistent number of columns)
colCount = rows[0].length;
// let's shift the first row as a heading if it is a heading
if (hasHeadings) {
headings = rows.shift();
// let's shift the first row as a heading
headings = rows.shift();
for (i = 0; i < headings.length; ++i) {
var headContent = showdown.subParser('makeMarkdown.tableCell')(headings[i], globals),
allign = '---';
for (i = 0; i < headings.length; ++i) {
var headContent = showdown.subParser('makeMarkdown.tableCell')(headings[i], globals),
align = '---';
if (headings[i].hasAttribute('style')) {
var style = headings[i].getAttribute('style').toLowerCase().replace(/\s/g, '');
switch (style) {
case 'text-align:left;':
allign = ':---';
break;
case 'text-align:right;':
allign = '---:';
break;
case 'text-align:center;':
allign = ':---:';
break;
}
if (headings[i].hasAttribute('style')) {
var style = headings[i].getAttribute('style').toLowerCase().replace(/\s/g, '');
switch (style) {
case 'text-align:left;':
align = ':---';
break;
case 'text-align:right;':
align = '---:';
break;
case 'text-align:center;':
align = ':---:';
break;
}
tableArray[0][i] = headContent.trim();
tableArray[1][i] = allign;
}
} else {
// since there's no headings, we will simulate it, counting the columns in the first line of the table
for (i = 0; i < colCount; ++i) {
tableArray[0][i] = ' ';
tableArray[1][i] = '---';
}
tableArray[0][i] = headContent.trim();
tableArray[1][i] = align;
}
// now iterate through the rows and create the pseudo output (not pretty yet)
for (i = 0; i < rows.length; ++i) {
var r = tableArray.push([]) - 1;
@ -133,7 +121,7 @@ showdown.subParser('makeMarkdown.table',
for (ii = 0; ii < colCount; ++ii) {
var cellContent = ' ';
if (typeof rows[i][ii] !== 'undefined') {
// Note: if rows[i][ii] is undefined, it means the row has less elements than the header,
// Note: if rows[i][ii] is undefined, it means the row has fewer elements than the header,
// and empty content will be added
cellContent = showdown.subParser('makeMarkdown.tableCell')(rows[i][ii], globals);
}