JS tests for addClass, removeClass, removeFromParent
This commit is contained in:
committed by
Sergey Mashkov
parent
bcbfa3c240
commit
a11e0a84b5
@@ -2,7 +2,7 @@
|
||||
|
||||
package test
|
||||
|
||||
import kotlin.js.dom.html5.localStorage
|
||||
import kotlin.browser.localStorage
|
||||
|
||||
fun foo() {
|
||||
localStorage
|
||||
|
||||
@@ -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() {}
|
||||
}
|
||||
@@ -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<Element> {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String>
|
||||
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 {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -37,7 +37,7 @@ class DomTest {
|
||||
val xml = e.toXmlString()
|
||||
println("element after text ${xml}")
|
||||
|
||||
assertEquals("hello", e.text)
|
||||
assertEquals("hello", e.textContent)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Node>(), 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()
|
||||
|
||||
@@ -145,7 +145,7 @@ fun betterFunction(f1: GenerateFunction, f2: GenerateFunction): GenerateFunction
|
||||
)
|
||||
|
||||
private fun <F, T> Pair<F, F>.map(block: (F) -> T) = block(first) to block(second)
|
||||
private fun Pair<String, String>.betterType() = if (listOf("dynamic", "Any", "Any").any { first.contains(it) }) first else second
|
||||
private fun Pair<String, String>.betterType() = if (listOf("dynamic", "Any").any { first.contains(it) }) first else second
|
||||
private fun Pair<String, String>.betterName() = if (((0..9).map { it.toString() } + listOf("arg")).none { first.toLowerCase().contains(it) }) first else second
|
||||
|
||||
fun <K, V> List<Pair<K, V>>.toMultiMap(): Map<K, List<V>> = groupBy { it.first }.mapValues { it.value.map { it.second } }
|
||||
|
||||
Reference in New Issue
Block a user