From 1533801fc890ee70178f04059422e9ce0e41d239 Mon Sep 17 00:00:00 2001 From: coderabhishek Date: Tue, 8 Mar 2016 00:19:05 +0530 Subject: [PATCH 1/3] std.xml: accept single quotes for attributes Fixes issue 4509 - XML parser in std.xml throws TagException if the attr value is put in apostrophes. --- std/xml.d | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/std/xml.d b/std/xml.d index 2806ccd3222..c1b6c7e3ab6 100644 --- a/std/xml.d +++ b/std/xml.d @@ -1072,9 +1072,10 @@ class Tag munch(s,whitespace); reqc(s,'='); munch(s,whitespace); - reqc(s,'"'); - string val = decode(munch(s,"^\""), DecodeMode.LOOSE); - reqc(s,'"'); + char quote = requireOneOf(s,"'\""); + char[2] notQuote = ['^', quote]; + string val = decode(munch(s,notQuote[]), DecodeMode.LOOSE); + reqc(s,quote); munch(s,whitespace); attr[key] = val; } @@ -2723,6 +2724,16 @@ EOS"; } @system unittest +{ + string test_xml = ``; + + DocumentParser parser = new DocumentParser(test_xml); +} + +unittest { string s = q"EOS @@ -2868,6 +2879,15 @@ private s = s[1..$]; } + char requireOneOf(ref string s, string chars) + { + if (s.length == 0 || indexOf(chars,s[0]) == -1) + throw new TagException(""); + char ch = s[0]; + s = s[1..$]; + return ch; + } + size_t hash(string s,size_t h=0) @trusted nothrow { return typeid(s).getHash(&s) + h; @@ -2980,3 +3000,5 @@ private throw new XMLException(s); } } + + From 85bb6c20f821e0aa861b1923eea966d3c6bdfa79 Mon Sep 17 00:00:00 2001 From: anonymous Date: Tue, 5 Jul 2016 23:21:17 +0200 Subject: [PATCH 2/3] add asserts and attributes Also remove extra trailing newlines at the end of the file. --- std/xml.d | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/std/xml.d b/std/xml.d index c1b6c7e3ab6..940d59f878c 100644 --- a/std/xml.d +++ b/std/xml.d @@ -2725,15 +2725,21 @@ EOS"; @system unittest { - string test_xml = ``; + xml:lang="en" version="1.0" attr='a"b"c'> + `; DocumentParser parser = new DocumentParser(test_xml); + parser.onStartTag["stream:stream"] = (ElementParser p) { + assert(p.tag.attr["xmlns"] == "jabber:'client'"); + assert(p.tag.attr["from"] == "jid.pl"); + assert(p.tag.attr["attr"] == "a\"b\"c"); + }; } -unittest +@system unittest { string s = q"EOS @@ -2879,7 +2885,7 @@ private s = s[1..$]; } - char requireOneOf(ref string s, string chars) + char requireOneOf(ref string s, string chars) @safe { if (s.length == 0 || indexOf(chars,s[0]) == -1) throw new TagException(""); @@ -3000,5 +3006,3 @@ private throw new XMLException(s); } } - - From bab9832adf38972ee35308046399a5bc28d8f1f1 Mon Sep 17 00:00:00 2001 From: anonymous Date: Wed, 6 Jul 2016 23:45:01 +0200 Subject: [PATCH 3/3] actually do the parsing --- std/xml.d | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/std/xml.d b/std/xml.d index 940d59f878c..943d47a0db3 100644 --- a/std/xml.d +++ b/std/xml.d @@ -2732,11 +2732,15 @@ EOS"; `; DocumentParser parser = new DocumentParser(test_xml); + bool tested = false; parser.onStartTag["stream:stream"] = (ElementParser p) { assert(p.tag.attr["xmlns"] == "jabber:'client'"); assert(p.tag.attr["from"] == "jid.pl"); assert(p.tag.attr["attr"] == "a\"b\"c"); + tested = true; }; + parser.parse(); + assert(tested); } @system unittest