started compiling more of the kotlin library to JS; now testing the String methods in JS

This commit is contained in:
James Strachan
2012-06-19 07:23:42 -05:00
parent 33b5e46201
commit 071e276e08
18 changed files with 705 additions and 362 deletions
+1 -189
View File
@@ -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<Char> = 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<String> = (this as java.lang.String).split(regex) as Array<String>
public inline fun String.split(ch : Char) : Array<String> = (this as java.lang.String).split(java.util.regex.Pattern.quote(ch.toString())) as Array<String>
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<String?> = (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
+195
View File
@@ -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<Char> = 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<String> = (this as java.lang.String).split(regex) as Array<String>
public inline fun String.split(ch : Char) : Array<String> = (this as java.lang.String).split(java.util.regex.Pattern.quote(ch.toString())) as Array<String>
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<String?> = (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
}
+49 -1
View File
@@ -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<Node> {
return this?.childNodes.toList()
@@ -130,8 +139,47 @@ inline fun NodeList?.toElementList(): List<Element> {
}
}
/** 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<Element> {
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>(element)
else
Collections.EMPTY_LIST as List<Element>
} else {
// assume its a vanilla element name
elements(selector)
}
} else {
Collections.EMPTY_LIST as List<Element>
}
}
/** 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<Element> {
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>(element)
else
Collections.EMPTY_LIST as List<Element>
} else {
// assume its a vanilla element name
elements(selector)
}
}
// Helper methods
+18 -50
View File
@@ -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<Node, String> { it.innerHTML }.makeString("")
/** Returns an [[Iterator]] of all the next [[Element]] siblings */
fun Node.nextElements(): Iterator<Element> = nextSiblings().filterIsInstance<Node, Element>(javaClass<Element>())
@@ -99,48 +117,6 @@ fun Node.nextElements(): Iterator<Element> = nextSiblings().filterIsInstance<Nod
fun Node.previousElements(): Iterator<Element> = previousSiblings().filterIsInstance<Node, Element>(javaClass<Element>())
/** 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<Element> {
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<Element>
else
Collections.EMPTY_LIST.sure() as List<Element>
} else {
// assume its a vanilla element name
elements(selector)
}
} else {
Collections.EMPTY_LIST as List<Element>
}
}
/** 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<Element> {
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<Element>
else
Collections.EMPTY_LIST.sure() as List<Element>
} else {
// assume its a vanilla element name
elements(selector)
}
}
var Element.classSet : Set<String>
get() {
val answer = LinkedHashSet<String>()
@@ -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
+69
View File
@@ -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])
}
}
+14 -58
View File
@@ -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() {
+3 -6
View File
@@ -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 {
@@ -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
@@ -50,6 +50,13 @@ import js.noImpl
return if (answer == "Void") "Unit" else if (answer == "Object") "Any" else answer
}
fun parameterTypeName(klass: Class<out Any?>?): 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<Class<out Any?>?, String>{ "arg${++counter}: ${simpleTypeName(it)}" }.makeString(", ")
val parameters = parameterTypes.map<Class<out Any?>?, String>{ "arg${++counter}: ${parameterTypeName(it)}" }.makeString(", ")
val returnType = simpleTypeName(method.getReturnType())
println(" public fun ${method.getName()}($parameters): $returnType = js.noImpl")
}