[evolution] Composer: Correct conversion of TABLE into Plain Text



commit a8a54896807b7e67d3c9b4939a30983daa0d321e
Author: Milan Crha <mcrha redhat com>
Date:   Fri Oct 21 10:20:18 2022 +0200

    Composer: Correct conversion of TABLE into Plain Text
    
    The <TR> is similar to <BR> for the Plain text, thus when reading it
    add the new line, if needed.
    
    The WebKit returns correct `\n` in the node's innerText, but using it
    as a plain text hides it for the user, thus split the lines and add
    them as separate paragraphs.

 data/webkit/e-convert.js      |  2 +-
 data/webkit/e-editor.js       | 21 ++++++++++++++++++---
 src/e-util/e-markdown-utils.c | 19 +++++++++++++------
 3 files changed, 32 insertions(+), 10 deletions(-)
---
diff --git a/data/webkit/e-convert.js b/data/webkit/e-convert.js
index a1fe4a4422..7cd54db2cd 100644
--- a/data/webkit/e-convert.js
+++ b/data/webkit/e-convert.js
@@ -982,7 +982,7 @@ EvoConvert.processNode = function(node, normalDivWidth, quoteLevel)
                        }
 
                        if ((!isBlockquote || !str.endsWith("\n")) &&
-                           str != "\n" && ((style && style.display == "block") || node.tagName == 
"ADDRESS")) {
+                           str != "\n" && ((style && style.display == "block") || node.tagName == "ADDRESS" 
|| node.tagName == "TR")) {
                                str += "\n";
                        }
                }
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index bff3f1a8ae..fb7d48381e 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -2286,10 +2286,25 @@ EvoEditor.convertTags = function()
        list = document.getElementsByTagName("TABLE");
 
        for (ii = list.length - 1; ii >= 0; ii--) {
-               var table = list[ii], textNode;
+               var table = list[ii], lines, jj;
+
+               lines = table.innerText.split("\n");
+
+               for (jj = 0; jj < lines.length; jj++) {
+                       var line, divNode;
+
+                       line = lines[jj];
+                       divNode = document.createElement("DIV");
+
+                       table.parentElement.insertBefore(divNode, table);
+
+                       if (!line.length) {
+                               divNode.appendChild(document.createElement("BR"));
+                       } else {
+                               divNode.innerText = line;
+                       }
+               }
 
-               textNode = document.createTextNode(table.innerText);
-               table.parentElement.insertBefore(textNode, table);
                table.remove();
        }
 
diff --git a/src/e-util/e-markdown-utils.c b/src/e-util/e-markdown-utils.c
index a57d6eff6c..0af3a8a14a 100644
--- a/src/e-util/e-markdown-utils.c
+++ b/src/e-util/e-markdown-utils.c
@@ -224,7 +224,10 @@ markdown_utils_sax_start_element_cb (gpointer ctx,
                        if (data->quote_prefix->len)
                                g_string_append (data->buffer, data->quote_prefix->str);
                } else if (!data->composer_quirks.enabled || !was_in_div_begin) {
-                       g_string_append (data->buffer, "<br>");
+                       if (data->in_pre)
+                               g_string_append_c (data->buffer, '\n');
+                       else
+                               g_string_append (data->buffer, "<br>");
                }
 
                return;
@@ -426,9 +429,9 @@ markdown_utils_sax_end_element_cb (gpointer ctx,
 
                if (data->in_pre > 0) {
                        data->in_pre--;
-                       if (data->in_pre == 0 && !data->plain_text)
-                               g_string_append (data->buffer, "```");
                        g_string_append_c (data->buffer, '\n');
+                       if (data->in_pre == 0 && !data->plain_text)
+                               g_string_append (data->buffer, "```\n");
                }
                return;
        }
@@ -444,6 +447,7 @@ markdown_utils_sax_end_element_cb (gpointer ctx,
 
        if (g_ascii_strcasecmp (name, "p") == 0 ||
            g_ascii_strcasecmp (name, "div") == 0 ||
+           g_ascii_strcasecmp (name, "tr") == 0 ||
            g_ascii_strcasecmp (name, "h1") == 0 ||
            g_ascii_strcasecmp (name, "h2") == 0 ||
            g_ascii_strcasecmp (name, "h3") == 0 ||
@@ -460,10 +464,13 @@ markdown_utils_sax_end_element_cb (gpointer ctx,
                                g_string_append (data->buffer, "<br>");
                }
 
-               data->in_paragraph_end = TRUE;
+               if (g_ascii_strcasecmp (name, "tr") != 0) {
+                       data->in_paragraph_end = TRUE;
+
+                       if (data->in_paragraph > 0)
+                               data->in_paragraph--;
+               }
 
-               if (data->in_paragraph > 0)
-                       data->in_paragraph--;
                return;
        }
 


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