From ec01200997704e5439bce15510f4aae2ca031343 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 7 Feb 2017 22:19:59 +0300 Subject: [PATCH] Introduce ItemArrayLike interface and replace multiple asList adapters with the single one. --- js/js.libraries/src/dom/Dom.kt | 14 -------- js/js.libraries/src/dom/DomJS.kt | 26 -------------- js/js.libraries/src/dom/ItemArrayLike.kt | 34 +++++++++++++++++++ js/js.libraries/src/generated/org.w3c.dom.kt | 30 ++++++++-------- .../src/generated/org.w3c.files.kt | 6 ++-- 5 files changed, 52 insertions(+), 58 deletions(-) create mode 100644 js/js.libraries/src/dom/ItemArrayLike.kt diff --git a/js/js.libraries/src/dom/Dom.kt b/js/js.libraries/src/dom/Dom.kt index 91e0d06eaac..a0e96448c57 100644 --- a/js/js.libraries/src/dom/Dom.kt +++ b/js/js.libraries/src/dom/Dom.kt @@ -62,12 +62,6 @@ fun Document?.elements(namespaceUri: String, localName: String): List { // END OF DEPRECATED -/** - * Returns a view of this [NodeList] as a list of nodes. - */ -//@Deprecated(W) -public fun NodeList.asList(): List = 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.filterElements(): List { public fun NodeList.filterElements(): List = asList().filterElements() -private class NodeListAsList(private val delegate: NodeList) : AbstractList() { - 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() { override fun get(index: Int): Element { diff --git a/js/js.libraries/src/dom/DomJS.kt b/js/js.libraries/src/dom/DomJS.kt index 60e2fd97b0d..3295cd4708f 100644 --- a/js/js.libraries/src/dom/DomJS.kt +++ b/js/js.libraries/src/dom/DomJS.kt @@ -18,29 +18,3 @@ operator fun Document?.get(selector: String): List { operator fun Element.get(selector: String): List { return querySelectorAll(selector).asList().filterElements() } - -private class HTMLCollectionListView(val collection: HTMLCollection) : AbstractList() { - 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 = HTMLCollectionListView(this) - -private class DOMTokenListView(val delegate: DOMTokenList) : AbstractList() { - 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 = DOMTokenListView(this) diff --git a/js/js.libraries/src/dom/ItemArrayLike.kt b/js/js.libraries/src/dom/ItemArrayLike.kt new file mode 100644 index 00000000000..531d5904810 --- /dev/null +++ b/js/js.libraries/src/dom/ItemArrayLike.kt @@ -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 { + val length: Int + fun item(index: Int): T? +} + +/** + * Returns the view of this `ItemArrayLike` collection as `List` + */ +public fun ItemArrayLike.asList(): List = object : AbstractList() { + 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)") + } +} \ No newline at end of file diff --git a/js/js.libraries/src/generated/org.w3c.dom.kt b/js/js.libraries/src/generated/org.w3c.dom.kt index 7fce03137ff..8fdbd2723af 100644 --- a/js/js.libraries/src/generated/org.w3c.dom.kt +++ b/js/js.libraries/src/generated/org.w3c.dom.kt @@ -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 { + 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 { + 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 { + 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 { + 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 { + override val length: Int + override fun item(index: Int): DOMRect? } @kotlin.internal.InlineOnly inline operator fun DOMRectList.get(index: Int): DOMRect? = asDynamic()[index] diff --git a/js/js.libraries/src/generated/org.w3c.files.kt b/js/js.libraries/src/generated/org.w3c.files.kt index 583d9e0dc92..4cf496dafb9 100644 --- a/js/js.libraries/src/generated/org.w3c.files.kt +++ b/js/js.libraries/src/generated/org.w3c.files.kt @@ -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 { + override open val length: Int + override fun item(index: Int): File? } @kotlin.internal.InlineOnly inline operator fun FileList.get(index: Int): File? = asDynamic()[index]