[evolution/wip/webkit2] Bug 746072 - Magic smiley replacements too aggressive



commit 7b53d5682882b926d32b8c0fe75ea0568a2f3f9a
Author: Tomas Popela <tpopela redhat com>
Date:   Wed Apr 22 10:57:38 2015 +0200

    Bug 746072 - Magic smiley replacements too aggressive
    
    Skip the dash when trying to validate i.e. :-) against :) and always
    insert the unicode zero width space when inserting new smiley the have
    the selection restore on the right place.

 web-extensions/e-html-editor-view-dom-functions.c |   53 +++++++++++++++------
 1 files changed, 38 insertions(+), 15 deletions(-)
---
diff --git a/web-extensions/e-html-editor-view-dom-functions.c 
b/web-extensions/e-html-editor-view-dom-functions.c
index e2a363f..a0143d2 100644
--- a/web-extensions/e-html-editor-view-dom-functions.c
+++ b/web-extensions/e-html-editor-view-dom-functions.c
@@ -1093,8 +1093,8 @@ emoticon_insert_span (EEmoticon *emoticon,
        EHTMLEditorHistoryEvent *ev = NULL;
        EHTMLEditorUndoRedoManager *manager;
        EHTMLEditorWebExtension *extension = load_context->extension;
-       gboolean misplaced_selection = FALSE, empty = FALSE, smiley_written;
-       gchar *node_text = NULL, *content;
+       gboolean misplaced_selection = FALSE, smiley_written;
+       gchar *node_text = NULL;
        const gchar *emoticon_start;
        WebKitDOMDocument *document = load_context->document;
        WebKitDOMElement *selection_start_marker, *selection_end_marker;
@@ -1225,10 +1225,6 @@ emoticon_insert_span (EEmoticon *emoticon,
        if (WEBKIT_DOM_IS_TEXT (node))
                node_text = webkit_dom_text_get_whole_text (WEBKIT_DOM_TEXT (node));
 
-       content = webkit_dom_node_get_text_content (selection_end_marker_parent);
-       empty = !*content || (g_strcmp0 (content, UNICODE_ZERO_WIDTH_SPACE) == 0);
-       g_free (content);
-
        if (misplaced_selection) {
                /* Insert smiley and selection markers after it */
                webkit_dom_node_insert_before (
@@ -1257,9 +1253,8 @@ emoticon_insert_span (EEmoticon *emoticon,
        }
 
        /* &#8203 == UNICODE_ZERO_WIDTH_SPACE */
-       if (empty || !smiley_written)
-               webkit_dom_html_element_insert_adjacent_html (
-                       WEBKIT_DOM_HTML_ELEMENT (span), "afterend", "&#8203;", NULL);
+       webkit_dom_html_element_insert_adjacent_html (
+               WEBKIT_DOM_HTML_ELEMENT (span), "afterend", "&#8203;", NULL);
 
        if (ev) {
                WebKitDOMDocumentFragment *fragment;
@@ -1281,12 +1276,40 @@ emoticon_insert_span (EEmoticon *emoticon,
                emoticon_start = g_utf8_strrchr (
                        node_text, -1, g_utf8_get_char (emoticon->text_face));
                /* Check if the written smiley is really the one that we inserted. */
-               if (emoticon_start && g_str_has_prefix (emoticon_start, emoticon->text_face)) {
-                       webkit_dom_character_data_delete_data (
-                               WEBKIT_DOM_CHARACTER_DATA (node),
-                               g_utf8_strlen (node_text, -1) - strlen (emoticon_start),
-                               strlen (emoticon->text_face),
-                               NULL);
+               if (emoticon_start) {
+                       /* The written smiley is the same as text version. */
+                       if (g_str_has_prefix (emoticon_start, emoticon->text_face)) {
+                               webkit_dom_character_data_delete_data (
+                                       WEBKIT_DOM_CHARACTER_DATA (node),
+                                       g_utf8_strlen (node_text, -1) - strlen (emoticon_start),
+                                       strlen (emoticon->text_face),
+                                       NULL);
+                       } else {
+                               gboolean same = TRUE;
+                               gint ii = 0, jj = 0;
+
+                               /* Try to recognize smileys without the dash e.g. :). */
+                               while (emoticon_start[ii] && emoticon->text_face[jj]) {
+                                       if (emoticon_start[ii] == ':' && emoticon->text_face[jj] == ':') {
+                                               if (emoticon->text_face[jj+1] && emoticon->text_face[jj+1] == 
'-')
+                                                       ii++;
+                                                       jj+=2;
+                                                       continue;
+                                       }
+                                       if (emoticon_start[ii] == emoticon->text_face[jj]) {
+                                               ii++;
+                                               jj++;
+                                       } else
+                                               same = FALSE;
+                               }
+                               if (same) {
+                                       webkit_dom_character_data_delete_data (
+                                               WEBKIT_DOM_CHARACTER_DATA (node),
+                                               g_utf8_strlen (node_text, -1) - strlen (emoticon_start),
+                                               ii,
+                                               NULL);
+                               }
+                       }
                }
 
                e_html_editor_web_extension_set_is_smiley_written (extension, FALSE);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]