This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-doxia.git


The following commit(s) were added to refs/heads/master by this push:
     new 5308136d Send a table caption before the table rows in the XHTML 
parsers
5308136d is described below

commit 5308136d06750f96a35177a698373c71273f2b62
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Nov 14 08:53:18 2026 +0000

    Send a table caption before the table rows in the XHTML parsers
    
    tableRows now waits for the first table child that is not a caption, so a
    caption comes before the rows as the Sink documents; the HTML sink output
    is the same apart from whitespace.
---
 .../maven/doxia/parser/Xhtml5BaseParser.java       | 40 ++++++++++++++-
 .../maven/doxia/parser/Xhtml5BaseParserTest.java   | 57 +++++++++++++++++++++-
 .../doxia/module/markdown/MarkdownParserTest.java  |  2 +-
 .../maven/doxia/module/xdoc/XdocIdentityTest.java  |  7 ++-
 .../doxia/module/xhtml5/Xhtml5IdentityTest.java    |  7 ++-
 5 files changed, 101 insertions(+), 12 deletions(-)

diff --git 
a/doxia-core/src/main/java/org/apache/maven/doxia/parser/Xhtml5BaseParser.java 
b/doxia-core/src/main/java/org/apache/maven/doxia/parser/Xhtml5BaseParser.java
index fd6d2cc6..0235434e 100644
--- 
a/doxia-core/src/main/java/org/apache/maven/doxia/parser/Xhtml5BaseParser.java
+++ 
b/doxia-core/src/main/java/org/apache/maven/doxia/parser/Xhtml5BaseParser.java
@@ -141,6 +141,16 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
 
     private LinkedList<String> capturedSinkEventNames;
 
+    /**
+     * For each open table, the grid flag of its {@code tableRows} event while 
that event is still to be sent,
+     * {@code null} once it has been sent. The event waits for the first child 
that is not a {@code caption},
+     * so that a caption reaches the sink before the rows, as {@link 
Sink#table(SinkEventAttributes)} documents.
+     */
+    private final LinkedList<Boolean> pendingTableRows = new LinkedList<>();
+
+    /** True whenever we are inside a &lt;caption&gt; tag. */
+    private boolean inTableCaption;
+
     @Override
     public void parse(Reader source, Sink sink, String reference) throws 
ParseException {
         init();
@@ -214,6 +224,9 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
     protected boolean baseStartTag(String elementName, SinkEventAttributeSet 
attribs, Sink sink) {
         boolean visited = true;
         isBeginningOfLineInsideBlock = true;
+        if (!inTableCaption && !"caption".equals(elementName)) {
+            emitPendingTableRows(sink);
+        }
         switch (elementName) {
             case "article":
                 sink.article(attribs);
@@ -452,6 +465,7 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
                 sink.tableCell(attribs);
                 break;
             case "caption":
+                inTableCaption = true;
                 sink.tableCaption(attribs);
                 break;
             case "br":
@@ -608,6 +622,10 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
             // 
----------------------------------------------------------------------
 
             case "table":
+                emitPendingTableRows(sink);
+                if (!pendingTableRows.isEmpty()) {
+                    pendingTableRows.removeLast();
+                }
                 sink.tableRows_();
                 sink.table_();
                 break;
@@ -622,6 +640,7 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
                 break;
             case "caption":
                 sink.tableCaption_();
+                inTableCaption = false;
                 break;
             case "article":
                 sink.article_();
@@ -737,6 +756,9 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
             text = collapseWhitespace(text);
         }
         if ((text != null && !text.isEmpty()) && !isScriptBlock()) {
+            if (!inTableCaption) {
+                emitPendingTableRows(sink);
+            }
             sink.text(text);
             isBeginningOfLineInsideBlock = false;
         }
@@ -796,6 +818,10 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
         isBeginningOfLineInsideBlock = false;
         String text = getText(parser);
 
+        if (!inTableCaption) {
+            // a sink may expect anything but whitespace inside a table to 
come after the start of its rows
+            emitPendingTableRows(sink);
+        }
         if ("PB".equals(text.trim())) {
             sink.pageBreak();
         } else {
@@ -810,6 +836,9 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
         isBeginningOfLineInsideBlock = false;
         String text = getText(parser);
 
+        if (!inTableCaption) {
+            emitPendingTableRows(sink);
+        }
         if (isScriptBlock()) {
             sink.unknown(CDATA, new Object[] {CDATA_TYPE, text}, null);
         } else {
@@ -1015,6 +1044,8 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
         this.openLists.clear();
         this.headingLevel = 0;
         this.inVerbatim = false;
+        this.pendingTableRows.clear();
+        this.inTableCaption = false;
     }
 
     private void handleAEnd(Sink sink) {
@@ -1198,6 +1229,13 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
             grid = true;
         }
 
-        sink.tableRows(null, grid);
+        pendingTableRows.addLast(grid);
+    }
+
+    private void emitPendingTableRows(Sink sink) {
+        if (!pendingTableRows.isEmpty() && pendingTableRows.getLast() != null) 
{
+            sink.tableRows(null, pendingTableRows.getLast());
+            pendingTableRows.set(pendingTableRows.size() - 1, null);
+        }
     }
 }
diff --git 
a/doxia-core/src/test/java/org/apache/maven/doxia/parser/Xhtml5BaseParserTest.java
 
b/doxia-core/src/test/java/org/apache/maven/doxia/parser/Xhtml5BaseParserTest.java
index b22b4f2d..ec54ebe7 100644
--- 
a/doxia-core/src/test/java/org/apache/maven/doxia/parser/Xhtml5BaseParserTest.java
+++ 
b/doxia-core/src/test/java/org/apache/maven/doxia/parser/Xhtml5BaseParserTest.java
@@ -869,6 +869,57 @@ class Xhtml5BaseParserTest extends AbstractParserTest {
         assertEquals("paragraph_", it.next().getName());
     }
 
+    @Test
+    void nestedTableWithCaption() throws Exception {
+        String text = 
"<table><tr><td><table><caption>inner</caption><tr><td>x</td></tr></table></td></tr></table>";
+        parser.parse(text, sink);
+        Iterator<SinkEventElement> it = sink.getEventList().iterator();
+
+        assertEquals("table", it.next().getName());
+        assertEquals("tableRows", it.next().getName());
+        assertEquals("tableRow", it.next().getName());
+        assertEquals("tableCell", it.next().getName());
+        assertEquals("table", it.next().getName());
+        assertEquals("tableCaption", it.next().getName());
+        assertEquals("text", it.next().getName());
+        assertEquals("tableCaption_", it.next().getName());
+        assertEquals("tableRows", it.next().getName());
+        assertEquals("tableRow", it.next().getName());
+        assertEquals("tableCell", it.next().getName());
+        assertEquals("text", it.next().getName());
+        assertEquals("tableCell_", it.next().getName());
+        assertEquals("tableRow_", it.next().getName());
+        assertEquals("tableRows_", it.next().getName());
+        assertEquals("table_", it.next().getName());
+        assertEquals("tableCell_", it.next().getName());
+        assertEquals("tableRow_", it.next().getName());
+        assertEquals("tableRows_", it.next().getName());
+        assertEquals("table_", it.next().getName());
+    }
+
+    @Test
+    void commentBeforeFirstRowStartsTheRows() throws Exception {
+        parser.setEmitComments(true);
+        String text = "<table>\n  <caption>c</caption>\n  <!-- rows -->\n  
<tr><td>x</td></tr>\n</table>";
+        parser.parse(text, sink);
+        Iterator<SinkEventElement> it = sink.getEventList().iterator();
+
+        assertEquals("table", it.next().getName());
+        SinkEventElement next = it.next();
+        while ("markupLineBreak".equals(next.getName())) {
+            next = it.next();
+        }
+        assertEquals("tableCaption", next.getName());
+        assertEquals("text", it.next().getName());
+        assertEquals("tableCaption_", it.next().getName());
+        next = it.next();
+        while ("markupLineBreak".equals(next.getName())) {
+            next = it.next();
+        }
+        assertEquals("tableRows", next.getName());
+        assertEquals("comment", it.next().getName());
+    }
+
     @Test
     void table() throws Exception {
         String text = 
"<table><caption></caption><tr><th></th></tr><tr><td></td></tr></table>";
@@ -877,13 +928,15 @@ class Xhtml5BaseParserTest extends AbstractParserTest {
 
         assertEquals("table", it.next().getName());
 
+        // the caption comes before the rows, as the Sink documents
+        assertEquals("tableCaption", it.next().getName());
+        assertEquals("tableCaption_", it.next().getName());
+
         // DOXIA-374
         SinkEventElement el = it.next();
         assertEquals("tableRows", el.getName());
         assertFalse((Boolean) el.getArgs()[1]);
 
-        assertEquals("tableCaption", it.next().getName());
-        assertEquals("tableCaption_", it.next().getName());
         assertEquals("tableRow", it.next().getName());
         assertEquals("tableHeaderCell", it.next().getName());
         assertEquals("tableHeaderCell_", it.next().getName());
diff --git 
a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownParserTest.java
 
b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownParserTest.java
index 5d7a434a..54f48722 100644
--- 
a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownParserTest.java
+++ 
b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownParserTest.java
@@ -625,8 +625,8 @@ class MarkdownParserTest extends AbstractParserTest {
                 "paragraph_",
                 "markupLineBreak",
                 "table",
-                "tableRows",
                 "markupLineBreak",
+                "tableRows",
                 "unknown", // tbody start
                 "tableRow",
                 "tableHeaderCell",
diff --git 
a/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocIdentityTest.java
 
b/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocIdentityTest.java
index 35023f1d..1e7d88a3 100644
--- 
a/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocIdentityTest.java
+++ 
b/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocIdentityTest.java
@@ -60,13 +60,12 @@ class XdocIdentityTest extends AbstractIdentityTest {
         String captionTag = expected.substring(iStartCaption, iEndCaption) + 
EOL + EOL + EOL;
         expected = DoxiaStringUtils.replace(expected, captionTag, "");
 
-        int iStartTableRows =
-                expected.substring(0, 
iStartCaption).lastIndexOf("begin:tableRows") + "begin:tableRows".length();
+        // the parser sends the caption before the rows, as HTML has it
+        int iStartTableRows = expected.substring(0, 
iStartCaption).lastIndexOf("begin:tableRows");
 
         StringBuilder text = new StringBuilder();
         text.append(expected, 0, iStartTableRows);
-        text.append(EOL).append(EOL).append(EOL);
-        text.append(captionTag.subSequence(0, 
captionTag.indexOf("end:tableCaption") + "end:tableCaption".length()));
+        text.append(captionTag);
         text.append(expected.substring(iStartTableRows));
 
         return text.toString();
diff --git 
a/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5IdentityTest.java
 
b/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5IdentityTest.java
index 757f0a80..4d9488ef 100644
--- 
a/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5IdentityTest.java
+++ 
b/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5IdentityTest.java
@@ -60,13 +60,12 @@ class Xhtml5IdentityTest extends AbstractIdentityTest {
         String captionTag = expected.substring(iStartCaption, iEndCaption) + 
EOL + EOL + EOL;
         expected = DoxiaStringUtils.replace(expected, captionTag, "");
 
-        int iStartTableRows =
-                expected.substring(0, 
iStartCaption).lastIndexOf("begin:tableRows") + "begin:tableRows".length();
+        // the parser sends the caption before the rows, as HTML has it
+        int iStartTableRows = expected.substring(0, 
iStartCaption).lastIndexOf("begin:tableRows");
 
         StringBuilder text = new StringBuilder();
         text.append(expected, 0, iStartTableRows);
-        text.append(EOL).append(EOL).append(EOL);
-        text.append(captionTag.subSequence(0, 
captionTag.indexOf("end:tableCaption") + "end:tableCaption".length()));
+        text.append(captionTag);
         text.append(expected.substring(iStartTableRows));
 
         return text.toString();

Reply via email to