1 # include <string>
2 # include <vector>
3 # include <gio/gio.h>
4 # include <iostream>
5 
6 #ifdef ASTROID_WEBEXTENSION
7 # include <webkit2/webkit-web-extension.h>
8 # else
9 # include <webkit2/webkit2.h>
10 # endif
11 
12 # include "dom_utils.hh"
13 
14 # include <boost/log/core.hpp>
15 # include <boost/log/trivial.hpp>
16 # define LOG(x) BOOST_LOG_TRIVIAL(x)
17 # define warn warning
18 
19 using std::vector;
20 
21 namespace Astroid {
assemble_data_uri(const char * mime_type,const gchar * data,gsize len)22   std::string DomUtils::assemble_data_uri (const char * mime_type, const gchar * data, gsize len) {
23     return DomUtils::assemble_data_uri (std::string (mime_type), std::string (data, len));
24   }
25 
assemble_data_uri(const std::string mime_type,const std::string data)26   std::string DomUtils::assemble_data_uri (const std::string mime_type, const std::string data) {
27 
28     std::string base64 = "data:" + mime_type + ";base64," + Glib::Base64::encode (data);
29 
30     return base64;
31   }
32 
33 # ifdef ASTROID_WEBEXTENSION
34 
35   /* clone and create html elements */
make_message_div(WebKitDOMDocument * d)36   WebKitDOMHTMLElement * DomUtils::make_message_div (WebKitDOMDocument * d) {
37     /* clone div from template in html file */
38     WebKitDOMHTMLElement * e = clone_node (WEBKIT_DOM_NODE(get_by_id (d, "email_template")));
39     return e;
40   }
41 
clone_get_by_id(WebKitDOMDocument * node,ustring id,bool deep)42   WebKitDOMHTMLElement * DomUtils::clone_get_by_id (
43       WebKitDOMDocument * node,
44       ustring         id,
45       bool            deep) {
46 
47     return clone_node (WEBKIT_DOM_NODE (get_by_id (node, id)), deep);
48   }
49 
clone_select(WebKitDOMNode * node,ustring selector,bool deep)50   WebKitDOMHTMLElement * DomUtils::clone_select (
51       WebKitDOMNode * node,
52       ustring         selector,
53       bool            deep) {
54 
55     return clone_node (WEBKIT_DOM_NODE(select (node, selector)), deep);
56   }
57 
clone_node(WebKitDOMNode * node,bool deep)58   WebKitDOMHTMLElement * DomUtils::clone_node (
59       WebKitDOMNode * node,
60       bool            deep) {
61 
62     GError * gerr = NULL;
63     return WEBKIT_DOM_HTML_ELEMENT(webkit_dom_node_clone_node_with_error (node, deep, &gerr));
64   }
65 
select(WebKitDOMNode * node,ustring selector)66   WebKitDOMHTMLElement * DomUtils::select (
67       WebKitDOMNode * node,
68       ustring         selector) {
69 
70     GError * gerr = NULL;
71     WebKitDOMHTMLElement *e;
72 
73     if (WEBKIT_DOM_IS_DOCUMENT(node)) {
74       e = WEBKIT_DOM_HTML_ELEMENT(
75         webkit_dom_document_query_selector (WEBKIT_DOM_DOCUMENT(node),
76                                             selector.c_str(),
77                                             &gerr));
78     } else {
79       /* ..DOMElement */
80       e = WEBKIT_DOM_HTML_ELEMENT(
81         webkit_dom_element_query_selector (WEBKIT_DOM_ELEMENT(node),
82                                             selector.c_str(),
83                                             &gerr));
84     }
85 
86     if (gerr != NULL)
87       LOG (debug) << "dom: error: " << gerr->message;
88 
89     return e;
90   }
91 
get_by_id(WebKitDOMDocument * d,ustring id)92   WebKitDOMElement * DomUtils::get_by_id (WebKitDOMDocument * d, ustring id) {
93     WebKitDOMElement * en = webkit_dom_document_get_element_by_id (d, id.c_str());
94 
95     return en;
96   }
97 
switch_class(WebKitDOMDOMTokenList * list,ustring c,bool v)98   bool DomUtils::switch_class (WebKitDOMDOMTokenList * list, ustring c, bool v)
99   {
100     GError * err = NULL;
101 
102     bool x = webkit_dom_dom_token_list_contains (list, c.c_str ());
103 
104     if (v && !x) {
105       webkit_dom_dom_token_list_add (list, &err, c.c_str (), NULL );
106     } else if (!v && x) {
107       webkit_dom_dom_token_list_remove (list, &err, c.c_str (), NULL );
108     }
109 
110     return x;
111   }
112 
in_view(WebKitWebPage * page,ustring eid)113   bool DomUtils::in_view (WebKitWebPage * page, ustring eid) {
114     WebKitDOMDocument * d = webkit_web_page_get_dom_document (page);
115     WebKitDOMDOMWindow * w = webkit_dom_document_get_default_view (d);
116     WebKitDOMElement * body = WEBKIT_DOM_ELEMENT(webkit_dom_document_get_body (d));
117 
118     WebKitDOMElement * e = webkit_dom_document_get_element_by_id (d, eid.c_str());
119 
120     double scrolled = webkit_dom_dom_window_get_scroll_y (w);
121     double height   = webkit_dom_element_get_client_height (body);
122 
123     double clientY = webkit_dom_element_get_offset_top (e);
124     double clientH = webkit_dom_element_get_client_height (e);
125 
126     g_object_unref (e);
127     g_object_unref (body);
128     g_object_unref (w);
129     g_object_unref (d);
130 
131     return ( (clientY >= scrolled) &&
132            ( (clientY + clientH) <= (scrolled + height) ));
133   }
134 
135 # endif
136 
137 }
138 
139