support jquery style element / css style / id lookups of elements on a document/element - along with test cases

This commit is contained in:
James Strachan
2012-02-27 08:49:47 +00:00
parent 9e6a9e2dc7
commit dd36efd24d
3 changed files with 92 additions and 6 deletions
+2
View File
@@ -10,6 +10,8 @@ inline fun String.equalsIgnoreCase(anotherString: String) = (this as java.lang.S
inline fun String.indexOf(str : String) = (this as java.lang.String).indexOf(str)
inline fun String.matches(regex : String): Boolean = (this as java.lang.String).matches(regex)
inline fun String.indexOf(str : String, fromIndex : Int) = (this as java.lang.String).indexOf(str, fromIndex)
inline fun String.replace(oldChar: Char, newChar : Char) = (this as java.lang.String).replace(oldChar, newChar).sure()
+43 -6
View File
@@ -56,6 +56,7 @@ var Element.id : String
get() = this.getAttribute("id")?: ""
set(value) {
this.setAttribute("id", value)
this.setIdAttribute("id", true)
}
var Element.style : String
@@ -73,12 +74,32 @@ set(value) {
// Helper methods
fun Element.hasClass(cssClass: String): Boolean {
val c = this.cssClass
return if (c != null)
c.matches("""(^|\s+)$cssClass($|\s+)""")
else false
}
/** 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) {
root.get(selector)
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
this?.getElementsByTagName(selector).toElementList()
}
} else {
Collections.EMPTY_LIST as List<Element>
}
@@ -86,13 +107,20 @@ fun Document?.get(selector: String): 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> {
if (selector.startsWith(".")) {
// TODO filter by CSS style class
return if (selector == "*") {
elements
} else if (selector.startsWith(".")) {
elements.filter{ it.hasClass(selector.substring(1)) }.toList()
} else if (selector.startsWith("#")) {
// TODO lookup by ID
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
this.getElementsByTagName(selector).toElementList()
}
// TODO assume its a vanilla element name
return this.getElementsByTagName(selector).toElementList()
}
/** Returns the attribute value or null if its not present */
@@ -105,6 +133,13 @@ inline fun Element?.children(): List<Node> {
return this?.getChildNodes().toList()
}
val Document?.elements : List<Element>
get() = this?.getElementsByTagName("*").toElementList()
val Element?.elements : List<Element>
get() = this?.getElementsByTagName("*").toElementList()
inline fun Element?.elementsByTagNameNS(namespaceUri: String?, localName: String?): List<Element> {
return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList()
}
@@ -188,6 +223,8 @@ class ElementListAsList(val nodeList: NodeList): AbstractList<Element>() {
override fun size(): Int = nodeList.getLength()
}
// Syntax sugar
inline fun Node.plus(child: Node?): Node {
+47
View File
@@ -20,8 +20,10 @@ class DomBuilderTest() : TestSupport() {
style = "bold"
cssClass = "bar"
addElement("child") {
id = "id2"
cssClass = "another"
addElement("grandChild") {
id = "id3"
cssClass = "tiny"
addText("Hello World!")
// TODO support neater syntax sugar for adding text?
@@ -31,6 +33,51 @@ class DomBuilderTest() : TestSupport() {
}
println("builder document: ${doc.toXmlString()}")
// test css selections on document
assertEquals(0, doc[".doesNotExist"].size())
assertEquals(1, doc[".another"].size())
assertEquals(1, doc[".tiny"].size())
assertEquals(1, doc[".bar"].size())
// element tag selections
assertEquals(0, doc["doesNotExist"].size())
assertEquals(1, doc["foo"].size())
assertEquals(1, doc["child"].size())
assertEquals(1, doc["grandChild"].size())
// id selections
assertEquals(1, doc["#id1"].size())
assertEquals(1, doc["#id2"].size())
assertEquals(1, doc["#id3"].size())
val root = doc.rootElement
if (root != null) {
assert {
root.hasClass("bar")
}
// test css selections on element
assertEquals(0, root[".doesNotExist"].size())
assertEquals(1, root[".another"].size())
assertEquals(1, root[".tiny"].size())
assertEquals(0, root[".bar"].size())
// element tag selections
assertEquals(0, root["doesNotExist"].size())
assertEquals(0, root["foo"].size())
assertEquals(1, root["child"].size())
assertEquals(1, root["grandChild"].size())
// id selections
assertEquals(1, root["#id1"].size())
assertEquals(1, root["#id2"].size())
assertEquals(1, root["#id3"].size())
} else {
fail("No root!")
}
val grandChild = doc["grandChild"].first
if (grandChild != null) {
println("got element ${grandChild.toXmlString()} with text '${grandChild.text}`")