Skip to content

Commit

Permalink
Fix rendering empty tables
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Oct 20, 2024
1 parent b19a53a commit 8459943
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ public void process(StructuralNode node) {
final Sink sink = getSink();
sink.table();
sink.tableRows(new int[]{JUSTIFY_LEFT}, false);
List<Row> header = tableNode.getHeader();
final List<Row> header = tableNode.getHeader();
final List<Row> rows = tableNode.getBody();

if (header.isEmpty() && rows.isEmpty()) {
return;
}

if (!header.isEmpty()) {
sink.tableRow();

Expand All @@ -58,7 +64,7 @@ public void process(StructuralNode node) {
sink.tableRow_();
}

for (Row row : tableNode.getBody()) {
for (Row row : rows) {
sink.tableRow();
for (Cell cell : row.getCells()) {
sink.tableCell();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

import org.apache.maven.doxia.sink.Sink;
import org.asciidoctor.ast.Block;
import org.asciidoctor.ast.Cell;
import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.ListItem;
import org.asciidoctor.ast.Row;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.ast.Table;
import org.asciidoctor.jruby.ast.impl.BlockImpl;
import org.asciidoctor.jruby.ast.impl.DocumentImpl;
import org.asciidoctor.jruby.ast.impl.SectionImpl;
Expand Down Expand Up @@ -105,6 +108,11 @@ void should_process_section_node() {
@Test
void should_process_table_node() {
StructuralNode mockNode = mockNode("table", TableImpl.class);
Cell mockCell = Mockito.mock(Cell.class);
Mockito.when(mockCell.getText()).thenReturn("Cell text");
Row mockRow = Mockito.mock(Row.class);
Mockito.when(mockRow.getCells()).thenReturn(List.of(mockCell));
Mockito.when(((Table) mockNode).getBody()).thenReturn(List.of(mockRow));

nodeSinker.sink(mockNode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ class TableNodeProcessorTest {
private NodeProcessor nodeProcessor;
private StringWriter sinkWriter;

@Test
void should_convert_empty_table() {
String content = "= Document tile\n" +
"\n" +
"\n" +
"== Section\n" +
"|===\n" +
"|===";

String html = process(content);

// Header for now is just first row with class=a
assertThat(html)
.isEmpty();
}

@Test
void should_convert_table_with_header() {
String content = documentWithTable(true, noCaption, emptyList());
Expand Down

0 comments on commit 8459943

Please sign in to comment.