@@ -5,36 +5,41 @@ import kotlinx.html.dom.*
55import org.w3c.dom.*
66import kotlin.reflect.*
77
8- fun <F : Any , T : Any > F.injectTo (bean : T , field : KMutableProperty1 <T , in F >) {
8+ fun <F : Any , T : Any > F.injectTo (bean : T , field : KMutableProperty1 <T , in F >) {
99 field.set(bean, this )
1010}
1111
12- private fun <F : Any , T : Any > F.injectToUnsafe (bean : T , field : KMutableProperty1 <T , out F >) {
12+ private fun <F : Any , T : Any > F.injectToUnsafe (bean : T , field : KMutableProperty1 <T , out F >) {
1313 injectTo(bean, field.asDynamic())
1414}
1515
1616interface InjectCapture
17- class InjectByClassName (val className : String ) : InjectCapture
18- class InjectByTagName (val tagName : String ) : InjectCapture
17+ class InjectByClassName (val className : String ) : InjectCapture
18+ class InjectByTagName (val tagName : String ) : InjectCapture
1919object InjectRoot : InjectCapture
2020interface CustomCapture : InjectCapture {
21- fun apply (element : HTMLElement ) : Boolean
21+ fun apply (element : HTMLElement ): Boolean
2222}
2323
24- class InjectorConsumer <out T : Any >(val downstream : TagConsumer <HTMLElement >, val bean : T , rules : List <Pair <InjectCapture , KMutableProperty1 <T , out HTMLElement >>>) : TagConsumer<HTMLElement> by downstream {
24+ class InjectorConsumer <out T : Any >(
25+ val downstream : TagConsumer <HTMLElement >,
26+ val bean : T ,
27+ rules : List <Pair <InjectCapture , KMutableProperty1 <T , out HTMLElement >>>
28+ ) : TagConsumer<HTMLElement> by downstream {
2529
2630 private val classesMap: Map <String , List <KMutableProperty1 <T , out HTMLElement >>> = rules
27- .filter { it.first is InjectByClassName }
28- .map { it.first as InjectByClassName to it.second }
29- .groupBy ({ it.first.className }, { it.second })
31+ .filter { it.first is InjectByClassName }
32+ .map { it.first as InjectByClassName to it.second }
33+ .groupBy({ it.first.className }, { it.second })
3034
3135 private val tagNamesMap = rules
32- .filter { it.first is InjectByTagName }
33- .map { it.first as InjectByTagName to it.second }
34- .groupBy({ it.first.tagName.toLowerCase() }, { it.second })
36+ .filter { it.first is InjectByTagName }
37+ .map { it.first as InjectByTagName to it.second }
38+ .groupBy({ it.first.tagName.toLowerCase() }, { it.second })
3539
3640 private val rootCaptures = rules.filter { it.first == InjectRoot }.map { it.second }
37- private val customCaptures = rules.filter {it.first is CustomCapture }.map {it.first as CustomCapture to it.second}
41+ private val customCaptures =
42+ rules.filter { it.first is CustomCapture }.map { it.first as CustomCapture to it.second }
3843
3944 override fun onTagEnd (tag : Tag ) {
4045 downstream.onTagEnd(tag)
@@ -53,7 +58,7 @@ class InjectorConsumer<out T: Any>(val downstream : TagConsumer<HTMLElement>, va
5358 }
5459 }
5560
56- customCaptures.filter { it.first.apply (node) }.map {it.second}.forEach { field ->
61+ customCaptures.filter { it.first.apply (node) }.map { it.second }.forEach { field ->
5762 node.injectToUnsafe(bean, field)
5863 }
5964 }
@@ -68,10 +73,16 @@ class InjectorConsumer<out T: Any>(val downstream : TagConsumer<HTMLElement>, va
6873 }
6974}
7075
71- fun <T : Any > TagConsumer<HTMLElement>.inject (bean : T , rules : List <Pair <InjectCapture , KMutableProperty1 <T , out HTMLElement >>>) : TagConsumer <HTMLElement > = InjectorConsumer (this , bean, rules)
76+ fun <T : Any > TagConsumer<HTMLElement>.inject (
77+ bean : T ,
78+ rules : List <Pair <InjectCapture , KMutableProperty1 <T , out HTMLElement >>>
79+ ): TagConsumer <HTMLElement > = InjectorConsumer (this , bean, rules)
7280
73- fun <T : Any > HTMLElement.appendAndInject (bean : T , rules : List <Pair <InjectCapture , KMutableProperty1 <T , out HTMLElement >>>, block : TagConsumer <HTMLElement >.() -> Unit ) : List <HTMLElement > = append {
81+ fun <T : Any > HTMLElement.appendAndInject (
82+ bean : T ,
83+ rules : List <Pair <InjectCapture , KMutableProperty1 <T , out HTMLElement >>>,
84+ block : TagConsumer <HTMLElement >.() -> Unit
85+ ): List <HTMLElement > = append {
7486 InjectorConsumer (this @append, bean, rules).block()
7587 Unit
7688}
77-
0 commit comments