From 652836705bd2a11a939cf8d411743a5a8d805755 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sun, 21 May 2017 08:54:56 -0700 Subject: [PATCH] dom: intro `XMLTag::attributes_to_map` mapping `XMLStringAttr` to their value MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/dom/dom.nit | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/dom/dom.nit b/lib/dom/dom.nit index 8c4c08e..5a1b8c0 100644 --- a/lib/dom/dom.nit +++ b/lib/dom/dom.nit @@ -67,3 +67,27 @@ redef class XMLStartTag abort end end + +redef class XMLAttrTag + + # Attributes as a map (ignoring malformed attributes) + # + # ~~~ + # var xml = """ + # + # """.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 -- 1.7.9.5