Skip to content

Commit 62cafb9

Browse files
rupert-madden-abbotttipsy
authored andcommitted
Allow nulls in join() (fix #109)
* Discard nulls in join. * Add test.
1 parent af7c986 commit 62cafb9

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/main/java/j2html/tags/DomContentJoiner.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public static UnescapedText join(CharSequence delimiter, boolean fixPeriodAndCom
99
sb.append(((String) o).trim()).append(delimiter);
1010
} else if (o instanceof DomContent) {
1111
sb.append(((DomContent) o).render().trim()).append(delimiter);
12+
} else if (o == null) {
13+
//Discard null objects so iff/iffelse can be used with join
1214
} else {
1315
throw new RuntimeException("You can only join DomContent and String objects");
1416
}

src/test/java/j2html/tags/TagCreatorTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public void testJoin() throws Exception {
6565
assertThat(actual, is(expected));
6666
}
6767

68+
@Test
69+
public void testJoinWithNulls() throws Exception {
70+
String expected = "This is my joined string. It has ignored null content in the middle.";
71+
String actual = join("This is my joined string.", iff(false, "this should not be displayed"), "It has ignored null content in the middle.").render();
72+
assertThat(actual, is(expected));
73+
}
74+
6875
@Test
6976
public void testEach() throws Exception {
7077
String j2htmlMap = ul().with(

0 commit comments

Comments
 (0)