2 * Copyright (C) 2010 Igalia S.L.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "EditingCallbacks.h"
32 #include "DumpRenderTree.h"
33 #include "LayoutTestController.h"
35 #include <webkit/webkit.h>
36 #include <wtf/gobject/GOwnPtr.h>
37 #include <wtf/text/CString.h>
39 static CString dumpNodePath(WebKitDOMNode* node)
41 GOwnPtr<gchar> nodeName(webkit_dom_node_get_node_name(node));
42 GString* path = g_string_new(nodeName.get());
44 WebKitDOMNode* parent = webkit_dom_node_get_parent_node(node);
46 GOwnPtr<gchar> parentName(webkit_dom_node_get_node_name(parent));
48 g_string_append(path, " > ");
49 g_string_append(path, parentName.get());
50 parent = webkit_dom_node_get_parent_node(parent);
53 GOwnPtr<gchar> pathBuffer(g_string_free(path, FALSE));
54 return pathBuffer.get();
57 static CString dumpRange(WebKitDOMRange* range)
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()));
74 static const char* insertActionString(WebKitInsertAction 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";
85 return "WebViewInsertActionTyped";
88 static const char* selectionAffinityString(WebKitSelectionAffinity affinity)
91 case WEBKIT_SELECTION_AFFINITY_UPSTREAM:
92 return "NSSelectionAffinityUpstream";
93 case WEBKIT_SELECTION_AFFINITY_DOWNSTREAM:
94 return "NSSelectionAffinityDownstream";
97 return "NSSelectionAffinityUpstream";
100 gboolean shouldBeginEditing(WebKitWebView* webView, WebKitDOMRange* range)
102 if (!done && gLayoutTestController->dumpEditingCallbacks())
103 printf("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n", dumpRange(range).data());
107 gboolean shouldEndEditing(WebKitWebView* webView, WebKitDOMRange* range)
109 if (!done && gLayoutTestController->dumpEditingCallbacks())
110 printf("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n", dumpRange(range).data());
114 gboolean shouldInsertNode(WebKitWebView* webView, WebKitDOMNode* node, WebKitDOMRange* range, WebKitInsertAction action)
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));
123 gboolean shouldInsertText(WebKitWebView* webView, const gchar* text, WebKitDOMRange* range, WebKitInsertAction action)
125 if (!done && gLayoutTestController->dumpEditingCallbacks()) {
126 printf("EDITING DELEGATE: shouldInsertText:%s replacingDOMRange:%s givenAction:%s\n",
127 text, dumpRange(range).data(), insertActionString(action));
132 gboolean shouldDeleteRange(WebKitWebView* webView, WebKitDOMRange* range)
134 if (!done && gLayoutTestController->dumpEditingCallbacks())
135 printf("EDITING DELEGATE: shouldDeleteDOMRange:%s\n", dumpRange(range).data());
139 gboolean shouldShowDeleteInterfaceForElement(WebKitWebView* webView, WebKitDOMHTMLElement* element)
141 GOwnPtr<gchar> elementClassName(webkit_dom_html_element_get_class_name(element));
142 return g_str_equal(elementClassName.get(), "needsDeletionUI");
145 gboolean shouldChangeSelectedRange(WebKitWebView* webView, WebKitDOMRange* fromRange, WebKitDOMRange* toRange, WebKitSelectionAffinity affinity, gboolean stillSelecting)
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");
155 gboolean shouldApplyStyle(WebKitWebView* webView, WebKitDOMCSSStyleDeclaration* style, WebKitDOMRange* range)
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());
165 void editingBegan(WebKitWebView*)
167 if (!done && gLayoutTestController->dumpEditingCallbacks())
168 printf("EDITING DELEGATE: webViewDidBeginEditing:WebViewDidBeginEditingNotification\n");
171 void userChangedContents(WebKitWebView*)
173 if (!done && gLayoutTestController->dumpEditingCallbacks())
174 printf("EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification\n");
177 void editingEnded(WebKitWebView*)
179 if (!done && gLayoutTestController->dumpEditingCallbacks())
180 printf("EDITING DELEGATE: webViewDidEndEditing:WebViewDidEndEditingNotification\n");
183 void selectionChanged(WebKitWebView*)
185 if (!done && gLayoutTestController->dumpEditingCallbacks())
186 printf("EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification\n");
189 void connectEditingCallbacks(WebKitWebView* webView)
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,