support jquery style element / css style / id lookups of elements on a document/element - along with test cases
This commit is contained in:
@@ -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.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.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()
|
inline fun String.replace(oldChar: Char, newChar : Char) = (this as java.lang.String).replace(oldChar, newChar).sure()
|
||||||
|
|||||||
+43
-6
@@ -56,6 +56,7 @@ var Element.id : String
|
|||||||
get() = this.getAttribute("id")?: ""
|
get() = this.getAttribute("id")?: ""
|
||||||
set(value) {
|
set(value) {
|
||||||
this.setAttribute("id", value)
|
this.setAttribute("id", value)
|
||||||
|
this.setIdAttribute("id", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
var Element.style : String
|
var Element.style : String
|
||||||
@@ -73,12 +74,32 @@ set(value) {
|
|||||||
|
|
||||||
|
|
||||||
// Helper methods
|
// 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 #) */
|
/** 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> {
|
fun Document?.get(selector: String): List<Element> {
|
||||||
val root = this?.getDocumentElement()
|
val root = this?.getDocumentElement()
|
||||||
return if (root != null) {
|
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 {
|
} else {
|
||||||
Collections.EMPTY_LIST as List<Element>
|
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 #) */
|
/** 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> {
|
fun Element.get(selector: String): List<Element> {
|
||||||
if (selector.startsWith(".")) {
|
return if (selector == "*") {
|
||||||
// TODO filter by CSS style class
|
elements
|
||||||
|
} else if (selector.startsWith(".")) {
|
||||||
|
elements.filter{ it.hasClass(selector.substring(1)) }.toList()
|
||||||
} else if (selector.startsWith("#")) {
|
} 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 */
|
/** Returns the attribute value or null if its not present */
|
||||||
@@ -105,6 +133,13 @@ inline fun Element?.children(): List<Node> {
|
|||||||
return this?.getChildNodes().toList()
|
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> {
|
inline fun Element?.elementsByTagNameNS(namespaceUri: String?, localName: String?): List<Element> {
|
||||||
return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList()
|
return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList()
|
||||||
}
|
}
|
||||||
@@ -188,6 +223,8 @@ class ElementListAsList(val nodeList: NodeList): AbstractList<Element>() {
|
|||||||
override fun size(): Int = nodeList.getLength()
|
override fun size(): Int = nodeList.getLength()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Syntax sugar
|
// Syntax sugar
|
||||||
|
|
||||||
inline fun Node.plus(child: Node?): Node {
|
inline fun Node.plus(child: Node?): Node {
|
||||||
|
|||||||
@@ -20,8 +20,10 @@ class DomBuilderTest() : TestSupport() {
|
|||||||
style = "bold"
|
style = "bold"
|
||||||
cssClass = "bar"
|
cssClass = "bar"
|
||||||
addElement("child") {
|
addElement("child") {
|
||||||
|
id = "id2"
|
||||||
cssClass = "another"
|
cssClass = "another"
|
||||||
addElement("grandChild") {
|
addElement("grandChild") {
|
||||||
|
id = "id3"
|
||||||
cssClass = "tiny"
|
cssClass = "tiny"
|
||||||
addText("Hello World!")
|
addText("Hello World!")
|
||||||
// TODO support neater syntax sugar for adding text?
|
// TODO support neater syntax sugar for adding text?
|
||||||
@@ -31,6 +33,51 @@ class DomBuilderTest() : TestSupport() {
|
|||||||
}
|
}
|
||||||
println("builder document: ${doc.toXmlString()}")
|
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
|
val grandChild = doc["grandChild"].first
|
||||||
if (grandChild != null) {
|
if (grandChild != null) {
|
||||||
println("got element ${grandChild.toXmlString()} with text '${grandChild.text}`")
|
println("got element ${grandChild.toXmlString()} with text '${grandChild.text}`")
|
||||||
|
|||||||
Reference in New Issue
Block a user