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 8383305a Close heading sections left open at the end of a document
8383305a is described below

commit 8383305a90be909fc3cd19b71bc79964648facd3
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Nov 14 10:35:18 2026 +0000

    Close heading sections left open at the end of a document
    
    Xhtml5BaseParser opened a section for each heading but only the body end 
tag of Xhtml5Parser closed them, so a fragment or a document without a body 
element left them open.
---
 .../maven/doxia/parser/AbstractXmlParser.java      | 14 ++++
 .../maven/doxia/parser/Xhtml5BaseParser.java       | 11 +++
 .../apache/maven/doxia/macro/toc/TocMacroTest.java |  5 +-
 .../maven/doxia/parser/Xhtml5BaseParserTest.java   | 96 ++++++++++++++++++++--
 4 files changed, 115 insertions(+), 11 deletions(-)

diff --git 
a/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java 
b/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
index ea8f1c2b..bdbbdee9 100644
--- 
a/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
+++ 
b/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
@@ -270,6 +270,20 @@ public abstract class AbstractXmlParser extends 
AbstractParser implements XmlMar
                 throw new XmlPullParserException("Failed to parse next token", 
parser, io);
             }
         }
+
+        handleEndDocument(sink);
+    }
+
+    /**
+     * Called once when the end of the document has been reached, before the 
parser state is reset.
+     * Subclasses can use it to emit events that close constructs still open 
at that point.
+     * The default implementation does nothing.
+     *
+     * @param sink the sink to receive the events.
+     * @since 2.2.1
+     */
+    protected void handleEndDocument(Sink sink) {
+        // nop
     }
 
     /**
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 6b80aa6d..fd6d2cc6 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
@@ -994,6 +994,17 @@ public class Xhtml5BaseParser extends AbstractXmlParser 
implements HtmlMarkup {
         return id;
     }
 
+    /**
+     * {@inheritDoc}
+     *
+     * Closes the sections that were opened implicitly for headings and are 
still open, for example
+     * in a fragment without a closing {@code <body>} element.
+     */
+    @Override
+    protected void handleEndDocument(Sink sink) {
+        emitHeadingSections(0, sink, false);
+    }
+
     @Override
     protected void init() {
         super.init();
diff --git 
a/doxia-core/src/test/java/org/apache/maven/doxia/macro/toc/TocMacroTest.java 
b/doxia-core/src/test/java/org/apache/maven/doxia/macro/toc/TocMacroTest.java
index 03cd31d3..1cadaa0b 100644
--- 
a/doxia-core/src/test/java/org/apache/maven/doxia/macro/toc/TocMacroTest.java
+++ 
b/doxia-core/src/test/java/org/apache/maven/doxia/macro/toc/TocMacroTest.java
@@ -189,7 +189,7 @@ class TocMacroTest {
         String actualLinkTarget = (String) link.getArgs()[0];
         AbstractParserTest.assertSinkEquals(it.next(), "text", "1 Headline", 
null);
         AbstractParserTest.assertSinkEquals(
-                it, "link_", "listItem_", "list_", "section1", 
"sectionTitle1", "text", "sectionTitle1_");
+                it, "link_", "listItem_", "list_", "section1", 
"sectionTitle1", "text", "sectionTitle1_", "section1_");
 
         // check html output as well (without the actual TOC)
         StringWriter out = new StringWriter();
@@ -197,7 +197,8 @@ class TocMacroTest {
         parser.addSinkWrapperFactory(new 
CreateAnchorsForIndexEntriesFactory());
         parser.parse(sourceContent, sink2);
         assertEquals(
-                "<section><a id=\"" + actualLinkTarget.substring(1) + 
"\"></a>" + Markup.EOL + "<h1>1 Headline</h1>",
+                "<section><a id=\"" + actualLinkTarget.substring(1) + 
"\"></a>" + Markup.EOL
+                        + "<h1>1 Headline</h1></section>",
                 out.toString());
     }
 
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 16273d16..b22b4f2d 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
@@ -18,7 +18,10 @@
  */
 package org.apache.maven.doxia.parser;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Iterator;
+import java.util.List;
 
 import org.apache.maven.doxia.sink.SinkEventAttributeSet;
 import org.apache.maven.doxia.sink.impl.SinkEventElement;
@@ -85,10 +88,9 @@ class Xhtml5BaseParserTest extends AbstractParserTest {
         assertEquals("section1", it.next().getName());
         assertEquals("sectionTitle1", it.next().getName());
         assertEquals("sectionTitle1_", it.next().getName());
-        // this one is missing because we enclose everything in <p> which is 
not valid xhtml,
-        // needs to be tested in overriding parser, eg XhtmlParser, XdocParser.
-        // assertEquals("section1_", it.next().getName());
         assertEquals("paragraph_", it.next().getName());
+        // the section opened for the heading is closed at the end of the 
document
+        assertEquals("section1_", it.next().getName());
         assertFalse(it.hasNext());
     }
 
@@ -122,14 +124,90 @@ class Xhtml5BaseParserTest extends AbstractParserTest {
         assertEquals("section2", it.next().getName());
         assertEquals("sectionTitle2", it.next().getName());
         assertEquals("sectionTitle2_", it.next().getName());
-        // these two are missing because we enclose everything in <p> which is 
not valid xhtml,
-        // needs to be tested in overriding parser, eg XhtmlParser, XdocParser.
-        // assertEquals("section2_", it.next().getName());
-        // assertEquals("section1_", it.next().getName());
         assertEquals("paragraph_", it.next().getName());
+        // the sections opened for the headings are closed at the end of the 
document
+        assertEquals("section2_", it.next().getName());
+        assertEquals("section1_", it.next().getName());
         assertFalse(it.hasNext());
     }
 
+    @Test
+    void headingSectionsAreClosedAtEndOfFragment() throws Exception {
+        parser.parse("<div><h1>Title</h1><p>text</p></div>", sink);
+
+        assertEquals(
+                Arrays.asList(
+                        "division",
+                        "section1",
+                        "sectionTitle1",
+                        "text",
+                        "sectionTitle1_",
+                        "paragraph",
+                        "text",
+                        "paragraph_",
+                        "division_",
+                        "section1_"),
+                eventNames());
+    }
+
+    @Test
+    void headingSectionsOfLowerLevelsAreClosedAtEndOfFragment() throws 
Exception {
+        parser.parse("<div><h2>A</h2><h3>B</h3></div>", sink);
+
+        assertEquals(
+                Arrays.asList(
+                        "division",
+                        "section1",
+                        "section2",
+                        "sectionTitle2",
+                        "text",
+                        "sectionTitle2_",
+                        "section3",
+                        "sectionTitle3",
+                        "text",
+                        "sectionTitle3_",
+                        "division_",
+                        "section3_",
+                        "section2_",
+                        "section1_"),
+                eventNames());
+    }
+
+    @Test
+    void explicitSectionIsNotClosedTwice() throws Exception {
+        parser.parse("<section><h1>A</h1><p>x</p></section>", sink);
+
+        assertEquals(
+                Arrays.asList(
+                        "section1",
+                        "sectionTitle1",
+                        "text",
+                        "sectionTitle1_",
+                        "paragraph",
+                        "text",
+                        "paragraph_",
+                        "section1_"),
+                eventNames());
+    }
+
+    @Test
+    void parserCanBeReused() throws Exception {
+        parser.parse("<div><h2>A</h2></div>", sink);
+        sink.reset();
+        parser.parse("<div><h1>B</h1></div>", sink);
+
+        assertEquals(
+                Arrays.asList(
+                        "division", "section1", "sectionTitle1", "text", 
"sectionTitle1_", "division_", "section1_"),
+                eventNames());
+    }
+
+    private List<String> eventNames() {
+        List<String> names = new ArrayList<>();
+        sink.getEventList().forEach(e -> names.add(e.getName()));
+        return names;
+    }
+
     @Test
     void sectionsAndHeadingsOnDifferentLevels() throws Exception {
         // section on higher level than heading
@@ -531,8 +609,7 @@ class Xhtml5BaseParserTest extends AbstractParserTest {
         assertEquals("A", textEvt.getArgs()[0]);
 
         assertEquals("paragraph_", it.next().getName());
-        // FIXME
-        //        assertEquals("section1_", it.next().getName());
+        assertEquals("section1_", it.next().getName());
 
         assertFalse(it.hasNext());
     }
@@ -572,6 +649,7 @@ class Xhtml5BaseParserTest extends AbstractParserTest {
         assertEquals(">", textEvt.getArgs()[0]);
 
         assertEquals("paragraph_", it.next().getName());
+        assertEquals("section1_", it.next().getName());
 
         assertFalse(it.hasNext());
     }

Reply via email to