diff --git a/libraries/stdlib/js/src/kotlin/browser/declarations.kt b/libraries/stdlib/js/src/kotlin/browser/declarations.kt index 83177cb6a36..b590a266191 100644 --- a/libraries/stdlib/js/src/kotlin/browser/declarations.kt +++ b/libraries/stdlib/js/src/kotlin/browser/declarations.kt @@ -6,12 +6,33 @@ package kotlin.browser import org.w3c.dom.* +import kotlin.internal.LowPriorityInOverloadResolution +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.browser.window' instead.", + replaceWith = ReplaceWith("window", "kotlinx.browser.window") +) +@LowPriorityInOverloadResolution public external val window: Window +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.browser.document' instead.", + replaceWith = ReplaceWith("document", "kotlinx.browser.document") +) +@LowPriorityInOverloadResolution public external val document: Document +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.browser.localStorage' instead.", + replaceWith = ReplaceWith("localStorage", "kotlinx.browser.localStorage") +) +@LowPriorityInOverloadResolution public external val localStorage: Storage +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.browser.sessionStorage' instead.", + replaceWith = ReplaceWith("sessionStorage", "kotlinx.browser.sessionStorage") +) +@LowPriorityInOverloadResolution public external val sessionStorage: Storage diff --git a/libraries/stdlib/js/src/kotlin/dom/Builders.kt b/libraries/stdlib/js/src/kotlin/dom/Builders.kt index d6b33089e3f..20944865cb3 100644 --- a/libraries/stdlib/js/src/kotlin/dom/Builders.kt +++ b/libraries/stdlib/js/src/kotlin/dom/Builders.kt @@ -5,20 +5,33 @@ package kotlin.dom -import org.w3c.dom.* +import org.w3c.dom.Document +import org.w3c.dom.Element +import kotlin.internal.LowPriorityInOverloadResolution +import kotlinx.dom.appendElement as newAppendElement +import kotlinx.dom.createElement as newCreateElement /** * Creates a new element with the specified [name]. * * The element is initialized with the speicifed [init] function. */ -public fun Document.createElement(name: String, init: Element.() -> Unit): Element = createElement(name).apply(init) +@LowPriorityInOverloadResolution +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.dom.createElement' instead.", + replaceWith = ReplaceWith("this.createElement(name, init)", "kotlinx.dom.createElement") +) +public inline fun Document.createElement(name: String, noinline init: Element.() -> Unit): Element = this.newCreateElement(name, init) /** * Appends a newly created element with the specified [name] to this element. * * The element is initialized with the speicifed [init] function. */ -public fun Element.appendElement(name: String, init: Element.() -> Unit): Element = - ownerDocument!!.createElement(name, init).also { appendChild(it) } +@LowPriorityInOverloadResolution +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.dom.appendElement' instead.", + replaceWith = ReplaceWith("this.appendElement(name, init)", "kotlinx.dom.appendElement") +) +public inline fun Element.appendElement(name: String, noinline init: Element.() -> Unit): Element = this.newAppendElement(name, init) diff --git a/libraries/stdlib/js/src/kotlin/dom/Classes.kt b/libraries/stdlib/js/src/kotlin/dom/Classes.kt index 9007132736f..b326a21392e 100644 --- a/libraries/stdlib/js/src/kotlin/dom/Classes.kt +++ b/libraries/stdlib/js/src/kotlin/dom/Classes.kt @@ -5,44 +5,40 @@ package kotlin.dom -import org.w3c.dom.* +import org.w3c.dom.Element +import kotlin.internal.LowPriorityInOverloadResolution +import kotlinx.dom.addClass as newAddClass +import kotlinx.dom.hasClass as newHasClass +import kotlinx.dom.removeClass as newRemoveClass /** Returns true if the element has the given CSS class style in its 'class' attribute */ -fun Element.hasClass(cssClass: String): Boolean = className.matches("""(^|.*\s+)$cssClass($|\s+.*)""".toRegex()) +@LowPriorityInOverloadResolution +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.dom.hasClass' instead.", + replaceWith = ReplaceWith("this.hasClass(cssClass)", "kotlinx.dom.hasClass") +) +inline fun Element.hasClass(cssClass: String): Boolean = this.newHasClass(cssClass) /** * Adds CSS class to element. Has no effect if all specified classes are already in class attribute of the element * * @return true if at least one class has been added */ -fun Element.addClass(vararg cssClasses: String): Boolean { - val missingClasses = cssClasses.filterNot { hasClass(it) } - if (missingClasses.isNotEmpty()) { - val presentClasses = className.trim() - className = buildString { - append(presentClasses) - if (!presentClasses.isEmpty()) { - append(" ") - } - missingClasses.joinTo(this, " ") - } - return true - } - - return false -} +@LowPriorityInOverloadResolution +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.dom.addClass' instead.", + replaceWith = ReplaceWith("this.addClass(cssClasses)", "kotlinx.dom.addClass") +) +inline fun Element.addClass(vararg cssClasses: String): Boolean = this.newAddClass(*cssClasses) /** * Removes all [cssClasses] from element. Has no effect if all specified classes are missing in class attribute of the element * * @return true if at least one class has been removed */ -fun Element.removeClass(vararg cssClasses: String): Boolean { - if (cssClasses.any { hasClass(it) }) { - val toBeRemoved = cssClasses.toSet() - className = className.trim().split("\\s+".toRegex()).filter { it !in toBeRemoved }.joinToString(" ") - return true - } - - return false -} +@LowPriorityInOverloadResolution +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.dom.removeClass' instead.", + replaceWith = ReplaceWith("this.removeClass(cssClasses)", "kotlinx.dom.removeClass") +) +inline fun Element.removeClass(vararg cssClasses: String): Boolean = this.newRemoveClass(*cssClasses) \ No newline at end of file diff --git a/libraries/stdlib/js/src/kotlin/dom/Dom.kt b/libraries/stdlib/js/src/kotlin/dom/Dom.kt index 6b4c7e3294e..bcfb0ae761f 100644 --- a/libraries/stdlib/js/src/kotlin/dom/Dom.kt +++ b/libraries/stdlib/js/src/kotlin/dom/Dom.kt @@ -5,16 +5,30 @@ package kotlin.dom -import org.w3c.dom.* +import org.w3c.dom.Element +import org.w3c.dom.Node +import kotlin.internal.LowPriorityInOverloadResolution +import kotlinx.dom.isElement as newIsElement +import kotlinx.dom.isText as newIsText /** * Gets a value indicating whether this node is a TEXT_NODE or a CDATA_SECTION_NODE. */ +@LowPriorityInOverloadResolution +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.dom.isText' instead.", + replaceWith = ReplaceWith("this.isText", "kotlinx.dom.isText") +) public val Node.isText: Boolean - get() = nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE + inline get() = this.newIsText /** * Gets a value indicating whether this node is an [Element]. */ +@LowPriorityInOverloadResolution +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.dom.isElement' instead.", + replaceWith = ReplaceWith("this.isElement", "kotlinx.dom.isElement") +) public val Node.isElement: Boolean - get() = nodeType == Node.ELEMENT_NODE + inline get() = this.newIsElement diff --git a/libraries/stdlib/js/src/kotlin/dom/Mutations.kt b/libraries/stdlib/js/src/kotlin/dom/Mutations.kt index e935af359c6..6dd22a3d382 100644 --- a/libraries/stdlib/js/src/kotlin/dom/Mutations.kt +++ b/libraries/stdlib/js/src/kotlin/dom/Mutations.kt @@ -5,21 +5,28 @@ package kotlin.dom -import org.w3c.dom.* +import org.w3c.dom.Element +import org.w3c.dom.Node +import kotlin.internal.LowPriorityInOverloadResolution +import kotlinx.dom.appendText as newAppendText +import kotlinx.dom.clear as newClear /** Removes all the children from this node. */ -public fun Node.clear() { - while (hasChildNodes()) { - removeChild(firstChild!!) - } -} +@LowPriorityInOverloadResolution +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.dom.clear' instead.", + replaceWith = ReplaceWith("this.clear()", "kotlinx.dom.clear") +) +public inline fun Node.clear() = this.newClear() /** * Creates text node and append it to the element. * * @return this element */ -fun Element.appendText(text: String): Element { - appendChild(ownerDocument!!.createTextNode(text)) - return this -} +@LowPriorityInOverloadResolution +@Deprecated( + message = "This API is moved to another package, use 'kotlinx.dom.appendText' instead.", + replaceWith = ReplaceWith("this.appendText(text)", "kotlinx.dom.appendText") +) +inline fun Element.appendText(text: String): Element = this.newAppendText(text) diff --git a/libraries/stdlib/js/src/kotlinx/browser/declarations.kt b/libraries/stdlib/js/src/kotlinx/browser/declarations.kt new file mode 100644 index 00000000000..a33bada9067 --- /dev/null +++ b/libraries/stdlib/js/src/kotlinx/browser/declarations.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlinx.browser + +import org.w3c.dom.* + +public external val window: Window + +public external val document: Document + +public external val localStorage: Storage + +public external val sessionStorage: Storage + diff --git a/libraries/stdlib/js/src/kotlinx/dom/Builders.kt b/libraries/stdlib/js/src/kotlinx/dom/Builders.kt new file mode 100644 index 00000000000..24a75f02b43 --- /dev/null +++ b/libraries/stdlib/js/src/kotlinx/dom/Builders.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlinx.dom + +import org.w3c.dom.* + +/** + * Creates a new element with the specified [name]. + * + * The element is initialized with the speicifed [init] function. + */ +public fun Document.createElement(name: String, init: Element.() -> Unit): Element = createElement(name).apply(init) + +/** + * Appends a newly created element with the specified [name] to this element. + * + * The element is initialized with the speicifed [init] function. + */ +public fun Element.appendElement(name: String, init: Element.() -> Unit): Element = + ownerDocument!!.createElement(name, init).also { appendChild(it) } + diff --git a/libraries/stdlib/js/src/kotlinx/dom/Classes.kt b/libraries/stdlib/js/src/kotlinx/dom/Classes.kt new file mode 100644 index 00000000000..ef65cc1a5c3 --- /dev/null +++ b/libraries/stdlib/js/src/kotlinx/dom/Classes.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlinx.dom + +import org.w3c.dom.* + +/** Returns true if the element has the given CSS class style in its 'class' attribute */ +fun Element.hasClass(cssClass: String): Boolean = className.matches("""(^|.*\s+)$cssClass($|\s+.*)""".toRegex()) + +/** + * Adds CSS class to element. Has no effect if all specified classes are already in class attribute of the element + * + * @return true if at least one class has been added + */ +fun Element.addClass(vararg cssClasses: String): Boolean { + val missingClasses = cssClasses.filterNot { hasClass(it) } + if (missingClasses.isNotEmpty()) { + val presentClasses = className.trim() + className = buildString { + append(presentClasses) + if (!presentClasses.isEmpty()) { + append(" ") + } + missingClasses.joinTo(this, " ") + } + return true + } + + return false +} + +/** + * Removes all [cssClasses] from element. Has no effect if all specified classes are missing in class attribute of the element + * + * @return true if at least one class has been removed + */ +fun Element.removeClass(vararg cssClasses: String): Boolean { + if (cssClasses.any { hasClass(it) }) { + val toBeRemoved = cssClasses.toSet() + className = className.trim().split("\\s+".toRegex()).filter { it !in toBeRemoved }.joinToString(" ") + return true + } + + return false +} diff --git a/libraries/stdlib/js/src/kotlinx/dom/Dom.kt b/libraries/stdlib/js/src/kotlinx/dom/Dom.kt new file mode 100644 index 00000000000..66154e9fd7c --- /dev/null +++ b/libraries/stdlib/js/src/kotlinx/dom/Dom.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlinx.dom + +import org.w3c.dom.* + +/** + * Gets a value indicating whether this node is a TEXT_NODE or a CDATA_SECTION_NODE. + */ +public val Node.isText: Boolean + get() = nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE + +/** + * Gets a value indicating whether this node is an [Element]. + */ +public val Node.isElement: Boolean + get() = nodeType == Node.ELEMENT_NODE diff --git a/libraries/stdlib/js/src/kotlinx/dom/Mutations.kt b/libraries/stdlib/js/src/kotlinx/dom/Mutations.kt new file mode 100644 index 00000000000..52e98fedd7f --- /dev/null +++ b/libraries/stdlib/js/src/kotlinx/dom/Mutations.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlinx.dom + +import org.w3c.dom.* + +/** Removes all the children from this node. */ +public fun Node.clear() { + while (hasChildNodes()) { + removeChild(firstChild!!) + } +} + +/** + * Creates text node and append it to the element. + * + * @return this element + */ +fun Element.appendText(text: String): Element { + appendChild(ownerDocument!!.createTextNode(text)) + return this +}