You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kw...@apache.org on 2024/12/22 16:17:26 UTC

(maven-doxia) branch bugfix/trim-whitespace-inside-verbatim-code created (now e3e06962)

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

kwin pushed a change to branch bugfix/trim-whitespace-inside-verbatim-code
in repository https://gitbox.apache.org/repos/asf/maven-doxia.git


      at e3e06962 [DOXIA-764] Strip leading newline after verbatim(SinkEventAttributeSet.SOURCE)

This branch includes the following new commits:

     new e3e06962 [DOXIA-764] Strip leading newline after verbatim(SinkEventAttributeSet.SOURCE)

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(maven-doxia) 01/01: [DOXIA-764] Strip leading newline after verbatim(SinkEventAttributeSet.SOURCE)

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch bugfix/trim-whitespace-inside-verbatim-code
in repository https://gitbox.apache.org/repos/asf/maven-doxia.git

commit e3e06962e409a1924f041e21115ceee407cf4828
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Sun Dec 22 17:17:16 2024 +0100

    [DOXIA-764] Strip leading newline after
    verbatim(SinkEventAttributeSet.SOURCE)
---
 .../maven/doxia/sink/impl/Xhtml5BaseSink.java      | 29 +++++++++++++++++-----
 .../maven/doxia/sink/impl/Xhtml5BaseSinkTest.java  | 12 +++++++++
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java
index 2e3ca4f5..9119137e 100644
--- a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java
+++ b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSink.java
@@ -38,7 +38,6 @@ import org.apache.maven.doxia.markup.HtmlMarkup;
 import org.apache.maven.doxia.markup.Markup;
 import org.apache.maven.doxia.sink.Sink;
 import org.apache.maven.doxia.sink.SinkEventAttributes;
-import org.apache.maven.doxia.sink.impl.Xhtml5BaseSink.VerbatimMode;
 import org.apache.maven.doxia.util.DoxiaUtils;
 import org.apache.maven.doxia.util.HtmlTools;
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
@@ -82,7 +81,9 @@ public class Xhtml5BaseSink extends AbstractXmlSink implements HtmlMarkup {
         /** Inside {@code <pre>} */
         ON,
         /** Inside {@code <pre><code>} */
-        ON_WITH_CODE
+        ON_WITH_CODE,
+        /** Same as {@link #ON_WITH_CODE} but after some text has been emitted */
+        ON_WITH_CODE_AFTER_TEXT
     }
     /** An indication on if we're in verbatim mode and if so, surrounded by which tags. */
     private VerbatimMode verbatimMode;
@@ -885,7 +886,7 @@ public class Xhtml5BaseSink extends AbstractXmlSink implements HtmlMarkup {
      */
     @Override
     public void verbatim_() {
-        if (verbatimMode == VerbatimMode.ON_WITH_CODE) {
+        if (verbatimMode == VerbatimMode.ON_WITH_CODE || verbatimMode == VerbatimMode.ON_WITH_CODE_AFTER_TEXT) {
             writeEndTag(HtmlMarkup.CODE);
         }
         writeEndTag(HtmlMarkup.PRE);
@@ -1439,10 +1440,26 @@ public class Xhtml5BaseSink extends AbstractXmlSink implements HtmlMarkup {
         }
         if (headFlag) {
             getTextBuffer().append(text);
-        } else if (isVerbatim()) {
-            verbatimContent(text);
         } else {
-            content(text);
+            switch (getVerbatimMode()) {
+                case ON_WITH_CODE:
+                    // trim the first newline for backwards compatibility
+                    // as used to be emitted inside pre directly
+                    // https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element
+                    // "In the HTML syntax, a leading newline character immediately following the pre element start tag
+                    // is stripped."
+                    // as now emitted inside <pre><code> the stripping is no longer performed by the browser and needs
+                    // to be done server-side
+                    text = StringUtils.stripStart(text, "\r\n");
+                    verbatimMode = VerbatimMode.ON_WITH_CODE_AFTER_TEXT;
+                case ON_WITH_CODE_AFTER_TEXT:
+                case ON:
+                    verbatimContent(text);
+                    break;
+                default:
+                    content(text);
+                    break;
+            }
         }
         if (attributes != null) {
             inline_();
diff --git a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSinkTest.java b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSinkTest.java
index 63c6e436..c92bd806 100644
--- a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSinkTest.java
+++ b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/Xhtml5BaseSinkTest.java
@@ -686,6 +686,18 @@ public class Xhtml5BaseSinkTest {
         assertEquals(expected, writer.toString());
     }
 
+    @Test
+    public void testVerbatimSourceWithNewline() {
+        try (Xhtml5BaseSink sink = new Xhtml5BaseSink(writer)) {
+            sink.verbatim(SinkEventAttributeSet.SOURCE);
+            sink.text(EOL + "firstLine");
+            sink.text(EOL + "secondLine");
+            sink.verbatim_();
+        }
+        // first newline must be stripped as same as with verbatim without source
+        assertEquals("<pre><code>firstLine" + EOL + "secondLine</code></pre>", writer.toString());
+    }
+
     /**
      * Test of horizontalRule method, of class Xhtml5BaseSink.
      */