This commit is contained in:
tipsy
2015-04-24 20:26:08 +02:00
parent 77c5fa2026
commit eafaf6fa1f
3 changed files with 16 additions and 19 deletions

View File

@@ -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() {

View File

@@ -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("<a href=\"http://example.org\"></a>"));
}
}

View File

@@ -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("<a href=\"http://example.org\"></a>"));
}
@Test
public void testRender() throws Exception {
ContainerTag testTag = new ContainerTag("a");