[evolution] Bug 753474 - Pressing Enter (Return) twice in a bulleted list to exit the list creates a paragraph w



commit f8678285083214f3f30a80309a832d23e989244e
Author: Tomas Popela <tpopela redhat com>
Date:   Tue Aug 11 13:27:49 2015 +0200

    Bug 753474 - Pressing Enter (Return) twice in a bulleted list to exit the list creates a paragraph with 
negative margin
    
    Instead of leaving the operation on WebKit do it ourselves.

 e-util/e-html-editor-selection.c |  252 ++---------------------------------
 e-util/e-html-editor-selection.h |    3 -
 e-util/e-html-editor-utils.c     |  269 ++++++++++++++++++++++++++++++++++++
 e-util/e-html-editor-utils.h     |   37 +++++
 e-util/e-html-editor-view.c      |  277 ++++++++++++++++++++++----------------
 e-util/e-html-editor-view.h      |    2 -
 6 files changed, 474 insertions(+), 366 deletions(-)
---
diff --git a/e-util/e-html-editor-selection.c b/e-util/e-html-editor-selection.c
index 00fb95b..3f56ffe 100644
--- a/e-util/e-html-editor-selection.c
+++ b/e-util/e-html-editor-selection.c
@@ -1358,62 +1358,6 @@ get_list_node_from_child (WebKitDOMNode *child)
        return webkit_dom_node_get_parent_node (parent);
 }
 
-static gboolean
-node_is_list (WebKitDOMNode *node)
-{
-       return node && (
-               WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (node) ||
-               WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (node));
-}
-
-static gboolean
-node_is_list_or_item (WebKitDOMNode *node)
-{
-       return node && (node_is_list (node) || WEBKIT_DOM_IS_HTMLLI_ELEMENT (node));
-}
-
-static void
-merge_list_into_list (WebKitDOMNode *from,
-                      WebKitDOMNode *to,
-                      gboolean insert_before)
-{
-       WebKitDOMNode *item;
-
-       if (!(to && from))
-               return;
-
-       while ((item = webkit_dom_node_get_first_child (from)) != NULL) {
-               if (insert_before)
-                       webkit_dom_node_insert_before (
-                               to, item, webkit_dom_node_get_last_child (to), NULL);
-               else
-                       webkit_dom_node_append_child (to, item, NULL);
-       }
-
-       if (!webkit_dom_node_get_first_child (from))
-               remove_node (from);
-}
-
-static void
-merge_lists_if_possible (WebKitDOMNode *list)
-{
-       EHTMLEditorSelectionBlockFormat format, prev, next;
-       WebKitDOMNode *prev_sibling, *next_sibling;
-
-       prev_sibling = webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (list));
-       next_sibling = webkit_dom_node_get_next_sibling (WEBKIT_DOM_NODE (list));
-
-       format = e_html_editor_selection_get_list_format_from_node (list),
-       prev = e_html_editor_selection_get_list_format_from_node (prev_sibling);
-       next = e_html_editor_selection_get_list_format_from_node (next_sibling);
-
-       if (format == prev && format != -1 && prev != -1)
-               merge_list_into_list (prev_sibling, list, TRUE);
-
-       if (format == next && format != -1 && next != -1)
-               merge_list_into_list (next_sibling, list, FALSE);
-}
-
 static WebKitDOMElement *
 do_format_change_list_to_list (WebKitDOMElement *list_to_process,
                                WebKitDOMElement *new_list_template,
@@ -1421,7 +1365,7 @@ do_format_change_list_to_list (WebKitDOMElement *list_to_process,
 {
        EHTMLEditorSelectionBlockFormat current_format;
 
-       current_format = e_html_editor_selection_get_list_format_from_node (
+       current_format = get_list_format_from_node (
                WEBKIT_DOM_NODE (list_to_process));
        if (to == current_format) {
                /* Same format, skip it. */
@@ -1992,47 +1936,6 @@ get_block_node (WebKitDOMRange *range)
 }
 
 /**
- * e_html_editor_selection_get_list_format_from_node:
- * @node: an #WebKitDOMNode
- *
- * Returns block format of given list.
- *
- * Returns: #EHTMLEditorSelectionBlockFormat
- */
-EHTMLEditorSelectionBlockFormat
-e_html_editor_selection_get_list_format_from_node (WebKitDOMNode *node)
-{
-       EHTMLEditorSelectionBlockFormat format =
-               E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST;
-
-       if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (node))
-               return -1;
-
-       if (WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (node))
-               return format;
-
-       if (WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (node)) {
-               gchar *type_value = webkit_dom_element_get_attribute (
-                       WEBKIT_DOM_ELEMENT (node), "type");
-
-               if (!type_value)
-                       return E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST;
-
-               if (!*type_value)
-                       format = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST;
-               else if (g_ascii_strcasecmp (type_value, "A") == 0)
-                       format = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA;
-               else if (g_ascii_strcasecmp (type_value, "I") == 0)
-                       format = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN;
-               g_free (type_value);
-
-               return format;
-       }
-
-       return -1;
-}
-
-/**
  * e_html_editor_selection_get_block_format:
  * @selection: an #EHTMLEditorSelection
  *
@@ -2064,9 +1967,9 @@ e_html_editor_selection_get_block_format (EHTMLEditorSelection *selection)
                tmp_element = e_html_editor_dom_node_find_parent_element (node, "OL");
                if (tmp_element) {
                        if (webkit_dom_node_contains (WEBKIT_DOM_NODE (tmp_element), WEBKIT_DOM_NODE 
(element))) 
-                               result = e_html_editor_selection_get_list_format_from_node (WEBKIT_DOM_NODE 
(element));
+                               result = get_list_format_from_node (WEBKIT_DOM_NODE (element));
                        else
-                               result = e_html_editor_selection_get_list_format_from_node (WEBKIT_DOM_NODE 
(tmp_element));
+                               result = get_list_format_from_node (WEBKIT_DOM_NODE (tmp_element));
                } else
                        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST;
        } else if ((element = e_html_editor_dom_node_find_parent_element (node, "OL")) != NULL) {
@@ -2075,11 +1978,11 @@ e_html_editor_selection_get_block_format (EHTMLEditorSelection *selection)
                tmp_element = e_html_editor_dom_node_find_parent_element (node, "UL");
                if (tmp_element) {
                        if (webkit_dom_node_contains (WEBKIT_DOM_NODE (element), WEBKIT_DOM_NODE 
(tmp_element))) 
-                               result = e_html_editor_selection_get_list_format_from_node (WEBKIT_DOM_NODE 
(element));
+                               result = get_list_format_from_node (WEBKIT_DOM_NODE (element));
                        else
-                               result = e_html_editor_selection_get_list_format_from_node (WEBKIT_DOM_NODE 
(tmp_element));
+                               result = get_list_format_from_node (WEBKIT_DOM_NODE (tmp_element));
                } else
-                       result = e_html_editor_selection_get_list_format_from_node (WEBKIT_DOM_NODE 
(element));
+                       result = get_list_format_from_node (WEBKIT_DOM_NODE (element));
        } else if (e_html_editor_dom_node_find_parent_element (node, "PRE")) {
                result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PRE;
        } else if (e_html_editor_dom_node_find_parent_element (node, "ADDRESS")) {
@@ -2308,60 +2211,6 @@ indent_block (EHTMLEditorSelection *selection,
        return tmp;
 }
 
-static WebKitDOMNode *
-split_list_into_two (WebKitDOMDocument *document,
-                     WebKitDOMNode *item)
-{
-       WebKitDOMDocumentFragment *fragment;
-       WebKitDOMNode *parent, *prev_parent, *tmp;
-
-       fragment = webkit_dom_document_create_document_fragment (document);
-
-       tmp = item;
-       parent = webkit_dom_node_get_parent_node (item);
-       while (!WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
-               WebKitDOMNode *clone, *first_child, *insert_before = NULL, *sibling;
-
-               first_child = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (fragment));
-               clone = webkit_dom_node_clone_node (parent, FALSE);
-               webkit_dom_node_insert_before (
-                       WEBKIT_DOM_NODE (fragment), clone, first_child, NULL);
-
-               if (first_child)
-                       insert_before = webkit_dom_node_get_first_child (first_child);
-
-               while (first_child && (sibling = webkit_dom_node_get_next_sibling (first_child)))
-                       webkit_dom_node_insert_before (first_child, sibling, insert_before, NULL);
-
-               while ((sibling = webkit_dom_node_get_next_sibling (tmp)))
-                       webkit_dom_node_append_child (clone, sibling, NULL);
-
-               webkit_dom_node_insert_before (
-                       clone, tmp, webkit_dom_node_get_first_child (clone), NULL);
-
-               prev_parent = parent;
-               tmp = webkit_dom_node_get_next_sibling (parent);
-               parent = webkit_dom_node_get_parent_node (parent);
-               if (WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
-                       first_child = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (fragment));
-                       insert_before = webkit_dom_node_get_first_child (first_child);
-                       while (first_child && (sibling = webkit_dom_node_get_next_sibling (first_child))) {
-                               webkit_dom_node_insert_before (
-                                       first_child, sibling, insert_before, NULL);
-                       }
-               }
-       }
-
-       tmp = webkit_dom_node_insert_before (
-               parent,
-               webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (fragment)),
-               webkit_dom_node_get_next_sibling (prev_parent),
-               NULL);
-       remove_node_if_empty (prev_parent);
-
-       return tmp;
-}
-
 static void
 remove_node_and_parents_if_empty (WebKitDOMNode *node)
 {
@@ -2423,7 +2272,7 @@ do_format_change_list_to_block (EHTMLEditorSelection *selection,
        }
 
        if (webkit_dom_node_contains (source_list, WEBKIT_DOM_NODE (selection_end)))
-               source_list = split_list_into_two (document, item);
+               source_list = split_list_into_two (item);
        else {
                source_list = webkit_dom_node_get_next_sibling (source_list);
        }
@@ -2677,87 +2526,6 @@ process_block_to_block (EHTMLEditorSelection *selection,
        return after_selection_end;
 }
 
-static WebKitDOMElement *
-create_selection_marker (WebKitDOMDocument *document,
-                         gboolean start)
-{
-       WebKitDOMElement *element;
-
-       element = webkit_dom_document_create_element (
-               document, "SPAN", NULL);
-       webkit_dom_element_set_id (
-               element,
-               start ? "-x-evo-selection-start-marker" :
-                       "-x-evo-selection-end-marker");
-
-       return element;
-}
-
-static void
-remove_selection_markers (WebKitDOMDocument *document)
-{
-       WebKitDOMElement *marker;
-
-       marker = webkit_dom_document_get_element_by_id (
-               document, "-x-evo-selection-start-marker");
-       if (marker)
-               remove_node (WEBKIT_DOM_NODE (marker));
-       marker = webkit_dom_document_get_element_by_id (
-               document, "-x-evo-selection-end-marker");
-       if (marker)
-               remove_node (WEBKIT_DOM_NODE (marker));
-}
-
-static void
-add_selection_markers_into_element_start (WebKitDOMDocument *document,
-                                          WebKitDOMElement *element,
-                                          WebKitDOMElement **selection_start_marker,
-                                          WebKitDOMElement **selection_end_marker)
-{
-       WebKitDOMElement *marker;
-
-       remove_selection_markers (document);
-       marker = create_selection_marker (document, FALSE);
-       webkit_dom_node_insert_before (
-               WEBKIT_DOM_NODE (element),
-               WEBKIT_DOM_NODE (marker),
-               webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (element)),
-               NULL);
-       if (selection_end_marker)
-               *selection_end_marker = marker;
-
-       marker = create_selection_marker (document, TRUE);
-       webkit_dom_node_insert_before (
-               WEBKIT_DOM_NODE (element),
-               WEBKIT_DOM_NODE (marker),
-               webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (element)),
-               NULL);
-       if (selection_start_marker)
-               *selection_start_marker = marker;
-}
-
-static void
-add_selection_markers_into_element_end (WebKitDOMDocument *document,
-                                        WebKitDOMElement *element,
-                                        WebKitDOMElement **selection_start_marker,
-                                        WebKitDOMElement **selection_end_marker)
-{
-       WebKitDOMElement *marker;
-
-       remove_selection_markers (document);
-       marker = create_selection_marker (document, TRUE);
-       webkit_dom_node_append_child (
-               WEBKIT_DOM_NODE (element), WEBKIT_DOM_NODE (marker), NULL);
-       if (selection_start_marker)
-               *selection_start_marker = marker;
-
-       marker = create_selection_marker (document, FALSE);
-       webkit_dom_node_append_child (
-               WEBKIT_DOM_NODE (element), WEBKIT_DOM_NODE (marker), NULL);
-       if (selection_end_marker)
-               *selection_end_marker = marker;
-}
-
 static void
 format_change_block_to_block (EHTMLEditorSelection *selection,
                               EHTMLEditorSelectionBlockFormat format,
@@ -3021,8 +2789,8 @@ format_change_list_to_list (EHTMLEditorSelection *selection,
                }
        }
 
-       prev = e_html_editor_selection_get_list_format_from_node (prev_list);
-       next = e_html_editor_selection_get_list_format_from_node (next_list);
+       prev = get_list_format_from_node (prev_list);
+       next = get_list_format_from_node (next_list);
 
        if (format == prev && format != -1 && prev != -1) {
                if (selection_starts_in_first_child && selection_ends_in_last_child) {
@@ -3834,7 +3602,7 @@ indent_list (EHTMLEditorSelection *selection,
                WebKitDOMNode *source_list = webkit_dom_node_get_parent_node (item);
                EHTMLEditorSelectionBlockFormat format;
 
-               format = e_html_editor_selection_get_list_format_from_node (source_list);
+               format = get_list_format_from_node (source_list);
 
                list = create_list_element (
                        selection, document, format, get_list_level (item), html_mode);
diff --git a/e-util/e-html-editor-selection.h b/e-util/e-html-editor-selection.h
index 544c09e..b1028e2 100644
--- a/e-util/e-html-editor-selection.h
+++ b/e-util/e-html-editor-selection.h
@@ -242,9 +242,6 @@ void                e_html_editor_selection_extend  (EHTMLEditorSelection *selection,
                                                 EHTMLEditorSelectionGranularity granularity);
 void           e_html_editor_selection_scroll_to_caret
                                                (EHTMLEditorSelection *selection);
-EHTMLEditorSelectionBlockFormat
-               e_html_editor_selection_get_list_format_from_node
-                                               (WebKitDOMNode *node);
 EHTMLEditorSelectionAlignment
                e_html_editor_selection_get_list_alignment_from_node
                                                (WebKitDOMNode *node);
diff --git a/e-util/e-html-editor-utils.c b/e-util/e-html-editor-utils.c
index cf240ee..48b22bf 100644
--- a/e-util/e-html-editor-utils.c
+++ b/e-util/e-html-editor-utils.c
@@ -312,3 +312,272 @@ remove_node_if_empty (WebKitDOMNode *node)
                g_free (text_content);
        }
 }
+
+WebKitDOMNode *
+split_list_into_two (WebKitDOMNode *item)
+{
+       WebKitDOMDocument *document;
+       WebKitDOMDocumentFragment *fragment;
+       WebKitDOMNode *parent, *prev_parent, *tmp;
+
+       document = webkit_dom_node_get_owner_document (item);
+       fragment = webkit_dom_document_create_document_fragment (document);
+
+       tmp = item;
+       parent = webkit_dom_node_get_parent_node (item);
+       while (!WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
+               WebKitDOMNode *clone, *first_child, *insert_before = NULL, *sibling;
+
+               first_child = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (fragment));
+               clone = webkit_dom_node_clone_node (parent, FALSE);
+               webkit_dom_node_insert_before (
+                       WEBKIT_DOM_NODE (fragment), clone, first_child, NULL);
+
+               if (first_child)
+                       insert_before = webkit_dom_node_get_first_child (first_child);
+
+               while (first_child && (sibling = webkit_dom_node_get_next_sibling (first_child)))
+                       webkit_dom_node_insert_before (first_child, sibling, insert_before, NULL);
+
+               while ((sibling = webkit_dom_node_get_next_sibling (tmp)))
+                       webkit_dom_node_append_child (clone, sibling, NULL);
+
+               webkit_dom_node_insert_before (
+                       clone, tmp, webkit_dom_node_get_first_child (clone), NULL);
+
+               prev_parent = parent;
+               tmp = webkit_dom_node_get_next_sibling (parent);
+               parent = webkit_dom_node_get_parent_node (parent);
+               if (WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
+                       first_child = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (fragment));
+                       insert_before = webkit_dom_node_get_first_child (first_child);
+                       while (first_child && (sibling = webkit_dom_node_get_next_sibling (first_child))) {
+                               webkit_dom_node_insert_before (
+                                       first_child, sibling, insert_before, NULL);
+                       }
+               }
+       }
+
+       tmp = webkit_dom_node_insert_before (
+               parent,
+               webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (fragment)),
+               webkit_dom_node_get_next_sibling (prev_parent),
+               NULL);
+       remove_node_if_empty (prev_parent);
+
+       return tmp;
+}
+
+WebKitDOMElement *
+create_selection_marker (WebKitDOMDocument *document,
+                         gboolean start)
+{
+       WebKitDOMElement *element;
+
+       element = webkit_dom_document_create_element (
+               document, "SPAN", NULL);
+       webkit_dom_element_set_id (
+               element,
+               start ? "-x-evo-selection-start-marker" :
+                       "-x-evo-selection-end-marker");
+
+       return element;
+}
+
+void
+remove_selection_markers (WebKitDOMDocument *document)
+{
+       WebKitDOMElement *marker;
+
+       marker = webkit_dom_document_get_element_by_id (
+               document, "-x-evo-selection-start-marker");
+       if (marker)
+               remove_node (WEBKIT_DOM_NODE (marker));
+       marker = webkit_dom_document_get_element_by_id (
+               document, "-x-evo-selection-end-marker");
+       if (marker)
+               remove_node (WEBKIT_DOM_NODE (marker));
+}
+
+void
+add_selection_markers_into_element_start (WebKitDOMDocument *document,
+                                          WebKitDOMElement *element,
+                                          WebKitDOMElement **selection_start_marker,
+                                          WebKitDOMElement **selection_end_marker)
+{
+       WebKitDOMElement *marker;
+
+       remove_selection_markers (document);
+       marker = create_selection_marker (document, FALSE);
+       webkit_dom_node_insert_before (
+               WEBKIT_DOM_NODE (element),
+               WEBKIT_DOM_NODE (marker),
+               webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (element)),
+               NULL);
+       if (selection_end_marker)
+               *selection_end_marker = marker;
+
+       marker = create_selection_marker (document, TRUE);
+       webkit_dom_node_insert_before (
+               WEBKIT_DOM_NODE (element),
+               WEBKIT_DOM_NODE (marker),
+               webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (element)),
+               NULL);
+       if (selection_start_marker)
+               *selection_start_marker = marker;
+}
+
+void
+add_selection_markers_into_element_end (WebKitDOMDocument *document,
+                                        WebKitDOMElement *element,
+                                        WebKitDOMElement **selection_start_marker,
+                                        WebKitDOMElement **selection_end_marker)
+{
+       WebKitDOMElement *marker;
+
+       remove_selection_markers (document);
+       marker = create_selection_marker (document, TRUE);
+       webkit_dom_node_append_child (
+               WEBKIT_DOM_NODE (element), WEBKIT_DOM_NODE (marker), NULL);
+       if (selection_start_marker)
+               *selection_start_marker = marker;
+
+       marker = create_selection_marker (document, FALSE);
+       webkit_dom_node_append_child (
+               WEBKIT_DOM_NODE (element), WEBKIT_DOM_NODE (marker), NULL);
+       if (selection_end_marker)
+               *selection_end_marker = marker;
+}
+
+gboolean
+node_is_list (WebKitDOMNode *node)
+{
+       return node && (
+               WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (node) ||
+               WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (node));
+}
+
+gboolean
+node_is_list_or_item (WebKitDOMNode *node)
+{
+       return node && (node_is_list (node) || WEBKIT_DOM_IS_HTMLLI_ELEMENT (node));
+}
+/**
+ * get_list_format_from_node:
+ * @node: an #WebKitDOMNode
+ *
+ * Returns block format of given list.
+ *
+ * Returns: #EHTMLEditorSelectionBlockFormat
+ */
+EHTMLEditorSelectionBlockFormat
+get_list_format_from_node (WebKitDOMNode *node)
+{
+       EHTMLEditorSelectionBlockFormat format =
+               E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST;
+
+       if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (node))
+               return -1;
+
+       if (WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (node))
+               return format;
+
+       if (WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (node)) {
+               gchar *type_value = webkit_dom_element_get_attribute (
+                       WEBKIT_DOM_ELEMENT (node), "type");
+
+               if (!type_value)
+                       return E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST;
+
+               if (!*type_value)
+                       format = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST;
+               else if (g_ascii_strcasecmp (type_value, "A") == 0)
+                       format = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA;
+               else if (g_ascii_strcasecmp (type_value, "I") == 0)
+                       format = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN;
+               g_free (type_value);
+
+               return format;
+       }
+
+       return -1;
+}
+
+void
+merge_list_into_list (WebKitDOMNode *from,
+                      WebKitDOMNode *to,
+                      gboolean insert_before)
+{
+       WebKitDOMNode *item, *insert_before_node;
+
+       if (!(to && from))
+               return;
+
+       insert_before_node = webkit_dom_node_get_first_child (to);
+       while ((item = webkit_dom_node_get_first_child (from)) != NULL) {
+               if (insert_before)
+                       webkit_dom_node_insert_before (
+                               to, item, insert_before_node, NULL);
+               else
+                       webkit_dom_node_append_child (to, item, NULL);
+       }
+
+       if (!webkit_dom_node_get_first_child (from))
+               remove_node (from);
+}
+
+void
+merge_lists_if_possible (WebKitDOMNode *list)
+{
+       EHTMLEditorSelectionBlockFormat format, prev, next;
+       gint ii, length;
+       WebKitDOMNode *prev_sibling, *next_sibling;
+       WebKitDOMNodeList *lists;
+
+       prev_sibling = webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (list));
+       next_sibling = webkit_dom_node_get_next_sibling (WEBKIT_DOM_NODE (list));
+
+       format = get_list_format_from_node (list),
+       prev = get_list_format_from_node (prev_sibling);
+       next = get_list_format_from_node (next_sibling);
+
+       if (format == prev && format != -1 && prev != -1)
+               merge_list_into_list (prev_sibling, list, TRUE);
+
+       if (format == next && format != -1 && next != -1)
+               merge_list_into_list (next_sibling, list, FALSE);
+
+       lists = webkit_dom_element_query_selector_all (
+               WEBKIT_DOM_ELEMENT (list), "ol + ol, ul + ul", NULL);
+       length = webkit_dom_node_list_get_length (lists);
+       for (ii = 0; ii < length; ii++) {
+               WebKitDOMNode *node;
+
+               node = webkit_dom_node_list_item (lists, ii);
+               merge_lists_if_possible (node);
+       }
+}
+
+WebKitDOMElement *
+get_parent_block_element (WebKitDOMNode *node)
+{
+       WebKitDOMElement *parent = webkit_dom_node_get_parent_element (node);
+
+       if (WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent))
+               return WEBKIT_DOM_ELEMENT (node);
+
+       while (parent &&
+              !WEBKIT_DOM_IS_HTML_DIV_ELEMENT (parent) &&
+              !WEBKIT_DOM_IS_HTML_QUOTE_ELEMENT (parent) &&
+              !WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (parent) &&
+              !WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (parent) &&
+              !WEBKIT_DOM_IS_HTML_PRE_ELEMENT (parent) &&
+              !WEBKIT_DOM_IS_HTML_HEADING_ELEMENT (parent) &&
+              !WEBKIT_DOM_IS_HTML_TABLE_CELL_ELEMENT (parent) &&
+              !element_has_tag (parent, "address")) {
+               parent = webkit_dom_node_get_parent_element (
+                       WEBKIT_DOM_NODE (parent));
+       }
+
+       return parent;
+}
diff --git a/e-util/e-html-editor-utils.h b/e-util/e-html-editor-utils.h
index a63af0e..efc8662 100644
--- a/e-util/e-html-editor-utils.h
+++ b/e-util/e-html-editor-utils.h
@@ -25,6 +25,8 @@
 #ifndef E_HTML_EDITOR_UTILS_H
 #define E_HTML_EDITOR_UTILS_H
 
+#include <e-util/e-util-enums.h>
+
 #include <webkit/webkitdom.h>
 
 G_BEGIN_DECLS
@@ -64,6 +66,41 @@ void         remove_node                     (WebKitDOMNode *node);
 
 void           remove_node_if_empty            (WebKitDOMNode *node);
 
+WebKitDOMNode *        split_list_into_two             (WebKitDOMNode *item);
+
+WebKitDOMElement *
+               create_selection_marker         (WebKitDOMDocument *document,
+                                                gboolean start);
+
+void           add_selection_markers_into_element_start
+                                               (WebKitDOMDocument *document,
+                                                WebKitDOMElement *element,
+                                                WebKitDOMElement **selection_start_marker,
+                                                WebKitDOMElement **selection_end_marker);
+
+void           add_selection_markers_into_element_end
+                                               (WebKitDOMDocument *document,
+                                                WebKitDOMElement *element,
+                                                WebKitDOMElement **selection_start_marker,
+                                                WebKitDOMElement **selection_end_marker);
+
+void           remove_selection_markers        (WebKitDOMDocument *document);
+
+gboolean       node_is_list                    (WebKitDOMNode *node);
+
+gboolean       node_is_list_or_item            (WebKitDOMNode *node);
+
+EHTMLEditorSelectionBlockFormat
+               get_list_format_from_node       (WebKitDOMNode *node);
+
+void           merge_list_into_list            (WebKitDOMNode *from,
+                                                WebKitDOMNode *to,
+                                                gboolean insert_before);
+
+void           merge_lists_if_possible         (WebKitDOMNode *list);
+
+WebKitDOMElement *
+               get_parent_block_element        (WebKitDOMNode *node);
 G_END_DECLS
 
 #endif /* E_HTML_EDITOR_UTILS_H */
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index e5455b6..4d18953 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -469,30 +469,6 @@ html_editor_view_should_show_delete_interface_for_element (EHTMLEditorView *view
        return FALSE;
 }
 
-WebKitDOMElement *
-get_parent_block_element (WebKitDOMNode *node)
-{
-       WebKitDOMElement *parent = webkit_dom_node_get_parent_element (node);
-
-       if (WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent))
-               return WEBKIT_DOM_ELEMENT (node);
-
-       while (parent &&
-              !WEBKIT_DOM_IS_HTML_DIV_ELEMENT (parent) &&
-              !WEBKIT_DOM_IS_HTML_QUOTE_ELEMENT (parent) &&
-              !WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (parent) &&
-              !WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (parent) &&
-              !WEBKIT_DOM_IS_HTML_PRE_ELEMENT (parent) &&
-              !WEBKIT_DOM_IS_HTML_HEADING_ELEMENT (parent) &&
-              !WEBKIT_DOM_IS_HTML_TABLE_CELL_ELEMENT (parent) &&
-              !element_has_tag (parent, "address")) {
-               parent = webkit_dom_node_get_parent_element (
-                       WEBKIT_DOM_NODE (parent));
-       }
-
-       return parent;
-}
-
 static void
 perform_spell_check (WebKitDOMDOMSelection *dom_selection,
                      WebKitDOMRange *start_range,
@@ -599,87 +575,6 @@ e_html_editor_view_force_spell_check_for_current_paragraph (EHTMLEditorView *vie
        e_html_editor_selection_restore (selection);
 }
 
-static WebKitDOMElement *
-create_selection_marker (WebKitDOMDocument *document,
-                         gboolean start)
-{
-       WebKitDOMElement *element;
-
-       element = webkit_dom_document_create_element (
-               document, "SPAN", NULL);
-       webkit_dom_element_set_id (
-               element,
-               start ? "-x-evo-selection-start-marker" :
-                       "-x-evo-selection-end-marker");
-
-       return element;
-}
-
-static void
-remove_selection_markers (WebKitDOMDocument *document)
-{
-       WebKitDOMElement *marker;
-
-       marker = webkit_dom_document_get_element_by_id (
-               document, "-x-evo-selection-start-marker");
-       if (marker)
-               remove_node (WEBKIT_DOM_NODE (marker));
-       marker = webkit_dom_document_get_element_by_id (
-               document, "-x-evo-selection-end-marker");
-       if (marker)
-               remove_node (WEBKIT_DOM_NODE (marker));
-}
-
-static void
-add_selection_markers_into_element_start (WebKitDOMDocument *document,
-                                          WebKitDOMElement *element,
-                                          WebKitDOMElement **selection_start_marker,
-                                          WebKitDOMElement **selection_end_marker)
-{
-       WebKitDOMElement *marker;
-
-       remove_selection_markers (document);
-       marker = create_selection_marker (document, FALSE);
-       webkit_dom_node_insert_before (
-               WEBKIT_DOM_NODE (element),
-               WEBKIT_DOM_NODE (marker),
-               webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (element)),
-               NULL);
-       if (selection_end_marker)
-               *selection_end_marker = marker;
-
-       marker = create_selection_marker (document, TRUE);
-       webkit_dom_node_insert_before (
-               WEBKIT_DOM_NODE (element),
-               WEBKIT_DOM_NODE (marker),
-               webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (element)),
-               NULL);
-       if (selection_start_marker)
-               *selection_start_marker = marker;
-}
-
-static void
-add_selection_markers_into_element_end (WebKitDOMDocument *document,
-                                        WebKitDOMElement *element,
-                                        WebKitDOMElement **selection_start_marker,
-                                        WebKitDOMElement **selection_end_marker)
-{
-       WebKitDOMElement *marker;
-
-       remove_selection_markers (document);
-       marker = create_selection_marker (document, TRUE);
-       webkit_dom_node_append_child (
-               WEBKIT_DOM_NODE (element), WEBKIT_DOM_NODE (marker), NULL);
-       if (selection_start_marker)
-               *selection_start_marker = marker;
-
-       marker = create_selection_marker (document, FALSE);
-       webkit_dom_node_append_child (
-               WEBKIT_DOM_NODE (element), WEBKIT_DOM_NODE (marker), NULL);
-       if (selection_end_marker)
-               *selection_end_marker = marker;
-}
-
 static void
 refresh_spell_check (EHTMLEditorView *view,
                      gboolean enable_spell_check)
@@ -4920,6 +4815,102 @@ insert_tabulator (EHTMLEditorView *view)
 }
 
 static gboolean
+return_pressed_in_empty_list_item (EHTMLEditorView *view,
+                                   gboolean save_history)
+{
+       EHTMLEditorSelection *selection;
+       WebKitDOMDocument *document;
+       WebKitDOMElement *selection_start_marker, *selection_end_marker;
+       WebKitDOMNode *parent, *node;
+
+       selection = e_html_editor_view_get_selection (view);
+       if (!e_html_editor_selection_is_collapsed (selection))
+               return FALSE;
+
+       e_html_editor_selection_save (selection);
+
+       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
+       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");
+
+       parent = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (selection_start_marker));
+       if (!WEBKIT_DOM_IS_HTMLLI_ELEMENT (parent)) {
+               e_html_editor_selection_restore (selection);
+               return FALSE;
+       }
+
+       node = webkit_dom_node_get_next_sibling (WEBKIT_DOM_NODE (selection_end_marker));
+       /* Check if return was pressed inside an empty list item. */
+       if (!webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (selection_start_marker)) &&
+           (!node || (node && WEBKIT_DOM_IS_HTMLBR_ELEMENT (node) && !webkit_dom_node_get_next_sibling 
(node)))) {
+               EHTMLEditorViewHistoryEvent *ev;
+               WebKitDOMDocumentFragment *fragment;
+               WebKitDOMElement *paragraph;
+               WebKitDOMNode *list;
+
+               if (save_history) {
+                       /* Insert new history event for Return to have the right coordinates.
+                        * The fragment will be added later. */
+                       ev = g_new0 (EHTMLEditorViewHistoryEvent, 1);
+                       ev->type = HISTORY_INPUT;
+
+                       e_html_editor_selection_get_selection_coordinates (
+                               selection,
+                               &ev->before.start.x,
+                               &ev->before.start.y,
+                               &ev->before.end.x,
+                               &ev->before.end.y);
+
+                       fragment = webkit_dom_document_create_document_fragment (document);
+               }
+
+               list = split_list_into_two (parent);
+
+               if (save_history) {
+                       webkit_dom_node_append_child (
+                               WEBKIT_DOM_NODE (fragment),
+                               parent,
+                               NULL);
+               } else {
+                       remove_node (parent);
+               }
+
+               paragraph = prepare_paragraph (selection, document, TRUE);
+
+               webkit_dom_node_insert_before (
+                       webkit_dom_node_get_parent_node (list),
+                       WEBKIT_DOM_NODE (paragraph),
+                       list,
+                       NULL);
+
+               e_html_editor_selection_restore (selection);
+
+               if (save_history) {
+                       e_html_editor_selection_get_selection_coordinates (
+                               selection,
+                               &ev->after.start.x,
+                               &ev->after.start.y,
+                               &ev->after.end.x,
+                               &ev->after.end.y);
+
+                       ev->data.fragment = fragment;
+
+                       e_html_editor_view_insert_new_history_event (view, ev);
+               }
+
+               e_html_editor_view_set_changed (view, TRUE);
+
+               return TRUE;
+       }
+
+       e_html_editor_selection_restore (selection);
+
+       return FALSE;
+}
+
+static gboolean
 html_editor_view_key_press_event (GtkWidget *widget,
                                   GdkEventKey *event)
 {
@@ -4990,6 +4981,11 @@ html_editor_view_key_press_event (GtkWidget *widget,
                        remove_input_event_listener_from_body (view);
                        return split_citation (view);
                }
+
+               /* If the ENTER key is pressed inside an empty list item then the list
+                * is broken into two and empty paragraph is inserted between lists. */
+               if (return_pressed_in_empty_list_item (view, TRUE))
+                       return TRUE;
        }
 
        if (event->keyval == GDK_KEY_BackSpace) {
@@ -8092,8 +8088,7 @@ process_list_to_plain_text (EHTMLEditorView *view,
        gint word_wrap_length = e_html_editor_selection_get_word_wrap_length (
                e_html_editor_view_get_selection (view));
 
-       format = e_html_editor_selection_get_list_format_from_node (
-               WEBKIT_DOM_NODE (element));
+       format = get_list_format_from_node (WEBKIT_DOM_NODE (element));
 
        /* Process list items to plain text */
        item = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (element));
@@ -8296,8 +8291,7 @@ process_list_to_plain_text (EHTMLEditorView *view,
 
                        g_free (item_str);
                        g_string_free (item_value, TRUE);
-               } else if (WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (item) ||
-                          WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (item)) {
+               } else if (node_is_list (item)) {
                        process_list_to_plain_text (
                                view, WEBKIT_DOM_ELEMENT (item), level + 1, output);
                        item = webkit_dom_node_get_next_sibling (item);
@@ -8536,8 +8530,7 @@ process_elements (EHTMLEditorView *view,
                    element_has_class (WEBKIT_DOM_ELEMENT (child), "-x-evo-indented"))
                        process_blockquote (WEBKIT_DOM_ELEMENT (child));
 
-               if (WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (child) ||
-                   WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (child)) {
+               if (node_is_list (child)) {
                        if (to_plain_text) {
                                if (changing_mode) {
                                        content = webkit_dom_html_element_get_outer_html (
@@ -8934,9 +8927,7 @@ toggle_paragraphs_style_in_element (EHTMLEditorView *view,
                        parent = webkit_dom_node_get_parent_node (node);
                        /* If the paragraph is inside indented paragraph don't set
                         * the style as it will be inherited */
-                       if (WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent) &&
-                           (WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (node) ||
-                            WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (node))) {
+                       if (WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent) && node_is_list (node)) {
                                gint offset;
 
                                offset = WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (node) ?
@@ -9259,8 +9250,7 @@ process_content_for_plain_text (EHTMLEditorView *view)
 
                paragraph = webkit_dom_node_list_item (paragraphs, ii);
 
-               if (WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (paragraph) ||
-                   WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (paragraph)) {
+               if (node_is_list (paragraph)) {
                        WebKitDOMNode *item = webkit_dom_node_get_first_child (paragraph);
 
                        while (item) {
@@ -11792,7 +11782,7 @@ undo_delete (EHTMLEditorView *view,
 
        /* Redoing Return key press */
        if (empty) {
-               WebKitDOMNode *node;
+               WebKitDOMNode *node, *tmp_node;
 
                range = get_range_for_point (document, event->before.start);
                webkit_dom_dom_selection_remove_all_ranges (dom_selection);
@@ -11804,7 +11794,17 @@ undo_delete (EHTMLEditorView *view,
                if (!node)
                        return;
 
-               element = get_parent_block_element (node);
+               tmp_node = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (event->data.fragment));
+               if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (tmp_node) &&
+                   WEBKIT_DOM_IS_HTMLBR_ELEMENT (webkit_dom_node_get_last_child (tmp_node)))
+                       if (return_pressed_in_empty_list_item (view, FALSE))
+                               return;
+
+               if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (webkit_dom_node_get_parent_node (node)))
+                       element = webkit_dom_node_get_parent_element (node);
+               else
+                       element = get_parent_block_element (node);
+
                webkit_dom_node_insert_before (
                        webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (element)),
                        fragment,
@@ -12947,6 +12947,40 @@ undo_redo_remove_link (EHTMLEditorView *view,
 }
 
 static void
+undo_return_in_empty_list_item (EHTMLEditorView *view,
+                                EHTMLEditorViewHistoryEvent *event)
+{
+       WebKitDOMDocument *document;
+       WebKitDOMElement *selection_start_marker;
+       WebKitDOMNode *parent;
+
+       e_html_editor_selection_save (view->priv->selection);
+
+       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
+       selection_start_marker = webkit_dom_document_get_element_by_id (document, 
"-x-evo-selection-start-marker");
+       parent = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (selection_start_marker));
+
+       if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (parent)) {
+               WebKitDOMNode *parent_list;
+
+               remove_selection_markers (document);
+               webkit_dom_node_insert_before (
+                       webkit_dom_node_get_parent_node (parent),
+                       webkit_dom_node_clone_node (WEBKIT_DOM_NODE (event->data.fragment), TRUE),
+                       webkit_dom_node_get_next_sibling (parent),
+                       NULL);
+
+               parent_list = parent;
+               while (node_is_list_or_item (webkit_dom_node_get_parent_node (parent_list)))
+                       parent_list = webkit_dom_node_get_parent_node (parent_list);
+
+               merge_lists_if_possible (parent_list);
+       }
+
+       e_html_editor_selection_restore (view->priv->selection);
+}
+
+static void
 undo_input (EHTMLEditorView *view,
             EHTMLEditorViewHistoryEvent *event)
 {
@@ -12954,7 +12988,7 @@ undo_input (EHTMLEditorView *view,
        WebKitDOMDocument *document;
        WebKitDOMDOMWindow *dom_window;
        WebKitDOMDOMSelection *dom_selection;
-       WebKitDOMNode *node;
+       WebKitDOMNode *node, *tmp_node;
 
        document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
        dom_window = webkit_dom_document_get_default_view (document);
@@ -12999,6 +13033,11 @@ undo_input (EHTMLEditorView *view,
                remove_node (node);
        }
 
+       tmp_node = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (event->data.fragment));
+       if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (tmp_node) &&
+           WEBKIT_DOM_IS_HTMLBR_ELEMENT (webkit_dom_node_get_last_child (tmp_node)))
+               undo_return_in_empty_list_item (view, event);
+
        g_object_unref (dom_window);
        g_object_unref (dom_selection);
 }
diff --git a/e-util/e-html-editor-view.h b/e-util/e-html-editor-view.h
index a9646fa..818f487 100644
--- a/e-util/e-html-editor-view.h
+++ b/e-util/e-html-editor-view.h
@@ -277,8 +277,6 @@ void                e_html_editor_view_set_is_message_from_edit_as_new
 void           e_html_editor_view_insert_quoted_text
                                                (EHTMLEditorView *view,
                                                 const gchar *text);
-WebKitDOMElement *
-               get_parent_block_element        (WebKitDOMNode *node);
 void           e_html_editor_view_set_link_color
                                                (EHTMLEditorView *view,
                                                 GdkRGBA *color);


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