[evolution/wip/webkit2] Bug 740410 - Paragraph keeps Preformatted after paste



commit 7ed2306f3b9e6f9636f5710b62af167de3456e82
Author: Tomas Popela <tpopela redhat com>
Date:   Thu Nov 27 17:41:02 2014 +0100

    Bug 740410 - Paragraph keeps Preformatted after paste
    
    The problem was that the code didn't know anything about the type
    of block that we were pasting to, but was expecting the "Normal" block.

 e-util/e-html-editor-view-dom-functions.c |   87 +++++++++++++++++------------
 1 files changed, 51 insertions(+), 36 deletions(-)
---
diff --git a/e-util/e-html-editor-view-dom-functions.c b/e-util/e-html-editor-view-dom-functions.c
index fce2ca0..f70aa14 100644
--- a/e-util/e-html-editor-view-dom-functions.c
+++ b/e-util/e-html-editor-view-dom-functions.c
@@ -1487,12 +1487,19 @@ dom_convert_and_insert_html_into_selection (WebKitDOMDocument *document,
        gboolean has_selection;
        gchar *inner_html;
        gint citation_level;
-       WebKitDOMElement *element;
-       WebKitDOMNode *node;
-       WebKitDOMRange *range;
+       WebKitDOMElement *selection_start_marker, *selection_end_marker, *element;
+       WebKitDOMNode *node, *current_block;
 
        remove_input_event_listener_from_body (document);
 
+       e_html_editor_selection_save (selection);
+       selection_start_marker = webkit_dom_document_get_element_by_id (
+               document, "-x-evo-selection-start-marker");
+       selection_end_marker = webkit_dom_document_get_element_by_id (
+               document, "-x-evo-selection-end-marker");
+       current_block = get_parent_block_node_from_child (
+               WEBKIT_DOM_NODE (selection_start_marker));
+
        element = webkit_dom_document_create_element (document, "div", NULL);
        if (is_html) {
                gchar *inner_text;
@@ -1511,30 +1518,21 @@ dom_convert_and_insert_html_into_selection (WebKitDOMDocument *document,
 
        inner_html = webkit_dom_html_element_get_inner_html (
                WEBKIT_DOM_HTML_ELEMENT (element));
-       parse_html_into_paragraphs (document, element, inner_html);
+       parse_html_into_paragraphs (document, element, current_block, inner_html);
 
        g_free (inner_html);
 
        has_selection = !dom_selection_is_collapsed (document);
 
-       range = dom_get_range (document);
-       node = webkit_dom_range_get_end_container (range, NULL);
-       citation_level = get_citation_level (node, FALSE);
+       citation_level = get_citation_level (WEBKIT_DOM_NODE (selection_end_marker), FALSE);
        /* Pasting into the citation */
        if (citation_level > 0) {
                gint length;
                gint word_wrap_length = extension->priv->word_wrap_length;
-               WebKitDOMElement *selection_start_marker, *selection_end_marker;
                WebKitDOMElement *br;
                WebKitDOMNode *first_paragraph, *last_paragraph;
                WebKitDOMNode *child, *parent;
 
-               dom_selection_save (selection);
-               selection_start_marker = webkit_dom_document_get_element_by_id (
-                       document, "-x-evo-selection-start-marker");
-               selection_end_marker = webkit_dom_document_get_element_by_id (
-                       document, "-x-evo-selection-end-marker");
-
                first_paragraph = webkit_dom_node_get_first_child (
                        WEBKIT_DOM_NODE (element));
                last_paragraph = webkit_dom_node_get_last_child (
@@ -1680,15 +1678,20 @@ dom_convert_and_insert_html_into_selection (WebKitDOMDocument *document,
                                document, E_HTML_EDITOR_VIEW_COMMAND_DELETE, NULL);
 
                dom_restore_caret_position (document);
+               g_object_unref (element);
                goto out;
        }
 
+       remove_node (WEBKIT_DOM_NODE (selection_start_marker));
+       remove_node (WEBKIT_DOM_NODE (selection_end_marker));
+
        inner_html = webkit_dom_html_element_get_inner_html (
                WEBKIT_DOM_HTML_ELEMENT (element));
        dom_exec_command (
                document, E_HTML_EDITOR_VIEW_COMMAND_INSERT_HTML, inner_html);
        g_free (inner_html);
 
+       g_object_unref (element);
        dom_selection_save (document);
 
        element = webkit_dom_document_query_selector (
@@ -1712,7 +1715,6 @@ dom_convert_and_insert_html_into_selection (WebKitDOMDocument *document,
        if (element) {
                WebKitDOMNode *parent;
                WebKitDOMNode *child;
-               WebKitDOMElement *selection_marker;
 
                parent = webkit_dom_node_get_parent_node (
                        WEBKIT_DOM_NODE (element));
@@ -1729,14 +1731,14 @@ dom_convert_and_insert_html_into_selection (WebKitDOMDocument *document,
                        }
                }
 
-               selection_marker = webkit_dom_document_get_element_by_id (
+               selection_end_marker = webkit_dom_document_get_element_by_id (
                        document, "-x-evo-selection-end-marker");
 
                if (has_selection) {
                        /* Everything after the selection end marker have to be in separate
                         * paragraph */
                        child = webkit_dom_node_get_next_sibling (
-                               WEBKIT_DOM_NODE (selection_marker));
+                               WEBKIT_DOM_NODE (selection_end_marker));
                        /* Move the elements that are in the same paragraph as the selection end
                         * on the end of pasted text, but avoid BR on the end of paragraph */
                        while (child) {
@@ -1752,11 +1754,11 @@ dom_convert_and_insert_html_into_selection (WebKitDOMDocument *document,
                        webkit_dom_node_insert_before (
                                webkit_dom_node_get_parent_node (
                                        webkit_dom_node_get_parent_node (
-                                               WEBKIT_DOM_NODE (selection_marker))),
+                                               WEBKIT_DOM_NODE (selection_end_marker))),
                                parent,
                                webkit_dom_node_get_next_sibling (
                                        webkit_dom_node_get_parent_node (
-                                               WEBKIT_DOM_NODE (selection_marker))),
+                                               WEBKIT_DOM_NODE (selection_end_marker))),
                                NULL);
                        node = parent;
                } else {
@@ -1769,15 +1771,15 @@ dom_convert_and_insert_html_into_selection (WebKitDOMDocument *document,
                        /* Restore caret on the end of pasted text */
                        webkit_dom_node_insert_before (
                                node,
-                               WEBKIT_DOM_NODE (selection_marker),
+                               WEBKIT_DOM_NODE (selection_end_marker),
                                webkit_dom_node_get_first_child (node),
                                NULL);
 
-                       selection_marker = webkit_dom_document_get_element_by_id (
+                       selection_start_marker = webkit_dom_document_get_element_by_id (
                                document, "-x-evo-selection-start-marker");
                        webkit_dom_node_insert_before (
                                node,
-                               WEBKIT_DOM_NODE (selection_marker),
+                               WEBKIT_DOM_NODE (selection_start_marker),
                                webkit_dom_node_get_first_child (node),
                                NULL);
                }
@@ -1791,8 +1793,7 @@ dom_convert_and_insert_html_into_selection (WebKitDOMDocument *document,
                /* When pasting the content that was copied from the composer, WebKit
                 * restores the selection wrongly, thus is saved wrongly and we have
                 * to fix it */
-               WebKitDOMElement *selection_start_marker, *selection_end_marker;
-               WebKitDOMNode *paragraph, *parent;
+               WebKitDOMNode *paragraph, *parent, *clone1, *clone2;
 
                selection_start_marker = webkit_dom_document_get_element_by_id (
                        document, "-x-evo-selection-start-marker");
@@ -1802,8 +1803,12 @@ dom_convert_and_insert_html_into_selection (WebKitDOMDocument *document,
                paragraph = get_parent_block_node_from_child (
                        WEBKIT_DOM_NODE (selection_start_marker));
                parent = webkit_dom_node_get_parent_node (paragraph);
-               if (element_has_class (WEBKIT_DOM_ELEMENT (paragraph), "-x-evo-paragraph") &&
-                   element_has_class (WEBKIT_DOM_ELEMENT (parent), "-x-evo-paragraph"))
+               webkit_dom_element_remove_attribute (WEBKIT_DOM_ELEMENT (parent), "id");
+
+               /* Check if WebKit created wrong structure */
+               clone1 = webkit_dom_node_clone_node (WEBKIT_DOM_NODE (paragraph), FALSE);
+               clone2 = webkit_dom_node_clone_node (WEBKIT_DOM_NODE (parent), FALSE);
+               if (webkit_dom_node_is_equal_node (clone1, clone2))
                        fix_structure_after_pasting_multiline_content (paragraph);
 
                webkit_dom_node_insert_before (
@@ -2842,11 +2847,15 @@ append_new_paragraph (WebKitDOMElement *parent,
 static WebKitDOMElement *
 create_and_append_new_paragraph (WebKitDOMDocument *document,
                                  WebKitDOMElement *parent,
+                                 WebKitDOMNode *block,
                                  const gchar *content)
 {
        WebKitDOMElement *paragraph;
 
-       paragraph = dom_get_paragraph_element (document, -1, 0);
+       if (!block || WEBKIT_DOM_IS_HTML_DIV_ELEMENT (block))
+               paragraph = dom_get_paragraph_element (document, -1, 0);
+       else
+               paragraph = WEBKIT_DOM_ELEMENT (webkit_dom_node_clone_node (block, FALSE));
 
        webkit_dom_html_element_set_inner_html (
                WEBKIT_DOM_HTML_ELEMENT (paragraph),
@@ -2930,6 +2939,7 @@ check_if_end_paragraph (const gchar *input,
 static void
 parse_html_into_paragraphs (WebKitDOMDocument *document,
                             WebKitDOMElement *blockquote,
+                            WebKitDOMNode *block,
                             const gchar *html)
 {
        gboolean ignore_next_br = FALSE;
@@ -3037,14 +3047,17 @@ parse_html_into_paragraphs (WebKitDOMDocument *document,
 
                        if (g_strcmp0 (rest_to_insert, UNICODE_ZERO_WIDTH_SPACE) == 0) {
                                paragraph = create_and_append_new_paragraph (
-                                       document, blockquote, "<br>");
+                                       document, blockquote, block, "<br>");
                        } else if (prevent_block) {
                                gchar *html;
                                gchar *new_content;
 
-                               if (!paragraph)
-                                       paragraph = dom_get_paragraph_element (
-                                               document, -1, 0);
+                               if (!paragraph) {
+                                      if (!block || WEBKIT_DOM_IS_HTML_DIV_ELEMENT (block))
+                                               paragraph =dom_get_paragraph_element (document, -1, 0);
+                                       else
+                                               paragraph = WEBKIT_DOM_ELEMENT (webkit_dom_node_clone_node 
(block, FALSE));
+                               }
 
                                html = webkit_dom_html_element_get_inner_html (
                                        WEBKIT_DOM_HTML_ELEMENT (paragraph));
@@ -3064,7 +3077,7 @@ parse_html_into_paragraphs (WebKitDOMDocument *document,
                                g_free (new_content);
                        } else
                                paragraph = create_and_append_new_paragraph (
-                                       document, blockquote, rest_to_insert);
+                                       document, blockquote, block, rest_to_insert);
 
                        if (rest_to_insert && *rest_to_insert && prevent_block && paragraph) {
                                glong length = 0;
@@ -3093,13 +3106,14 @@ parse_html_into_paragraphs (WebKitDOMDocument *document,
                                        append_new_paragraph (blockquote, &paragraph);
 
                                paragraph = create_and_append_new_paragraph (
-                                       document, blockquote, "<br>");
+                                       document, blockquote, block, "<br>");
 
                                citation_was_first_element = FALSE;
                        } else if (first_element && !citation_was_first_element) {
                                paragraph = create_and_append_new_paragraph (
                                        document,
                                        blockquote,
+                                       block,
                                        "<br class=\"-x-evo-first-br\">");
                        }
                }
@@ -3173,10 +3187,10 @@ parse_html_into_paragraphs (WebKitDOMDocument *document,
 
                if (g_strcmp0 (rest_to_insert, UNICODE_ZERO_WIDTH_SPACE) == 0)
                        create_and_append_new_paragraph (
-                               document, blockquote, "<br>");
+                               document, blockquote, block, "<br>");
                else
                        create_and_append_new_paragraph (
-                               document, blockquote, rest_to_insert);
+                               document, blockquote, block, rest_to_insert);
 
                g_free (rest_to_insert);
        }
@@ -3513,7 +3527,7 @@ html_editor_convert_view_content (EHTMLEditorWebExtension *extension,
                empty = FALSE;
 
        if (!empty)
-               parse_html_into_paragraphs (document, content_wrapper, inner_html);
+               parse_html_into_paragraphs (document, content_wrapper, NULL, inner_html);
 
        if (!cite_body) {
                if (!empty) {
@@ -4797,6 +4811,7 @@ convert_element_from_html_to_plain_text (WebKitDOMDocument *document,
        parse_html_into_paragraphs (
                document,
                main_blockquote ? blockquote : WEBKIT_DOM_ELEMENT (element),
+               NULL,
                inner_html);
 
        if (main_blockquote) {


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