JS fix stdlib API to conform to new DOM3
This commit is contained in:
committed by
Sergey Mashkov
parent
374dcd56f2
commit
4bc91ba652
@@ -42,122 +42,4 @@ private class HTMLCollectionListView(val collection: HTMLCollection) : AbstractL
|
||||
|
||||
public fun HTMLCollection.asList(): List<HTMLElement> = HTMLCollectionListView(this)
|
||||
|
||||
private class NodeListAsList(val delegate: NodeList) : AbstractList<Node>() {
|
||||
override fun size(): Int = delegate.length
|
||||
|
||||
override fun get(index: Int): Node =
|
||||
if (index in 0..size() - 1) delegate.get(index)!!
|
||||
else throw IndexOutOfBoundsException("index $index is not in range [0 .. ${size() - 1})")
|
||||
}
|
||||
|
||||
public fun NodeList.asList() : List<Node> = NodeListAsList(this)
|
||||
|
||||
/**
|
||||
* Adds CSS class to element. Has no effect if all specified classes are already in class attribute of the element
|
||||
*/
|
||||
public fun Element.addClass(vararg cssClasses: String): Boolean {
|
||||
val missingClasses = cssClasses.filterNot { hasClass(it) }
|
||||
if (missingClasses.isNotEmpty()) {
|
||||
className = StringBuilder {
|
||||
append(className)
|
||||
missingClasses.joinTo(this, " ", " ")
|
||||
}.toString()
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all [cssClasses] from element. Has no effect if all specified classes are missing in class attribute of the element
|
||||
*/
|
||||
public fun Element.removeClass(vararg cssClasses: String): Boolean {
|
||||
if (cssClasses.any { hasClass(it) }) {
|
||||
val toBeRemoved = cssClasses.toSet()
|
||||
className = className.splitWithRegex("\\s+").filter { it !in toBeRemoved }.joinToString(" ")
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds [cssClasses] to element if []condition] is true or removes it otherwise.
|
||||
* Has no effect if class attribute already has corresponding content
|
||||
*/
|
||||
public fun Element.addOrRemoveClassWhen(condition : Boolean, vararg cssClasses : String) {
|
||||
if (condition) {
|
||||
addClass(*cssClasses)
|
||||
} else {
|
||||
removeClass(*cssClasses)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds [trueClassName] class to element if [condition] is true and removes [falseClassName].
|
||||
* If [condition] is false then [trueClassName] will be removed and [falseClassName] will be added.
|
||||
* Has no effect if class attribute already has corresponding content
|
||||
*/
|
||||
public fun Element.swapClassWhen(condition: Boolean, trueClassName: String, falseClassName: String) {
|
||||
if (condition) {
|
||||
addClass(trueClassName)
|
||||
removeClass(falseClassName)
|
||||
} else {
|
||||
removeClass(trueClassName)
|
||||
addClass(falseClassName)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If [condition] is true then [attributeName] with value [attributeValue] will be added to the element, otherwise attribute with
|
||||
* name [attributeName] will be removed
|
||||
*/
|
||||
public fun Element.addOrRemoveAttributeWhen(condition : Boolean, attributeName : String, attributeValue : String = attributeName) {
|
||||
if (condition) {
|
||||
setAttribute(attributeName, attributeValue)
|
||||
} else {
|
||||
removeAttribute(attributeName)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns true if the element has the given CSS class style in its 'class' attribute */
|
||||
public fun Element.hasClass(cssClass: String): Boolean {
|
||||
val c = this.className
|
||||
return c.matches("""(^|.*\s+)$cssClass($|\s+.*)""")
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all child nodes
|
||||
*/
|
||||
public fun Node.clear() {
|
||||
while (hasChildNodes()) {
|
||||
removeChild(firstChild!!)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes this node from parent node. Does nothing if no parent node
|
||||
*/
|
||||
public fun Node.removeFromParent() {
|
||||
parentNode?.removeChild(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* it is *true* when [Node.nodeType] is TEXT_NODE or CDATA_SECTION_NODE
|
||||
*/
|
||||
public val Node.isText : Boolean
|
||||
get() = nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE
|
||||
|
||||
/**
|
||||
* Creates text node and append it to the element
|
||||
*/
|
||||
public fun Element.appendText(text : String, doc : Document = this.ownerDocument!!) {
|
||||
appendChild(doc.createTextNode(text))
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the node to the specified parent element
|
||||
*/
|
||||
public fun Node.appendTo(parent : Element) {
|
||||
parent.appendChild(this)
|
||||
}
|
||||
public fun HTMLCollection?.toElementList() : List<Element> = this?.asList() ?: emptyList()
|
||||
@@ -188,40 +188,22 @@ public native trait HTMLOptionsCollection {
|
||||
|
||||
deprecated("Use org.w3c.dom instead")
|
||||
public native trait HTMLDocument : Document {
|
||||
public native var title: String
|
||||
public native val referrer: String
|
||||
public native val domain: String
|
||||
public native val URL: String
|
||||
public native var body: HTMLElement
|
||||
public native val images: HTMLCollection
|
||||
public native val applets: HTMLCollection
|
||||
public native val links: HTMLCollection
|
||||
public native val forms: HTMLCollection
|
||||
public native val anchors: HTMLCollection
|
||||
public native var cookie: HTMLCollection
|
||||
public native fun open(): Unit
|
||||
public native fun close(): Unit
|
||||
public native fun write(text: String): Unit
|
||||
public native fun writeln(text: String): Unit
|
||||
public native fun getElementsByName(elementName: String): NodeList
|
||||
public native var compatMode: String
|
||||
public native var onload: () -> Unit
|
||||
public native var onunload: () -> Unit
|
||||
}
|
||||
|
||||
deprecated("Use org.w3c.dom instead")
|
||||
public native trait HTMLElement : Element {
|
||||
public native var id: String
|
||||
public native var title: String
|
||||
public native var lang: String
|
||||
public native var dir: String
|
||||
public native var className: String
|
||||
public native var style: CSSStyleDeclaration
|
||||
public native var clientWidth: Double
|
||||
public native var clientHeight: Double
|
||||
public native var clientTop: Double
|
||||
public native var clientLeft: Double
|
||||
public native var innerHTML: String
|
||||
public native var offsetWidth: Double
|
||||
public native var offsetHeight: Double
|
||||
public native var offsetTop: Double
|
||||
|
||||
@@ -17,10 +17,10 @@
|
||||
package kotlin.js.dom.html5
|
||||
|
||||
native
|
||||
public val localStorage: org.w3c.dom3.Storage = noImpl
|
||||
public val localStorage: org.w3c.dom.Storage = noImpl
|
||||
|
||||
native
|
||||
public val sessionStorage: org.w3c.dom3.Storage = noImpl
|
||||
public val sessionStorage: org.w3c.dom.Storage = noImpl
|
||||
|
||||
native
|
||||
deprecated("Use org.w3c.dom")
|
||||
|
||||
@@ -4,9 +4,7 @@ import org.w3c.dom.Document
|
||||
import org.w3c.dom.Node
|
||||
|
||||
deprecated("use org.w3c.dom instead: create document directly via constructor Document() or use document.implementation.createDocument")
|
||||
public fun createDocument(): Document {
|
||||
return kotlin.js.dom.html.document.implementation.createDocument(null, null, null)
|
||||
}
|
||||
public fun createDocument(): Document = Document()
|
||||
|
||||
deprecated("use org.w3c.dom instead")
|
||||
native public val Node.outerHTML: String get() = noImpl
|
||||
|
||||
Reference in New Issue
Block a user