[evolution] EHTMLEditorView - Fix appending a text to the link
- From: Tomas Popela <tpopela src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution] EHTMLEditorView - Fix appending a text to the link
- Date: Tue, 23 Jun 2015 14:19:17 +0000 (UTC)
commit f7cbd1865d8479911417fbafde422558eb44476f
Author: Tomas Popela <tpopela redhat com>
Date: Mon Jun 15 13:19:09 2015 +0200
EHTMLEditorView - Fix appending a text to the link
Don't try to append the text to the link if it starts with punctuation
or contains spaces. Also fix one leak.
e-util/e-html-editor-view.c | 37 +++++++++++++++++++++++++------------
1 files changed, 25 insertions(+), 12 deletions(-)
---
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index bfcc88b..9ae398c 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -39,6 +39,8 @@
#define URL_PROTOCOLS "news|telnet|nntp|file|https?|s?ftp||webcal|localhost"
#define URL_PATTERN "((((" URL_PROTOCOLS ")\\:\\/\\/)|(www\\.|ftp\\.))[^\\s\\/\\$\\.\\?#].[^\\s]*)"
#define URL_PATTERN_SPACE URL_PATTERN "\\s"
+/* Taken from camel-url-scanner.c */
+#define URL_INVALID_TRAILING_CHARS ",.:;?!-|}])\""
/* http://www.w3.org/TR/html5/forms.html#valid-e-mail-address */
#define E_MAIL_PATTERN \
@@ -1537,13 +1539,12 @@ html_editor_view_check_magic_links (EHTMLEditorView *view,
urls = g_match_info_fetch_all (match_info);
if (urls) {
- gchar *final_url, *url_end_raw;
+ const gchar *end_of_match = NULL;
+ gchar *final_url, *url_end_raw, *url_text;
glong url_start, url_end, url_length;
WebKitDOMDocument *document;
- WebKitDOMNode *url_text_node_clone;
WebKitDOMText *url_text_node;
WebKitDOMElement *anchor;
- const gchar* url_text;
document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
@@ -1556,8 +1557,18 @@ html_editor_view_check_magic_links (EHTMLEditorView *view,
* that we get from g_match_info_fetch_pos are not UTF-8 aware */
url_end_raw = g_strndup(node_text, end_pos_url);
url_end = g_utf8_strlen (url_end_raw, -1);
-
url_length = g_utf8_strlen (urls[0], -1);
+
+ end_of_match = url_end_raw + end_pos_url - (include_space ? 3 : 2);
+ /* URLs are extremely unlikely to end with any punctuation, so
+ * strip any trailing punctuation off from link and put it after
+ * the link. Do the same for any closing double-quotes as well. */
+ while (end_of_match && end_of_match != url_end_raw && strchr (URL_INVALID_TRAILING_CHARS,
*end_of_match)) {
+ url_length--;
+ url_end--;
+ end_of_match--;
+ }
+
url_start = url_end - url_length;
webkit_dom_text_split_text (
@@ -1565,12 +1576,11 @@ html_editor_view_check_magic_links (EHTMLEditorView *view,
include_space ? url_end - 1 : url_end,
NULL);
- url_text_node = webkit_dom_text_split_text (
+ webkit_dom_text_split_text (
WEBKIT_DOM_TEXT (node), url_start, NULL);
- url_text_node_clone = webkit_dom_node_clone_node (
- WEBKIT_DOM_NODE (url_text_node), TRUE);
- url_text = webkit_dom_text_get_whole_text (
- WEBKIT_DOM_TEXT (url_text_node_clone));
+ url_text_node = WEBKIT_DOM_TEXT (webkit_dom_node_get_next_sibling (node));
+ url_text = webkit_dom_character_data_get_data (
+ WEBKIT_DOM_CHARACTER_DATA (url_text_node));
if (g_str_has_prefix (url_text, "www."))
final_url = g_strconcat ("http://" , url_text, NULL);
@@ -1603,6 +1613,7 @@ html_editor_view_check_magic_links (EHTMLEditorView *view,
g_free (url_end_raw);
g_free (final_url);
+ g_free (url_text);
} else {
gboolean appending_to_link = FALSE;
gchar *href, *text, *url, *text_to_append = NULL;
@@ -1618,7 +1629,8 @@ html_editor_view_check_magic_links (EHTMLEditorView *view,
if (WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (prev_sibling)) {
text_to_append = webkit_dom_node_get_text_content (node);
if (text_to_append && *text_to_append &&
- !g_unichar_isspace (g_utf8_get_char (text_to_append))) {
+ !strstr (text_to_append, " ") &&
+ !strchr (URL_INVALID_TRAILING_CHARS, *text_to_append)) {
appending_to_link = TRUE;
parent = WEBKIT_DOM_ELEMENT (prev_sibling);
@@ -2863,7 +2875,8 @@ body_input_event_cb (WebKitDOMElement *element,
text = webkit_dom_node_get_text_content (node);
- if (g_strcmp0 (text, "") != 0 && !g_unichar_isspace (g_utf8_get_char (text))) {
+ if (text && *text && !strstr (text, " ") &&
+ !strchr (URL_INVALID_TRAILING_CHARS, *text)) {
WebKitDOMNode *prev_sibling;
prev_sibling = webkit_dom_node_get_previous_sibling (node);
@@ -5800,7 +5813,7 @@ create_anchor_for_link (const GMatchInfo *info,
/* URLs are extremely unlikely to end with any punctuation, so
* strip any trailing punctuation off from link and put it after
* the link. Do the same for any closing double-quotes as well. */
- while (end_of_match && end_of_match != match && strchr (",.:;?!-|}])\"", *end_of_match)) {
+ while (end_of_match && end_of_match != match && strchr (URL_INVALID_TRAILING_CHARS, *end_of_match)) {
truncate_from_end++;
end_of_match--;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]