[evolution] EHTMLEditorSelection - Fix change of a block format to another one when the citation is selected



commit 35a69d4085ac6964a78862a2fbfcaf9f6e13d896
Author: Tomas Popela <tpopela redhat com>
Date:   Wed Oct 15 15:00:45 2014 +0200

    EHTMLEditorSelection - Fix change of a block format to another one when the citation is selected

 e-util/e-html-editor-selection.c |  211 +++++++++++++++++++++++++++-----------
 1 files changed, 149 insertions(+), 62 deletions(-)
---
diff --git a/e-util/e-html-editor-selection.c b/e-util/e-html-editor-selection.c
index 6bb0dc4..a2e7feb 100644
--- a/e-util/e-html-editor-selection.c
+++ b/e-util/e-html-editor-selection.c
@@ -1821,63 +1821,67 @@ get_citation_level (WebKitDOMNode *node)
        return level;
 }
 
-static void
-format_change_block_to_block (EHTMLEditorSelection *selection,
-                              EHTMLEditorSelectionBlockFormat format,
-                              EHTMLEditorView *view,
-                              const gchar *value,
-                              WebKitDOMDocument *document)
+static gboolean
+is_citation_node (WebKitDOMNode *node)
 {
-       gboolean after_selection_end = FALSE, html_mode;
-       WebKitDOMElement *selection_start_marker, *selection_end_marker, *element;
-       WebKitDOMNode *block, *next_block;
+       char *value;
 
-       e_html_editor_selection_save (selection);
-       selection_start_marker = webkit_dom_document_query_selector (
-               document, "span#-x-evo-selection-start-marker", NULL);
-       selection_end_marker = webkit_dom_document_query_selector (
-               document, "span#-x-evo-selection-end-marker", NULL);
+       if (!WEBKIT_DOM_IS_HTML_QUOTE_ELEMENT (node))
+               return FALSE;
 
-       /* If the selection was not saved, move it into the first child of body */
-       if (!selection_start_marker || !selection_end_marker) {
-               WebKitDOMHTMLElement *body;
+       value = webkit_dom_element_get_attribute (WEBKIT_DOM_ELEMENT (node), "type");
 
-               body = webkit_dom_document_get_body (document);
-               selection_start_marker = webkit_dom_document_create_element (
-                       document, "SPAN", NULL);
-               webkit_dom_element_set_id (
-                       selection_start_marker, "-x-evo-selection-start-marker");
-               webkit_dom_node_insert_before (
-                       webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
-                       WEBKIT_DOM_NODE (selection_start_marker),
-                       webkit_dom_node_get_first_child (
-                               webkit_dom_node_get_first_child (
-                                       WEBKIT_DOM_NODE (body))),
-                       NULL);
-               selection_end_marker = webkit_dom_document_create_element (
-                       document, "SPAN", NULL);
-               webkit_dom_element_set_id (
-                       selection_end_marker, "-x-evo-selection-end-marker");
-               webkit_dom_node_insert_before (
-                       webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
-                       WEBKIT_DOM_NODE (selection_end_marker),
-                       webkit_dom_node_get_first_child (
-                               webkit_dom_node_get_first_child (
-                                       WEBKIT_DOM_NODE (body))),
-                       NULL);
+       /* citation == <blockquote type='cite'> */
+       if (g_strcmp0 (value, "cite") == 0) {
+               g_free (value);
+               return TRUE;
+       } else {
+               g_free (value);
+               return FALSE;
        }
+}
 
-       block = get_parent_block_node_from_child (
-               WEBKIT_DOM_NODE (selection_start_marker));
-
-       html_mode = e_html_editor_view_get_html_mode (view);
+static gboolean
+process_block_to_block (EHTMLEditorSelection *selection,
+                        EHTMLEditorView *view,
+                       WebKitDOMDocument *document,
+                        EHTMLEditorSelectionBlockFormat format,
+                       const gchar *value,
+                        WebKitDOMNode *block,
+                        WebKitDOMNode *end_block,
+                        gboolean html_mode)
+{
+       gboolean after_selection_end = FALSE;
+       WebKitDOMNode *next_block;
 
-       /* Process all blocks that are in the selection one by one */
-       while (block && !after_selection_end) {
+       while (!after_selection_end && block) {
                gboolean quoted = FALSE;
                gboolean empty = FALSE;
                gchar *content;
                WebKitDOMNode *child;
+               WebKitDOMElement *element;
+
+               if (is_citation_node (block)) {
+                       gboolean finished;
+
+                       next_block = webkit_dom_node_get_next_sibling (block);
+                       finished = process_block_to_block (
+                               selection,
+                               view,
+                               document,
+                               format,
+                               value,
+                               webkit_dom_node_get_first_child (block),
+                               end_block,
+                               html_mode);
+
+                       if (finished)
+                               return TRUE;
+
+                       block = next_block;
+
+                       continue;
+               }
 
                if (webkit_dom_element_query_selector (
                        WEBKIT_DOM_ELEMENT (block), "span.-x-evo-quoted", NULL)) {
@@ -1888,8 +1892,7 @@ format_change_block_to_block (EHTMLEditorSelection *selection,
                if (!html_mode)
                        remove_wrapping_from_element (WEBKIT_DOM_ELEMENT (block));
 
-               after_selection_end = webkit_dom_node_contains (
-                       block, WEBKIT_DOM_NODE (selection_end_marker));
+               after_selection_end = webkit_dom_node_is_same_node (block, end_block);
 
                next_block = webkit_dom_node_get_next_sibling (block);
 
@@ -1905,16 +1908,22 @@ format_change_block_to_block (EHTMLEditorSelection *selection,
                empty = !*content || (g_strcmp0 (content, UNICODE_ZERO_WIDTH_SPACE) == 0);
                g_free (content);
 
-               if (empty) {
-                       webkit_dom_html_element_set_inner_html (
-                               WEBKIT_DOM_HTML_ELEMENT (element),
-                               UNICODE_ZERO_WIDTH_SPACE,
-                               NULL);
-               }
+               while ((child = webkit_dom_node_get_first_child (block))) {
+                       if (WEBKIT_DOM_IS_HTMLBR_ELEMENT (child))
+                               empty = FALSE;
 
-               while ((child = webkit_dom_node_get_first_child (block)))
                        webkit_dom_node_append_child (
                                WEBKIT_DOM_NODE (element), child, NULL);
+               }
+
+               if (empty) {
+                       WebKitDOMElement *br;
+
+                       br = webkit_dom_document_create_element (
+                               document, "BR", NULL);
+                       webkit_dom_node_append_child (
+                               WEBKIT_DOM_NODE (element), WEBKIT_DOM_NODE (br), NULL);
+               }
 
                webkit_dom_node_insert_before (
                        webkit_dom_node_get_parent_node (block),
@@ -1924,6 +1933,17 @@ format_change_block_to_block (EHTMLEditorSelection *selection,
 
                remove_node (block);
 
+               if (!next_block && !after_selection_end) {
+                       gint citation_level;
+
+                       citation_level = get_citation_level (WEBKIT_DOM_NODE (element));
+
+                       if (citation_level > 0) {
+                               next_block = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (element));
+                               next_block = webkit_dom_node_get_next_sibling (next_block);
+                       }
+               }
+
                block = next_block;
 
                if (format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH && !html_mode) {
@@ -1940,6 +1960,67 @@ format_change_block_to_block (EHTMLEditorSelection *selection,
                        e_html_editor_view_quote_plain_text_element (view, element);
        }
 
+       return after_selection_end;
+}
+
+static void
+format_change_block_to_block (EHTMLEditorSelection *selection,
+                              EHTMLEditorSelectionBlockFormat format,
+                              EHTMLEditorView *view,
+                              const gchar *value,
+                              WebKitDOMDocument *document)
+{
+       gboolean after_selection_end = FALSE, html_mode;
+       WebKitDOMElement *selection_start_marker, *selection_end_marker, *element;
+       WebKitDOMNode *block, *next_block, *end_block;
+
+       e_html_editor_selection_save (selection);
+       selection_start_marker = webkit_dom_document_query_selector (
+               document, "span#-x-evo-selection-start-marker", NULL);
+       selection_end_marker = webkit_dom_document_query_selector (
+               document, "span#-x-evo-selection-end-marker", NULL);
+
+       /* If the selection was not saved, move it into the first child of body */
+       if (!selection_start_marker || !selection_end_marker) {
+               WebKitDOMHTMLElement *body;
+
+               body = webkit_dom_document_get_body (document);
+               selection_start_marker = webkit_dom_document_create_element (
+                       document, "SPAN", NULL);
+               webkit_dom_element_set_id (
+                       selection_start_marker, "-x-evo-selection-start-marker");
+               webkit_dom_node_insert_before (
+                       webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
+                       WEBKIT_DOM_NODE (selection_start_marker),
+                       webkit_dom_node_get_first_child (
+                               webkit_dom_node_get_first_child (
+                                       WEBKIT_DOM_NODE (body))),
+                       NULL);
+               selection_end_marker = webkit_dom_document_create_element (
+                       document, "SPAN", NULL);
+               webkit_dom_element_set_id (
+                       selection_end_marker, "-x-evo-selection-end-marker");
+               webkit_dom_node_insert_before (
+                       webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
+                       WEBKIT_DOM_NODE (selection_end_marker),
+                       webkit_dom_node_get_first_child (
+                               webkit_dom_node_get_first_child (
+                                       WEBKIT_DOM_NODE (body))),
+                       NULL);
+       }
+
+       block = get_parent_block_node_from_child (
+               WEBKIT_DOM_NODE (selection_start_marker));
+
+       end_block = get_parent_block_node_from_child (
+               WEBKIT_DOM_NODE (selection_end_marker));
+
+       html_mode = e_html_editor_view_get_html_mode (view);
+
+       /* Process all blocks that are in the selection one by one */
+       process_block_to_block (
+               selection, view, document, format, value, block, end_block, html_mode);
+
        e_html_editor_selection_restore (selection);
 }
 
@@ -2051,17 +2132,23 @@ format_change_block_to_list (EHTMLEditorSelection *selection,
                empty = !*content || (g_strcmp0 (content, UNICODE_ZERO_WIDTH_SPACE) == 0);
                g_free (content);
 
+               while ((child = webkit_dom_node_get_first_child (block))) {
+                       if (WEBKIT_DOM_IS_HTMLBR_ELEMENT (child))
+                               empty = FALSE;
+
+                       webkit_dom_node_append_child (
+                               WEBKIT_DOM_NODE (item), child, NULL);
+               }
+
                /* We have to use again the hidden space to move caret into newly inserted list */
                if (empty) {
-                       webkit_dom_html_element_set_inner_html (
-                               WEBKIT_DOM_HTML_ELEMENT (item),
-                               UNICODE_ZERO_WIDTH_SPACE,
-                               NULL);
-               }
+                       WebKitDOMElement *br;
 
-               while ((child = webkit_dom_node_get_first_child (block)))
+                       br = webkit_dom_document_create_element (
+                               document, "BR", NULL);
                        webkit_dom_node_append_child (
-                               WEBKIT_DOM_NODE (item), child, NULL);
+                               WEBKIT_DOM_NODE (item), WEBKIT_DOM_NODE (br), NULL);
+               }
 
                webkit_dom_node_append_child (
                        WEBKIT_DOM_NODE (list), WEBKIT_DOM_NODE (item), NULL);


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