File tree Expand file tree Collapse file tree 3 files changed +61
-2
lines changed Expand file tree Collapse file tree 3 files changed +61
-2
lines changed Original file line number Diff line number Diff line change 44import j2html .attributes .Attribute ;
55import java .io .IOException ;
66import java .util .ArrayList ;
7+ import java .util .Iterator ;
78
89public 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11package j2html .tags ;
22
3- import j2html .Config ;
4- import org .junit .Test ;
53import static j2html .TagCreator .body ;
64import static j2html .TagCreator .div ;
75import static j2html .TagCreator .footer ;
1513import static j2html .TagCreator .tag ;
1614import static org .hamcrest .MatcherAssert .assertThat ;
1715import static org .hamcrest .Matchers .is ;
16+ import j2html .Config ;
17+ import j2html .model .DynamicHrefAttribute ;
18+ import org .junit .Test ;
1819
1920public 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}
You can’t perform that action at this time.
0 commit comments