[epiphany] ephy-web-dom-utils: add ephy_web_dom_utils_get_selection_as_string()



commit b516557124a4372e47418cfc51692c2d75e89aec
Author: Claudio Saavedra <csaavedra igalia com>
Date:   Thu Jun 4 15:53:00 2015 +0300

    ephy-web-dom-utils: add ephy_web_dom_utils_get_selection_as_string()
    
    The DOMSelection::toString() API is only bound to JavaScript. This method
    does not work with selection inside input elements.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=663545

 lib/ephy-web-dom-utils.c |   22 ++++++++++++++++++++++
 lib/ephy-web-dom-utils.h |    4 ++++
 2 files changed, 26 insertions(+), 0 deletions(-)
---
diff --git a/lib/ephy-web-dom-utils.c b/lib/ephy-web-dom-utils.c
index 75c586c..2a75da8 100644
--- a/lib/ephy-web-dom-utils.c
+++ b/lib/ephy-web-dom-utils.c
@@ -492,3 +492,25 @@ ephy_web_dom_utils_get_absolute_bottom_for_element (WebKitDOMElement *element,
 
   *y += offset_height;
 }
+
+/**
+ * ephy_web_dom_utils_get_selection_as_string:
+ * @selection: the #WebKitDOMDOMSelection.
+ *
+ * Gets the contents of the selection as a string.
+ *
+ * Returns: a newly allocated string with the selection or %NULL.
+ **/
+char *
+ephy_web_dom_utils_get_selection_as_string   (WebKitDOMDOMSelection *selection)
+{
+  char *string;
+  WebKitDOMRange *range = webkit_dom_dom_selection_get_range_at (selection, 0, NULL);
+  if (!range)
+    return NULL;
+
+  string = webkit_dom_range_to_string (range, NULL);
+  g_object_unref (range);
+
+  return string;
+}
diff --git a/lib/ephy-web-dom-utils.h b/lib/ephy-web-dom-utils.h
index caa8228..c2cc9a5 100644
--- a/lib/ephy-web-dom-utils.h
+++ b/lib/ephy-web-dom-utils.h
@@ -25,6 +25,8 @@
 #define EPHY_WEB_DOM_UTILS_H
 
 #include <webkitdom/webkitdom.h>
+#define WEBKIT_DOM_USE_UNSTABLE_API
+#include <webkitdom/WebKitDOMDOMSelection.h>
 
 G_BEGIN_DECLS
 
@@ -48,6 +50,8 @@ void ephy_web_dom_utils_get_absolute_bottom_for_element (WebKitDOMElement *eleme
 void ephy_web_dom_utils_get_absolute_position_for_element(WebKitDOMElement *element,
                                                           long             *x,
                                                           long             *y);
+
+char *ephy_web_dom_utils_get_selection_as_string (WebKitDOMDOMSelection *selection);
 G_END_DECLS
 
 #endif


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