Skip to content

Commit b00f543

Browse files
committed
Allow numbers to be passed as element content
1 parent 82d87b6 commit b00f543

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/main/kotlin/org/celtric/kotlin/html/_building_blocks.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ sealed class Element(val name: String, val _isBlock: Boolean, val content: Any,
4242
val renderableContent: Node = when {
4343
content == Unit -> Text("")
4444
content is String -> Text(content)
45+
content is Number -> Text(content.toString())
4546
content is Node -> content
4647
(content is List<*> && content.first() is Node) -> @Suppress("UNCHECKED_CAST") NodeList(content as List<Node>)
47-
else -> throw HTMLException("Content must be String, Node or List<Node>, ${contentType(content)} given.")
48+
else -> throw HTMLException("Content must be String, Number, Node or List<Node>, ${contentType(content)} given.")
4849
}
4950

5051
val renderedContent =
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.celtric.kotlin.html
2+
3+
import org.junit.jupiter.api.Test
4+
5+
internal class ElementContentTest {
6+
7+
@Test fun `numbers are automatically cast to text`() {
8+
span { 1 }.assertRenders("<span>1</span>")
9+
span { 1L }.assertRenders("<span>1</span>")
10+
span { 1.0 }.assertRenders("<span>1.0</span>")
11+
span { 1.0f }.assertRenders("<span>1.0</span>")
12+
}
13+
}

src/test/kotlin/org/celtric/kotlin/html/ErrorsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ internal class ErrorsTest {
99
val elementWithInvalidContent = span { listOf(listOf("Foo")) }
1010

1111
assertThrows(HTMLException::class.java, { elementWithInvalidContent.render() })
12-
.let { assertEquals("Content must be String, Node or List<Node>, SingletonList<SingletonList<String>> given.", it.message) }
12+
.let { assertEquals("Content must be String, Number, Node or List<Node>, SingletonList<SingletonList<String>> given.", it.message) }
1313
}
1414
}

0 commit comments

Comments
 (0)