[evolution/wip/webkit2] EHTMLEditorSelection - Fix the "Wrap Lines" action



commit dac1a6c3b7b23ed6415ae819ce45b6e6afeb1246
Author: Tomas Popela <tpopela redhat com>
Date:   Thu Apr 9 16:23:26 2015 +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-html-editor-selection-dom-functions.c        |  185 +++++++-------------
 1 files changed, 62 insertions(+), 123 deletions(-)
---
diff --git a/web-extensions/e-html-editor-selection-dom-functions.c 
b/web-extensions/e-html-editor-selection-dom-functions.c
index 092008e..a170e8b 100644
--- a/web-extensions/e-html-editor-selection-dom-functions.c
+++ b/web-extensions/e-html-editor-selection-dom-functions.c
@@ -2538,149 +2538,88 @@ static void
 dom_wrap_lines (WebKitDOMDocument *document,
                 EHTMLEditorWebExtension *extension)
 {
+       gboolean after_selection_end = FALSE, html_mode;
        gint word_wrap_length;
-       WebKitDOMRange *range;
-       WebKitDOMElement *active_paragraph, *caret;
+       WebKitDOMElement *selection_start_marker, *selection_end_marker;
+       WebKitDOMNode *block, *next_block;
 
        word_wrap_length = e_html_editor_web_extension_get_word_wrap_length (extension);
 
-       caret = dom_save_caret_position (document);
-       if (dom_selection_is_collapsed (document)) {
-               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 = dom_get_current_range (document);
-               if (!range)
-                       return;
-
-               end_container = webkit_dom_range_get_common_ancestor_container (range, NULL);
-
-               /* 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 =
-                               dom_node_find_parent_element (parent, "DIV");
-
-                       if (element_has_class (parent_div, "-x-evo-paragraph")) {
-                               paragraph = WEBKIT_DOM_NODE (parent_div);
-                       } else {
-                               if (!caret)
-                                       return;
-
-                               /* 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 (
-                                                       dom_put_node_into_paragraph (
-                                                               document,
-                                                               extension,
-                                                               paragraph,
-                                                               WEBKIT_DOM_NODE (caret)));
-                               } else {
-                                       /* When some weird element is selected, return */
-                                       dom_clear_caret_position_marker (document);
-                                       return;
-                               }
-                       }
-               }
+       dom_selection_save (document);
+       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 (!paragraph)
-                       return;
+       /* If the selection was not saved, move it into the first child of body */
+       if (!selection_start_marker || !selection_end_marker) {
+               WebKitDOMHTMLElement *body;
 
-               webkit_dom_element_remove_attribute (
-                       WEBKIT_DOM_ELEMENT (paragraph), "style");
+               body = webkit_dom_document_get_body (document);
+               selection_start_marker = webkit_dom_document_create_element (
+                       document, "SPAN", NULL);
                webkit_dom_element_set_id (
-                       WEBKIT_DOM_ELEMENT (paragraph), "-x-evo-active-paragraph");
-
-               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;
+                       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);
+       }
 
-                               node = webkit_dom_node_get_first_child (paragraph);
+       block = get_parent_block_node_from_child (
+               WEBKIT_DOM_NODE (selection_start_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;
+       html_mode = e_html_editor_web_extension_get_html_mode (extension);
 
-                               node = webkit_dom_node_get_last_child (paragraph);
+       /* 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_character_data_delete_data (
-                                       WEBKIT_DOM_CHARACTER_DATA (node),
-                                       g_utf8_strlen (text_content, -1) -1,
-                                       1,
-                                       NULL);
-                       }
+               if (webkit_dom_element_query_selector (
+                       WEBKIT_DOM_ELEMENT (block), "span.-x-evo-quoted", NULL)) {
+                       quoted = TRUE;
+                       dom_remove_quoting_from_element (WEBKIT_DOM_ELEMENT (block));
                }
-               g_free (text_content);
 
-               wrap_lines (
-                       document, extension, paragraph, FALSE, word_wrap_length);
+               if (!html_mode)
+                       dom_remove_wrapping_from_element (WEBKIT_DOM_ELEMENT (block));
 
-       } else {
-               dom_save_caret_position (document);
-               /* If we have selection -> wrap it */
-               wrap_lines (
-                       document, extension, NULL, FALSE, word_wrap_length); }
-
-       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 */
-       dom_restore_caret_position (document);
-
-       /* Set paragraph as non-active */
-       if (active_paragraph)
-               webkit_dom_element_remove_attribute (
-                       WEBKIT_DOM_ELEMENT (active_paragraph), "id");
-}
+               after_selection_end = webkit_dom_node_contains (
+                       block, WEBKIT_DOM_NODE (selection_end_marker));
 
-WebKitDOMElement *
-dom_wrap_paragraph_length (WebKitDOMDocument *document,
-                           EHTMLEditorWebExtension *extension,
-                           WebKitDOMElement *paragraph,
-                           gint length)
-{
-       g_return_val_if_fail (WEBKIT_DOM_IS_ELEMENT (paragraph), NULL);
-       g_return_val_if_fail (length >= MINIMAL_PARAGRAPH_WIDTH, NULL);
+               next_block = webkit_dom_node_get_next_sibling (block);
 
-       return wrap_lines (document, extension, WEBKIT_DOM_NODE (paragraph), FALSE, length);
-}
+               citation_level = get_citation_level (block);
+               quote = citation_level ? citation_level * 2 : 0;
 
-static gint
-get_citation_level (WebKitDOMNode *node)
-{
-       WebKitDOMNode *parent = webkit_dom_node_get_parent_node (node);
-       gint level = 0;
+               wrapped_paragraph = dom_wrap_paragraph_length (
+                       document, extension, WEBKIT_DOM_ELEMENT (block), word_wrap_length - quote);
 
-       while (parent && !WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
-               if (WEBKIT_DOM_IS_HTML_QUOTE_ELEMENT (parent) &&
-                   webkit_dom_element_has_attribute (WEBKIT_DOM_ELEMENT (parent), "type"))
-                       level++;
+               if (quoted && !html_mode)
+                       dom_quote_plain_text_element (document, wrapped_paragraph);
 
-               parent = webkit_dom_node_get_parent_node (parent);
+               block = next_block;
        }
 
-       return level;
+       dom_selection_restore (document);
+
+       dom_force_spell_check_for_current_paragraph (document, extension);
 }
 
 void


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