added more working DOM test cases for JS

This commit is contained in:
James Strachan
2012-07-03 19:54:32 +01:00
parent 81c764daf8
commit ffffe84fcb
7 changed files with 87 additions and 26 deletions
+4 -3
View File
@@ -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
@@ -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");
}
@@ -92,6 +92,7 @@ public abstract class Config {
*/
@NotNull
public static final List<String> LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList(
"/stdlib/dom.kt",
"/stdlib/jutil.kt",
"/stdlib/JUMaps.kt",
"/stdlib/test.kt",
+20 -1
View File
@@ -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<Node>, xmlDeclaration: Boolean = false): String {
// TODO this should work...
// return this.map<Node,String>{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
+4 -19
View File
@@ -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<Node>, xmlDeclaration: Boolean = false): String {
// TODO this should work...
// return this.map<Node,String>{it.toXmlString()}.makeString("")
val builder = StringBuilder()
for (n in nodes) {
builder.append(n.toXmlString(xmlDeclaration))
}
return builder.toString().sure()
}
+4 -2
View File
@@ -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\")")
+52
View File
@@ -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\")")
}
}