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

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

commit 9fdcfc962f16074fe44fd18f663c569761e6910a
Author: Michael Osipov <[email protected]>
AuthorDate: Wed May 18 23:12:22 2022 +0200

    [DOXIA-590] Either provided element class or default class gets ignored
    
    Co-authored-by: Fred Eckertson <[email protected]>
    
    This closes #98
---
 .../maven/doxia/sink/impl/Xhtml5BaseSink.java      | 92 +++++++++-------------
 .../maven/doxia/sink/impl/XhtmlBaseSink.java       | 91 +++++++++------------
 .../maven/doxia/sink/impl/Xhtml5BaseSinkTest.java  | 61 +++++++++++++-
 .../maven/doxia/sink/impl/XhtmlBaseSinkTest.java   | 63 ++++++++++++++-
 4 files changed, 197 insertions(+), 110 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 891471ad..91593240 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
@@ -1507,9 +1507,8 @@ public class Xhtml5BaseSink
     }
 
     /**
-     * The default class style is <code>a</code> or <code>b</code> depending 
the row id.
+     * Rows are striped with two colors by adding the class <code>a</code> or 
<code>b</code>. {@inheritDoc}
      *
-     * {@inheritDoc}
      * @see javax.swing.text.html.HTML.Tag#TR
      */
     @Override
@@ -1524,29 +1523,31 @@ public class Xhtml5BaseSink
     }
 
     /**
-     * The default class style is <code>a</code> or <code>b</code> depending 
the row id.
+     * Rows are striped with two colors by adding the class <code>a</code> or 
<code>b</code>. {@inheritDoc}
      *
-     * {@inheritDoc}
      * @see javax.swing.text.html.HTML.Tag#TR
      */
     @Override
     public void tableRow( SinkEventAttributes attributes )
     {
-        MutableAttributeSet att = new SinkEventAttributeSet();
+        MutableAttributeSet attrs = SinkUtils.filterAttributes(
+                attributes, SinkUtils.SINK_TR_ATTRIBUTES );
 
-        if ( evenTableRow )
+        if ( attrs == null )
         {
-            att.addAttribute( Attribute.CLASS, "a" );
+            attrs = new SinkEventAttributeSet();
         }
-        else
+
+        String rowClass = evenTableRow ? "a" : "b";
+        if ( attrs.isDefined( Attribute.CLASS.toString() ) )
         {
-            att.addAttribute( Attribute.CLASS, "b" );
+            String givenRowClass = (String) attrs.getAttribute( 
Attribute.CLASS.toString() );
+            rowClass = givenRowClass + " " + rowClass;
         }
 
-        att.addAttributes( SinkUtils.filterAttributes(
-                attributes, SinkUtils.SINK_TR_ATTRIBUTES  ) );
+        attrs.addAttribute( Attribute.CLASS, rowClass );
 
-        writeStartTag( HtmlMarkup.TR, att );
+        writeStartTag( HtmlMarkup.TR, attrs );
 
         evenTableRow = !evenTableRow;
 
@@ -1774,72 +1775,57 @@ public class Xhtml5BaseSink
         }
     }
 
-    /** {@inheritDoc} */
+    /**
+     * The default style class for external link is <code>externalLink</code>.
+     *
+     * {@inheritDoc}
+     * @see javax.swing.text.html.HTML.Tag#A
+     **/
     @Override
     public void link( String name )
     {
         link( name, null );
     }
 
-    /** {@inheritDoc} */
-    @Override
-    public void link( String name, SinkEventAttributes attributes )
-    {
-        if ( attributes == null )
-        {
-            link( name, null, null );
-        }
-        else
-        {
-            String target = (String) attributes.getAttribute( 
Attribute.TARGET.toString() );
-            MutableAttributeSet atts = SinkUtils.filterAttributes(
-                    attributes, SinkUtils.SINK_LINK_ATTRIBUTES  );
-
-            link( name, target, atts );
-        }
-    }
-
     /**
-     * Adds a link with an optional target.
      * The default style class for external link is <code>externalLink</code>.
      *
-     * @param href the link href.
-     * @param target the link target, may be null.
-     * @param attributes an AttributeSet, may be null.
-     *      This is supposed to be filtered already.
+     * {@inheritDoc}
      * @see javax.swing.text.html.HTML.Tag#A
-     */
-    private void link( String href, String target, MutableAttributeSet 
attributes )
+     **/
+    @Override
+    public void link( String name, SinkEventAttributes attributes )
     {
-        Objects.requireNonNull( href, "href cannot be null" );
+        Objects.requireNonNull( name, "name cannot be null" );
 
         if ( headFlag )
         {
             return;
         }
 
-        MutableAttributeSet att = new SinkEventAttributeSet();
+        MutableAttributeSet atts = SinkUtils.filterAttributes(
+                attributes, SinkUtils.SINK_LINK_ATTRIBUTES );
 
-        if ( DoxiaUtils.isExternalLink( href  ) )
+        if ( atts == null )
         {
-            att.addAttribute( Attribute.CLASS, "externalLink" );
+            atts = new SinkEventAttributeSet();
         }
 
-        att.addAttribute( Attribute.HREF, HtmlTools.escapeHTML( href  ) );
-
-        if ( target != null )
+        if ( DoxiaUtils.isExternalLink( name ) )
         {
-            att.addAttribute( Attribute.TARGET, target );
-        }
+            String linkClass = "externalLink";
+            if ( atts.isDefined( Attribute.CLASS.toString() ) )
+             {
+                 String givenLinkClass = (String) atts.getAttribute( 
Attribute.CLASS.toString() );
+                 linkClass = givenLinkClass + " " + linkClass;
+             }
 
-        if ( attributes != null )
-        {
-            attributes.removeAttribute( Attribute.HREF.toString() );
-            attributes.removeAttribute( Attribute.TARGET.toString() );
-            att.addAttributes( attributes );
+             atts.addAttribute( Attribute.CLASS, linkClass );
         }
 
-        writeStartTag( HtmlMarkup.A, att );
+        atts.addAttribute( Attribute.HREF, HtmlTools.escapeHTML( name ) );
+
+        writeStartTag( HtmlMarkup.A, atts );
     }
 
     /**
diff --git 
a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSink.java 
b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSink.java
index 8bccda25..1cc3f798 100644
--- 
a/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSink.java
+++ 
b/doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSink.java
@@ -1385,9 +1385,8 @@ public class XhtmlBaseSink
     }
 
     /**
-     * The default class style is <code>a</code> or <code>b</code> depending 
the row id.
+     * Rows are striped with two colors by adding the class <code>a</code> or 
<code>b</code>. {@inheritDoc}
      *
-     * {@inheritDoc}
      * @see javax.swing.text.html.HTML.Tag#TR
      */
     @Override
@@ -1402,29 +1401,30 @@ public class XhtmlBaseSink
     }
 
     /**
-     * The default class style is <code>a</code> or <code>b</code> depending 
the row id.
+     * Rows are striped with two colors by adding the class <code>a</code> or 
<code>b</code>. {@inheritDoc}
      *
-     * {@inheritDoc}
      * @see javax.swing.text.html.HTML.Tag#TR
      */
     @Override
     public void tableRow( SinkEventAttributes attributes )
     {
-        MutableAttributeSet att = new SinkEventAttributeSet();
+        MutableAttributeSet attrs = SinkUtils.filterAttributes( attributes, 
SinkUtils.SINK_TR_ATTRIBUTES );
 
-        if ( evenTableRow )
+        if ( attrs == null )
         {
-            att.addAttribute( Attribute.CLASS, "a" );
+            attrs = new SinkEventAttributeSet();
         }
-        else
+
+        String rowClass = evenTableRow ? "a" : "b";
+        if ( attrs.isDefined( Attribute.CLASS.toString() ) )
         {
-            att.addAttribute( Attribute.CLASS, "b" );
+            String givenRowClass = (String) attrs.getAttribute( 
Attribute.CLASS.toString() );
+            rowClass = givenRowClass + " " + rowClass;
         }
 
-        att.addAttributes( SinkUtils.filterAttributes(
-                attributes, SinkUtils.SINK_TR_ATTRIBUTES  ) );
+        attrs.addAttribute( Attribute.CLASS, rowClass );
 
-        writeStartTag( HtmlMarkup.TR, att );
+        writeStartTag( HtmlMarkup.TR, attrs );
 
         evenTableRow = !evenTableRow;
 
@@ -1652,72 +1652,57 @@ public class XhtmlBaseSink
         }
     }
 
-    /** {@inheritDoc} */
+    /**
+     * The default style class for external link is <code>externalLink</code>.
+     *
+     * {@inheritDoc}
+     * @see javax.swing.text.html.HTML.Tag#A
+     **/
     @Override
     public void link( String name )
     {
         link( name, null );
     }
 
-    /** {@inheritDoc} */
-    @Override
-    public void link( String name, SinkEventAttributes attributes )
-    {
-        if ( attributes == null )
-        {
-            link( name, null, null );
-        }
-        else
-        {
-            String target = (String) attributes.getAttribute( 
Attribute.TARGET.toString() );
-            MutableAttributeSet atts = SinkUtils.filterAttributes(
-                    attributes, SinkUtils.SINK_LINK_ATTRIBUTES  );
-
-            link( name, target, atts );
-        }
-    }
-
     /**
-     * Adds a link with an optional target.
      * The default style class for external link is <code>externalLink</code>.
      *
-     * @param href the link href.
-     * @param target the link target, may be null.
-     * @param attributes an AttributeSet, may be null.
-     *      This is supposed to be filtered already.
+     * {@inheritDoc}
      * @see javax.swing.text.html.HTML.Tag#A
-     */
-    private void link( String href, String target, MutableAttributeSet 
attributes )
+     **/
+    @Override
+    public void link( String name, SinkEventAttributes attributes )
     {
-        Objects.requireNonNull( href, "href cannot be null" );
+        Objects.requireNonNull( name, "name cannot be null" );
 
         if ( headFlag )
         {
             return;
         }
 
-        MutableAttributeSet att = new SinkEventAttributeSet();
+        MutableAttributeSet atts = SinkUtils.filterAttributes(
+                attributes, SinkUtils.SINK_LINK_ATTRIBUTES );
 
-        if ( DoxiaUtils.isExternalLink( href  ) )
+        if ( atts == null )
         {
-            att.addAttribute( Attribute.CLASS, "externalLink" );
+            atts = new SinkEventAttributeSet();
         }
 
-        att.addAttribute( Attribute.HREF, HtmlTools.escapeHTML( href  ) );
-
-        if ( target != null )
+        if ( DoxiaUtils.isExternalLink( name ) )
         {
-            att.addAttribute( Attribute.TARGET, target );
-        }
+            String linkClass = "externalLink";
+            if ( atts.isDefined( Attribute.CLASS.toString() ) )
+             {
+                 String givenLinkClass = (String) atts.getAttribute( 
Attribute.CLASS.toString() );
+                 linkClass = givenLinkClass + " " + linkClass;
+             }
 
-        if ( attributes != null )
-        {
-            attributes.removeAttribute( Attribute.HREF.toString() );
-            attributes.removeAttribute( Attribute.TARGET.toString() );
-            att.addAttributes( attributes );
+             atts.addAttribute( Attribute.CLASS, linkClass );
         }
 
-        writeStartTag( HtmlMarkup.A, att );
+        atts.addAttribute( Attribute.HREF, HtmlTools.escapeHTML( name ) );
+
+        writeStartTag( HtmlMarkup.A, atts );
     }
 
     /**
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 cf8f52c5..86edcc21 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
@@ -1085,15 +1085,23 @@ public class Xhtml5BaseSinkTest
         {
             sink = new Xhtml5BaseSink( writer );
 
+            sink.tableRows( null, false );
             sink.tableRow( attributes );
             sink.tableRow_();
+            sink.tableRow();
+            sink.tableRow_();
+            sink.tableRows_();
+            sink.table_();
         }
         finally
         {
             sink.close();
         }
 
-        assertEquals( "<tr class=\"a\" style=\"bold\"></tr>", 
writer.toString() );
+        String xmlExpected = "<table border=\"0\" class=\"bodyTable\">" + EOL 
+ "<tr style=\"bold\" class=\"a\"></tr>"
+            + EOL + "<tr class=\"b\"></tr></table>";
+
+        assertEquals( xmlExpected, writer.toString() );
     }
 
     /**
@@ -1207,7 +1215,56 @@ public class Xhtml5BaseSinkTest
             sink.close();
         }
 
-        assertEquals( "<a href=\"link.html\" style=\"bold\"></a>", 
writer.toString() );
+        assertEquals( "<a style=\"bold\" href=\"link.html\"></a>", 
writer.toString() );
+    }
+
+    /**
+     * Test of link method for an external link.
+     */
+    @Test
+    public void testLinkExternal()
+    {
+        final String name = "https://www.apache.org";;
+
+        try
+        {
+            sink = new Xhtml5BaseSink( writer );
+            sink.link( name, attributes );
+            sink.link_();
+        }
+        finally
+        {
+            sink.close();
+        }
+
+        assertEquals( "<a style=\"bold\" class=\"externalLink\" 
href=\"https://www.apache.org\";></a>",
+                      writer.toString() );
+    }
+
+    /**
+     * Test of link method for an external link when a css class is provided.
+     */
+    @Test
+    public void testLinkExternalClassExtend()
+    {
+        final String name = "https://www.apache.org";;
+        SinkEventAttributeSet attributes2 = new SinkEventAttributeSet();
+        attributes2.addAttributes( attributes );
+        attributes2.addAttribute( SinkEventAttributes.CLASS, "cs1 cs2" );
+
+        try
+        {
+            sink = new Xhtml5BaseSink( writer );
+            sink.link( name, attributes2 );
+            sink.link_();
+        }
+        finally
+        {
+            sink.close();
+        }
+
+        assertEquals( "<a style=\"bold\" class=\"cs1 cs2 externalLink\" 
href=\"https://www.apache.org\";></a>",
+                      writer.toString() );
     }
 
     /**
diff --git 
a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSinkTest.java
 
b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSinkTest.java
index 813d56f8..ecacdd4a 100644
--- 
a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSinkTest.java
+++ 
b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/XhtmlBaseSinkTest.java
@@ -47,6 +47,8 @@ public class XhtmlBaseSinkTest
     private XhtmlBaseSink sink;
     private Writer writer;
 
+    String EOL = System.lineSeparator();
+
     @BeforeEach
     public void setUp()
     {
@@ -705,15 +707,23 @@ public class XhtmlBaseSinkTest
         {
             sink = new XhtmlBaseSink( writer );
 
+            sink.tableRows( null, false );
             sink.tableRow( attributes );
             sink.tableRow_();
+            sink.tableRow();
+            sink.tableRow_();
+            sink.tableRows_();
+            sink.table_();
         }
         finally
         {
             sink.close();
         }
 
-        assertEquals( "<tr class=\"a\" style=\"bold\"></tr>", 
writer.toString() );
+        String xmlExpected = "<table border=\"0\" class=\"bodyTable\">" + EOL 
+ "<tr style=\"bold\" class=\"a\"></tr>"
+            + EOL + "<tr class=\"b\"></tr></table>";
+
+        assertEquals( xmlExpected, writer.toString() );
     }
 
     /**
@@ -827,7 +837,56 @@ public class XhtmlBaseSinkTest
             sink.close();
         }
 
-        assertEquals( "<a href=\"link.html\" style=\"bold\"></a>", 
writer.toString() );
+        assertEquals( "<a style=\"bold\" href=\"link.html\"></a>", 
writer.toString() );
+    }
+
+    /**
+     * Test of link method for an external link.
+     */
+    @Test
+    public void testLinkExternal()
+    {
+        final String name = "https://www.apache.org";;
+
+        try
+        {
+            sink = new XhtmlBaseSink( writer );
+            sink.link( name, attributes );
+            sink.link_();
+        }
+        finally
+        {
+            sink.close();
+        }
+
+        assertEquals( "<a style=\"bold\" class=\"externalLink\" 
href=\"https://www.apache.org\";></a>",
+                      writer.toString() );
+    }
+
+    /**
+     * Test of link method for an external link when a css class is provided.
+     */
+    @Test
+    public void testLinkExternalClassExtend()
+    {
+        final String name = "https://www.apache.org";;
+        SinkEventAttributeSet attributes2 = new SinkEventAttributeSet();
+        attributes2.addAttributes( attributes );
+        attributes2.addAttribute( SinkEventAttributes.CLASS, "cs1 cs2" );
+
+        try
+        {
+            sink = new XhtmlBaseSink( writer );
+            sink.link( name, attributes2 );
+            sink.link_();
+        }
+        finally
+        {
+            sink.close();
+        }
+
+        assertEquals( "<a style=\"bold\" class=\"cs1 cs2 externalLink\" 
href=\"https://www.apache.org\";></a>",
+                      writer.toString() );
     }
 
     /**

Reply via email to