/pidgin/main: 4998e86453d0: GTK debug window: append using js, n...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Wed Nov 21 12:16:02 EST 2012


Changeset: 4998e86453d00899635c3e11e24df656f73668a9
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-11-21 18:15 +0100
Branch:	 default
URL: http://hg.pidgin.im/pidgin/main/rev/4998e86453d0

Description:

GTK debug window: append using js, not html

diffstat:

 libpurple/util.c  |  21 +++++++++++
 libpurple/util.h  |   9 +++++
 pidgin/gtkdebug.c |  97 ++++++++++++++++++++++++++++++++----------------------
 3 files changed, 87 insertions(+), 40 deletions(-)

diffs (195 lines):

diff --git a/libpurple/util.c b/libpurple/util.c
--- a/libpurple/util.c
+++ b/libpurple/util.c
@@ -4692,6 +4692,27 @@ purple_escape_filename(const char *str)
 	return buf;
 }
 
+gchar * purple_escape_js(const gchar *str)
+{
+	gchar *tmp, *esc;
+
+	esc = tmp = purple_utf8_try_convert(str);
+
+	esc = purple_strreplace(esc, "\\", "\\\\");
+	g_free(tmp); tmp = esc;
+
+	esc = purple_strreplace(esc, "'", "\\'");
+	g_free(tmp); tmp = esc;
+
+	esc = purple_strreplace(esc, "\n", "\\n");
+	g_free(tmp); tmp = esc;
+
+	esc = purple_strreplace(esc, "\r", "");
+	g_free(tmp); tmp = esc;
+
+	return esc;
+}
+
 void purple_restore_default_signal_handlers(void)
 {
 #ifndef _WIN32
diff --git a/libpurple/util.h b/libpurple/util.h
--- a/libpurple/util.h
+++ b/libpurple/util.h
@@ -1390,6 +1390,15 @@ const char *purple_unescape_filename(con
 const char *purple_escape_filename(const char *str);
 
 /**
+ * Escapes javascript-unfriendly substrings from a string.
+ *
+ * @param str The string to escape.
+ *
+ * @return The javascript-safe string (must be g_free'd after use).
+ */
+gchar * purple_escape_js(const gchar *str);
+
+/**
  * Restore default signal handlers for signals which might reasonably have
  * handlers. This should be called by a fork()'d child process, since child processes
  * inherit the handlers of the parent.
diff --git a/pidgin/gtkdebug.c b/pidgin/gtkdebug.c
--- a/pidgin/gtkdebug.c
+++ b/pidgin/gtkdebug.c
@@ -76,7 +76,22 @@ typedef struct
 	/* Regex */ \
 	"div.hide{display:none;}" \
 	"span.regex{background-color:#ffafaf;font-weight:bold;}" \
-	"</style></head><body class=l%d></body></html>"
+	"</style><script>" \
+	"function append(level, time, cat, msg) {" \
+		"var div = document.createElement('div');" \
+		"div.className = 'l' + level;" \
+		"div.appendChild(document.createTextNode('(' + time + ') '));" \
+		"if (cat) {" \
+			"var cat_n = document.createElement('b');" \
+			"cat_n.appendChild(document.createTextNode(cat + ':'));" \
+			"div.appendChild(cat_n);" \
+			"div.appendChild(document.createTextNode(' '));" \
+		"}" \
+		"div.appendChild(document.createTextNode(msg));" \
+		"document.body.appendChild(div);" \
+		"alert('appended');" \
+	"}" \
+	"</script></head><body class=l%d></body></html>"
 
 static DebugWindow *debug_win = NULL;
 static guint debug_enabled_timer = 0;
@@ -639,25 +654,39 @@ toolbar_context(GtkWidget *toolbar, GdkE
 }
 
 static void
-regex_html_appended_cb(GtkWebView *webview, WebKitDOMRange *range, DebugWindow *win)
+debug_window_appended(DebugWindow *win)
 {
-	if (!win || !win->window)
+	WebKitDOMDocument *dom;
+	WebKitDOMHTMLElement *body;
+	WebKitDOMNode *div;
+
+	if (!gtk_toggle_tool_button_get_active(
+		GTK_TOGGLE_TOOL_BUTTON(win->filter)))
 		return;
 
-	if (gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(win->filter))) {
-		WebKitDOMDocument *dom;
-		WebKitDOMHTMLElement *body;
-		WebKitDOMNode *div;
+	dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(win->text));
+	body = webkit_dom_document_get_body(dom);
+	div = webkit_dom_node_get_last_child(WEBKIT_DOM_NODE(body));
 
-		dom = webkit_web_view_get_dom_document(WEBKIT_WEB_VIEW(win->text));
-		body = webkit_dom_document_get_body(dom);
-		div = webkit_dom_node_get_last_child(WEBKIT_DOM_NODE(body));
+	if (webkit_dom_element_webkit_matches_selector(
+		WEBKIT_DOM_ELEMENT(div), "body>div:not(#pause)", NULL))
+		regex_match(win, dom, div);
+}
 
-		if (webkit_dom_element_webkit_matches_selector(WEBKIT_DOM_ELEMENT(div),
-		                                               "body>div:not(#pause)",
-		                                               NULL))
-			regex_match(win, dom, div);
+static gboolean debug_window_alert_cb(WebKitWebView *webview,
+	WebKitWebFrame *frame, gchar *message, gpointer _win)
+{
+	DebugWindow *win = _win;
+
+	if (!win || !win->window)
+		return FALSE;
+
+	if (g_strcmp0(message, "appended") == 0) {
+		debug_window_appended(win);
+		return TRUE;
 	}
+
+	return FALSE;
 }
 
 static DebugWindow *
@@ -831,8 +860,8 @@ debug_window_new(void)
 	gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
 	gtk_widget_show(frame);
 
-	g_signal_connect(G_OBJECT(win->text), "html-appended",
-	                 G_CALLBACK(regex_html_appended_cb), win);
+	g_signal_connect(G_OBJECT(win->text), "script-alert",
+		G_CALLBACK(debug_window_alert_cb), win);
 
 	clear_cb(NULL, win);
 
@@ -1000,41 +1029,29 @@ pidgin_debug_window_hide(void)
 
 static void
 pidgin_debug_print(PurpleDebugLevel level, const char *category,
-					 const char *arg_s)
+	const char *arg_s)
 {
-	gchar *ts_s;
-	gchar *esc_s, *cat_s, *tmp, *s;
+	gchar *esc_s;
 	const char *mdate;
 	time_t mtime;
+	gchar *js;
 
-	if (debug_win == NULL ||
-		!purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/debug/enabled"))
-	{
+	if (debug_win == NULL)
 		return;
-	}
+	if (!purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/debug/enabled"))
+		return;
 
 	mtime = time(NULL);
 	mdate = purple_utf8_strftime("%H:%M:%S", localtime(&mtime));
-	ts_s = g_strdup_printf("(%s) ", mdate);
-	if (category == NULL)
-		cat_s = g_strdup("");
-	else
-		cat_s = g_strdup_printf("<b>%s:</b> ", category);
 
-	tmp = purple_utf8_try_convert(arg_s);
-	esc_s = g_markup_escape_text(tmp, -1);
+	esc_s = purple_escape_js(arg_s);
 
-	s = g_strdup_printf("<div class=\"l%d\">%s%s%s</div>",
-	                    level, ts_s, cat_s, esc_s);
+	js = g_strdup_printf("append(%d, '%s', '%s', '%s');",
+		level, mdate, category ? category : "", esc_s);
+	g_free(esc_s);
 
-	g_free(ts_s);
-	g_free(cat_s);
-	g_free(esc_s);
-	g_free(tmp);
-
-	gtk_webview_append_html(GTK_WEBVIEW(debug_win->text), s);
-
-	g_free(s);
+	gtk_webview_safe_execute_script(GTK_WEBVIEW(debug_win->text), js);
+	g_free(js);
 }
 
 static gboolean



More information about the Commits mailing list