From 0d34e46eb932b1089a6b6fff68fa32145879ecd2 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Mon, 5 Mar 2012 12:07:23 +0000 Subject: [PATCH] tidy up the dom API a little --- stdlib/ktSrc/dom/Dom.kt | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/stdlib/ktSrc/dom/Dom.kt b/stdlib/ktSrc/dom/Dom.kt index 15e10aca02f..10316768927 100644 --- a/stdlib/ktSrc/dom/Dom.kt +++ b/stdlib/ktSrc/dom/Dom.kt @@ -165,7 +165,7 @@ fun Document?.get(selector: String): List { Collections.EMPTY_LIST.sure() as List } else { // assume its a vanilla element name - this?.getElementsByTagName(selector).toElementList() + elements(selector) } } else { Collections.EMPTY_LIST as List @@ -186,7 +186,7 @@ fun Element.get(selector: String): List { Collections.EMPTY_LIST.sure() as List } else { // assume its a vanilla element name - this.getElementsByTagName(selector).toElementList() + elements(selector) } } @@ -200,18 +200,32 @@ inline fun Element?.children(): List { return this?.getChildNodes().toList() } +/** The child elements of this document */ val Document?.elements : List get() = this?.getElementsByTagName("*").toElementList() +/** The child elements of this elements */ val Element?.elements : List get() = this?.getElementsByTagName("*").toElementList() -inline fun Element?.elementsByTagNameNS(namespaceUri: String?, localName: String?): List { +/** Returns all the child elements given the local element name */ +inline fun Element?.elements(localName: String?): List { + return this?.getElementsByTagName(localName).toElementList() +} + +/** Returns all the elements given the local element name */ +inline fun Document?.elements(localName: String?): List { + return this?.getElementsByTagName(localName).toElementList() +} + +/** Returns all the child elements given the namespace URI and local element name */ +inline fun Element?.elements(namespaceUri: String?, localName: String?): List { return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList() } -inline fun Document?.elementsByTagNameNS(namespaceUri: String?, localName: String?): List { +/** Returns all the elements given the namespace URI and local element name */ +inline fun Document?.elements(namespaceUri: String?, localName: String?): List { return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList() } @@ -308,27 +322,25 @@ inline fun Element.plusAssign(text: String?): Element = this.addText(text) // Builder -/* -Creates a new element which can be configured via a function -*/ +/** + * Creates a new element which can be configured via a function + */ fun Document.createElement(name: String, init: Element.()-> Unit): Element { val elem = this.createElement(name).sure() elem.init() return elem } -/* -Creates a new element to an element which has an owner Document which can be configured via a function -*/ +/** + * Creates a new element to an element which has an owner Document which can be configured via a function + */ fun Element.createElement(name: String, doc: Document? = null, init: Element.()-> Unit): Element { val elem = ownerDocument(doc).createElement(name).sure() elem.init() return elem } -/* -Returns the owner document of the element or uses the provided document -*/ +/** Returns the owner document of the element or uses the provided document */ fun Node.ownerDocument(doc: Document? = null): Document { val answer = if (this is Document) this as Document else if (doc == null) this.getOwnerDocument()