changeset 27393:57fdb7853fc7

Create a purple_markup_escape_text() function and use it in one place in oscar. This function is identical to glib's g_markup_escape_text() except that it does not replace ' with ' ' is not a valid HTML entity in HTML 4, and IE7 displays it as the raw characters and not as an apostrophe. gtk of course displays it as an apostrophe, but gtk seems to have no problems with unescaped apostrophes I really don't know why g_markup_escape_text() escapes this character. So this change should not affect Pidgin at all, and it should help any user of libpurple who displays our HTML in IE (or possibly other web browsers--I'm not sure how webkit handles ') Are people ok with this change? We should probably change a lot of other places to use this function instead of the glib one. Basically anything that converts text to html should use this. I think anything that escapes XML should continue using g_markup_escape_text(). And entry_key_pressed() in Finch can be changed to use this instead of g_markup_escape_text() and purple_strreplace()
author Mark Doliner <mark@kingant.net>
date Mon, 06 Jul 2009 23:46:56 +0000
parents d7ac2ad0db5a
children 66425e99d741
files libpurple/protocols/oscar/oscar.c libpurple/util.c libpurple/util.h
diffstat 3 files changed, 83 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/oscar/oscar.c	Mon Jul 06 23:24:37 2009 +0000
+++ b/libpurple/protocols/oscar/oscar.c	Mon Jul 06 23:46:56 2009 +0000
@@ -2222,7 +2222,7 @@
 		message = oscar_encoding_to_utf8(account, info->status_encoding,
 										 info->status, info->status_len);
 
-	tmp2 = tmp = (message ? g_markup_escape_text(message, -1) : NULL);
+	tmp2 = tmp = (message ? purple_markup_escape_text(message, -1) : NULL);
 
 	if (strcmp(status_id, OSCAR_STATUS_ID_AVAILABLE) == 0) {
 		if (info->itmsurl_encoding && info->itmsurl && info->itmsurl_len)
--- a/libpurple/util.c	Mon Jul 06 23:24:37 2009 +0000
+++ b/libpurple/util.c	Mon Jul 06 23:46:56 2009 +0000
@@ -942,6 +942,77 @@
  * Markup Functions
  **************************************************************************/
 
+/*
+ * This function is stolen from glib's gmarkup.c and modified to not
+ * replace ' with &apos;
+ */
+static void append_escaped_text(GString *str,
+		const gchar *text, gssize length)
+{
+	const gchar *p;
+	const gchar *end;
+	gunichar c;
+
+	p = text;
+	end = text + length;
+
+	while (p != end)
+	{
+		const gchar *next;
+		next = g_utf8_next_char (p);
+
+		switch (*p)
+		{
+			case '&':
+				g_string_append (str, "&amp;");
+				break;
+
+			case '<':
+				g_string_append (str, "&lt;");
+				break;
+
+			case '>':
+				g_string_append (str, "&gt;");
+				break;
+
+			case '"':
+				g_string_append (str, "&quot;");
+				break;
+
+			default:
+				c = g_utf8_get_char (p);
+				if ((0x1 <= c && c <= 0x8) ||
+						(0xb <= c && c <= 0xc) ||
+						(0xe <= c && c <= 0x1f) ||
+						(0x7f <= c && c <= 0x84) ||
+						(0x86 <= c && c <= 0x9f))
+					g_string_append_printf (str, "&#x%x;", c);
+				else
+					g_string_append_len (str, p, next - p);
+				break;
+		}
+
+		p = next;
+	}
+}
+
+/* This function is stolen from glib's gmarkup.c */
+gchar *purple_markup_escape_text(const gchar *text, gssize length)
+{
+	GString *str;
+
+	g_return_val_if_fail(text != NULL, NULL);
+
+	if (length < 0)
+		length = strlen(text);
+
+	/* prealloc at least as long as original text */
+	str = g_string_sized_new(length);
+	append_escaped_text(str, text, length);
+
+	return g_string_free(str, FALSE);
+}
+
 const char *
 purple_markup_unescape_entity(const char *text, int *length)
 {
--- a/libpurple/util.h	Mon Jul 06 23:24:37 2009 +0000
+++ b/libpurple/util.h	Mon Jul 06 23:46:56 2009 +0000
@@ -415,6 +415,17 @@
 /*@{*/
 
 /**
+ * Escapes special characters in a plain-text string so they display
+ * correctly as HTML.  For example, & is replaced with &amp; and < is
+ * replaced with &lt;
+ *
+ * This is exactly the same as g_markup_escape_text(), except that it
+ * does not change ' to &apos; because &apos; is not a valid HTML 4 entity,
+ * and is displayed literally in IE7.
+ */
+gchar *purple_markup_escape_text(const gchar *text, gssize length);
+
+/**
  * Finds an HTML tag matching the given name.
  *
  * This locates an HTML tag's start and end, and stores its attributes