diff --git a/js/js.libraries/src/core/dom.kt b/js/js.libraries/src/core/dom.kt index ede8fd82827..0e6b77e0f65 100644 --- a/js/js.libraries/src/core/dom.kt +++ b/js/js.libraries/src/core/dom.kt @@ -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 } diff --git a/js/js.translator/testFiles/kotlin_lib.js b/js/js.translator/testFiles/kotlin_lib.js index 17541e01fbc..625c6809031 100644 --- a/js/js.translator/testFiles/kotlin_lib.js +++ b/js/js.translator/testFiles/kotlin_lib.js @@ -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) { diff --git a/libraries/pom.xml b/libraries/pom.xml index 5690447ac4f..d8ff914ba60 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -65,8 +65,12 @@ examples/kotlin-java-example + diff --git a/libraries/stdlib/src/kotlin/dom/Dom.kt b/libraries/stdlib/src/kotlin/dom/Dom.kt index 1a9093166c7..8cb94ebb9f3 100644 --- a/libraries/stdlib/src/kotlin/dom/Dom.kt +++ b/libraries/stdlib/src/kotlin/dom/Dom.kt @@ -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() { class ElementListAsList(val nodeList: NodeList): AbstractList() { 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() { /** 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 diff --git a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt index ce512009d96..e69d56f7661 100644 --- a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt +++ b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt @@ -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("") }