Introduce ItemArrayLike interface and replace multiple asList adapters with the single one.

This commit is contained in:
Ilya Gorbunov
2017-02-07 22:19:59 +03:00
parent 41c980bcab
commit ec01200997
5 changed files with 52 additions and 58 deletions
-14
View File
@@ -62,12 +62,6 @@ fun Document?.elements(namespaceUri: String, localName: String): List<Element> {
// END OF DEPRECATED
/**
* Returns a view of this [NodeList] as a list of nodes.
*/
//@Deprecated(W)
public fun NodeList.asList(): List<Node> = NodeListAsList(this)
/**
* Returns a view of this [NodeList] as a list of elements assuming that it contains only elements.
*
@@ -95,14 +89,6 @@ public fun List<Node>.filterElements(): List<Element> {
public fun NodeList.filterElements(): List<Element> = asList().filterElements()
private class NodeListAsList(private val delegate: NodeList) : AbstractList<Node>() {
override val size: Int get() = delegate.length
override fun get(index: Int): Node = when {
index in 0..size - 1 -> delegate.item(index)!!
else -> throw IndexOutOfBoundsException("index $index is not in range [0 .. ${size - 1})")
}
}
private class ElementListAsList(private val nodeList: NodeList) : AbstractList<Element>() {
override fun get(index: Int): Element {
-26
View File
@@ -18,29 +18,3 @@ operator fun Document?.get(selector: String): List<Element> {
operator fun Element.get(selector: String): List<Element> {
return querySelectorAll(selector).asList().filterElements()
}
private class HTMLCollectionListView(val collection: HTMLCollection) : AbstractList<HTMLElement>() {
override val size: Int get() = collection.length
override fun get(index: Int): HTMLElement =
when {
index in 0..size - 1 -> collection.item(index) as HTMLElement
else -> throw IndexOutOfBoundsException("index $index is not in range [0 .. ${size - 1})")
}
}
//@Deprecated(W)
public fun HTMLCollection.asList(): List<HTMLElement> = HTMLCollectionListView(this)
private class DOMTokenListView(val delegate: DOMTokenList) : AbstractList<String>() {
override val size: Int get() = delegate.length
override fun get(index: Int) =
when {
index in 0..size - 1 -> delegate.item(index)!!
else -> throw IndexOutOfBoundsException("index $index is not in range [0 .. ${size - 1})")
}
}
//@Deprecated(W)
public fun DOMTokenList.asList(): List<String> = DOMTokenListView(this)
+34
View File
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2017 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 org.w3c.dom
public external interface ItemArrayLike<out T> {
val length: Int
fun item(index: Int): T?
}
/**
* Returns the view of this `ItemArrayLike<T>` collection as `List<T>`
*/
public fun <T> ItemArrayLike<T>.asList(): List<T> = object : AbstractList<T>() {
override val size: Int get() = this@asList.length
override fun get(index: Int): T = when {
index in 0..lastIndex -> this@asList.item(index) as T
else -> throw IndexOutOfBoundsException("index $index is not in range [0..$lastIndex)")
}
}
+15 -15
View File
@@ -2499,15 +2499,15 @@ public external interface Slotable {
val assignedSlot: HTMLSlotElement?
}
public external abstract class NodeList {
open val length: Int
fun item(index: Int): Node?
public external abstract class NodeList : ItemArrayLike<Node> {
override open val length: Int
override fun item(index: Int): Node?
}
@kotlin.internal.InlineOnly inline operator fun NodeList.get(index: Int): Node? = asDynamic()[index]
public external abstract class HTMLCollection : UnionElementOrHTMLCollection {
open val length: Int
fun item(index: Int): Element?
public external abstract class HTMLCollection : UnionElementOrHTMLCollection, ItemArrayLike<Element> {
override open val length: Int
override fun item(index: Int): Element?
fun namedItem(name: String): Element?
}
@kotlin.internal.InlineOnly inline operator fun HTMLCollection.get(index: Int): Element? = asDynamic()[index]
@@ -2775,9 +2775,9 @@ public inline fun ShadowRootInit(mode: ShadowRootMode?): ShadowRootInit {
return o
}
public external abstract class NamedNodeMap {
open val length: Int
fun item(index: Int): Attr?
public external abstract class NamedNodeMap : ItemArrayLike<Attr> {
override open val length: Int
override fun item(index: Int): Attr?
fun getNamedItem(qualifiedName: String): Attr?
fun getNamedItemNS(namespace: String?, localName: String): Attr?
fun setNamedItem(attr: Attr): Attr?
@@ -2935,10 +2935,10 @@ public external interface NodeFilter {
}
}
public external abstract class DOMTokenList {
open val length: Int
public external abstract class DOMTokenList : ItemArrayLike<String> {
override open val length: Int
open var value: String
fun item(index: Int): String?
override fun item(index: Int): String?
fun contains(token: String): Boolean
fun add(vararg tokens: String): Unit
fun remove(vararg tokens: String): Unit
@@ -3037,9 +3037,9 @@ public inline fun DOMRectInit(x: Double? = 0.0, y: Double? = 0.0, width: Double?
return o
}
public external interface DOMRectList {
val length: Int
fun item(index: Int): DOMRect?
public external interface DOMRectList : ItemArrayLike<DOMRect> {
override val length: Int
override fun item(index: Int): DOMRect?
}
@kotlin.internal.InlineOnly inline operator fun DOMRectList.get(index: Int): DOMRect? = asDynamic()[index]
@@ -66,9 +66,9 @@ public inline fun FilePropertyBag(lastModified: Int? = null, type: String? = "")
return o
}
public external abstract class FileList {
open val length: Int
fun item(index: Int): File?
public external abstract class FileList : ItemArrayLike<File> {
override open val length: Int
override fun item(index: Int): File?
}
@kotlin.internal.InlineOnly inline operator fun FileList.get(index: Int): File? = asDynamic()[index]