Rename WebKitTools to Tools
[WebKit-https.git] / Tools / DumpRenderTree / gtk / EditingCallbacks.cpp
1 /*
2  * Copyright (C) 2010 Igalia S.L.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "EditingCallbacks.h"
31
32 #include "CString.h"
33 #include "DumpRenderTree.h"
34 #include "GOwnPtr.h"
35 #include "LayoutTestController.h"
36 #include <gtk/gtk.h>
37 #include <webkit/webkit.h>
38
39 static CString dumpNodePath(WebKitDOMNode* node)
40 {
41     GOwnPtr<gchar> nodeName(webkit_dom_node_get_node_name(node));
42     GString* path = g_string_new(nodeName.get());
43
44     WebKitDOMNode* parent = webkit_dom_node_get_parent_node(node);
45     while (parent) {
46         GOwnPtr<gchar> parentName(webkit_dom_node_get_node_name(parent));
47
48         g_string_append(path, " > ");
49         g_string_append(path, parentName.get());
50         parent = webkit_dom_node_get_parent_node(parent);
51     }
52
53     GOwnPtr<gchar> pathBuffer(g_string_free(path, FALSE));
54     return pathBuffer.get();
55 }
56
57 static CString dumpRange(WebKitDOMRange* range)
58 {
59     if (!range)
60         return "(null)";
61
62     GOwnPtr<GError> error1;
63     GOwnPtr<GError> error2;
64     GOwnPtr<GError> error3;
65     GOwnPtr<GError> error4;
66     GOwnPtr<gchar> dump(g_strdup_printf("range from %li of %s to %li of %s",
67         webkit_dom_range_get_start_offset(range, &error1.outPtr()),
68         dumpNodePath(webkit_dom_range_get_start_container(range, &error2.outPtr())).data(),
69         webkit_dom_range_get_end_offset(range, &error3.outPtr()),
70         dumpNodePath(webkit_dom_range_get_end_container(range, &error4.outPtr())).data()));
71     return dump.get();
72 }
73
74 static const char* insertActionString(WebKitInsertAction action)
75 {
76     switch (action) {
77     case WEBKIT_INSERT_ACTION_TYPED:
78         return "WebViewInsertActionTyped";
79     case WEBKIT_INSERT_ACTION_PASTED:
80         return "WebViewInsertActionPasted";
81     case WEBKIT_INSERT_ACTION_DROPPED:
82         return "WebViewInsertActionDropped";
83     }
84     ASSERT_NOT_REACHED();
85     return "WebViewInsertActionTyped";
86 }
87
88 static const char* selectionAffinityString(WebKitSelectionAffinity affinity)
89 {
90     switch (affinity) {
91     case WEBKIT_SELECTION_AFFINITY_UPSTREAM:
92         return "NSSelectionAffinityUpstream";
93     case WEBKIT_SELECTION_AFFINITY_DOWNSTREAM:
94         return "NSSelectionAffinityDownstream";
95     }
96     ASSERT_NOT_REACHED();
97     return "NSSelectionAffinityUpstream";
98 }
99
100 gboolean shouldBeginEditing(WebKitWebView* webView, WebKitDOMRange* range)
101 {
102     if (!done && gLayoutTestController->dumpEditingCallbacks())
103         printf("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n", dumpRange(range).data());
104     return TRUE;
105 }
106
107 gboolean shouldEndEditing(WebKitWebView* webView, WebKitDOMRange* range)
108 {
109     if (!done && gLayoutTestController->dumpEditingCallbacks())
110         printf("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n", dumpRange(range).data());
111     return TRUE;
112 }
113
114 gboolean shouldInsertNode(WebKitWebView* webView, WebKitDOMNode* node, WebKitDOMRange* range, WebKitInsertAction action)
115 {
116     if (!done && gLayoutTestController->dumpEditingCallbacks()) {
117         printf("EDITING DELEGATE: shouldInsertNode:%s replacingDOMRange:%s givenAction:%s\n",
118                dumpNodePath(node).data(), dumpRange(range).data(), insertActionString(action));
119     }
120     return TRUE;
121 }
122
123 gboolean shouldInsertText(WebKitWebView* webView, const gchar* text, WebKitDOMRange* range, WebKitInsertAction action)
124 {
125     if (!done && gLayoutTestController->dumpEditingCallbacks()) {
126         printf("EDITING DELEGATE: shouldInsertText:%s replacingDOMRange:%s givenAction:%s\n",
127                text, dumpRange(range).data(), insertActionString(action));
128     }
129     return TRUE;
130 }
131
132 gboolean shouldDeleteRange(WebKitWebView* webView, WebKitDOMRange* range)
133 {
134     if (!done && gLayoutTestController->dumpEditingCallbacks())
135         printf("EDITING DELEGATE: shouldDeleteDOMRange:%s\n", dumpRange(range).data());
136     return TRUE;
137 }
138
139 gboolean shouldShowDeleteInterfaceForElement(WebKitWebView* webView, WebKitDOMHTMLElement* element)
140 {
141     GOwnPtr<gchar> elementClassName(webkit_dom_html_element_get_class_name(element));
142     return g_str_equal(elementClassName.get(), "needsDeletionUI");
143 }
144
145 gboolean shouldChangeSelectedRange(WebKitWebView* webView, WebKitDOMRange* fromRange, WebKitDOMRange* toRange, WebKitSelectionAffinity affinity, gboolean stillSelecting)
146 {
147     if (!done && gLayoutTestController->dumpEditingCallbacks()) {
148         printf("EDITING DELEGATE: shouldChangeSelectedDOMRange:%s toDOMRange:%s affinity:%s stillSelecting:%s\n",
149                dumpRange(fromRange).data(), dumpRange(toRange).data(), selectionAffinityString(affinity),
150                stillSelecting ? "TRUE" : "FALSE");
151     }
152     return TRUE;
153 }
154
155 gboolean shouldApplyStyle(WebKitWebView* webView, WebKitDOMCSSStyleDeclaration* style, WebKitDOMRange* range)
156 {
157     if (!done && gLayoutTestController->dumpEditingCallbacks()) {
158         GOwnPtr<gchar> styleText(webkit_dom_css_style_declaration_get_css_text(style));
159         printf("EDITING DELEGATE: shouldApplyStyle:%s toElementsInDOMRange:%s\n",
160                styleText.get(), dumpRange(range).data());
161     }
162     return TRUE;
163 }
164
165 void editingBegan(WebKitWebView*)
166 {
167     if (!done && gLayoutTestController->dumpEditingCallbacks())
168         printf("EDITING DELEGATE: webViewDidBeginEditing:WebViewDidBeginEditingNotification\n");
169 }
170
171 void userChangedContents(WebKitWebView*)
172 {
173     if (!done && gLayoutTestController->dumpEditingCallbacks())
174         printf("EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification\n");
175 }
176
177 void editingEnded(WebKitWebView*)
178 {
179     if (!done && gLayoutTestController->dumpEditingCallbacks())
180         printf("EDITING DELEGATE: webViewDidEndEditing:WebViewDidEndEditingNotification\n");
181 }
182
183 void selectionChanged(WebKitWebView*)
184 {
185     if (!done && gLayoutTestController->dumpEditingCallbacks())
186         printf("EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification\n");
187 }
188
189 void connectEditingCallbacks(WebKitWebView* webView)
190 {
191     g_object_connect(G_OBJECT(webView),
192                      "signal::should-begin-editing", shouldBeginEditing, 0,
193                      "signal::should-end-editing", shouldEndEditing, 0,
194                      "signal::should-insert-node", shouldInsertNode, 0,
195                      "signal::should-insert-text", shouldInsertText, 0,
196                      "signal::should-delete-range", shouldDeleteRange, 0,
197                      "signal::should-show-delete-interface-for-element", shouldShowDeleteInterfaceForElement, 0,
198                      "signal::should-change-selected-range", shouldChangeSelectedRange, 0,
199                      "signal::should-apply-style", shouldApplyStyle, 0,
200                      "signal::editing-began", editingBegan, 0,
201                      "signal::user-changed-contents", userChangedContents, 0,
202                      "signal::editing-ended", editingEnded, 0,
203                      "signal::selection-changed", selectionChanged, 0,
204                      NULL);
205 }
206