From bedadb14723d028b82c9564559299bdd95cd44af Mon Sep 17 00:00:00 2001 From: James Strachan Date: Thu, 24 May 2012 09:56:57 +0100 Subject: [PATCH] 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 --- .../stdlib/src/kotlin/browser/Properties.kt | 14 ++++++++ libraries/stdlib/src/kotlin/browser/ReadMe.md | 3 ++ libraries/stdlib/test/browser/BrowserTest.kt | 33 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 libraries/stdlib/src/kotlin/browser/Properties.kt create mode 100644 libraries/stdlib/src/kotlin/browser/ReadMe.md create mode 100644 libraries/stdlib/test/browser/BrowserTest.kt diff --git a/libraries/stdlib/src/kotlin/browser/Properties.kt b/libraries/stdlib/src/kotlin/browser/Properties.kt new file mode 100644 index 00000000000..5c2061f298b --- /dev/null +++ b/libraries/stdlib/src/kotlin/browser/Properties.kt @@ -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 + } \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/browser/ReadMe.md b/libraries/stdlib/src/kotlin/browser/ReadMe.md new file mode 100644 index 00000000000..c8d6f392f31 --- /dev/null +++ b/libraries/stdlib/src/kotlin/browser/ReadMe.md @@ -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. diff --git a/libraries/stdlib/test/browser/BrowserTest.kt b/libraries/stdlib/test/browser/BrowserTest.kt new file mode 100644 index 00000000000..348fbbcd308 --- /dev/null +++ b/libraries/stdlib/test/browser/BrowserTest.kt @@ -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 + } +} \ No newline at end of file