diff --git a/idea/testData/quickfix/autoImports/libraryPropertyJsRuntime.kt.after b/idea/testData/quickfix/autoImports/libraryPropertyJsRuntime.kt.after index 4cbcb3c35cd..16a10b06211 100644 --- a/idea/testData/quickfix/autoImports/libraryPropertyJsRuntime.kt.after +++ b/idea/testData/quickfix/autoImports/libraryPropertyJsRuntime.kt.after @@ -2,7 +2,7 @@ package test -import kotlin.js.dom.html5.localStorage +import kotlin.browser.localStorage fun foo() { localStorage diff --git a/js/js.libraries/src/html5/storage.kt b/js/js.libraries/src/html5/storage.kt deleted file mode 100644 index dfd752a8f84..00000000000 --- a/js/js.libraries/src/html5/storage.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package kotlin.js.dom.html5 - -native -deprecated("Use declaration from kotlin.browser") -public val localStorage: org.w3c.dom.Storage = noImpl - -native -deprecated("Use declaration from kotlin.browser") -public val sessionStorage: org.w3c.dom.Storage = noImpl - -native -deprecated("Use declarations from org.w3c.dom") -public trait Storage { - public val length: Int - get() = noImpl - - public fun key(index: Int): String? = noImpl - - native("getItem") - public fun get(key: String): String? = noImpl - native("setItem") - public fun set(key: String, value: String) {} - - native("removeItem") - public fun remove(key: String) {} - public fun clear() {} -} \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/dom/Dom.kt b/libraries/stdlib/src/kotlin/dom/Dom.kt index 7693cf7e575..d1bad57d82b 100644 --- a/libraries/stdlib/src/kotlin/dom/Dom.kt +++ b/libraries/stdlib/src/kotlin/dom/Dom.kt @@ -46,18 +46,6 @@ public var Element.childrenText: String element.addText(value) } -public var Element.id: String - get() = this.getAttribute("id") ?: "" - set(value) { - this.setAttribute("id", value) - } - -public var Element.style: String - get() = this.getAttribute("style") ?: "" - set(value) { - this.setAttribute("style", value) - } - public var Element.classes: String get() = this.getAttribute("class") ?: "" set(value) { @@ -179,9 +167,13 @@ public fun Element.get(selector: String): List { public fun Element.addClass(vararg cssClasses: String): Boolean { val missingClasses = cssClasses.filterNot { hasClass(it) } if (missingClasses.isNotEmpty()) { + val presentClasses = classes.trim() classes = StringBuilder { - append(classes) - missingClasses.joinTo(this, " ", " ") + append(presentClasses) + if (presentClasses != "") { + append(" ") + } + missingClasses.joinTo(this, " ") }.toString() return true } @@ -195,7 +187,7 @@ public fun Element.addClass(vararg cssClasses: String): Boolean { public fun Element.removeClass(vararg cssClasses: String): Boolean { if (cssClasses.any { hasClass(it) }) { val toBeRemoved = cssClasses.toSet() - classes = classes.split("\\s+").filter { it !in toBeRemoved }.joinToString(" ") + classes = classes.trim().split("\\s+").filter { it !in toBeRemoved }.joinToString(" ") return true } diff --git a/libraries/stdlib/src/kotlin/dom/DomJVM.kt b/libraries/stdlib/src/kotlin/dom/DomJVM.kt index 909f2f2fd2c..198bee65762 100644 --- a/libraries/stdlib/src/kotlin/dom/DomJVM.kt +++ b/libraries/stdlib/src/kotlin/dom/DomJVM.kt @@ -92,6 +92,18 @@ public val CharacterData.length: Int public val NamedNodeMap.length: Int get() = this.getLength() +public var Element.id: String + get() = this.getAttribute("id") ?: "" + set(value) { + this.setAttribute("id", value) + this.setIdAttribute("id", true) + } + +public var Element.style: String + get() = this.getAttribute("style") ?: "" + set(value) { + this.setAttribute("style", value) + } /** * Returns the HTML representation of the node @@ -133,26 +145,6 @@ public var Element.classSet: MutableSet this.classes = value.join(" ") } -/** Adds the given CSS class to this element's 'class' attribute */ -public fun Element.addClass(cssClass: String): Boolean { - val classSet = this.classSet - val answer = classSet.add(cssClass) - if (answer) { - this.classSet = classSet - } - return answer -} - -/** Removes the given CSS class to this element's 'class' attribute */ -public fun Element.removeClass(cssClass: String): Boolean { - val classSet = this.classSet - val answer = classSet.remove(cssClass) - if (answer) { - this.classSet = classSet - } - return answer -} - /** Creates a new document with the given document builder*/ public fun createDocument(builder: DocumentBuilder): Document { diff --git a/libraries/stdlib/test/browser/BrowserTest.kt b/libraries/stdlib/test/browser/BrowserTest.kt index 348fbbcd308..ddb24a92971 100644 --- a/libraries/stdlib/test/browser/BrowserTest.kt +++ b/libraries/stdlib/test/browser/BrowserTest.kt @@ -12,7 +12,7 @@ class BrowserTest { registerBrowserPage() val h1 = document["h1"].first() - assertEquals("Hello World!", h1.text) + assertEquals("Hello World!", h1.textContent) } protected fun registerBrowserPage() { diff --git a/libraries/stdlib/test/dom/DomBuilderTest.kt b/libraries/stdlib/test/dom/DomBuilderTest.kt index c15ecd09e49..55b0bfc1d11 100644 --- a/libraries/stdlib/test/dom/DomBuilderTest.kt +++ b/libraries/stdlib/test/dom/DomBuilderTest.kt @@ -88,7 +88,7 @@ class DomBuilderTest() { } val grandChild = doc["grandChild"].firstOrNull() if (grandChild != null) { - assertEquals("Hello World!", grandChild.text) + assertEquals("Hello World!", grandChild.textContent) assertEquals(" bar tiny", grandChild.attribute("class")) // test the classSet diff --git a/libraries/stdlib/test/dom/DomTest.kt b/libraries/stdlib/test/dom/DomTest.kt index 4acd5e38673..ca502ec7615 100644 --- a/libraries/stdlib/test/dom/DomTest.kt +++ b/libraries/stdlib/test/dom/DomTest.kt @@ -37,7 +37,7 @@ class DomTest { val xml = e.toXmlString() println("element after text ${xml}") - assertEquals("hello", e.text) + assertEquals("hello", e.textContent) } diff --git a/libraries/stdlib/test/js/JsDomTest.kt b/libraries/stdlib/test/js/JsDomTest.kt index 57ec600978d..977926b7574 100644 --- a/libraries/stdlib/test/js/JsDomTest.kt +++ b/libraries/stdlib/test/js/JsDomTest.kt @@ -25,9 +25,7 @@ class JsDomTest { assertCssClass(e, "bar") } - // TODO - not sure why this fails inside JUnit - seems to work fine in QUnit in a browser? - // test - fun addText() { + test fun addText() { var doc = document assertNotNull(doc, "Should have created a document") @@ -37,12 +35,115 @@ class JsDomTest { val xml = e.toXmlString() println("element after text ${xml}") - assertEquals("hello", e.text) - + assertEquals("hello", e.textContent) } + test fun testAddClassMissing() { + val e = document.createElement("e")!! + assertNotNull(e) - fun assertCssClass(e: Element, value: String?): Unit { + e.classes = "class1" + + e.addClass("class1", "class2") + + assertEquals("class1 class2", e.classes) + } + + test fun testAddClassPresent() { + val e = document.createElement("e")!! + assertNotNull(e) + + e.classes = "class2 class1" + + e.addClass("class1", "class2") + + assertEquals("class2 class1", e.classes) + } + + test fun testAddClassUndefinedClasses() { + val e = document.createElement("e")!! + e.addClass("class1") + assertEquals("class1", e.classes) + } + + test fun testRemoveClassMissing() { + val e = document.createElement("e")!! + assertNotNull(e) + + e.classes = "class2 class1" + + e.removeClass("class3") + + assertEquals("class2 class1", e.classes) + } + + test fun testRemoveClassPresent1() { + val e = document.createElement("e")!! + assertNotNull(e) + + e.classes = "class2 class1" + + e.removeClass("class2") + + assertEquals("class1", e.classes) + } + + test fun testRemoveClassPresent2() { + val e = document.createElement("e")!! + assertNotNull(e) + + e.classes = "class2 class1" + + e.removeClass("class1") + + assertEquals("class2", e.classes) + } + + test fun testRemoveClassPresent3() { + val e = document.createElement("e")!! + assertNotNull(e) + + e.classes = "class2 class1 class3" + + e.removeClass("class1") + + assertEquals("class2 class3", e.classes) + } + + test fun testRemoveClassUndefinedClasses() { + val e = document.createElement("e")!! + e.removeClass("class1") + assertEquals("", e.classes) + } + + test fun testRemoveFromParent() { + val doc = document + + val parent = doc.createElement("pp") + val child = doc.createElement("cc") + parent.appendChild(child) + + assertEquals(parent, child.parentNode) + assertEquals(listOf(child), parent.childNodes.asList()) + + child.removeFromParent() + + assertNull(child.parentNode) + assertEquals(emptyList(), parent.childNodes.asList()) + + child.removeFromParent() + assertNull(child.parentNode) + } + + test fun testRemoveFromParentOrphanNode() { + val child = document.createElement("cc") + + child.removeFromParent() + + assertNull(child.parentNode) + } + + private fun assertCssClass(e: Element, value: String?): Unit { val cl = e.classes val cl2 = e.getAttribute("class") ?: "" val xml = e.toXmlString() diff --git a/libraries/tools/idl2k/src/main/kotlin/render.kt b/libraries/tools/idl2k/src/main/kotlin/render.kt index c8b2627ef4a..839339ace8b 100644 --- a/libraries/tools/idl2k/src/main/kotlin/render.kt +++ b/libraries/tools/idl2k/src/main/kotlin/render.kt @@ -145,7 +145,7 @@ fun betterFunction(f1: GenerateFunction, f2: GenerateFunction): GenerateFunction ) private fun Pair.map(block: (F) -> T) = block(first) to block(second) -private fun Pair.betterType() = if (listOf("dynamic", "Any", "Any").any { first.contains(it) }) first else second +private fun Pair.betterType() = if (listOf("dynamic", "Any").any { first.contains(it) }) first else second private fun Pair.betterName() = if (((0..9).map { it.toString() } + listOf("arg")).none { first.toLowerCase().contains(it) }) first else second fun List>.toMultiMap(): Map> = groupBy { it.first }.mapValues { it.value.map { it.second } }