diff --git a/js/js.libraries/.idea/.name b/js/js.libraries/.idea/.name new file mode 100644 index 00000000000..6ee8c70eed4 --- /dev/null +++ b/js/js.libraries/.idea/.name @@ -0,0 +1 @@ +js.libraries \ No newline at end of file diff --git a/js/js.libraries/src/core/dom.kt b/js/js.libraries/src/core/dom.kt index 84c3a3b1aeb..f20c5b076a0 100644 --- a/js/js.libraries/src/core/dom.kt +++ b/js/js.libraries/src/core/dom.kt @@ -26,10 +26,10 @@ native public trait CDATASection: Text { native public trait CharacterData: Node { public var data: String public val length: Int - public fun replaceData(arg1: Int, arg2: Int, arg3: String): Unit = js.noImpl + public fun replaceData(arg1: Int, arg2: Int, arg3: String?): Unit = js.noImpl public fun substringData(arg1: Int, arg2: Int): String = js.noImpl - public fun appendData(arg1: String): Unit = js.noImpl - public fun insertData(arg1: Int, arg2: String): Unit = js.noImpl + public fun appendData(arg1: String?): Unit = js.noImpl + public fun insertData(arg1: Int, arg2: String?): Unit = js.noImpl public fun deleteData(arg1: Int, arg2: Int): Unit = js.noImpl } @@ -47,23 +47,23 @@ native public trait Document: Node { public val xmlEncoding: String public var xmlStandalone: Boolean public var xmlVersion: String - public fun createElement(arg1: String): Element = js.noImpl - public fun createComment(arg1: String): Comment = js.noImpl + public fun createElement(arg1: String?): Element = js.noImpl + public fun createComment(arg1: String?): Comment = js.noImpl public fun createDocumentFragment(): DocumentFragment = js.noImpl - public fun createTextNode(arg1: String): Text = js.noImpl - public fun createCDATASection(arg1: String): CDATASection = js.noImpl - public fun createProcessingInstruction(arg1: String, arg2: String): ProcessingInstruction = js.noImpl - public fun createAttribute(arg1: String): Attr = js.noImpl - public fun createEntityReference(arg1: String): EntityReference = js.noImpl - public fun getElementsByTagName(arg1: String): NodeList = js.noImpl + public fun createTextNode(arg1: String?): Text = js.noImpl + public fun createCDATASection(arg1: String?): CDATASection = js.noImpl + public fun createProcessingInstruction(arg1: String?, arg2: String?): ProcessingInstruction = js.noImpl + public fun createAttribute(arg1: String?): Attr = js.noImpl + public fun createEntityReference(arg1: String?): EntityReference = js.noImpl + public fun getElementsByTagName(arg1: String?): NodeList = js.noImpl public fun importNode(arg1: Node, arg2: Boolean): Node = js.noImpl - public fun createElementNS(arg1: String, arg2: String): Element = js.noImpl - public fun createAttributeNS(arg1: String, arg2: String): Attr = js.noImpl - public fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl - public fun getElementById(arg1: String): Element = js.noImpl + public fun createElementNS(arg1: String?, arg2: String?): Element = js.noImpl + public fun createAttributeNS(arg1: String?, arg2: String?): Attr = js.noImpl + public fun getElementsByTagNameNS(arg1: String?, arg2: String?): NodeList = js.noImpl + public fun getElementById(arg1: String?): Element = js.noImpl public fun adoptNode(arg1: Node): Node = js.noImpl public fun normalizeDocument(): Unit = js.noImpl - public fun renameNode(arg1: Node, arg2: String, arg3: String): Node = js.noImpl + public fun renameNode(arg1: Node, arg2: String?, arg3: String?): Node = js.noImpl } native public trait DocumentFragment: Node { @@ -80,9 +80,9 @@ native public trait DocumentType: Node { native public trait DOMConfiguration { public val parameterNames: DOMStringList - public fun setParameter(arg1: String, arg2: Any): Unit = js.noImpl - public fun getParameter(arg1: String): Any = js.noImpl - public fun canSetParameter(arg1: String, arg2: Any): Boolean = js.noImpl + public fun setParameter(arg1: String?, arg2: Any): Unit = js.noImpl + public fun getParameter(arg1: String?): Any = js.noImpl + public fun canSetParameter(arg1: String?, arg2: Any): Boolean = js.noImpl } native public trait DOMError { @@ -105,10 +105,10 @@ native public trait DOMErrorHandler { } native public trait DOMImplementation { - public fun getFeature(arg1: String, arg2: String): Any = js.noImpl - public fun hasFeature(arg1: String, arg2: String): Boolean = js.noImpl - public fun createDocumentType(arg1: String, arg2: String, arg3: String): DocumentType = js.noImpl - public fun createDocument(arg1: String, arg2: String, arg3: DocumentType): Document = js.noImpl + public fun getFeature(arg1: String?, arg2: String?): Any = js.noImpl + public fun hasFeature(arg1: String?, arg2: String?): Boolean = js.noImpl + public fun createDocumentType(arg1: String?, arg2: String?, arg3: String?): DocumentType = js.noImpl + public fun createDocument(arg1: String?, arg2: String?, arg3: DocumentType?): Document = js.noImpl } native public trait DOMImplementationList { @@ -127,28 +127,28 @@ native public trait DOMLocator { native public trait DOMStringList { public val length: Int - public fun contains(arg1: String): Boolean = js.noImpl + public fun contains(arg1: String?): Boolean = js.noImpl public fun item(arg1: Int): String = js.noImpl } native public trait Element: Node { public val schemaTypeInfo: TypeInfo public val tagName: String - public fun getAttribute(arg1: String): String = js.noImpl - public fun setAttribute(arg1: String, arg2: String): Unit = js.noImpl - public fun getElementsByTagName(arg1: String): NodeList = js.noImpl - public fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl - public fun removeAttribute(arg1: String): Unit = js.noImpl - public fun getAttributeNode(arg1: String): Attr = js.noImpl + public fun getAttribute(arg1: String?): String = js.noImpl + public fun setAttribute(arg1: String?, arg2: String?): Unit = js.noImpl + public fun getElementsByTagName(arg1: String?): NodeList = js.noImpl + public fun getElementsByTagNameNS(arg1: String?, arg2: String?): NodeList = js.noImpl + public fun removeAttribute(arg1: String?): Unit = js.noImpl + public fun getAttributeNode(arg1: String?): Attr = js.noImpl public fun removeAttributeNode(arg1: Attr): Attr = js.noImpl - public fun getAttributeNS(arg1: String, arg2: String): String = js.noImpl - public fun setAttributeNS(arg1: String, arg2: String, arg3: String): Unit = js.noImpl - public fun removeAttributeNS(arg1: String, arg2: String): Unit = js.noImpl - public fun getAttributeNodeNS(arg1: String, arg2: String): Attr = js.noImpl - public fun hasAttribute(arg1: String): Boolean = js.noImpl - public fun hasAttributeNS(arg1: String, arg2: String): Boolean = js.noImpl - public fun setIdAttribute(arg1: String, arg2: Boolean): Unit = js.noImpl - public fun setIdAttributeNS(arg1: String, arg2: String, arg3: Boolean): Unit = js.noImpl + public fun getAttributeNS(arg1: String?, arg2: String?): String = js.noImpl + public fun setAttributeNS(arg1: String?, arg2: String?, arg3: String?): Unit = js.noImpl + public fun removeAttributeNS(arg1: String?, arg2: String?): Unit = js.noImpl + public fun getAttributeNodeNS(arg1: String?, arg2: String?): Attr = js.noImpl + public fun hasAttribute(arg1: String?): Boolean = js.noImpl + public fun hasAttributeNS(arg1: String?, arg2: String?): Boolean = js.noImpl + public fun setIdAttribute(arg1: String?, arg2: Boolean): Unit = js.noImpl + public fun setIdAttributeNS(arg1: String?, arg2: String?, arg3: Boolean): Unit = js.noImpl public fun setIdAttributeNode(arg1: Attr, arg2: Boolean): Unit = js.noImpl public fun setAttributeNode(arg1: Attr): Attr = js.noImpl public fun setAttributeNodeNS(arg1: Attr): Attr = js.noImpl @@ -169,18 +169,18 @@ native public trait EntityReference: Node { native public trait NameList { public val length: Int public fun getName(arg1: Int): String = js.noImpl - public fun contains(arg1: String): Boolean = js.noImpl + public fun contains(arg1: String?): Boolean = js.noImpl public fun getNamespaceURI(arg1: Int): String = js.noImpl - public fun containsNS(arg1: String, arg2: String): Boolean = js.noImpl + public fun containsNS(arg1: String?, arg2: String?): Boolean = js.noImpl } native public trait NamedNodeMap { public val length: Int public fun item(arg1: Int): Node = js.noImpl - public fun getNamedItem(arg1: String): Node = js.noImpl - public fun removeNamedItem(arg1: String): Node = js.noImpl - public fun getNamedItemNS(arg1: String, arg2: String): Node = js.noImpl - public fun removeNamedItemNS(arg1: String, arg2: String): Node = js.noImpl + public fun getNamedItem(arg1: String?): Node = js.noImpl + public fun removeNamedItem(arg1: String?): Node = js.noImpl + public fun getNamedItemNS(arg1: String?, arg2: String?): Node = js.noImpl + public fun removeNamedItemNS(arg1: String?, arg2: String?): Node = js.noImpl public fun setNamedItem(arg1: Node): Node = js.noImpl public fun setNamedItemNS(arg1: Node): Node = js.noImpl } @@ -203,11 +203,12 @@ native public trait Node { public val previousSibling: Node public var textContent: String public fun normalize(): Unit = js.noImpl - public fun isSupported(arg1: String, arg2: String): Boolean = js.noImpl - public fun getFeature(arg1: String, arg2: String): Any = js.noImpl + public fun isSupported(arg1: String?, arg2: String?): Boolean = js.noImpl + public fun getUserData(arg1: String?): Any = js.noImpl + public fun setUserData(arg1: String?, arg2: Any, arg3: UserDataHandler): Any = js.noImpl + public fun getFeature(arg1: String?, arg2: String?): Any = js.noImpl public fun hasAttributes(): Boolean = js.noImpl public fun removeChild(arg1: Node): Node = js.noImpl - public fun getUserData(arg1: String): Any = js.noImpl public fun replaceChild(arg1: Node, arg2: Node): Node = js.noImpl public fun insertBefore(arg1: Node, arg2: Node): Node = js.noImpl public fun appendChild(arg1: Node): Node = js.noImpl @@ -215,11 +216,10 @@ native public trait Node { public fun cloneNode(arg1: Boolean): Node = js.noImpl public fun compareDocumentPosition(arg1: Node): Short = js.noImpl public fun isSameNode(arg1: Node): Boolean = js.noImpl - public fun lookupPrefix(arg1: String): String = js.noImpl - public fun isDefaultNamespace(arg1: String): Boolean = js.noImpl - public fun lookupNamespaceURI(arg1: String): String = js.noImpl + public fun lookupPrefix(arg1: String?): String = js.noImpl + public fun isDefaultNamespace(arg1: String?): Boolean = js.noImpl + public fun lookupNamespaceURI(arg1: String?): String = js.noImpl public fun isEqualNode(arg1: Node): Boolean = js.noImpl - public fun setUserData(arg1: String, arg2: Any, arg3: UserDataHandler): Any = js.noImpl class object { public val ELEMENT_NODE: Short = 1 @@ -262,13 +262,13 @@ native public trait Text: CharacterData { public val wholeText: String public fun splitText(arg1: Int): Text = js.noImpl public fun isElementContentWhitespace(): Boolean = js.noImpl - public fun replaceWholeText(arg1: String): Text = js.noImpl + public fun replaceWholeText(arg1: String?): Text = js.noImpl } native public trait TypeInfo { public val typeName: String public val typeNamespace: String - public fun isDerivedFrom(arg1: String, arg2: String, arg3: Int): Boolean = js.noImpl + public fun isDerivedFrom(arg1: String?, arg2: String?, arg3: Int): Boolean = js.noImpl class object { public val DERIVATION_RESTRICTION: Int = 1 @@ -279,7 +279,7 @@ native public trait TypeInfo { } native public trait UserDataHandler { - public fun handle(arg1: Short, arg2: String, arg3: Any, arg4: Node, arg5: Node): Unit = js.noImpl + public fun handle(arg1: Short, arg2: String?, arg3: Any, arg4: Node, arg5: Node): Unit = js.noImpl class object { public val NODE_CLONED: Short = 1 diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index 50659c3ab67..44b6c7f9b06 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -25,6 +25,12 @@ library val Collections = object { library("collectionsMax") public fun max(col : Collection, comp : Comparator) : T = js.noImpl + + // TODO should be immutable! + private val emptyList = ArrayList() + + public val EMPTY_LIST: List + get() = emptyList as List } library @@ -201,4 +207,3 @@ public class StringBuilder() { library class NoSuchElementException() : Exception() {} - diff --git a/js/js.libraries/src/core/stringDefs.kt b/js/js.libraries/src/core/stringDefs.kt new file mode 100644 index 00000000000..0cd5c9a2751 --- /dev/null +++ b/js/js.libraries/src/core/stringDefs.kt @@ -0,0 +1,161 @@ +package js + +import java.util.List + +native public fun String.toUpperCase() : String = js.noImpl + +native public fun String.toLowerCase() : String = js.noImpl + +native public fun String.indexOf(str : String) : Int = js.noImpl +native public fun String.indexOf(str : String, fromIndex : Int) : Int = js.noImpl + +native public fun String.lastIndexOf(str: String) : Int = js.noImpl +native public fun String.lastIndexOf(str : String, fromIndex : Int) : Int = js.noImpl + +native public fun String.split(regex : String) : Array = js.noImpl + +native public fun String.substring(beginIndex : Int) : String = js.noImpl +native public fun String.substring(beginIndex : Int, endIndex : Int) : String = js.noImpl + +native public fun String.charAt(index : Int) : Char = js.noImpl + +native public fun String.concat(str : String) : String = js.noImpl + +native public fun String.match(regex : String) : Array = js.noImpl + +native public fun String.trim() : String = js.noImpl + +native public val String.length : Int + get() = js.noImpl + + +/* + +native public fun String.equalsIgnoreCase(anotherString: String) : Boolean = (this as java.lang.String).equalsIgnoreCase(anotherString) + +native public fun String.hashCode() : Int = (this as java.lang.String).hashCode() + +native public fun String.replace(oldChar: Char, newChar : Char) : String = (this as java.lang.String).replace(oldChar, newChar).sure() + +native public fun String.replaceAll(regex: String, replacement : String) : String = (this as java.lang.String).replaceAll(regex, replacement).sure() + + +native public fun String.length() : Int = (this as java.lang.String).length() + +native public fun String.getBytes() : ByteArray = (this as java.lang.String).getBytes().sure() + +native public fun String.toCharArray() : CharArray = (this as java.lang.String).toCharArray().sure() + +native public fun String.toCharList(): List = toCharArray().toList() + +native public fun String.format(format : String, vararg args : Any?) : String = java.lang.String.format(format, args).sure() + + +native public fun String.split(ch : Char) : Array = (this as java.lang.String).split(java.util.regex.Pattern.quote(ch.toString())) as Array + +native public fun String.startsWith(prefix: String) : Boolean = (this as java.lang.String).startsWith(prefix) + +native public fun String.startsWith(prefix: String, toffset: Int) : Boolean = (this as java.lang.String).startsWith(prefix, toffset) + +native public fun String.startsWith(ch: Char) : Boolean = (this as java.lang.String).startsWith(ch.toString()) + +native public fun String.contains(seq: CharSequence) : Boolean = (this as java.lang.String).contains(seq) + +native public fun String.endsWith(suffix: String) : Boolean = (this as java.lang.String).endsWith(suffix) + +native public fun String.endsWith(ch: Char) : Boolean = (this as java.lang.String).endsWith(ch.toString()) + +// "constructors" for String + +native public fun String(bytes : ByteArray, offset : Int, length : Int, charsetName : String) : String = java.lang.String(bytes, offset, length, charsetName) as String + +native public fun String(bytes : ByteArray, offset : Int, length : Int, charset : java.nio.charset.Charset) : String = java.lang.String(bytes, offset, length, charset) as String + +native public fun String(bytes : ByteArray, charsetName : String?) : String = java.lang.String(bytes, charsetName) as String + +native public fun String(bytes : ByteArray, charset : java.nio.charset.Charset) : String = java.lang.String(bytes, charset) as String + +native public fun String(bytes : ByteArray, i : Int, i1 : Int) : String = java.lang.String(bytes, i, i1) as String + +native public fun String(bytes : ByteArray) : String = java.lang.String(bytes) as String + +native public fun String(chars : CharArray) : String = java.lang.String(chars) as String + +native public fun String(stringBuffer : java.lang.StringBuffer) : String = java.lang.String(stringBuffer) as String + +native public fun String(stringBuilder : java.lang.StringBuilder) : String = java.lang.String(stringBuilder) as String + +native public fun String.replaceFirst(regex : String, replacement : String) : String = (this as java.lang.String).replaceFirst(regex, replacement).sure() + + +native public fun String.split(regex : String, limit : Int) : Array = (this as java.lang.String).split(regex, limit).sure() + +native public fun String.codePointAt(index : Int) : Int = (this as java.lang.String).codePointAt(index).sure() + +native public fun String.codePointBefore(index : Int) : Int = (this as java.lang.String).codePointBefore(index).sure() + +native public fun String.codePointCount(beginIndex : Int, endIndex : Int) : Int = (this as java.lang.String).codePointCount(beginIndex, endIndex) + +native public fun String.compareToIgnoreCase(str : String) : Int = (this as java.lang.String).compareToIgnoreCase(str).sure() + + +native public fun String.contentEquals(cs : CharSequence) : Boolean = (this as java.lang.String).contentEquals(cs).sure() + +native public fun String.contentEquals(sb : StringBuffer) : Boolean = (this as java.lang.String).contentEquals(sb).sure() + +native public fun String.getBytes(charset : java.nio.charset.Charset) : ByteArray = (this as java.lang.String).getBytes(charset).sure() + +native public fun String.getBytes(charsetName : String) : ByteArray = (this as java.lang.String).getBytes(charsetName).sure() + +native public fun String.getChars(srcBegin : Int, srcEnd : Int, dst : CharArray, dstBegin : Int) : Tuple0 = (this as java.lang.String).getChars(srcBegin, srcEnd, dst, dstBegin).sure() + +native public fun String.intern() : String = (this as java.lang.String).intern().sure() + +native public fun String.isEmpty() : Boolean = (this as java.lang.String).isEmpty().sure() + + +native public fun String.offsetByCodePoints(index : Int, codePointOffset : Int) : Int = (this as java.lang.String).offsetByCodePoints(index, codePointOffset).sure() + +native public fun String.regionMatches(ignoreCase : Boolean, toffset : Int, other : String, ooffset : Int, len : Int) : Boolean = (this as java.lang.String).regionMatches(ignoreCase, toffset, other, ooffset, len).sure() + +native public fun String.regionMatches(toffset : Int, other : String, ooffset : Int, len : Int) : Boolean = (this as java.lang.String).regionMatches(toffset, other, ooffset, len).sure() + +native public fun String.replace(target : CharSequence, replacement : CharSequence) : String = (this as java.lang.String).replace(target, replacement).sure() + +native public fun String.subSequence(beginIndex : Int, endIndex : Int) : CharSequence = (this as java.lang.String).subSequence(beginIndex, endIndex).sure() + +native public fun String.toLowerCase(locale : java.util.Locale) : String = (this as java.lang.String).toLowerCase(locale).sure() + +native public fun String.toUpperCase(locale : java.util.Locale) : String = (this as java.lang.String).toUpperCase(locale).sure() + + +native public fun CharSequence.charAt(index : Int) : Char = (this as java.lang.CharSequence).charAt(index) + +native public fun CharSequence.get(index : Int) : Char = charAt(index) + +native public fun CharSequence.subSequence(start : Int, end : Int) : CharSequence? = (this as java.lang.CharSequence).subSequence(start, end) + +native public fun CharSequence.get(start : Int, end : Int) : CharSequence? = subSequence(start, end) + +native public fun CharSequence.toString() : String? = (this as java.lang.CharSequence).toString() + +native public fun String.toByteArray(encoding: String?=null):ByteArray { + if(encoding==null) { + return (this as java.lang.String).getBytes().sure() + } else { + return (this as java.lang.String).getBytes(encoding).sure() + } +} +native public fun String.toByteArray(encoding: java.nio.charset.Charset):ByteArray = (this as java.lang.String).getBytes(encoding).sure() + +native public fun String.toBoolean() : Boolean = java.lang.Boolean.parseBoolean(this).sure() +native public fun String.toShort() : Short = java.lang.Short.parseShort(this).sure() +native public fun String.toInt() : Int = java.lang.Integer.parseInt(this).sure() +native public fun String.toLong() : Long = java.lang.Long.parseLong(this).sure() +native public fun String.toFloat() : Float = java.lang.Float.parseFloat(this).sure() +native public fun String.toDouble() : Double = java.lang.Double.parseDouble(this).sure() + +native public fun String.toRegex(flags: Int=0): java.util.regex.Pattern { + return java.util.regex.Pattern.compile(this, flags).sure() +} +*/ \ No newline at end of file diff --git a/js/js.libraries/src/core/strings.kt b/js/js.libraries/src/core/strings.kt new file mode 100644 index 00000000000..213d6f05f63 --- /dev/null +++ b/js/js.libraries/src/core/strings.kt @@ -0,0 +1,60 @@ +package js + +public inline fun String.lastIndexOf(ch : Char, fromIndex : Int) : Int = lastIndexOf(ch.toString(), fromIndex) +public inline fun String.lastIndexOf(ch: Char) : Int = lastIndexOf(ch.toString()) + +public inline fun String.indexOf(ch : Char) : Int = indexOf(ch.toString()) +public inline fun String.indexOf(ch : Char, fromIndex : Int) : Int = indexOf(ch.toString(), fromIndex) + +public inline fun String.matches(regex : String) : Boolean { + val result = this.match(regex) + return result != null && result.size > 0 +} + +public inline fun String.length(): Int = length + +inline val String.size : Int +get() = length + +public inline fun String.startsWith(ch: Char): Boolean { + return if (size > 0) charAt(0) == ch else false +} + +public inline fun String.endsWith(ch: Char): Boolean { + val s = size + return if (s > 0) charAt(s - 1) == ch else false +} + +public inline fun String.startsWith(text: String): Boolean { + val size = text.length + return if (size <= this.length) { + substring(0, size) == text + } else false +} + +public inline fun String.endsWith(text: String): Boolean { + val matchSize = text.length + val thisSize = this.length + return if (matchSize <= thisSize) { + substring(thisSize - matchSize, thisSize) == text + } else false +} + + +/** + * Returns a copy of this string capitalised if it is not empty or already starting with an uppper case letter, otherwise returns this + * + * @includeFunctionBody ../../test/StringTest.kt capitalize + */ +public inline fun String.capitalize(): String { + return if (notEmpty()) substring(0, 1).toUpperCase() + substring(1) else this +} + +/** + * Returns a copy of this string with the first letter lower case if it is not empty or already starting with a lower case letter, otherwise returns this + * + * @includeFunctionBody ../../test/StringTest.kt decapitalize + */ +public inline fun String.decapitalize(): String { + return if (notEmpty()) substring(0, 1).toLowerCase() + substring(1) else this +} diff --git a/js/js.libraries/src/stdlib/dom.kt b/js/js.libraries/src/stdlib/dom.kt new file mode 100644 index 00000000000..c2e55bf2e5c --- /dev/null +++ b/js/js.libraries/src/stdlib/dom.kt @@ -0,0 +1,16 @@ +package kotlin.dom + +import org.w3c.dom.Document +import org.w3c.dom.Node + +fun createDocument(): Document { + return browser.document.implementation.createDocument(null, null, null) +} + +native public val Node.outerHTML: String +get() = js.noImpl + +/** Converts the node to an XML String */ +public fun Node.toXmlString(xmlDeclaration: Boolean = false): String { + return this.outerHTML +} diff --git a/libraries/stdlib/src/kotlin/Strings.kt b/libraries/stdlib/src/kotlin/Strings.kt index 4b67cc6fd30..1f946eef835 100644 --- a/libraries/stdlib/src/kotlin/Strings.kt +++ b/libraries/stdlib/src/kotlin/Strings.kt @@ -1,27 +1,8 @@ package kotlin -import java.io.StringReader import java.util.List import java.util.ArrayList -public inline fun String.lastIndexOf(str: String) : Int = (this as java.lang.String).lastIndexOf(str) - -public inline fun String.lastIndexOf(ch: Char) : Int = (this as java.lang.String).lastIndexOf(ch.toString()) - -public inline fun String.equalsIgnoreCase(anotherString: String) : Boolean = (this as java.lang.String).equalsIgnoreCase(anotherString) - -public inline fun String.hashCode() : Int = (this as java.lang.String).hashCode() - -public inline fun String.indexOf(str : String) : Int = (this as java.lang.String).indexOf(str) - -public inline fun String.indexOf(str : String, fromIndex : Int) : Int = (this as java.lang.String).indexOf(str, fromIndex) - -public inline fun String.replace(oldChar: Char, newChar : Char) : String = (this as java.lang.String).replace(oldChar, newChar).sure() - -public inline fun String.replaceAll(regex: String, replacement : String) : String = (this as java.lang.String).replaceAll(regex, replacement).sure() - -public inline fun String.trim() : String = (this as java.lang.String).trim().sure() - /** Returns the string with leading and trailing text matching the given string removed */ public inline fun String.trim(text: String) : String = trimLeading(text).trimTrailing(text) @@ -37,24 +18,6 @@ public inline fun String.trimLeading(prefix: String): String { return answer } -/** - * Returns a copy of this string capitalised if it is not empty or already starting with an uppper case letter, otherwise returns this - * - * @includeFunctionBody ../../test/StringTest.kt capitalize - */ -public inline fun String.capitalize(): String { - return if (notEmpty() && charAt(0).isLowerCase()) substring(0, 1).toUpperCase() + substring(1) else this -} - -/** - * Returns a copy of this string with the first letter lower case if it is not empty or already starting with a lower case letter, otherwise returns this - * - * @includeFunctionBody ../../test/StringTest.kt decapitalize - */ -public inline fun String.decapitalize(): String { - return if (notEmpty() && charAt(0).isUpperCase()) substring(0, 1).toLowerCase() + substring(1) else this -} - /** Returns the string with the trailing postfix of this string removed */ public inline fun String.trimTrailing(postfix: String): String { var answer = this @@ -64,94 +27,9 @@ public inline fun String.trimTrailing(postfix: String): String { return answer } -public inline fun String.toUpperCase() : String = (this as java.lang.String).toUpperCase().sure() - -public inline fun String.toLowerCase() : String = (this as java.lang.String).toLowerCase().sure() - -public inline fun String.length() : Int = (this as java.lang.String).length() - -public inline fun String.getBytes() : ByteArray = (this as java.lang.String).getBytes().sure() - -public inline fun String.toCharArray() : CharArray = (this as java.lang.String).toCharArray().sure() - -public inline fun String.toCharList(): List = toCharArray().toList() - -public inline fun String.format(format : String, vararg args : Any?) : String = java.lang.String.format(format, args).sure() - -public inline fun String.split(regex : String) : Array = (this as java.lang.String).split(regex) as Array - -public inline fun String.split(ch : Char) : Array = (this as java.lang.String).split(java.util.regex.Pattern.quote(ch.toString())) as Array - -public inline fun String.substring(beginIndex : Int) : String = (this as java.lang.String).substring(beginIndex).sure() - -public inline fun String.substring(beginIndex : Int, endIndex : Int) : String = (this as java.lang.String).substring(beginIndex, endIndex).sure() - -public inline fun String.startsWith(prefix: String) : Boolean = (this as java.lang.String).startsWith(prefix) - -public inline fun String.startsWith(prefix: String, toffset: Int) : Boolean = (this as java.lang.String).startsWith(prefix, toffset) - -public inline fun String.startsWith(ch: Char) : Boolean = (this as java.lang.String).startsWith(ch.toString()) - -public inline fun String.contains(seq: CharSequence) : Boolean = (this as java.lang.String).contains(seq) - -public inline fun String.endsWith(suffix: String) : Boolean = (this as java.lang.String).endsWith(suffix) - -public inline fun String.endsWith(ch: Char) : Boolean = (this as java.lang.String).endsWith(ch.toString()) - -inline val String.size : Int -get() = length() - -inline val String.reader : StringReader -get() = StringReader(this) - -// "constructors" for String - -public inline fun String(bytes : ByteArray, offset : Int, length : Int, charsetName : String) : String = java.lang.String(bytes, offset, length, charsetName) as String - -public inline fun String(bytes : ByteArray, offset : Int, length : Int, charset : java.nio.charset.Charset) : String = java.lang.String(bytes, offset, length, charset) as String - -public inline fun String(bytes : ByteArray, charsetName : String?) : String = java.lang.String(bytes, charsetName) as String - -public inline fun String(bytes : ByteArray, charset : java.nio.charset.Charset) : String = java.lang.String(bytes, charset) as String - -public inline fun String(bytes : ByteArray, i : Int, i1 : Int) : String = java.lang.String(bytes, i, i1) as String - -public inline fun String(bytes : ByteArray) : String = java.lang.String(bytes) as String - -public inline fun String(chars : CharArray) : String = java.lang.String(chars) as String - -public inline fun String(stringBuffer : java.lang.StringBuffer) : String = java.lang.String(stringBuffer) as String - -public inline fun String(stringBuilder : java.lang.StringBuilder) : String = java.lang.String(stringBuilder) as String - /** Returns true if the string is not null and not empty */ public inline fun String?.notEmpty() : Boolean = this != null && this.length() > 0 -public inline fun String.toByteArray(encoding: String?=null):ByteArray { - if(encoding==null) { - return (this as java.lang.String).getBytes().sure() - } else { - return (this as java.lang.String).getBytes(encoding).sure() - } -} -public inline fun String.toByteArray(encoding: java.nio.charset.Charset):ByteArray = (this as java.lang.String).getBytes(encoding).sure() - -public inline fun String.toBoolean() : Boolean = java.lang.Boolean.parseBoolean(this).sure() -public inline fun String.toShort() : Short = java.lang.Short.parseShort(this).sure() -public inline fun String.toInt() : Int = java.lang.Integer.parseInt(this).sure() -public inline fun String.toLong() : Long = java.lang.Long.parseLong(this).sure() -public inline fun String.toFloat() : Float = java.lang.Float.parseFloat(this).sure() -public inline fun String.toDouble() : Double = java.lang.Double.parseDouble(this).sure() - -/** - * Converts the string into a regular expression [[Pattern]] optionally - * with the specified flags from [[Pattern]] or'd together - * so that strings can be split or matched on. - */ -public inline fun String.toRegex(flags: Int=0): java.util.regex.Pattern { - return java.util.regex.Pattern.compile(this, flags).sure() -} - /** Iterator for characters of given CharSequence */ @@ -164,80 +42,14 @@ public inline fun CharSequence.iterator() : CharIterator = object: jet.CharItera get() = index < length } -public inline fun String.replaceFirst(regex : String, replacement : String) : String = (this as java.lang.String).replaceFirst(regex, replacement).sure() - -public inline fun String.charAt(index : Int) : Char = (this as java.lang.String).charAt(index).sure() - -public inline fun String.split(regex : String, limit : Int) : Array = (this as java.lang.String).split(regex, limit).sure() - -public inline fun String.codePointAt(index : Int) : Int = (this as java.lang.String).codePointAt(index).sure() - -public inline fun String.codePointBefore(index : Int) : Int = (this as java.lang.String).codePointBefore(index).sure() - -public inline fun String.codePointCount(beginIndex : Int, endIndex : Int) : Int = (this as java.lang.String).codePointCount(beginIndex, endIndex) - -public inline fun String.compareToIgnoreCase(str : String) : Int = (this as java.lang.String).compareToIgnoreCase(str).sure() - -public inline fun String.concat(str : String) : String = (this as java.lang.String).concat(str).sure() - -public inline fun String.contentEquals(cs : CharSequence) : Boolean = (this as java.lang.String).contentEquals(cs).sure() - -public inline fun String.contentEquals(sb : StringBuffer) : Boolean = (this as java.lang.String).contentEquals(sb).sure() - -public inline fun String.getBytes(charset : java.nio.charset.Charset) : ByteArray = (this as java.lang.String).getBytes(charset).sure() - -public inline fun String.getBytes(charsetName : String) : ByteArray = (this as java.lang.String).getBytes(charsetName).sure() - -public inline fun String.getChars(srcBegin : Int, srcEnd : Int, dst : CharArray, dstBegin : Int) : Tuple0 = (this as java.lang.String).getChars(srcBegin, srcEnd, dst, dstBegin).sure() - -public inline fun String.indexOf(ch : Char) : Int = (this as java.lang.String).indexOf(ch.toString()).sure() - -public inline fun String.indexOf(ch : Char, fromIndex : Int) : Int = (this as java.lang.String).indexOf(ch.toString(), fromIndex).sure() - -public inline fun String.intern() : String = (this as java.lang.String).intern().sure() - -public inline fun String.isEmpty() : Boolean = (this as java.lang.String).isEmpty().sure() - -public inline fun String.lastIndexOf(ch : Char, fromIndex : Int) : Int = (this as java.lang.String).lastIndexOf(ch.toString(), fromIndex).sure() - -public inline fun String.lastIndexOf(str : String, fromIndex : Int) : Int = (this as java.lang.String).lastIndexOf(str, fromIndex).sure() - -public inline fun String.matches(regex : String) : Boolean = (this as java.lang.String).matches(regex).sure() - -public inline fun String.offsetByCodePoints(index : Int, codePointOffset : Int) : Int = (this as java.lang.String).offsetByCodePoints(index, codePointOffset).sure() - -public inline fun String.regionMatches(ignoreCase : Boolean, toffset : Int, other : String, ooffset : Int, len : Int) : Boolean = (this as java.lang.String).regionMatches(ignoreCase, toffset, other, ooffset, len).sure() - -public inline fun String.regionMatches(toffset : Int, other : String, ooffset : Int, len : Int) : Boolean = (this as java.lang.String).regionMatches(toffset, other, ooffset, len).sure() - -public inline fun String.replace(target : CharSequence, replacement : CharSequence) : String = (this as java.lang.String).replace(target, replacement).sure() - -public inline fun String.subSequence(beginIndex : Int, endIndex : Int) : CharSequence = (this as java.lang.String).subSequence(beginIndex, endIndex).sure() - -public inline fun String.toLowerCase(locale : java.util.Locale) : String = (this as java.lang.String).toLowerCase(locale).sure() - -public inline fun String.toUpperCase(locale : java.util.Locale) : String = (this as java.lang.String).toUpperCase(locale).sure() - /** Returns the string if it is not null or the empty string if its null */ public inline fun String?.orEmpty(): String = this ?: "" // "Extension functions" for CharSequence -public inline fun CharSequence.length() : Int = (this as java.lang.CharSequence).length() - inline val CharSequence.size : Int -get() = length() - -public inline fun CharSequence.charAt(index : Int) : Char = (this as java.lang.CharSequence).charAt(index) - -public inline fun CharSequence.get(index : Int) : Char = charAt(index) - -public inline fun CharSequence.subSequence(start : Int, end : Int) : CharSequence? = (this as java.lang.CharSequence).subSequence(start, end) - -public inline fun CharSequence.get(start : Int, end : Int) : CharSequence? = subSequence(start, end) - -public inline fun CharSequence.toString() : String? = (this as java.lang.CharSequence).toString() +get() = this.length /** * Counts the number of characters which match the given predicate diff --git a/libraries/stdlib/src/kotlin/StringsJVM.kt b/libraries/stdlib/src/kotlin/StringsJVM.kt new file mode 100644 index 00000000000..eaa74059bc5 --- /dev/null +++ b/libraries/stdlib/src/kotlin/StringsJVM.kt @@ -0,0 +1,195 @@ +package kotlin + +import java.io.StringReader +import java.util.List + +public inline fun String.lastIndexOf(str: String) : Int = (this as java.lang.String).lastIndexOf(str) + +public inline fun String.lastIndexOf(ch: Char) : Int = (this as java.lang.String).lastIndexOf(ch.toString()) + +public inline fun String.equalsIgnoreCase(anotherString: String) : Boolean = (this as java.lang.String).equalsIgnoreCase(anotherString) + +public inline fun String.hashCode() : Int = (this as java.lang.String).hashCode() + +public inline fun String.indexOf(str : String) : Int = (this as java.lang.String).indexOf(str) + +public inline fun String.indexOf(str : String, fromIndex : Int) : Int = (this as java.lang.String).indexOf(str, fromIndex) + +public inline fun String.replace(oldChar: Char, newChar : Char) : String = (this as java.lang.String).replace(oldChar, newChar).sure() + +public inline fun String.replaceAll(regex: String, replacement : String) : String = (this as java.lang.String).replaceAll(regex, replacement).sure() + +public inline fun String.trim() : String = (this as java.lang.String).trim().sure() + +public inline fun String.toUpperCase() : String = (this as java.lang.String).toUpperCase().sure() + +public inline fun String.toLowerCase() : String = (this as java.lang.String).toLowerCase().sure() + +public inline fun String.length() : Int = (this as java.lang.String).length() + +public inline fun String.getBytes() : ByteArray = (this as java.lang.String).getBytes().sure() + +public inline fun String.toCharArray() : CharArray = (this as java.lang.String).toCharArray().sure() + +public inline fun String.toCharList(): List = toCharArray().toList() + +public inline fun String.format(format : String, vararg args : Any?) : String = java.lang.String.format(format, args).sure() + +public inline fun String.split(regex : String) : Array = (this as java.lang.String).split(regex) as Array + +public inline fun String.split(ch : Char) : Array = (this as java.lang.String).split(java.util.regex.Pattern.quote(ch.toString())) as Array + +public inline fun String.substring(beginIndex : Int) : String = (this as java.lang.String).substring(beginIndex).sure() + +public inline fun String.substring(beginIndex : Int, endIndex : Int) : String = (this as java.lang.String).substring(beginIndex, endIndex).sure() + +public inline fun String.startsWith(prefix: String) : Boolean = (this as java.lang.String).startsWith(prefix) + +public inline fun String.startsWith(prefix: String, toffset: Int) : Boolean = (this as java.lang.String).startsWith(prefix, toffset) + +public inline fun String.startsWith(ch: Char) : Boolean = (this as java.lang.String).startsWith(ch.toString()) + +public inline fun String.contains(seq: CharSequence) : Boolean = (this as java.lang.String).contains(seq) + +public inline fun String.endsWith(suffix: String) : Boolean = (this as java.lang.String).endsWith(suffix) + +public inline fun String.endsWith(ch: Char) : Boolean = (this as java.lang.String).endsWith(ch.toString()) + +// "constructors" for String + +public inline fun String(bytes : ByteArray, offset : Int, length : Int, charsetName : String) : String = java.lang.String(bytes, offset, length, charsetName) as String + +public inline fun String(bytes : ByteArray, offset : Int, length : Int, charset : java.nio.charset.Charset) : String = java.lang.String(bytes, offset, length, charset) as String + +public inline fun String(bytes : ByteArray, charsetName : String?) : String = java.lang.String(bytes, charsetName) as String + +public inline fun String(bytes : ByteArray, charset : java.nio.charset.Charset) : String = java.lang.String(bytes, charset) as String + +public inline fun String(bytes : ByteArray, i : Int, i1 : Int) : String = java.lang.String(bytes, i, i1) as String + +public inline fun String(bytes : ByteArray) : String = java.lang.String(bytes) as String + +public inline fun String(chars : CharArray) : String = java.lang.String(chars) as String + +public inline fun String(stringBuffer : java.lang.StringBuffer) : String = java.lang.String(stringBuffer) as String + +public inline fun String(stringBuilder : java.lang.StringBuilder) : String = java.lang.String(stringBuilder) as String + +public inline fun String.replaceFirst(regex : String, replacement : String) : String = (this as java.lang.String).replaceFirst(regex, replacement).sure() + +public inline fun String.charAt(index : Int) : Char = (this as java.lang.String).charAt(index).sure() + +public inline fun String.split(regex : String, limit : Int) : Array = (this as java.lang.String).split(regex, limit).sure() + +public inline fun String.codePointAt(index : Int) : Int = (this as java.lang.String).codePointAt(index).sure() + +public inline fun String.codePointBefore(index : Int) : Int = (this as java.lang.String).codePointBefore(index).sure() + +public inline fun String.codePointCount(beginIndex : Int, endIndex : Int) : Int = (this as java.lang.String).codePointCount(beginIndex, endIndex) + +public inline fun String.compareToIgnoreCase(str : String) : Int = (this as java.lang.String).compareToIgnoreCase(str).sure() + +public inline fun String.concat(str : String) : String = (this as java.lang.String).concat(str).sure() + +public inline fun String.contentEquals(cs : CharSequence) : Boolean = (this as java.lang.String).contentEquals(cs).sure() + +public inline fun String.contentEquals(sb : StringBuffer) : Boolean = (this as java.lang.String).contentEquals(sb).sure() + +public inline fun String.getBytes(charset : java.nio.charset.Charset) : ByteArray = (this as java.lang.String).getBytes(charset).sure() + +public inline fun String.getBytes(charsetName : String) : ByteArray = (this as java.lang.String).getBytes(charsetName).sure() + +public inline fun String.getChars(srcBegin : Int, srcEnd : Int, dst : CharArray, dstBegin : Int) : Tuple0 = (this as java.lang.String).getChars(srcBegin, srcEnd, dst, dstBegin).sure() + +public inline fun String.indexOf(ch : Char) : Int = (this as java.lang.String).indexOf(ch.toString()).sure() + +public inline fun String.indexOf(ch : Char, fromIndex : Int) : Int = (this as java.lang.String).indexOf(ch.toString(), fromIndex).sure() + +public inline fun String.intern() : String = (this as java.lang.String).intern().sure() + +public inline fun String.isEmpty() : Boolean = (this as java.lang.String).isEmpty().sure() + +public inline fun String.lastIndexOf(ch : Char, fromIndex : Int) : Int = (this as java.lang.String).lastIndexOf(ch.toString(), fromIndex).sure() + +public inline fun String.lastIndexOf(str : String, fromIndex : Int) : Int = (this as java.lang.String).lastIndexOf(str, fromIndex).sure() + +public inline fun String.matches(regex : String) : Boolean = (this as java.lang.String).matches(regex).sure() + +public inline fun String.offsetByCodePoints(index : Int, codePointOffset : Int) : Int = (this as java.lang.String).offsetByCodePoints(index, codePointOffset).sure() + +public inline fun String.regionMatches(ignoreCase : Boolean, toffset : Int, other : String, ooffset : Int, len : Int) : Boolean = (this as java.lang.String).regionMatches(ignoreCase, toffset, other, ooffset, len).sure() + +public inline fun String.regionMatches(toffset : Int, other : String, ooffset : Int, len : Int) : Boolean = (this as java.lang.String).regionMatches(toffset, other, ooffset, len).sure() + +public inline fun String.replace(target : CharSequence, replacement : CharSequence) : String = (this as java.lang.String).replace(target, replacement).sure() + +public inline fun String.subSequence(beginIndex : Int, endIndex : Int) : CharSequence = (this as java.lang.String).subSequence(beginIndex, endIndex).sure() + +public inline fun String.toLowerCase(locale : java.util.Locale) : String = (this as java.lang.String).toLowerCase(locale).sure() + +public inline fun String.toUpperCase(locale : java.util.Locale) : String = (this as java.lang.String).toUpperCase(locale).sure() + + +public inline fun CharSequence.charAt(index : Int) : Char = (this as java.lang.CharSequence).charAt(index) + +public inline fun CharSequence.get(index : Int) : Char = charAt(index) + +public inline fun CharSequence.subSequence(start : Int, end : Int) : CharSequence? = (this as java.lang.CharSequence).subSequence(start, end) + +public inline fun CharSequence.get(start : Int, end : Int) : CharSequence? = subSequence(start, end) + +public inline fun CharSequence.toString() : String? = (this as java.lang.CharSequence).toString() + +public inline fun CharSequence.length() : Int = (this as java.lang.CharSequence).length() + + +public inline fun String.toByteArray(encoding: String?=null):ByteArray { + if(encoding==null) { + return (this as java.lang.String).getBytes().sure() + } else { + return (this as java.lang.String).getBytes(encoding).sure() + } +} +public inline fun String.toByteArray(encoding: java.nio.charset.Charset):ByteArray = (this as java.lang.String).getBytes(encoding).sure() + +public inline fun String.toBoolean() : Boolean = java.lang.Boolean.parseBoolean(this).sure() +public inline fun String.toShort() : Short = java.lang.Short.parseShort(this).sure() +public inline fun String.toInt() : Int = java.lang.Integer.parseInt(this).sure() +public inline fun String.toLong() : Long = java.lang.Long.parseLong(this).sure() +public inline fun String.toFloat() : Float = java.lang.Float.parseFloat(this).sure() +public inline fun String.toDouble() : Double = java.lang.Double.parseDouble(this).sure() + +/** + * Converts the string into a regular expression [[Pattern]] optionally + * with the specified flags from [[Pattern]] or'd together + * so that strings can be split or matched on. + */ +public inline fun String.toRegex(flags: Int=0): java.util.regex.Pattern { + return java.util.regex.Pattern.compile(this, flags).sure() +} + +inline val String.reader : StringReader +get() = StringReader(this) + +inline val String.size : Int +get() = length() + + +/** + * Returns a copy of this string capitalised if it is not empty or already starting with an uppper case letter, otherwise returns this + * + * @includeFunctionBody ../../test/StringTest.kt capitalize + */ +public inline fun String.capitalize(): String { + return if (notEmpty() && charAt(0).isLowerCase()) substring(0, 1).toUpperCase() + substring(1) else this +} + +/** + * Returns a copy of this string with the first letter lower case if it is not empty or already starting with a lower case letter, otherwise returns this + * + * @includeFunctionBody ../../test/StringTest.kt decapitalize + */ +public inline fun String.decapitalize(): String { + return if (notEmpty() && charAt(0).isUpperCase()) substring(0, 1).toLowerCase() + substring(1) else this +} + diff --git a/libraries/stdlib/src/kotlin/dom/Dom.kt b/libraries/stdlib/src/kotlin/dom/Dom.kt index 34d6fdbefe0..0e4006ecf9b 100644 --- a/libraries/stdlib/src/kotlin/dom/Dom.kt +++ b/libraries/stdlib/src/kotlin/dom/Dom.kt @@ -74,6 +74,15 @@ set(value) { this.setAttribute("class", value) } +/** Returns true if the element has the given CSS class style in its 'class' attribute */ +fun Element.hasClass(cssClass: String): Boolean { + val c = this.classes + return if (c != null) + c.matches("""(^|.*\s+)$cssClass($|\s+.*)""") + else false +} + + /** Returns the children of the element as a list */ inline fun Element?.children(): List { return this?.childNodes.toList() @@ -130,8 +139,47 @@ inline fun NodeList?.toElementList(): List { } } +/** Searches for elements using the element name, an element ID (if prefixed with dot) or element class (if prefixed with #) */ +fun Document?.get(selector: String): List { + val root = this?.documentElement + return if (root != null) { + if (selector == "*") { + elements + } else if (selector.startsWith(".")) { + elements.filter{ it.hasClass(selector.substring(1)) }.toList() + } else if (selector.startsWith("#")) { + val id = selector.substring(1) + val element = this?.getElementById(id) + return if (element != null) + arrayList(element) + else + Collections.EMPTY_LIST as List + } else { + // assume its a vanilla element name + elements(selector) + } + } else { + Collections.EMPTY_LIST as List + } +} - +/** Searches for elements using the element name, an element ID (if prefixed with dot) or element class (if prefixed with #) */ +fun Element.get(selector: String): List { + return if (selector == "*") { + elements + } else if (selector.startsWith(".")) { + elements.filter{ it.hasClass(selector.substring(1)) }.toList() + } else if (selector.startsWith("#")) { + val element = this.ownerDocument?.getElementById(selector.substring(1)) + return if (element != null) + arrayList(element) + else + Collections.EMPTY_LIST as List + } else { + // assume its a vanilla element name + elements(selector) + } +} // Helper methods diff --git a/libraries/stdlib/src/kotlin/dom/DomJVM.kt b/libraries/stdlib/src/kotlin/dom/DomJVM.kt index dbcce1f6d5a..e7b5cad317e 100644 --- a/libraries/stdlib/src/kotlin/dom/DomJVM.kt +++ b/libraries/stdlib/src/kotlin/dom/DomJVM.kt @@ -92,6 +92,24 @@ val NamedNodeMap.length: Int get() = this.getLength() +/** + * Returns the HTML representation of the node + */ +public val Node.outerHTML: String +get() = toXmlString() + +/** + * Returns the HTML representation of the node + */ +public val Node.innerHTML: String +get() = childNodes.outerHTML + +/** + * Returns the HTML representation of the nodes + */ +public val NodeList.outerHTML: String +get() = toList().map { it.innerHTML }.makeString("") + /** Returns an [[Iterator]] of all the next [[Element]] siblings */ fun Node.nextElements(): Iterator = nextSiblings().filterIsInstance(javaClass()) @@ -99,48 +117,6 @@ fun Node.nextElements(): Iterator = nextSiblings().filterIsInstance = previousSiblings().filterIsInstance(javaClass()) -/** Searches for elements using the element name, an element ID (if prefixed with dot) or element class (if prefixed with #) */ -fun Document?.get(selector: String): List { - val root = this?.getDocumentElement() - return if (root != null) { - if (selector == "*") { - elements - } else if (selector.startsWith(".")) { - elements.filter{ it.hasClass(selector.substring(1)) }.toList() - } else if (selector.startsWith("#")) { - val id = selector.substring(1) - val element = this?.getElementById(id) - return if (element != null) - Collections.singletonList(element).sure() as List - else - Collections.EMPTY_LIST.sure() as List - } else { - // assume its a vanilla element name - elements(selector) - } - } else { - Collections.EMPTY_LIST as List - } -} - -/** Searches for elements using the element name, an element ID (if prefixed with dot) or element class (if prefixed with #) */ -fun Element.get(selector: String): List { - return if (selector == "*") { - elements - } else if (selector.startsWith(".")) { - elements.filter{ it.hasClass(selector.substring(1)) }.toList() - } else if (selector.startsWith("#")) { - val element = this.getOwnerDocument()?.getElementById(selector.substring(1)) - return if (element != null) - Collections.singletonList(element).sure() as List - else - Collections.EMPTY_LIST.sure() as List - } else { - // assume its a vanilla element name - elements(selector) - } -} - var Element.classSet : Set get() { val answer = LinkedHashSet() @@ -156,14 +132,6 @@ set(value) { this.classes = value.makeString(" ") } -/** Returns true if the element has the given CSS class style in its 'class' attribute */ -fun Element.hasClass(cssClass: String): Boolean { - val c = this.classes - return if (c != null) - c.matches("""(^|.*\s+)$cssClass($|\s+.*)""") - else false -} - /** Adds the given CSS class to this element's 'class' attribute */ fun Element.addClass(cssClass: String): Boolean { val classSet = this.classSet diff --git a/libraries/stdlib/test/StringJVMTest.kt b/libraries/stdlib/test/StringJVMTest.kt new file mode 100644 index 00000000000..b9d58c3815d --- /dev/null +++ b/libraries/stdlib/test/StringJVMTest.kt @@ -0,0 +1,69 @@ +package test.string + +import kotlin.test.* + +import org.junit.Test as test + +class StringJVMTest { + test fun stringIterator() { + var sum = 0 + for(c in "239") + sum += (c.toInt() - '0'.toInt()) + assertTrue(sum == 14) + } + + test fun stringBuilderIterator() { + var sum = 0 + val sb = StringBuilder() + for(c in "239") + sb.append(c) + + println(sb) + + for(c in sb) + sum += (c.toInt() - '0'.toInt()) + assertTrue(sum == 14) + } + + test fun orEmpty() { + val s: String? = "hey" + val ns: String? = null + + assertEquals("hey", s.orEmpty()) + assertEquals("", ns.orEmpty()) + } + + test fun toShort() { + assertEquals(77.toShort(), "77".toShort()) + } + + test fun toInt() { + assertEquals(77, "77".toInt()) + } + + test fun toLong() { + assertEquals(77.toLong(), "77".toLong()) + } + + test fun count() { + val text = "hello there\tfoo\nbar" + val whitespaceCount = text.count { it.isWhitespace() } + assertEquals(3, whitespaceCount) + } + + test fun testSplitByChar() { + val s = "ab\n[|^$&\\]^cd" + var list = s.split('b'); + assertEquals(2, list.size) + assertEquals("a", list[0]) + assertEquals("\n[|^$&\\]^cd", list[1]) + list = s.split('^') + assertEquals(3, list.size) + assertEquals("cd", list[2]) + list = s.split('.') + assertEquals(1, list.size) + assertEquals(s, list[0]) + } + + +} diff --git a/libraries/stdlib/test/StringTest.kt b/libraries/stdlib/test/StringTest.kt index 71895a05302..30e8a1ededc 100644 --- a/libraries/stdlib/test/StringTest.kt +++ b/libraries/stdlib/test/StringTest.kt @@ -1,69 +1,25 @@ -package test.string +package test -import kotlin.io.* import kotlin.test.* import org.junit.Test as test class StringTest { - test fun stringIterator() { - var sum = 0 - for(c in "239") - sum += (c.toInt() - '0'.toInt()) - assertTrue(sum == 14) + + test fun startsWith() { + assertTrue("abcd".startsWith("ab")) + assertTrue("abcd".startsWith("abcd")) + assertTrue("abcd".startsWith("a")) + assertFalse("abcd".startsWith("abcde")) + assertFalse("abcd".startsWith("b")) + assertFalse("".startsWith('a')) } - test fun stringBuilderIterator() { - var sum = 0 - val sb = StringBuilder() - for(c in "239") - sb.append(c) - - println(sb) - - for(c in sb) - sum += (c.toInt() - '0'.toInt()) - assertTrue(sum == 14) - } - - test fun orEmpty() { - val s: String? = "hey" - val ns: String? = null - - assertEquals("hey", s.orEmpty()) - assertEquals("", ns.orEmpty()) - } - - test fun toShort() { - assertEquals(77.toShort(), "77".toShort()) - } - - test fun toInt() { - assertEquals(77, "77".toInt()) - } - - test fun toLong() { - assertEquals(77.toLong(), "77".toLong()) - } - - test fun count() { - val text = "hello there\tfoo\nbar" - val whitespaceCount = text.count { it.isWhitespace() } - assertEquals(3, whitespaceCount) - } - - test fun testSplitByChar() { - val s = "ab\n[|^$&\\]^cd" - var list = s.split('b'); - assertEquals(2, list.size) - assertEquals("a", list[0]) - assertEquals("\n[|^$&\\]^cd", list[1]) - list = s.split('^') - assertEquals(3, list.size) - assertEquals("cd", list[2]) - list = s.split('.') - assertEquals(1, list.size) - assertEquals(s, list[0]) + test fun endsWith() { + assertTrue("abcd".endsWith("d")) + assertTrue("abcd".endsWith("abcd")) + assertFalse("abcd".endsWith("b")) + assertFalse("".endsWith('a')) } test fun testStartsWithChar() { diff --git a/libraries/stdlib/test/dom/DomBuilderTest.kt b/libraries/stdlib/test/dom/DomBuilderTest.kt index 6cd31a9f274..aa6681dfc26 100644 --- a/libraries/stdlib/test/dom/DomBuilderTest.kt +++ b/libraries/stdlib/test/dom/DomBuilderTest.kt @@ -1,16 +1,13 @@ package test.dom -import kotlin.* import kotlin.dom.* -import kotlin.util.* import kotlin.test.* import org.w3c.dom.* -import junit.framework.TestCase +import org.junit.Test as test -class DomBuilderTest() : TestCase() { +class DomBuilderTest() { - - fun testBuildDocument() { + test fun buildDocument() { var doc = createDocument() assertTrue { diff --git a/libraries/stdlib/test/dom/NextSiblingTest.kt b/libraries/stdlib/test/dom/NextSiblingTest.kt index 0c8649b8f11..b4ad9a166b0 100644 --- a/libraries/stdlib/test/dom/NextSiblingTest.kt +++ b/libraries/stdlib/test/dom/NextSiblingTest.kt @@ -1,8 +1,6 @@ package test.dom -import kotlin.* import kotlin.dom.* -import kotlin.util.* import kotlin.test.* import org.w3c.dom.* import org.junit.Test as test diff --git a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt index 094043717f4..6a990b16e98 100644 --- a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt +++ b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt @@ -50,6 +50,13 @@ import js.noImpl return if (answer == "Void") "Unit" else if (answer == "Object") "Any" else answer } + fun parameterTypeName(klass: Class?): String { + val answer = simpleTypeName(klass) + return if (answer == "String" || answer.endsWith("DocumentType")) { + answer + "?" + } else answer + } + for (klass in classes) { val interfaces = klass.getInterfaces() val extends = if (interfaces != null && interfaces.size == 1) ": ${interfaces[0]?.getSimpleName()}" else "" @@ -108,7 +115,7 @@ import js.noImpl // TODO in java 7 its not easy with reflection to get the parameter argument name... var counter = 0 - val parameters = parameterTypes.map?, String>{ "arg${++counter}: ${simpleTypeName(it)}" }.makeString(", ") + val parameters = parameterTypes.map?, String>{ "arg${++counter}: ${parameterTypeName(it)}" }.makeString(", ") val returnType = simpleTypeName(method.getReturnType()) println(" public fun ${method.getName()}($parameters): $returnType = js.noImpl") } diff --git a/libraries/tools/kotlin-js-library/pom.xml b/libraries/tools/kotlin-js-library/pom.xml index e3ba8ed196a..749a81ad04a 100644 --- a/libraries/tools/kotlin-js-library/pom.xml +++ b/libraries/tools/kotlin-js-library/pom.xml @@ -32,6 +32,7 @@ + @@ -39,18 +40,23 @@ + + + + @@ -61,6 +67,10 @@ + + + + + + + diff --git a/libraries/tools/pom.xml b/libraries/tools/pom.xml new file mode 100644 index 00000000000..045f2cf72d0 --- /dev/null +++ b/libraries/tools/pom.xml @@ -0,0 +1,28 @@ + + + + 4.0.0 + + + org.jetbrains.kotlin + kotlin-project + 0.1-SNAPSHOT + ../pom.xml + + + kotlin-js-tools + pom + + + A project to make it easier to compile and test the JS standard library as a single, repeatable build + + + + kotlin-js-library + kotlin-js-tests + kotlin-js-tests-junit + + +