added the start of an experimental browser API so we can access the browser environment from a typesafe kotlin API which can be implemented on a JVM (e.g. with JavaFX) or compiled natively to JS to reuse the browser global variables
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
package kotlin.browser
|
||||||
|
|
||||||
|
import org.w3c.dom.Document
|
||||||
|
|
||||||
|
private var _document: Document? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides access to the current active browsers DOM for the currently visible page.
|
||||||
|
*/
|
||||||
|
var document: Document
|
||||||
|
get() = _document!!
|
||||||
|
set(value) {
|
||||||
|
_document = value
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
## Browser API
|
||||||
|
|
||||||
|
Provides a simple typesafe API for accessing the browser context which can be used when Kotlin is compiled into JavaScript or when Kotlin is used on a JVM for example using [JavaFX](https://github.com/koolapp/koolapp/tree/master/koolapp-javafx), custom browser widgets such as in SWT or Qt, with remote control browser APIs like GWT or Selenium.
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package test.browser
|
||||||
|
|
||||||
|
import kotlin.dom.*
|
||||||
|
import kotlin.browser.*
|
||||||
|
|
||||||
|
import kotlin.test.*
|
||||||
|
import org.junit.Test as test
|
||||||
|
|
||||||
|
class BrowserTest {
|
||||||
|
|
||||||
|
test fun accessBrowserDOM() {
|
||||||
|
registerBrowserPage()
|
||||||
|
|
||||||
|
val h1 = document["h1"].first()
|
||||||
|
assertEquals("Hello World!", h1.text)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun registerBrowserPage() {
|
||||||
|
// lets simulate being a browser registering its DOM
|
||||||
|
val doc = createDocument()
|
||||||
|
doc.addElement("html") {
|
||||||
|
addElement("body") {
|
||||||
|
addElement("h1") {
|
||||||
|
addText("Hello World!")
|
||||||
|
}
|
||||||
|
addElement("p") {
|
||||||
|
addText("This is some text content")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document = doc
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user