diff --git a/js/js.libraries/src/stdlib/dom.kt b/js/js.libraries/src/stdlib/dom.kt index 991ddebc999..325cd39a9ce 100644 --- a/js/js.libraries/src/stdlib/dom.kt +++ b/js/js.libraries/src/stdlib/dom.kt @@ -11,6 +11,7 @@ native public val Node.outerHTML: String get() = js.noImpl /** Converts the node to an XML String */ -public fun Node.toXmlString(xmlDeclaration: Boolean = false): String { - return this.outerHTML -} +public fun Node.toXmlString(): String = this.outerHTML + +/** Converts the node to an XML String */ +public fun Node.toXmlString(xmlDeclaration: Boolean): String = this.outerHTML diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java index 8b86362b056..dba64def0f4 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java @@ -24,8 +24,9 @@ public class StdLibTestToJSTest extends StdLibTestSupport { public void testGenerateTestCase() throws Exception { generateJavaScriptFiles(EcmaVersion.all(), "libraries/stdlib/test", - //"dom/DomTest.kt", + "dom/DomTest.kt", "js/MapTest.kt", + "js/JsDomTest.kt", "ListTest.kt", "StringTest.kt"); } diff --git a/js/js.translator/src/org/jetbrains/k2js/config/Config.java b/js/js.translator/src/org/jetbrains/k2js/config/Config.java index fd7c43ae621..6692ce5ba2c 100644 --- a/js/js.translator/src/org/jetbrains/k2js/config/Config.java +++ b/js/js.translator/src/org/jetbrains/k2js/config/Config.java @@ -92,6 +92,7 @@ public abstract class Config { */ @NotNull public static final List LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList( + "/stdlib/dom.kt", "/stdlib/jutil.kt", "/stdlib/JUMaps.kt", "/stdlib/test.kt", diff --git a/libraries/stdlib/src/kotlin/dom/Dom.kt b/libraries/stdlib/src/kotlin/dom/Dom.kt index 923c47c00f3..226bf17a042 100644 --- a/libraries/stdlib/src/kotlin/dom/Dom.kt +++ b/libraries/stdlib/src/kotlin/dom/Dom.kt @@ -311,6 +311,25 @@ val NodeList?.last : Node? get() = this.tail +/** Converts the node list to an XML String */ +fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String { + return if (this == null) + "" else { + nodesToXmlString(this.toList(), xmlDeclaration) + } +} + +/** Converts the collection of nodes to an XML String */ +public fun nodesToXmlString(nodes: java.lang.Iterable, xmlDeclaration: Boolean = false): String { + // TODO this should work... + // return this.map{it.toXmlString()}.makeString("") + val builder = StringBuilder() + for (n in nodes) { + builder.append(n.toXmlString(xmlDeclaration)) + } + return builder.toString().sure() +} + // Syntax sugar inline fun Node.plus(child: Node?): Node { @@ -381,7 +400,7 @@ Adds a newly created text node to an element which either already has an owner D */ fun Element.addText(text: String?, doc: Document? = null): Element { if (text != null) { - val child = ownerDocument(doc).createTextNode(text) + val child = this.ownerDocument(doc).createTextNode(text) this.appendChild(child) } return this diff --git a/libraries/stdlib/src/kotlin/dom/DomJVM.kt b/libraries/stdlib/src/kotlin/dom/DomJVM.kt index e7b5cad317e..42d402dad8c 100644 --- a/libraries/stdlib/src/kotlin/dom/DomJVM.kt +++ b/libraries/stdlib/src/kotlin/dom/DomJVM.kt @@ -153,13 +153,6 @@ fun Element.removeClass(cssClass: String): Boolean { } -/** Converts the node list to an XML String */ -fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String { - return if (this == null) - "" else { - nodesToXmlString(this.toList(), xmlDeclaration) - } -} /** Creates a new document with the given document builder*/ public fun createDocument(builder: DocumentBuilder): Document { @@ -225,7 +218,10 @@ public fun createTransformer(source: Source? = null, factory: TransformerFactory } /** Converts the node to an XML String */ -public fun Node.toXmlString(xmlDeclaration: Boolean = false): String { +public fun Node.toXmlString(): String = toXmlString(false) + +/** Converts the node to an XML String */ +public fun Node.toXmlString(xmlDeclaration: Boolean): String { val writer = StringWriter() writeXmlString(writer, xmlDeclaration) return writer.toString().sure() @@ -237,14 +233,3 @@ public fun Node.writeXmlString(writer: Writer, xmlDeclaration: Boolean): Unit { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, if (xmlDeclaration) "no" else "yes") transformer.transform(DOMSource(this), StreamResult(writer)) } - -/** Converts the collection of nodes to an XML String */ -public fun nodesToXmlString(nodes: java.lang.Iterable, xmlDeclaration: Boolean = false): String { - // TODO this should work... - // return this.map{it.toXmlString()}.makeString("") - val builder = StringBuilder() - for (n in nodes) { - builder.append(n.toXmlString(xmlDeclaration)) - } - return builder.toString().sure() -} \ No newline at end of file diff --git a/libraries/stdlib/test/dom/DomTest.kt b/libraries/stdlib/test/dom/DomTest.kt index 5a0ae364c23..3cc7511aabb 100644 --- a/libraries/stdlib/test/dom/DomTest.kt +++ b/libraries/stdlib/test/dom/DomTest.kt @@ -34,7 +34,8 @@ class DomTest { val e = doc.createElement("foo")!! e + "hello" - println("element after text ${e.toXmlString()}") + val xml = e.toXmlString() + println("element after text ${xml}") assertEquals("hello", e.text) @@ -44,7 +45,8 @@ class DomTest { fun assertCssClass(e: Element, value: String?): Unit { val cl = e.classes val cl2 = e.getAttribute("class") - println("element ${e.toXmlString()} has cssClass `${cl}` class attr `${cl2}`") + val xml = e.toXmlString() + println("element ${xml} has cssClass `${cl}` class attr `${cl2}`") assertEquals(value, cl, "value of element.cssClass") assertEquals(value, cl2, "value of element.getAttribute(\"class\")") diff --git a/libraries/stdlib/test/js/JsDomTest.kt b/libraries/stdlib/test/js/JsDomTest.kt new file mode 100644 index 00000000000..5c1a3e2604e --- /dev/null +++ b/libraries/stdlib/test/js/JsDomTest.kt @@ -0,0 +1,52 @@ +package test.js + +import kotlin.* +import kotlin.browser.* +import kotlin.dom.* +import kotlin.test.* +import org.w3c.dom.* +import org.junit.Test as test + +class JsDomTest { + + test fun testCreateDocument() { + var doc = document + assertNotNull(doc, "Should have created a document") + + val e = doc.createElement("foo")!! + assertCssClass(e, "") + + // now lets update the cssClass property + e.classes = "foo" + assertCssClass(e, "foo") + + // now using the attribute directly + e.setAttribute("class", "bar") + assertCssClass(e, "bar") + } + + test fun addText() { + var doc = document + assertNotNull(doc, "Should have created a document") + + val e = doc.createElement("foo")!! + e + "hello" + + val xml = e.toXmlString() + println("element after text ${xml}") + + assertEquals("hello", e.text) + + } + + + fun assertCssClass(e: Element, value: String?): Unit { + val cl = e.classes + val cl2 = e.getAttribute("class") ?: "" + val xml = e.toXmlString() + println("element ${xml} has cssClass `${cl}` class attr `${cl2}`") + + assertEquals(value, cl, "value of element.cssClass") + assertEquals(value, cl2, "value of element.getAttribute(\"class\")") + } +}