[geary/wip/712912-select-quote: 6/7] Preserve newlines when quoting selected plain text



commit eaa8be5e463cc5f37c9c01508c73a37b85754d44
Author: Robert Schroll <rschroll gmail com>
Date:   Fri Sep 19 01:53:45 2014 -0400

    Preserve newlines when quoting selected plain text
    
    https://bugzilla.gnome.org/show_bug.cgi?id=712912

 .../conversation-viewer/conversation-viewer.vala   |   17 +++++++++++++++--
 src/client/util/util-webkit.vala                   |   13 +++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)
---
diff --git a/src/client/conversation-viewer/conversation-viewer.vala 
b/src/client/conversation-viewer/conversation-viewer.vala
index 66bf8e8..a7103a8 100644
--- a/src/client/conversation-viewer/conversation-viewer.vala
+++ b/src/client/conversation-viewer/conversation-viewer.vala
@@ -234,9 +234,22 @@ public class ConversationViewer : Gtk.Box {
         
         if (anchor_email != null && anchor_email == focus_email) {
             try {
+                WebKit.DOM.Range range = selection.get_range_at(0);
                 WebKit.DOM.HTMLElement dummy = (WebKit.DOM.HTMLElement) document.create_element("div");
-                dummy.append_child(selection.get_range_at(0).clone_contents());
-                quote = dummy.get_inner_html();
+                bool include_dummy = false;
+                WebKit.DOM.Node ancestor_node = range.get_common_ancestor_container();
+                WebKit.DOM.Element? ancestor = ancestor_node as WebKit.DOM.Element;
+                if (ancestor == null)
+                    ancestor = ancestor_node.get_parent_element();
+                // If the selection is part of a plain text message, we have to stick it in
+                // an appropriately styled div, so that new lines are preserved.
+                if (is_descendant_of(ancestor, ".plaintext")) {
+                    dummy.get_class_list().add("plaintext");
+                    dummy.set_attribute("style", "white-space: pre-wrap;");
+                    include_dummy = true;
+                }
+                dummy.append_child(range.clone_contents());
+                quote = include_dummy ? dummy.get_outer_html() : dummy.get_inner_html();
                 return anchor_email;
             } catch (Error error) {
                 debug("Problem getting selected text: %s", error.message);
diff --git a/src/client/util/util-webkit.vala b/src/client/util/util-webkit.vala
index 4bf3d4c..a5d347e 100644
--- a/src/client/util/util-webkit.vala
+++ b/src/client/util/util-webkit.vala
@@ -259,6 +259,19 @@ public WebKit.DOM.HTMLElement? closest_ancestor(WebKit.DOM.Element element, stri
     }
 }
 
+public bool is_descendant_of(WebKit.DOM.Element? element, string selector) {
+    try {
+        while (element != null) {
+            if (element.webkit_matches_selector(selector))
+                return true;
+            element = element.get_parent_element();
+        }
+    } catch (Error error) {
+        warning("Problem traversing DOM: %s", error.message);
+    }
+    return false;
+}
+
 public string decorate_quotes(string text) throws Error {
     int level = 0;
     string outtext = "";


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