From 7f97f9710ee5fe04f338c3d5ea7870fb1288503b Mon Sep 17 00:00:00 2001 From: James Strachan Date: Fri, 24 Feb 2012 17:54:03 +0000 Subject: [PATCH] added attribute lookup helper method --- stdlib/ktSrc/dom/Dom.kt | 5 +++++ testlib/test/dom/DomBuilderTest.kt | 1 + 2 files changed, 6 insertions(+) diff --git a/stdlib/ktSrc/dom/Dom.kt b/stdlib/ktSrc/dom/Dom.kt index 50751901975..870da1c1b8e 100644 --- a/stdlib/ktSrc/dom/Dom.kt +++ b/stdlib/ktSrc/dom/Dom.kt @@ -95,6 +95,11 @@ fun Element.get(selector: String): List { return this.getElementsByTagName(selector).toElementList() } +/** Returns the attribute value or null if its not present */ +inline fun Element?.attribute(name: String): String? { + return this?.getAttribute(name) +} + /** Returns the children of the element as a list */ inline fun Element?.children(): List { return this?.getChildNodes().toList() diff --git a/testlib/test/dom/DomBuilderTest.kt b/testlib/test/dom/DomBuilderTest.kt index 2548268e1d8..1bce2f89534 100644 --- a/testlib/test/dom/DomBuilderTest.kt +++ b/testlib/test/dom/DomBuilderTest.kt @@ -35,6 +35,7 @@ class DomBuilderTest() : TestSupport() { if (grandChild != null) { println("got element ${grandChild.toXmlString()} with text '${grandChild.text}`") assertEquals("Hello World!", grandChild.text) + assertEquals("tiny", grandChild.attribute("class") ?: "") } else { fail("Not an Element $grandChild") }