lib/dom: intro `XMLStartTag::data`
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 26 Jun 2015 17:29:37 +0000 (13:29 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 30 Jun 2015 16:38:00 +0000 (12:38 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/dom/dom.nit

index 273941a..786bdcc 100644 (file)
@@ -41,3 +41,29 @@ redef class XMLEntity
                return res
        end
 end
+
+redef class XMLStartTag
+
+       # Content of this XML tag held within a `PCDATA` or `CDATA`
+       #
+       # ~~~
+       # var code = """
+       # <?xml version="1.0" encoding="us-ascii"?>
+       # <animal>
+       #     <cat/>
+       #     <tiger>This is a white tiger!</tiger>
+       #     <cat/>
+       # </animal>"""
+       #
+       # var xml = code.to_xml
+       # assert xml["animal"].first["tiger"].first.as(XMLStartTag).data == "This is a white tiger!"
+       # ~~~
+       fun data: String
+       do
+               for child in children do
+                       if child isa PCDATA then return child.content
+                       if child isa CDATA then return child.content
+               end
+               abort
+       end
+end