diff --git a/src/tags/Tag.java b/src/tags/Tag.java
index 936df32..f621127 100644
--- a/src/tags/Tag.java
+++ b/src/tags/Tag.java
@@ -24,19 +24,15 @@ public abstract class Tag {
* @param name the attribute
* @param value the attribute value
*/
- public void setAttribute(String name, String value) {
- if (value != null) {
- for (Attribute attribute : attributes) {
- if (attribute.getName().equals(name)) {
- //if attribute exists we set the attribute value in stead of just adding a new attribute
- attribute.setValue(value);
- return;
- }
+ public boolean setAttribute(String name, String value) {
+ if (value == null) { return attributes.add(new Attribute(name)); }
+ for (Attribute attribute : attributes) {
+ if (attribute.getName().equals(name)) {
+ attribute.setValue(value); //update with new value
+ return true;
}
- attributes.add(new Attribute(name, value));
- } else {
- attributes.add(new Attribute(name));
}
+ return attributes.add(new Attribute(name, value));
}
public String render() {
diff --git a/test/attributes/AttributeTest.java b/test/attributes/AttributeTest.java
index 1343309..ca65bb3 100644
--- a/test/attributes/AttributeTest.java
+++ b/test/attributes/AttributeTest.java
@@ -1,6 +1,7 @@
package j2html.test.attributes;
import j2html.src.attributes.Attribute;
+import j2html.src.tags.ContainerTag;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
@@ -19,4 +20,12 @@ public class AttributeTest {
assertTrue(nullAttribute.render().equals(""));
}
+ @Test
+ public void testSetAttribute() throws Exception {
+ ContainerTag testTag = new ContainerTag("a");
+ testTag.setAttribute("href", "http://example.com");
+ testTag.setAttribute("href", "http://example.org");
+ assertTrue(testTag.render().equals(""));
+ }
+
}
\ No newline at end of file
diff --git a/test/tags/TagTest.java b/test/tags/TagTest.java
index e538df3..d340f03 100644
--- a/test/tags/TagTest.java
+++ b/test/tags/TagTest.java
@@ -8,14 +8,6 @@ import static org.junit.Assert.assertTrue;
public class TagTest {
- @Test
- public void testSetAttribute() throws Exception {
- ContainerTag testTag = new ContainerTag("a");
- testTag.setAttribute("href", "http://example.com");
- testTag.setAttribute("href", "http://example.org");
- assertTrue(testTag.render().equals(""));
- }
-
@Test
public void testRender() throws Exception {
ContainerTag testTag = new ContainerTag("a");