[evolution/gnome-3-30] I#107 - Reply with selection and URL can produce extra letters



commit 2e87004096f2bf7d6456d7a5dd78dfd5b3f37e22
Author: Milan Crha <mcrha redhat com>
Date:   Wed Sep 5 11:19:52 2018 +0200

    I#107 - Reply with selection and URL can produce extra letters
    
    Closes https://gitlab.gnome.org/GNOME/evolution/issues/107

 src/e-util/test-html-editor-units-bugs.c           | 68 ++++++++++++++++++++++
 .../web-extension/e-editor-dom-functions.c         | 61 ++++++-------------
 2 files changed, 87 insertions(+), 42 deletions(-)
---
diff --git a/src/e-util/test-html-editor-units-bugs.c b/src/e-util/test-html-editor-units-bugs.c
index 3837927657..c7f669d18d 100644
--- a/src/e-util/test-html-editor-units-bugs.c
+++ b/src/e-util/test-html-editor-units-bugs.c
@@ -1388,6 +1388,73 @@ test_issue_103 (TestFixture *fixture)
        #undef LONG_URL
 }
 
+static void
+test_issue_107 (TestFixture *fixture)
+{
+       if (!test_utils_process_commands (fixture,
+               "mode:plain\n")) {
+               g_test_fail ();
+               return;
+       }
+
+       test_utils_insert_content (fixture,
+               "<pre>text\n"
+               "<a href=\"https://www.01.org/\";>https://www.01.org/</a>&#160;?\n"
+               "<a href=\"https://www.02.org/\";>https://www.02.org/</a>&#160;A\n"
+               "<a href=\"https://www.03.org/\";>https://www.03.org/</a>&#160;ěšč\n"
+               "<a href=\"https://www.04.org/\";>https://www.04.org/</a> ?\n"
+               "<a href=\"https://www.05.org/\";>https://www.05.org/</a>\n"
+               "<a href=\"https://www.06.org/\";>https://www.06.org/</a>&#160;\n"
+               "<a href=\"https://www.07.org/\";>https://www.07.org/</a>&#160;&#160;\n"
+               "<a href=\"https://www.08.org/\";>https://www.08.org/</a>&#160;&gt;&#160;&lt;&#160;\n"
+               "&lt;<a href=\"https://www.09.org/\";>https://www.09.org/</a>&gt;\n"
+               "&lt;<a href=\"https://www.10.org/\";>https://www.10.org/</a>&#160;?&gt;\n"
+               "&#160;<a href=\"https://www.11.org/\";>https://www.11.org/</a>&#160;\n"
+               "&lt;&#160;<a href=\"https://www.12.org/\";>https://www.12.org/</a>&#160;&gt;\n"
+               "&#160;&lt;<a href=\"https://www.13.org/\";>https://www.13.org/</a>&gt;&#160;\n</pre>"
+               "<span class=\"-x-evo-to-body\" data-credits=\"On Today, User wrote:\"></span>"
+               "<span class=\"-x-evo-cite-body\"></span>",
+               E_CONTENT_EDITOR_INSERT_REPLACE_ALL | E_CONTENT_EDITOR_INSERT_TEXT_HTML);
+
+       if (!test_utils_run_simple_test (fixture,
+               "",
+               HTML_PREFIX
+               "<div style=\"width: 71ch;\">On Today, User wrote:</div>"
+               "<blockquote type=\"cite\" " BLOCKQUOTE_STYLE ">"
+               "<div style=\"width: 71ch;\">&gt; text</div>"
+               "<div style=\"width: 71ch;\">&gt; <a 
href=\"https://www.01.org/\";>https://www.01.org/</a>&nbsp;?</div>"
+               "<div style=\"width: 71ch;\">&gt; <a 
href=\"https://www.02.org/\";>https://www.02.org/</a>&nbsp;A</div>"
+               "<div style=\"width: 71ch;\">&gt; <a 
href=\"https://www.03.org/\";>https://www.03.org/</a>&nbsp;ěšč</div>"
+               "<div style=\"width: 71ch;\">&gt; <a href=\"https://www.04.org/\";>https://www.04.org/</a> 
?</div>"
+               "<div style=\"width: 71ch;\">&gt; <a 
href=\"https://www.05.org/\";>https://www.05.org/</a></div>"
+               "<div style=\"width: 71ch;\">&gt; <a 
href=\"https://www.06.org/\";>https://www.06.org/</a>&nbsp;</div>"
+               "<div style=\"width: 71ch;\">&gt; <a 
href=\"https://www.07.org/\";>https://www.07.org/</a>&nbsp;&nbsp;</div>"
+               "<div style=\"width: 71ch;\">&gt; <a 
href=\"https://www.08.org/\";>https://www.08.org/</a>&nbsp;&gt;&nbsp;&lt;&nbsp;</div>"
+               "<div style=\"width: 71ch;\">&gt; &lt;<a 
href=\"https://www.09.org/\";>https://www.09.org/</a>&gt;</div>"
+               "<div style=\"width: 71ch;\">&gt; &lt;<a 
href=\"https://www.10.org/\";>https://www.10.org/</a>&nbsp;?&gt;</div>"
+               "<div style=\"width: 71ch;\">&gt; &nbsp;<a 
href=\"https://www.11.org/\";>https://www.11.org/</a>&nbsp;</div>"
+               "<div style=\"width: 71ch;\">&gt; &lt;&nbsp;<a 
href=\"https://www.12.org/\";>https://www.12.org/</a>&nbsp;&gt;</div>"
+               "<div style=\"width: 71ch;\">&gt; &nbsp;&lt;<a 
href=\"https://www.13.org/\";>https://www.13.org/</a>&gt;&nbsp;</div>"
+               "</blockquote>" HTML_SUFFIX,
+               "On Today, User wrote:\n"
+               "> text\n"
+               "> https://www.01.org/ ?\n"
+               "> https://www.02.org/ A\n"
+               "> https://www.03.org/ ěšč\n"
+               "> https://www.04.org/ ?\n"
+               "> https://www.05.org/\n";
+               "> https://www.06.org/ \n"
+               "> https://www.07.org/  \n"
+               "> https://www.08.org/ > < \n"
+               "> <https://www.09.org/>\n"
+               "> <https://www.10.org/ ?>\n"
+               ">  https://www.11.org/ \n"
+               "> < https://www.12.org/ >\n"
+               ">  <https://www.13.org/> ")) {
+               g_test_fail ();
+       }
+}
+
 void
 test_add_html_editor_bug_tests (void)
 {
@@ -1419,4 +1486,5 @@ test_add_html_editor_bug_tests (void)
        test_utils_add_test ("/bug/750636", test_bug_750636);
        test_utils_add_test ("/issue/86", test_issue_86);
        test_utils_add_test ("/issue/103", test_issue_103);
+       test_utils_add_test ("/issue/107", test_issue_107);
 }
diff --git a/src/modules/webkit-editor/web-extension/e-editor-dom-functions.c 
b/src/modules/webkit-editor/web-extension/e-editor-dom-functions.c
index ff92ddc9c2..098daafdcd 100644
--- a/src/modules/webkit-editor/web-extension/e-editor-dom-functions.c
+++ b/src/modules/webkit-editor/web-extension/e-editor-dom-functions.c
@@ -4952,8 +4952,7 @@ create_anchor_for_link (const GMatchInfo *info,
                         GString *res,
                         gpointer data)
 {
-       gboolean link_surrounded, ending_with_nbsp = FALSE;
-       gint offset = 0, truncate_from_end = 0;
+       gint offset = 0, truncate_from_end = 0, match_len;
        gint match_start, match_end;
        gchar *match;
        const gchar *end_of_match = NULL;
@@ -4962,15 +4961,21 @@ create_anchor_for_link (const GMatchInfo *info,
        match = g_match_info_fetch (info, 0);
        g_match_info_fetch_pos (info, 0, &match_start, &match_end);
 
-       if (g_str_has_suffix (match, "&nbsp;")) {
-               ending_with_nbsp = TRUE;
-               truncate_from_end = 6;
+       match_len = strlen (match);
+       end_of_match = match + match_end - match_start - 1;
+       while (truncate_from_end + 4 < match_len) {
+               if (truncate_from_end + 6 < match_len && g_ascii_strncasecmp (match + match_end - match_start 
- truncate_from_end - 6, "&nbsp;", 6) == 0) {
+                       truncate_from_end += 6;
+                       end_of_match -= 6;
+               } else if (g_ascii_strncasecmp (match + match_end - match_start - truncate_from_end - 4, 
"&lt;", 4) == 0 ||
+                          g_ascii_strncasecmp (match + match_end - match_start - truncate_from_end - 4, 
"&gt;", 4) == 0) {
+                       truncate_from_end += 4;
+                       end_of_match -= 4;
+               } else {
+                       break;
+               }
        }
 
-       if (g_str_has_prefix (match, "&nbsp;"))
-               offset += 6;
-
-       end_of_match = match + match_end - match_start - 1;
        /* Taken from camel-url-scanner.c */
        /* URLs are extremely unlikely to end with any punctuation, so
         * strip any trailing punctuation off from link and put it after
@@ -4981,35 +4986,13 @@ create_anchor_for_link (const GMatchInfo *info,
        }
        end_of_match++;
 
-       link_surrounded =
-               g_str_has_suffix (res->str, "&lt;");
-
-       if (link_surrounded) {
-               if (end_of_match && *end_of_match && strlen (match) > strlen (end_of_match) + 3)
-                       link_surrounded = link_surrounded && g_str_has_prefix (end_of_match - 3, "&gt;");
-               else
-                       link_surrounded = link_surrounded && g_str_has_suffix (match, "&gt;");
-
-               if (link_surrounded) {
-                       truncate_from_end += 4;
-                       end_of_match -= 4;
-               }
-       }
-
-       /* The ending ';' was counted when looking for the invalid trailing characters, substract it. */
-       if (link_surrounded || ending_with_nbsp) {
-               truncate_from_end -= 1;
-               end_of_match += 1;
-       }
-
        /* If there is non-breaking space in the match, remove it and everything
         * after it from the match */
-       if (!g_str_has_prefix (match, "&nbsp;") && !g_str_has_suffix (match, "&nbsp;") && (nbsp_match = 
strstr (match, "&nbsp;"))) {
-               glong after_nbsp_length = g_utf8_strlen (nbsp_match, -1);
+       if (!g_str_has_prefix (match, "&nbsp;") && !g_str_has_suffix (match, "&nbsp;") &&
+           (nbsp_match = strstr (match, "&nbsp;")) && nbsp_match < end_of_match) {
+               glong after_nbsp_length = strlen (nbsp_match);
                truncate_from_end = after_nbsp_length;
-               end_of_match -= after_nbsp_length;
-               if (link_surrounded)
-                       end_of_match += 4;
+               end_of_match = match + match_end - match_start - after_nbsp_length;
        }
 
        g_string_append (res, "<a href=\"");
@@ -5020,18 +5003,12 @@ create_anchor_for_link (const GMatchInfo *info,
                g_string_truncate (res, res->len - truncate_from_end);
 
        g_string_append (res, "\">");
-       g_string_append (res, match + offset);
-       if (truncate_from_end > 0)
-               g_string_truncate (res, res->len - truncate_from_end);
-
+       g_string_append_len (res, match + offset, match_len - offset - (truncate_from_end > 0 ? 
truncate_from_end : 0));
        g_string_append (res, "</a>");
 
        if (truncate_from_end > 0)
                g_string_append (res, end_of_match);
 
-       if (ending_with_nbsp)
-               g_string_append (res, "&nbsp;");
-
        g_free (match);
 
        return FALSE;


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