Skip to content

Commit

Permalink
Migrate to 0.1-SNAPSHOT
Browse files Browse the repository at this point in the history
Migrate to new org.w3c.dom API
  • Loading branch information
Sergey Mashkov committed May 14, 2015
1 parent ef5318c commit c813f0b
Show file tree
Hide file tree
Showing 44 changed files with 1,103 additions and 717 deletions.
24 changes: 24 additions & 0 deletions generate/src/main/kotlin/attributes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,28 @@ fun <O : Appendable> O.facade(facade : AttributeFacade) {
}
}
}
}

fun <O: Appendable> O.eventProperty(parent : String, attribute : AttributeInfo) {
append("public ")
variable(receiver = parent, variable = Var(
name = attribute.fieldName + "Function",
type = "(Event) -> Unit",
mutable = true
))
emptyLine()

getter() defineIs(StringBuilder {
append("throw ")
functionCall("UnsupportedOperationException", listOf("You can't read variable ${attribute.fieldName}".quote()))
})
setter {
append("EventAttribute.")
functionCall("set", listOf(
"this",
attribute.name.quote(),
"newValue"
))
}
emptyLine()
}
2 changes: 1 addition & 1 deletion generate/src/main/kotlin/humanizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private fun String.replaceMistakesAndUglyWords() : String =


private fun String.replaceHyphensToCamelCase() : String =
this.split("[.:_\\-<>]")
this.split("[.:_\\-<>]".toRegex())
.mapIndexed { i, s ->
if (i == 0) s
else s.capitalize()
Expand Down
35 changes: 32 additions & 3 deletions generate/src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fun main(args: Array<String>) {

FileOutputStream("$todir/gen-builders.kt").writer().use {
it.with {
packg(packg)
packg(packg + ".impl")
emptyLine()
import("html4k.*")
import("html4k.impl.*")
Expand All @@ -106,6 +106,7 @@ fun main(args: Array<String>) {
emptyLine()
import("html4k.*")
import("html4k.impl.*")
import("html4k.attributes.*")
emptyLine()

warning()
Expand All @@ -124,6 +125,8 @@ fun main(args: Array<String>) {
packg(packg)
emptyLine()
import("html4k.*")
import("html4k.impl.*")
import("html4k.attributes.*")
emptyLine()

warning()
Expand All @@ -145,8 +148,9 @@ fun main(args: Array<String>) {
packg(packg + ".js")
emptyLine()
import("html4k.*")
import("kotlin.js.dom.html.*")
import("kotlin.js.dom.html5.*")
import("html4k.impl.*")
import("html4k.attributes.*")
import("org.w3c.dom.*")
emptyLine()

warning()
Expand All @@ -163,6 +167,28 @@ fun main(args: Array<String>) {
}
}

FileOutputStream("$jsdir/gen-event-attrs.kt").writer("UTF-8").use {
it.with {
packg(packg + ".js")
emptyLine()
import("html4k.*")
import("html4k.attributes.*")
import("html4k.dom.*")
import("org.w3c.dom.events.*")
emptyLine()

warning()
emptyLine()
emptyLine()

Repository.attributeFacades.filter { it.value.attributeNames.any { it.startsWith("on") } }.forEach { facade ->
facade.value.attributes.filter { it.name.startsWith("on") }.forEach {
eventProperty(facade.value.name.capitalize() + "Facade", it)
}
}
}
}

FileOutputStream("$todir/gen-enums.kt").writer("UTF-8").use {
it.with {
packg(packg)
Expand Down Expand Up @@ -201,6 +227,7 @@ fun main(args: Array<String>) {
packg(packg)
emptyLine()
import("html4k.*")
import("html4k.attributes.*")
emptyLine()

warning()
Expand All @@ -218,6 +245,8 @@ fun main(args: Array<String>) {
packg(packg)
emptyLine()
import("html4k.*")
import("html4k.impl.*")
import("html4k.attributes.*")
emptyLine()

warning()
Expand Down
4 changes: 2 additions & 2 deletions generate/src/main/kotlin/model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.TreeSet
object Repository {
val tags = TreeMap<String, TagInfo>()

[suppress("UNCHECKED_CAST")]
@suppress("UNCHECKED_CAST")
val attributeDelegateRequests = TreeSet<AttributeRequest> ({a, b ->
a.type.compareTo(b.type).let { typeComparison ->
if (typeComparison != 0) typeComparison
Expand Down Expand Up @@ -72,7 +72,7 @@ val HasType.typeName : String
get() = if (type == AttributeType.ENUM) enumTypeName else type.typeName

val AttributeInfo.fieldName : String
get() = if (safeAlias.isNotEmpty()) safeAlias else name
get() = if (safeAlias.isEmpty()) name else safeAlias

fun String.isLowerCase() = this.toLowerCase() == this

Expand Down
2 changes: 1 addition & 1 deletion generate/src/main/kotlin/xsdparser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fun fillRepository() {
parser.parse(SCHEME_URL)
val schema = parser.getResult().getSchema(HTML_NAMESPACE)

[suppress("UNCHECKED_CAST")]
@suppress("UNCHECKED_CAST")
val alreadyIncluded = TreeSet<String>() {a, b -> a.compareToIgnoreCase(b)} as MutableSet<String>
schema.getAttGroupDecls().values().forEach { attributeGroup ->
val requiredNames = HashSet<String>()
Expand Down
32 changes: 22 additions & 10 deletions js/src/main/kotlin/dom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import html4k.*
import html4k.js.*
import html4k.consumers.onFinalize
import html4k.consumers.onFinalizeMap
import org.w3c.dom.Node
import org.w3c.dom.*
import org.w3c.dom.events.Event
import java.util.ArrayList
import kotlin.dom.asList
import kotlin.dom.first
import kotlin.dom.toList
import kotlin.js.dom.html.*

class JSDOMBuilder<R : HTMLElement>(val document : HTMLDocument) : TagConsumer<R> {
native
nativeSetter
private fun HTMLElement.setEvent(name : String, callback : (Event) -> Unit) : Unit

class JSDOMBuilder<R : HTMLElement>(val document : Document) : TagConsumer<R> {
private val path = arrayListOf<HTMLElement>()
private var lastLeaved : HTMLElement? = null

Expand All @@ -36,6 +40,14 @@ class JSDOMBuilder<R : HTMLElement>(val document : HTMLDocument) : TagConsumer<R
path.last().setAttribute(attribute, value)
}

override fun onTagEvent(tag: Tag, event: String, value: (Event) -> Unit) {
if (path.isEmpty()) {
throw IllegalStateException("No current tag")
}

path.last().setEvent(event, value)
}

override fun onTagEnd(tag: Tag) {
if (path.isEmpty() || path.last().tagName.toLowerCase() != tag.tagName.toLowerCase()) {
throw IllegalStateException("We haven't entered tag ${tag.tagName} but trying to leave")
Expand All @@ -60,30 +72,30 @@ class JSDOMBuilder<R : HTMLElement>(val document : HTMLDocument) : TagConsumer<R
// stupid hack as browsers doesn't support createEntityReference
val s = document.createElement("span") as HTMLElement
s.innerHTML = entity.text
path.last().appendChild(s.childNodes.toList().filter { it.nodeType == Node.TEXT_NODE }.first())
path.last().appendChild(s.childNodes.asList().filter { it.nodeType == Node.TEXT_NODE }.first())

// other solution would be
// pathLast().innerHTML += entity.text
}

override fun finalize(): R = lastLeaved?.asR() ?: throw IllegalStateException("We can't finalize as there was no tags")

[suppress("UNCHECKED_CAST")]
@suppress("UNCHECKED_CAST")
private fun HTMLElement.asR() = this as R

}


public fun HTMLDocument.createTree() : TagConsumer<HTMLElement> = JSDOMBuilder(this)
public val HTMLDocument.create : TagConsumer<HTMLElement>
public fun Document.createTree() : TagConsumer<HTMLElement> = JSDOMBuilder(this)
public val Document.create : TagConsumer<HTMLElement>
get() = JSDOMBuilder(this)

public fun Node.append(block : TagConsumer<HTMLElement>.() -> Unit) : List<HTMLElement> =
ArrayList<HTMLElement>().let { result ->
(ownerDocument as HTMLDocument).createTree().onFinalize { it, partial -> if (!partial) {result.add(it); appendChild(it) } }.block()
ownerDocument!!.createTree().onFinalize { it, partial -> if (!partial) {result.add(it); appendChild(it) } }.block()

result
}

public val HTMLElement.append : TagConsumer<HTMLElement>
get() = (ownerDocument as HTMLDocument).createTree().onFinalize { element, partial -> if (!partial) { this@append.appendChild(element) } }
get() = ownerDocument!!.createTree().onFinalize { element, partial -> if (!partial) { this@append.appendChild(element) } }
10 changes: 10 additions & 0 deletions js/src/main/kotlin/events.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package html4k.dom

import html4k.Tag
import org.w3c.dom.events.Event

object EventAttribute {
fun set(tag : Tag, name : String, value : (Event) -> Unit) {
tag.consumer.onTagEvent(tag, name, value)
}
}
Loading

0 comments on commit c813f0b

Please sign in to comment.