From ee13df31e9b151a4aaeac6004c5437232847c464 Mon Sep 17 00:00:00 2001
From: Milan Crha <mcrha@redhat.com>
Date: Thu, 14 Jan 2010 13:17:19 +0000
Subject: Bug #606167 - Crash on composer Link editing

---
diff --git a/components/editor/gtkhtml-editor-signals.c b/components/editor/gtkhtml-editor-signals.c
index b1e613d..071cdf8 100644
--- a/components/editor/gtkhtml-editor-signals.c
+++ b/components/editor/gtkhtml-editor-signals.c
@@ -1018,6 +1018,42 @@ gtkhtml_editor_image_properties_url_entry_changed_cb (GtkWidget *window))
  * Link Properties Window
  *****************************************************************************/
 
+static gchar *
+sanitize_description_text (const gchar *ptext)
+{
+	gchar *p, *text = g_strdup (ptext);
+
+	if (!text)
+		return text;
+
+	/* replace line breakers with spaces */
+	for (p = text; *p; p++) {
+		if (*p == '\r' || *p == '\n')
+			*p = ' ';
+	}
+
+	return g_strstrip (text);
+}
+
+static void
+update_link_offsets (GtkhtmlEditor *editor)
+{
+	Link *link;
+	GtkHTML *html;
+
+	g_return_if_fail (editor != NULL);
+	g_return_if_fail (GTKHTML_IS_EDITOR (editor));
+	g_return_if_fail (editor->priv != NULL);
+
+	html = gtkhtml_editor_get_html (editor);
+
+	link = html_text_get_link_at_offset (HTML_TEXT (html->engine->cursor->object), html->engine->cursor->offset);
+	g_return_if_fail (link != NULL);
+
+	editor->priv->link_start_offset = link->start_offset;
+	editor->priv->link_end_offset = link->end_offset;
+}
+
 AUTOCONNECTED_SIGNAL_HANDLER (void
 gtkhtml_editor_link_properties_description_changed_cb (GtkWidget *window))
 {
@@ -1033,8 +1069,7 @@ gtkhtml_editor_link_properties_description_changed_cb (GtkWidget *window))
 	dsc_entry = WIDGET (LINK_PROPERTIES_DESCRIPTION_ENTRY);
 	url_entry = WIDGET (LINK_PROPERTIES_URL_ENTRY);
 
-	text = g_strstrip (g_strdup (
-		gtk_entry_get_text (GTK_ENTRY (dsc_entry))));
+	text = sanitize_description_text (gtk_entry_get_text (GTK_ENTRY (dsc_entry)));
 
 	editor->priv->link_custom_description = (*text != '\0');
 
@@ -1063,7 +1098,7 @@ gtkhtml_editor_link_properties_description_changed_cb (GtkWidget *window))
 			gtk_entry_get_text (GTK_ENTRY (url_entry)));
 
 		editor->priv->link_object = html->engine->cursor->object;
-		editor->priv->link_end_offset = start_offset + length;
+		update_link_offsets (editor);
 	}
 
 	g_free (text);
@@ -1094,7 +1129,9 @@ gtkhtml_editor_link_properties_url_changed_cb (GtkWidget *window))
 	if (html_engine_is_selection_active (html->engine)) {
 		html_engine_set_link (html->engine, text);
 	} else if (!editor->priv->link_custom_description) {
-		gtk_entry_set_text (GTK_ENTRY (dsc_entry), text);
+		gchar *descr = sanitize_description_text (text);
+		gtk_entry_set_text (GTK_ENTRY (dsc_entry), descr);
+		g_free (descr);
 		editor->priv->link_custom_description = FALSE;
 	} else {
 		gint start_offset;
@@ -1121,7 +1158,7 @@ gtkhtml_editor_link_properties_url_changed_cb (GtkWidget *window))
 			html->engine, descr, length, text);
 
 		editor->priv->link_object = html->engine->cursor->object;
-		editor->priv->link_end_offset = start_offset + length;
+		update_link_offsets (editor);
 	}
 
 	g_free (text);
@@ -1195,6 +1232,13 @@ gtkhtml_editor_link_properties_show_window_cb (GtkWidget *window))
 		GTK_ENTRY (url_entry),
 		(url != NULL) ? url : "http://");
 
+	if (dsc) {
+		gchar *tmp = dsc;
+
+		dsc = sanitize_description_text (dsc);
+		g_free (tmp);
+	}
+
 	gtk_entry_set_text (GTK_ENTRY (dsc_entry), dsc ? dsc : "");
 	gtk_widget_grab_focus (url_entry);
 
diff --git a/gtkhtml/htmlengine-edit-cut-and-paste.c b/gtkhtml/htmlengine-edit-cut-and-paste.c
index 6464823..17821b6 100644
--- a/gtkhtml/htmlengine-edit-cut-and-paste.c
+++ b/gtkhtml/htmlengine-edit-cut-and-paste.c
@@ -1490,7 +1490,7 @@ html_engine_paste_link (HTMLEngine *e, const gchar *text, gint len, const gchar
 	}
 
 	html_engine_paste_text (e, text, len);
-	html_text_add_link (HTML_TEXT (e->cursor->object), e, url, target, e->cursor->offset - len, e->cursor->offset);
+	html_text_add_link (HTML_TEXT (e->cursor->object), e, url, target, e->cursor->offset >= len ? e->cursor->offset - len : 0, e->cursor->offset);
 
 	g_free (url);
 }
diff --git a/gtkhtml/htmltext.c b/gtkhtml/htmltext.c
index e48bfff..1db8812 100644
--- a/gtkhtml/htmltext.c
+++ b/gtkhtml/htmltext.c
@@ -3111,7 +3111,7 @@ html_text_get_text (HTMLText *text, guint offset)
 {
 	gchar *s = text->text;
 
-	while (offset--)
+	while (offset-- && s && *s)
 		s = g_utf8_next_char (s);
 
 	return s;
--
cgit v0.8.3.1
