JS tests for addClass, removeClass, removeFromParent

This commit is contained in:
Sergey Mashkov
2015-05-12 14:34:19 +03:00
committed by Sergey Mashkov
parent bcbfa3c240
commit a11e0a84b5
9 changed files with 131 additions and 89 deletions
+7 -15
View File
@@ -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
}
+12 -20
View File
@@ -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 {