Permalink
Browse files

tidied up the sample HTML template

  • Loading branch information...
jstrachan committed Apr 13, 2012
1 parent 087d1cd commit 5a185f95500d2d907f825fbc415a463a832dcd53
@@ -28,10 +28,11 @@ fun Node.element(localName: String, init: Element.()-> Unit): Element {
}
fun Node.textElement(localName: String, text: String? = null, init: Element.()-> Unit): Element {
- return element(localName) {
+ val answer = element(localName) {
if (text != null) addText(text)
- init()
}
+ answer.init()
+ return answer
}
fun Node.html(init: Element.()-> Unit): Element = element("html", init)
@@ -52,23 +53,25 @@ fun Node.h4(text: String? = null, init: Element.()-> Unit): Element = textElemen
fun Node.p(init: Element.()-> Unit): Element = element("p", init)
+fun Node.a(href: String? = null, title: String? = null, text: String? = null): Element = a(href, title, text) {}
+
fun Node.a(href: String? = null, title: String? = null, text: String? = null, init: Element.()-> Unit): Element {
- return textElement("a", text) {
- if (href != null) setAttribute("href", href)
- if (title != null) setAttribute("title", title)
- init()
- }
+ val answer = textElement("a", text, init)
+ if (href != null) answer.setAttribute("href", href)
+ if (title != null) answer.setAttribute("title", title)
+ return answer
}
fun Node.img(src: String? = null, alt: String? = null): Element = img(src, alt) {}
fun Node.img(src: String? = null, alt: String? = null, init: Element.()-> Unit): Element {
- return element("img") {
+ val answer = element("img") {
if (src != null) setAttribute("src", src)
if (alt != null) setAttribute("alt", alt)
- init()
}
+ answer.init()
+ return answer
}
@@ -13,25 +13,20 @@ class HtmlTemplateTest {
val dom = document.html {
body {
h1("Hey")
- h1 {
- // TODO
- // ideally this would work
- // +"my title"
- this + "my title"
+ h2 {
+ text = "my title"
}
p {
- a(href = "hey", text = "link text") {
- // TODO doesn't work yet...
- // text = "whatnot"
- this + "whatnot"
- }
+ a(href = "hey", text = "link text")
+
+ this + "some text"
+
a(href = "cheese.html", title = "my link title") {
img(src = "blah.jpg")
}
}
-
}
}
- println("Xml is ${dom.toXmlString()}")
+ println("HTML is ${dom.toXmlString()}")
}
}
@@ -0,0 +1,26 @@
+package test.koolapp.template.html
+
+import org.koolapp.template.html.*
+import org.w3c.dom.*
+
+import kotlin.dom.*
+
+import org.junit.Test
+
+class HtmlTextTemplateTest {
+ Test fun createDom() {
+ val document = createDocument()
+ val dom = document.html {
+ body {
+ h1("Hey")
+ h1 {
+ // TODO
+ // ideally this would work
+ // +"my title"
+ this + "my title"
+ }
+ }
+ }
+ println("Xml is ${dom.toXmlString()}")
+ }
+}

0 comments on commit 5a185f9

Please sign in to comment.