From 1105d9c39741f0429dc5f5549b12ab351bcf94b3 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Mon, 22 Nov 2004 00:33:04 +0000 Subject: [project @ 2004-11-22 00:33:04 by jmb] Improve plain text rendering (converts occurrences of '<' with '<') svn path=/import/netsurf/; revision=1369 --- content/content.c | 2 +- render/textplain.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ render/textplain.h | 2 ++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/content/content.c b/content/content.c index 6e988814d..052a35fe7 100644 --- a/content/content.c +++ b/content/content.c @@ -165,7 +165,7 @@ static const struct handler_entry handler_map[] = { html_reformat, html_destroy, html_stop, html_redraw, html_open, html_close, true}, - {textplain_create, html_process_data, textplain_convert, + {textplain_create, textplain_process_data, textplain_convert, 0, 0, 0, 0, 0, 0, true}, {0, 0, css_convert, 0, css_destroy, 0, 0, 0, 0, false}, #ifdef WITH_JPEG diff --git a/render/textplain.c b/render/textplain.c index 0a7f5db7c..4b08e0acc 100644 --- a/render/textplain.c +++ b/render/textplain.c @@ -9,6 +9,7 @@ #include "netsurf/content/content.h" #include "netsurf/render/html.h" #include "netsurf/render/textplain.h" +#include "netsurf/utils/messages.h" static const char header[] = "
";
@@ -25,6 +26,52 @@ bool textplain_create(struct content *c, const char *params[])
 	return true;
 }
 
+bool textplain_process_data(struct content *c, char *data,
+		unsigned int size)
+{
+	unsigned int i, s;
+	char *d, *p;
+	union content_msg_data msg_data;
+	bool ret;
+
+	/* count number of '<' in data buffer */
+	for (d = data, i = 0, s = 0; i != size; i++, d++) {
+		if (*d == '<')
+			s++;
+	}
+
+	/* create buffer for modified input */
+	d = calloc(size + 3*s, sizeof(char));
+	if (!d) {
+		msg_data.error = messages_get("NoMemory");
+		content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
+		return false;
+	}
+
+	/* copy data across to modified buffer,
+	 * replacing occurrences of '<' with '<'
+	 * This prevents the parser stripping sequences of '<...>'
+	 */
+	for (p = d, i = 0, s = 0; i != size; i++, data++) {
+		if (*data == '<') {
+			*p++ = '&';
+			*p++ = 'l';
+			*p++ = 't';
+			*p++ = ';';
+			s += 4;
+		}
+		else {
+			*p++ = *data;
+			s++;
+		}
+	}
+
+	ret = html_process_data(c, d, s);
+
+	free(d);
+
+	return ret;
+}
 
 bool textplain_convert(struct content *c, int width, int height)
 {
diff --git a/render/textplain.h b/render/textplain.h
index 63550e181..74d029932 100644
--- a/render/textplain.h
+++ b/render/textplain.h
@@ -11,6 +11,8 @@
 struct content;
 
 bool textplain_create(struct content *c, const char *params[]);
+bool textplain_process_data(struct content *c, char *data,
+		unsigned int size);
 bool textplain_convert(struct content *c, int width, int height);
 
 #endif
-- 
cgit v1.2.3