Skip to content

Commit

Permalink
Defensive code to avoid 2 rowSpan+colSpan with a cell in between to c…
Browse files Browse the repository at this point in the history
…rash #59
  • Loading branch information
onizet committed Jul 15, 2024
1 parent 2f14068 commit d9ddbf4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Html2OpenXml/Expressions/Table/TableRowExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*/
using System;
using System.Collections.Generic;
using AngleSharp.Html.Dom;
using DocumentFormat.OpenXml;
Expand Down Expand Up @@ -47,7 +46,8 @@ public override IEnumerable<OpenXmlElement> Interpret (ParsingContext context)
cells.AddRange(rowNode.Cells);
foreach (var idx in carriedRowSpans.Columns)
{
cells.Insert(idx, null);
if (idx < cells.Count) cells.Insert(idx, null);
else cells.Add(null);
}

if (cells.Count == 0)
Expand Down
34 changes: 34 additions & 0 deletions test/HtmlToOpenXml.Tests/TableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,5 +562,39 @@ public void ParseConsecutiveTables()
Assert.That(elements[2], Is.TypeOf<Table>());
});
}

[Test]
public void ParseDoubleRowSpanSameLine()
{
var elements = converter.Parse(@"<table>
<tr>
<td rowspan=""2"" colspan=""2"">Cell 1.1</td>
<td>Cell 1.2</td>
<td rowspan=""2"">Cell 1.3</td>
</tr>
<tr>
<td>Cell 2.2</td>
</tr>
</table>");

Assert.That(elements, Has.Count.EqualTo(1));
Assert.That(elements, Has.All.TypeOf<Table>());
Assert.That(elements[0].GetFirstChild<TableGrid>()?.Elements<GridColumn>().Count(), Is.EqualTo(4));

Assert.That(elements, Has.Count.EqualTo(1));
Assert.That(elements, Has.All.TypeOf<Table>());
var rows = elements[0].Elements<TableRow>();
Assert.That(rows.Count(), Is.EqualTo(2));
Assert.That(rows.Select(r => r.Elements<TableCell>().Count()),
Has.All.EqualTo(3),
"All should have 3 cells");
Assert.That(rows.First().GetFirstChild<TableCell>()?.TableCellProperties?.GridSpan?.Val?.Value, Is.EqualTo(2));
Assert.That(rows.First().GetFirstChild<TableCell>()?.TableCellProperties?.VerticalMerge?.Val?.Value, Is.EqualTo(MergedCellValues.Restart));
Assert.That(rows.First().GetLastChild<TableCell>()?.TableCellProperties?.VerticalMerge?.Val?.Value, Is.EqualTo(MergedCellValues.Restart));

Assert.That(rows.ElementAt(1).GetFirstChild<TableCell>()?.TableCellProperties?.GridSpan?.Val?.Value, Is.EqualTo(2));
Assert.That(rows.ElementAt(1).GetFirstChild<TableCell>()?.TableCellProperties?.VerticalMerge?.Val?.Value, Is.EqualTo(MergedCellValues.Continue));
Assert.That(rows.ElementAt(1).GetLastChild<TableCell>()?.TableCellProperties?.VerticalMerge?.Val?.Value, Is.EqualTo(MergedCellValues.Continue));
}
}
}

0 comments on commit d9ddbf4

Please sign in to comment.