cleanup
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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>"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user