generate the Node.ELEMENT_NODE fields on the JS DOM API and refactored the kotlin.dom API so that it uses the Node.getNodeType() API rather than using "is" which is troublesome to implement on a browser/JS

This commit is contained in:
James Strachan
2012-05-31 09:08:12 +01:00
parent 00cf06e4f3
commit fe68aeaa78
5 changed files with 96 additions and 52 deletions
+36 -6
View File
@@ -15,9 +15,9 @@ native public trait Attr: Node {
fun getName(): String = js.noImpl
fun getValue(): String = js.noImpl
fun setValue(arg1: String): Unit = js.noImpl
fun getSchemaTypeInfo(): TypeInfo = js.noImpl
fun getSpecified(): Boolean = js.noImpl
fun getOwnerElement(): Element = js.noImpl
fun getSchemaTypeInfo(): TypeInfo = js.noImpl
fun isId(): Boolean = js.noImpl
}
@@ -44,6 +44,8 @@ native public trait Document: Node {
fun getXmlEncoding(): String = js.noImpl
fun createElement(arg1: String): Element = js.noImpl
fun createComment(arg1: String): Comment = js.noImpl
fun getElementsByTagName(arg1: String): NodeList = js.noImpl
fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl
fun getDoctype(): DocumentType = js.noImpl
fun getDocumentElement(): Element = js.noImpl
fun createDocumentFragment(): DocumentFragment = js.noImpl
@@ -52,11 +54,9 @@ native public trait Document: Node {
fun createProcessingInstruction(arg1: String, arg2: String): ProcessingInstruction = js.noImpl
fun createAttribute(arg1: String): Attr = js.noImpl
fun createEntityReference(arg1: String): EntityReference = js.noImpl
fun getElementsByTagName(arg1: String): NodeList = js.noImpl
fun importNode(arg1: Node, arg2: Boolean): Node = js.noImpl
fun createElementNS(arg1: String, arg2: String): Element = js.noImpl
fun createAttributeNS(arg1: String, arg2: String): Attr = js.noImpl
fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl
fun getElementById(arg1: String): Element = js.noImpl
fun getXmlStandalone(): Boolean = js.noImpl
fun setXmlStandalone(arg1: Boolean): Unit = js.noImpl
@@ -98,6 +98,9 @@ native public trait DOMError {
fun getSeverity(): Short = js.noImpl
fun getRelatedException(): Any = js.noImpl
fun getRelatedData(): Any = js.noImpl
public val SEVERITY_WARNING: Short = 1
public val SEVERITY_ERROR: Short = 2
public val SEVERITY_FATAL_ERROR: Short = 3
}
native public trait DOMErrorHandler {
@@ -129,21 +132,21 @@ native public trait DOMStringList {
native public trait Element: Node {
fun getAttribute(arg1: String): String = js.noImpl
fun setAttribute(arg1: String, arg2: String): Unit = js.noImpl
fun getSchemaTypeInfo(): TypeInfo = js.noImpl
fun getElementsByTagName(arg1: String): NodeList = js.noImpl
fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl
fun getTagName(): String = js.noImpl
fun removeAttribute(arg1: String): Unit = js.noImpl
fun getAttributeNode(arg1: String): Attr = js.noImpl
fun setAttributeNode(arg1: Attr): Attr = js.noImpl
fun removeAttributeNode(arg1: Attr): Attr = js.noImpl
fun getElementsByTagName(arg1: String): NodeList = js.noImpl
fun getAttributeNS(arg1: String, arg2: String): String = js.noImpl
fun setAttributeNS(arg1: String, arg2: String, arg3: String): Unit = js.noImpl
fun removeAttributeNS(arg1: String, arg2: String): Unit = js.noImpl
fun getAttributeNodeNS(arg1: String, arg2: String): Attr = js.noImpl
fun setAttributeNodeNS(arg1: Attr): Attr = js.noImpl
fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl
fun hasAttribute(arg1: String): Boolean = js.noImpl
fun hasAttributeNS(arg1: String, arg2: String): Boolean = js.noImpl
fun getSchemaTypeInfo(): TypeInfo = js.noImpl
fun setIdAttribute(arg1: String, arg2: Boolean): Unit = js.noImpl
fun setIdAttributeNS(arg1: String, arg2: String, arg3: Boolean): Unit = js.noImpl
fun setIdAttributeNode(arg1: Attr, arg2: Boolean): Unit = js.noImpl
@@ -218,6 +221,24 @@ native public trait Node {
fun isDefaultNamespace(arg1: String): Boolean = js.noImpl
fun lookupNamespaceURI(arg1: String): String = js.noImpl
fun isEqualNode(arg1: Node): Boolean = js.noImpl
public val ELEMENT_NODE: Short = 1
public val ATTRIBUTE_NODE: Short = 2
public val TEXT_NODE: Short = 3
public val CDATA_SECTION_NODE: Short = 4
public val ENTITY_REFERENCE_NODE: Short = 5
public val ENTITY_NODE: Short = 6
public val PROCESSING_INSTRUCTION_NODE: Short = 7
public val COMMENT_NODE: Short = 8
public val DOCUMENT_NODE: Short = 9
public val DOCUMENT_TYPE_NODE: Short = 10
public val DOCUMENT_FRAGMENT_NODE: Short = 11
public val NOTATION_NODE: Short = 12
public val DOCUMENT_POSITION_DISCONNECTED: Short = 1
public val DOCUMENT_POSITION_PRECEDING: Short = 2
public val DOCUMENT_POSITION_FOLLOWING: Short = 4
public val DOCUMENT_POSITION_CONTAINS: Short = 8
public val DOCUMENT_POSITION_CONTAINED_BY: Short = 16
public val DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: Short = 32
}
native public trait NodeList {
@@ -247,9 +268,18 @@ native public trait TypeInfo {
fun getTypeName(): String = js.noImpl
fun getTypeNamespace(): String = js.noImpl
fun isDerivedFrom(arg1: String, arg2: String, arg3: Int): Boolean = js.noImpl
public val DERIVATION_RESTRICTION: Int = 1
public val DERIVATION_EXTENSION: Int = 2
public val DERIVATION_UNION: Int = 4
public val DERIVATION_LIST: Int = 8
}
native public trait UserDataHandler {
fun handle(arg1: Short, arg2: String, arg3: Any, arg4: Node, arg5: Node): Unit = js.noImpl
public val NODE_CLONED: Short = 1
public val NODE_IMPORTED: Short = 2
public val NODE_DELETED: Short = 3
public val NODE_RENAMED: Short = 4
public val NODE_ADOPTED: Short = 5
}
+3
View File
@@ -122,6 +122,9 @@ var Kotlin;
})());
var isType = function (object, klass) {
if (object === null) {
return false;
}
var current = object.get_class();
while (current !== klass) {
if (current === null) {
+4
View File
@@ -65,8 +65,12 @@
<module>examples/kotlin-java-example</module>
<!--
TODO - disabled until the JS compiler can handle "Node.ELEMENT_NODE" expressions in DOM
<module>examples/js-example</module>
<module>examples/browser-example</module>
-->
</modules>
<dependencies>
+32 -46
View File
@@ -17,49 +17,42 @@ get() = if (this != null) this.getDocumentElement() else null
var Node.text : String
get() {
if (this is Element) {
return this.text
if (this.getNodeType() == Node.ELEMENT_NODE) {
val buffer = StringBuilder()
val nodeList = this.getChildNodes()
if (nodeList != null) {
var i = 0
val size = nodeList.getLength()
while (i < size) {
val node = nodeList.item(i)
if (node != null) {
if (node.isText()) {
buffer.append(node.getNodeValue())
}
}
i++
}
}
return buffer.toString().sure()
} else {
return this.getNodeValue() ?: ""
}
}
set(value) {
if (this is Element) {
this.text = value
if (this.getNodeType() == Node.ELEMENT_NODE) {
val element = this as Element
// lets remove all the previous text nodes first
for (node in element.children()) {
if (node.isText()) {
removeChild(node)
}
}
element.addText(value)
} else {
this.setNodeValue(value)
}
}
var Element.text : String
get() {
val buffer = StringBuilder()
val nodeList = this.getChildNodes()
if (nodeList != null) {
var i = 0
val size = nodeList.getLength()
while (i < size) {
val node = nodeList.item(i)
if (node != null) {
if (node.isText()) {
buffer.append(node.getNodeValue())
}
}
i++
}
}
return buffer.toString().sure()
}
set(value) {
// lets remove all the previous text nodes first
for (node in children()) {
if (node.isText()) {
removeChild(node)
}
}
addText(value)
}
var Element.id : String
get() = this.getAttribute("id")?: ""
set(value) {
@@ -188,14 +181,12 @@ class NodeListAsList(val nodeList: NodeList): AbstractList<Node>() {
class ElementListAsList(val nodeList: NodeList): AbstractList<Element>() {
override fun get(index: Int): Element {
val node = nodeList.item(index)
if (node is Element) {
return node
if (node == null) {
throw IndexOutOfBoundsException("NodeList does not contain a node at index: " + index)
} else if (node.getNodeType() == Node.ELEMENT_NODE) {
return node as Element
} else {
if (node == null) {
throw IndexOutOfBoundsException("NodeList does not contain a node at index: " + index)
} else {
throw IllegalArgumentException("Node is not an Element as expected but is $node")
}
throw IllegalArgumentException("Node is not an Element as expected but is $node")
}
}
@@ -239,13 +230,8 @@ class PreviousSiblingIterator(var node: Node) : AbstractIterator<Node>() {
/** Returns true if this node is a Text node or a CDATA node */
fun Node.isText(): Boolean {
/*
This code is easier to convert to JS
val nodeType = getNodeType()
return nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE
*/
return this is Text || this is CDATASection
}
/** Returns the attribute value or empty string if its not present */
@@ -309,7 +295,7 @@ fun Element.createElement(name: String, doc: Document? = null, init: Element.()-
/** Returns the owner document of the element or uses the provided document */
fun Node.ownerDocument(doc: Document? = null): Document {
val answer = if (this is Document) this as Document
val answer = if (this != null && this.getNodeType() == Node.DOCUMENT_NODE) this as Document
else if (doc == null) this.getOwnerDocument()
else doc
@@ -4,6 +4,7 @@ import java.io.File
import java.io.FileWriter
import java.io.PrintWriter
import org.w3c.dom.*
import java.lang.reflect.Modifier
/**
* This tool generates JavaScript stubs for classes available in the JDK which are already available in the browser environment
@@ -67,6 +68,26 @@ import js.noImpl
}
}
}
val fields = klass.getDeclaredFields()
if (fields != null) {
for (field in fields) {
if (field != null) {
val modifiers = field.getModifiers()
if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) {
try {
val value = field.get(null)
if (value != null) {
val fieldType = simpleTypeName(field.getType())
println(" public val ${field.getName()}: $fieldType = $value")
}
} catch (e: Exception) {
println("Caught: $e")
e.printStackTrace()
}
}
}
}
}
println("}")
println("")
}