[evolution] EHTMLEditorSelection - Fix the "Wrap Lines" action



commit f9cf3d322dcfd16092a29569f4601f4bbbf136d7
Author: Tomas Popela <tpopela redhat com>
Date:   Mon Oct 6 14:00:31 2014 +0200

    EHTMLEditorSelection - Fix the "Wrap Lines" action
    
    Base is on blocks inside the selection and not on text that is actually
    selected (that behaviour caused unwanted results).

 e-util/e-html-editor-selection.c |  159 +++++++++++++++-----------------------
 1 files changed, 63 insertions(+), 96 deletions(-)
---
diff --git a/e-util/e-html-editor-selection.c b/e-util/e-html-editor-selection.c
index 66f58c1..415c6ac 100644
--- a/e-util/e-html-editor-selection.c
+++ b/e-util/e-html-editor-selection.c
@@ -5565,9 +5565,10 @@ void
 e_html_editor_selection_wrap_lines (EHTMLEditorSelection *selection)
 {
        EHTMLEditorView *view;
-       WebKitDOMRange *range;
+       gboolean after_selection_end = FALSE, html_mode;
        WebKitDOMDocument *document;
-       WebKitDOMElement *active_paragraph, *caret;
+       WebKitDOMElement *selection_start_marker, *selection_end_marker;
+       WebKitDOMNode *block, *next_block;
 
        g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
 
@@ -5577,115 +5578,81 @@ e_html_editor_selection_wrap_lines (EHTMLEditorSelection *selection)
        document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
        g_object_unref (view);
 
-       caret = e_html_editor_selection_save_caret_position (selection);
-       if (e_html_editor_selection_is_collapsed (selection)) {
-               WebKitDOMNode *end_container;
-               WebKitDOMNode *parent;
-               WebKitDOMNode *paragraph;
-               gchar *text_content;
-
-               /* We need to save caret position and restore it after
-                * wrapping the selection, but we need to save it before we
-                * start to modify selection */
-               range = html_editor_selection_get_current_range (selection);
-               if (!range)
-                       return;
+       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);
 
-               end_container = webkit_dom_range_get_common_ancestor_container (range, NULL);
+       /* If the selection was not saved, move it into the first child of body */
+       if (!selection_start_marker || !selection_end_marker) {
+               WebKitDOMHTMLElement *body;
 
-               /* Wrap only text surrounded in DIV and P tags */
-               parent = webkit_dom_node_get_parent_node(end_container);
-               if (WEBKIT_DOM_IS_HTML_DIV_ELEMENT (parent) ||
-                   WEBKIT_DOM_IS_HTML_PARAGRAPH_ELEMENT (parent)) {
-                       element_add_class (
-                               WEBKIT_DOM_ELEMENT (parent), "-x-evo-paragraph");
-                       paragraph = parent;
-               } else {
-                       WebKitDOMElement *parent_div =
-                               e_html_editor_dom_node_find_parent_element (parent, "DIV");
+               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);
+       }
 
-                       if (element_has_class (parent_div, "-x-evo-paragraph")) {
-                               paragraph = WEBKIT_DOM_NODE (parent_div);
-                       } else {
-                               if (!caret)
-                                       return;
+       block = get_parent_block_node_from_child (
+               WEBKIT_DOM_NODE (selection_start_marker));
 
-                               /* We try to select previous sibling */
-                               paragraph = webkit_dom_node_get_previous_sibling (
-                                       WEBKIT_DOM_NODE (caret));
-                               if (paragraph) {
-                                       /* When there is just text without container
-                                        * we have to surround it with paragraph div */
-                                       if (WEBKIT_DOM_IS_TEXT (paragraph))
-                                               paragraph = WEBKIT_DOM_NODE (
-                                                       e_html_editor_selection_put_node_into_paragraph (
-                                                               selection, document, paragraph,
-                                                               WEBKIT_DOM_NODE (caret)));
-                               } else {
-                                       /* When some weird element is selected, return */
-                                       e_html_editor_selection_clear_caret_position_marker (selection);
-                                       return;
-                               }
-                       }
-               }
+       html_mode = e_html_editor_view_get_html_mode (view);
 
-               if (!paragraph)
-                       return;
+       /* Process all blocks that are in the selection one by one */
+       while (block && !after_selection_end) {
+               gboolean quoted = FALSE;
+               gint citation_level, quote;
+               WebKitDOMElement *wrapped_paragraph;
 
-               webkit_dom_element_remove_attribute (
-                       WEBKIT_DOM_ELEMENT (paragraph), "style");
-               webkit_dom_element_set_id (
-                       WEBKIT_DOM_ELEMENT (paragraph), "-x-evo-active-paragraph");
+               if (webkit_dom_element_query_selector (
+                       WEBKIT_DOM_ELEMENT (block), "span.-x-evo-quoted", NULL)) {
+                       quoted = TRUE;
+                       remove_quoting_from_element (WEBKIT_DOM_ELEMENT (block));
+               }
 
-               text_content = webkit_dom_node_get_text_content (paragraph);
-               /* If there is hidden space character in the beginning we remove it */
-               if (strstr (text_content, UNICODE_ZERO_WIDTH_SPACE)) {
-                       if (g_str_has_prefix (text_content, UNICODE_ZERO_WIDTH_SPACE)) {
-                               WebKitDOMNode *node;
+               if (!html_mode)
+                       remove_wrapping_from_element (WEBKIT_DOM_ELEMENT (block));
 
-                               node = webkit_dom_node_get_first_child (paragraph);
+               after_selection_end = webkit_dom_node_contains (
+                       block, WEBKIT_DOM_NODE (selection_end_marker));
 
-                               webkit_dom_character_data_delete_data (
-                                       WEBKIT_DOM_CHARACTER_DATA (node),
-                                       0,
-                                       1,
-                                       NULL);
-                       }
-                       if (g_str_has_suffix (text_content, UNICODE_ZERO_WIDTH_SPACE)) {
-                               WebKitDOMNode *node;
+               next_block = webkit_dom_node_get_next_sibling (block);
 
-                               node = webkit_dom_node_get_last_child (paragraph);
+               citation_level = get_citation_level (block);
+               quote = citation_level ? citation_level * 2 : 0;
 
-                               webkit_dom_character_data_delete_data (
-                                       WEBKIT_DOM_CHARACTER_DATA (node),
-                                       g_utf8_strlen (text_content, -1) -1,
-                                       1,
-                                       NULL);
-                       }
-               }
-               g_free (text_content);
+               wrapped_paragraph = e_html_editor_selection_wrap_paragraph_length (
+                       selection, WEBKIT_DOM_ELEMENT (block), selection->priv->word_wrap_length - quote);
 
-               wrap_lines (
-                       NULL, paragraph, document, FALSE,
-                       selection->priv->word_wrap_length);
+               if (quoted && !html_mode)
+                       e_html_editor_view_quote_plain_text_element (view, wrapped_paragraph);
 
-       } else {
-               e_html_editor_selection_save_caret_position (selection);
-               /* If we have selection -> wrap it */
-               wrap_lines (
-                       selection, NULL, document, FALSE,
-                       selection->priv->word_wrap_length);
+               block = next_block;
        }
 
-       active_paragraph = webkit_dom_document_get_element_by_id (
-               document, "-x-evo-active-paragraph");
-       /* We have to move caret on position where it was before modifying the text */
-       e_html_editor_selection_restore_caret_position (selection);
+       e_html_editor_selection_restore (selection);
 
-       /* Set paragraph as non-active */
-       if (active_paragraph)
-               webkit_dom_element_remove_attribute (
-                       WEBKIT_DOM_ELEMENT (active_paragraph), "id");
+       e_html_editor_view_force_spell_check_for_current_paragraph (view);
 }
 
 WebKitDOMElement *


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