dom: intro `XMLTag::attributes_to_map` mapping `XMLStringAttr` to their value
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 21 May 2017 15:54:56 +0000 (08:54 -0700)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 23 May 2017 16:30:23 +0000 (09:30 -0700)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/dom/dom.nit

index 8c4c08e..5a1b8c0 100644 (file)
@@ -67,3 +67,27 @@ redef class XMLStartTag
                abort
        end
 end
+
+redef class XMLAttrTag
+
+       # Attributes as a map (ignoring malformed attributes)
+       #
+       # ~~~
+       # var xml = """
+       # <student first="Snow" last="Man"/>
+       # """.to_xml
+       #
+       # var attributes = xml["student"].first.as(XMLAttrTag).attributes_to_map
+       # assert attributes.join(", ", ":") == "first:Snow, last:Man"
+       # ~~~
+       fun attributes_to_map: Map[String, String]
+       do
+               var m = new Map[String, String]
+               for a in attributes do
+                       if a isa XMLStringAttr then
+                               m[a.name] = a.value
+                       end
+               end
+               return m
+       end
+end