Skip to content

Commit 754003e

Browse files
kicktipptipsy
authored andcommitted
Add attr(Attribute attribute) to Tag
1 parent 6968478 commit 754003e

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/main/java/j2html/tags/Tag.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import j2html.attributes.Attribute;
55
import java.io.IOException;
66
import java.util.ArrayList;
7+
import java.util.Iterator;
78

89
public abstract class Tag<T extends Tag<T>> extends DomContent {
910
protected String tagName;
@@ -78,6 +79,28 @@ public T attr(String attribute, Object value) {
7879
setAttribute(attribute, value == null ? null : String.valueOf(value));
7980
return (T) this;
8081
}
82+
83+
/**
84+
* Adds the specified attribute. If the Tag previously contained an attribute with the same name, the old attribute is replaced by the specified attribute.
85+
*
86+
* @param attribute the attribute
87+
* @return itself for easy chaining
88+
*/
89+
public T attr(Attribute attribute) {
90+
Iterator<Attribute> iterator = attributes.iterator();
91+
String name = attribute.getName();
92+
if (name != null) {
93+
// name == null is allowed, but those Attributes are not rendered. So we add them anyway.
94+
while (iterator.hasNext()) {
95+
Attribute existingAttribute = iterator.next();
96+
if (existingAttribute.getName().equals(name)) {
97+
iterator.remove();
98+
}
99+
}
100+
}
101+
attributes.add(attribute);
102+
return (T) this;
103+
}
81104

82105
/**
83106
* Sets a custom attribute without value
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package j2html.model;
2+
3+
import j2html.attributes.Attribute;
4+
import java.io.IOException;
5+
6+
public class DynamicHrefAttribute extends Attribute {
7+
8+
public DynamicHrefAttribute() {
9+
super("href");
10+
}
11+
12+
@Override
13+
public void renderModel(Appendable writer, Object model) throws IOException {
14+
writer.append(" ");
15+
writer.append(getName());
16+
writer.append("=\"");
17+
writer.append(getUrl(model));
18+
writer.append("\"");
19+
}
20+
21+
public String getUrl(Object model) {
22+
return "/";
23+
}
24+
}

src/test/java/j2html/tags/TagTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package j2html.tags;
22

3-
import j2html.Config;
4-
import org.junit.Test;
53
import static j2html.TagCreator.body;
64
import static j2html.TagCreator.div;
75
import static j2html.TagCreator.footer;
@@ -15,6 +13,9 @@
1513
import static j2html.TagCreator.tag;
1614
import static org.hamcrest.MatcherAssert.assertThat;
1715
import static org.hamcrest.Matchers.is;
16+
import j2html.Config;
17+
import j2html.model.DynamicHrefAttribute;
18+
import org.junit.Test;
1819

1920
public class TagTest {
2021

@@ -92,5 +93,16 @@ public void testEmptyAttribute() throws Exception {
9293
assertThat(testTagAttrWithoutAttr.render(), is("<a attribute></a>"));
9394
}
9495

96+
@Test
97+
public void testDynamicAttribute() throws Exception {
98+
ContainerTag testTagWithAttrValueNull = new ContainerTag("a").attr(new DynamicHrefAttribute());
99+
assertThat(testTagWithAttrValueNull.render(), is("<a href=\"/\"></a>"));
100+
}
101+
102+
@Test
103+
public void testDynamicAttributeReplacement() throws Exception {
104+
ContainerTag testTagWithAttrValueNull = new ContainerTag("a").attr("href", "/link").attr(new DynamicHrefAttribute());
105+
assertThat(testTagWithAttrValueNull.render(), is("<a href=\"/\"></a>"));
106+
}
95107

96108
}

0 commit comments

Comments
 (0)