[Xfce4-commits] <postler:master> Escape '>' and escape arguments when linkifying

Christian Dywan noreply at xfce.org
Sat Jun 26 03:22:07 CEST 2010


Updating branch refs/heads/master
         to 5b1025bd6546b53c39c202e427f9abdb658b5bdc (commit)
       from ca7e6d033a8c2ac3647a3f1600459a0641131dca (commit)

commit 5b1025bd6546b53c39c202e427f9abdb658b5bdc
Author: Christian Dywan <christian at twotoasts.de>
Date:   Tue Jun 22 21:20:54 2010 +0200

    Escape '>' and escape arguments when linkifying

 postler/postler-content.vala |   15 +++++++++++----
 tests/parsing.vala           |   28 +++++++++++++++++++++++++++-
 2 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/postler/postler-content.vala b/postler/postler-content.vala
index 796ef76..fda1192 100644
--- a/postler/postler-content.vala
+++ b/postler/postler-content.vala
@@ -103,12 +103,18 @@ public class Postler.Content : WebKit.WebView {
         return mailer.substring (0, bracket).chomp ();
     }
 
-    internal static string linkify_address (string address, string? arguments) {
+    static string html_escape (string unescaped) {
+        return unescaped.replace ("\"", """)
+            .replace ("<", "<").replace (">", ">");
+    }
+
+    internal static string linkify_address (string address, string? arguments)
+        requires (arguments == null || !arguments.contains ("<")) {
         string[] parsed = Postler.Messages.parse_address (address);
         string name = parsed[0];
         string cleaned = parsed[1];
-        string quoted = cleaned.replace ("\"", """).replace ("<", "<");
-        string original = address.replace ("\"", """).replace ("<", "<");
+        string quoted = html_escape (cleaned);
+        string original = html_escape (address);
         return "<a href=\"" + quoted + (arguments != null ? arguments : "") +
             "\" title=\"" + original + "\">" + name + "</a>";
     }
@@ -389,7 +395,8 @@ public class Postler.Content : WebKit.WebView {
             /* Linkify From, To and Reply-To */
             /* FIXME: Use raw subject for argument? */
             /* TODO: Show addressbook icons beside addresses */
-            string arguments = "?from=" + recipient + "&subject=Re: " + subject;
+            string arguments = "?from=" + html_escape (recipient)
+                + "&subject=Re: " + subject;
             sender = linkify_address (from, arguments);
             if (recipient != "")
                 recipient = linkify_address (recipient, arguments);
diff --git a/tests/parsing.vala b/tests/parsing.vala
index 6ec88b2..ffa5e9f 100644
--- a/tests/parsing.vala
+++ b/tests/parsing.vala
@@ -27,7 +27,9 @@ const TestCase[] addresses = {
     { "kaufmann at heim.at", null },
     { "<kaufmann at heim.at>", "kaufmann at heim.at kaufmann at heim.at" },
     { "'Klaus' <klaus at heim.at>", "Klaus klaus at heim.at" },
-    { "\"Klaus\" <klaus at heim.at>", "Klaus klaus at heim.at" }
+    { "\"Klaus\" <klaus at heim.at>", "Klaus klaus at heim.at" },
+    { "=?iso-8859-1?Q?M=2C_Alf?= <alf.m at vw.de>",
+      "=?iso-8859-1?Q?M=2C_Alf?= alf.m at vw.de" }
 };
 
 void parsing_headers_address () {
@@ -40,6 +42,29 @@ void parsing_headers_address () {
     }
 }
 
+const TestCase[] linkifies = {
+    { "Kl <kl at ha.at>", "<a href=\"kl at ha.at\" title=\"Kl <kl at ha.at>\">Kl</a>" },
+    { "kl at ha.at", "<a href=\"kl at ha.at\" title=\"kl at ha.at\">kl at ha.at</a>" },
+    { "\"Kl\" <kl at ha.at>",
+      "<a href=\"kl at ha.at\" title=\""Kl" <kl at ha.at>\">Kl</a>" },
+    { "=?iso-8859-1?Q?M=2C_Alf?= <alf.m at vw.de>",
+      "<a href=\"alf.m at vw.de\" "
+      + "title=\"=?iso-8859-1?Q?M=2C_Alf?= <alf.m at vw.de>\">"
+      + "=?iso-8859-1?Q?M=2C_Alf?=</a>" }
+};
+
+void parsing_headers_linkify () {
+    foreach (var linkify in linkifies) {
+        string formatted = Postler.Content.linkify_address (linkify.data, null);
+        string expected = linkify.expected;
+        assert_string_equal (formatted, expected);
+    }
+    assert_string_equal (Postler.Content.linkify_address ("Kl <kl at ha.at>",
+        "?from=Kl <kl at ha.at>&subject=Re: Tomaten"),
+      "<a href=\"kl at ha.at?from=Kl <kl at ha.at>&subject=Re: Tomaten\" "
+      + "title=\"Kl <kl at ha.at>\">Kl</a>");
+}
+
 const TestCase[] mailers = {
     { "Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64)", "Claws Mail 3.7.6" },
     { "SAP Web 7.00", "SAP Web 7.00" },
@@ -76,6 +101,7 @@ void parsing_headers_encoded () {
 void main (string[] args) {
     Test.init (ref args);
     Test.add_func ("/parsing/headers/address", parsing_headers_address);
+    Test.add_func ("/parsing/headers/linkify", parsing_headers_linkify);
     Test.add_func ("/parsing/headers/mailer", parsing_headers_mailer);
     Test.add_func ("/parsing/headers/encoded", parsing_headers_encoded);
     Test.run ();



More information about the Xfce4-commits mailing list