From 26b916403303c2b622441e694f24abcfddd33a1c Mon Sep 17 00:00:00 2001 From: Tobias Bocanegra Date: Mon, 16 Dec 2024 14:36:27 +0100 Subject: [PATCH] fix: respect row- and colrspan in inline table fixes #610 --- src/mdast-table-handler.js | 9 +- test/fixtures/block-with-table.html | 10 +- test/fixtures/block-with-table.md | 4 + .../roundtrip-fixtures/rowspan.input.html | 95 +++++++++++++++ .../roundtrip-fixtures/rowspan.output.html | 110 ++++++++++++++++++ test/roundtrip/roundtrip.test.js | 1 + 6 files changed, 226 insertions(+), 3 deletions(-) create mode 100644 test/roundtrip/roundtrip-fixtures/rowspan.input.html create mode 100644 test/roundtrip/roundtrip-fixtures/rowspan.output.html diff --git a/src/mdast-table-handler.js b/src/mdast-table-handler.js index 93b51b91..6428c12b 100644 --- a/src/mdast-table-handler.js +++ b/src/mdast-table-handler.js @@ -17,11 +17,18 @@ export const TYPE_GT_ROW = 'gtRow'; export const TYPE_GT_CELL = 'gtCell'; function toGridCell(cell, state) { - return { + const node = { ...cell, children: state.all(cell), type: TYPE_GT_CELL, }; + if ('rowSpan' in node.properties) { + node.rowSpan = parseInt(node.properties.rowSpan, 10); + } + if ('colSpan' in node.properties) { + node.colSpan = parseInt(node.properties.colSpan, 10); + } + return node; } function toGridRow(row, state) { diff --git a/test/fixtures/block-with-table.html b/test/fixtures/block-with-table.html index 899e2535..2a441b95 100644 --- a/test/fixtures/block-with-table.html +++ b/test/fixtures/block-with-table.html @@ -25,9 +25,15 @@ - Table cell 1 + Table cell 1 Table cell 2 + + Table cell 2 + + + Table cell 3-4 + Table cell 3 @@ -77,4 +83,4 @@

table with no rows

- \ No newline at end of file + diff --git a/test/fixtures/block-with-table.md b/test/fixtures/block-with-table.md index 6b22351d..da04f082 100644 --- a/test/fixtures/block-with-table.md +++ b/test/fixtures/block-with-table.md @@ -11,6 +11,10 @@ index | | | Table head cell 1 | Table head cell 2 | | | | +===================+==============================+ | | | | Table cell 1 | Table cell 2 | | +| | | +------------------------------+ | +| | | | Table cell 2 | | +| | +--------------------------------------------------+ | +| | | Table cell 3-4 | | | | +-------------------+------------------------------+ | | | | Table cell 3 | **Table** cell 4 | | | | | | | | diff --git a/test/roundtrip/roundtrip-fixtures/rowspan.input.html b/test/roundtrip/roundtrip-fixtures/rowspan.input.html new file mode 100644 index 00000000..0bf6354f --- /dev/null +++ b/test/roundtrip/roundtrip-fixtures/rowspan.input.html @@ -0,0 +1,95 @@ + +
+
+
+
+
+
+

Table 4: + Preparing to calculate our test statistic

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Type of MovieSnackNo Snacks
ActionActual: 50
Expected: 64.58
Actual: 75
Expected: 60.42
+

Difference: 50 – 64.58 = -14.58

+

Squared Difference: 212.67

+

Divide by Expected: 212.67/64.58 = 3.29

+
+

Difference: 75 – 60.42 = 14.58

+

Squared Difference: 212.67

+

Divide by Expected: 212.67/60.42 = 3.52

+
ComedyActual: 125
Expected 155
Actual 175
Expected 145
+

Difference: 125 – 155 = -30

+

Squared Difference: 900

+

Divide by Expected: 900/155 = 5.81

+
+

Difference: 175 – 145 = 30

+

Squared Difference: 900

+

Divide by Expected: 900/145 = 6.21

+
FamilyActual: 90
Expected: 62
Actual: 30
Expected 58
+

Difference: 90 – 62 = 28

+

Squared Difference: 784

+

Divide by Expected: 784/62 = 12.65

+
+

Difference: 30 – 58 = -28

+

Squared Difference: 784

+

Divide by Expected: 784/58 = 13.52

+
HorrorActual: 45
Expected 28.42
Actual: 10
Expected 26.58
+

Difference: 45 – 28.42 = 16.58

+

Squared Difference: 275.01

+

Divide by Expected: 275.01/28.42 = 9.68

+
+

Difference: 10 – 26.58 = -16.58

+

Squared Difference: 275.01

+

Divide by Expected: 275.01/26.58 = 10.35

+
+

Lastly, to get our test statistic, we add the numbers in the final row for + each cell:

+
+
+
+
+
+ + diff --git a/test/roundtrip/roundtrip-fixtures/rowspan.output.html b/test/roundtrip/roundtrip-fixtures/rowspan.output.html new file mode 100644 index 00000000..057c1acd --- /dev/null +++ b/test/roundtrip/roundtrip-fixtures/rowspan.output.html @@ -0,0 +1,110 @@ + + + + Table 4: Preparing to calculate our test statistic + + + + + + + + + + + + + +
+
+
+
+
+
+

Table 4: Preparing to calculate our test statistic

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Type of MovieSnackNo Snacks
ActionActual: 50
Expected: 64.58
Actual: 75
Expected: 60.42
+

Difference: 50 – 64.58 = -14.58

+

Squared Difference: 212.67

+

Divide by Expected: 212.67/64.58 = 3.29

+
+

Difference: 75 – 60.42 = 14.58

+

Squared Difference: 212.67

+

Divide by Expected: 212.67/60.42 = 3.52

+
ComedyActual: 125
Expected 155
Actual 175
Expected 145
+

Difference: 125 – 155 = -30

+

Squared Difference: 900

+

Divide by Expected: 900/155 = 5.81

+
+

Difference: 175 – 145 = 30

+

Squared Difference: 900

+

Divide by Expected: 900/145 = 6.21

+
FamilyActual: 90
Expected: 62
Actual: 30
Expected 58
+

Difference: 90 – 62 = 28

+

Squared Difference: 784

+

Divide by Expected: 784/62 = 12.65

+
+

Difference: 30 – 58 = -28

+

Squared Difference: 784

+

Divide by Expected: 784/58 = 13.52

+
HorrorActual: 45
Expected 28.42
Actual: 10
Expected 26.58
+

Difference: 45 – 28.42 = 16.58

+

Squared Difference: 275.01

+

Divide by Expected: 275.01/28.42 = 9.68

+
+

Difference: 10 – 26.58 = -16.58

+

Squared Difference: 275.01

+

Divide by Expected: 275.01/26.58 = 10.35

+
+

Lastly, to get our test statistic, we add the numbers in the final row for each cell:

+
+
+
+
+
+ + + diff --git a/test/roundtrip/roundtrip.test.js b/test/roundtrip/roundtrip.test.js index 0914eab9..2aa22160 100644 --- a/test/roundtrip/roundtrip.test.js +++ b/test/roundtrip/roundtrip.test.js @@ -25,6 +25,7 @@ const specs = [ 'some-text', 'all-sections', 'microdata', + 'rowspan', ]; describe('Roundtrip tests', () => {