2 * Copyright (C) 2009 Collabora Ltd.
3 * Copyright (C) 2009 Igalia S.L.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #include "webkithittestresult.h"
26 #include "HitTestResult.h"
28 #include "WebKitDOMBinding.h"
29 #include "WebKitDOMNode.h"
30 #include "webkitenumtypes.h"
31 #include "webkitglobals.h"
32 #include "webkitglobalsprivate.h"
33 #include <glib/gi18n-lib.h>
34 #include <wtf/text/CString.h>
37 * SECTION:webkithittestresult
38 * @short_description: The target of a mouse event
40 * This class holds context information about the coordinates
41 * specified by a GDK event.
44 G_DEFINE_TYPE(WebKitHitTestResult, webkit_hit_test_result, G_TYPE_OBJECT)
46 struct _WebKitHitTestResultPrivate {
51 GRefPtr<WebKitDOMNode> innerNode;
64 static void webkit_hit_test_result_finalize(GObject* object)
66 WebKitHitTestResult* web_hit_test_result = WEBKIT_HIT_TEST_RESULT(object);
67 WebKitHitTestResultPrivate* priv = web_hit_test_result->priv;
69 g_free(priv->linkURI);
70 g_free(priv->imageURI);
71 g_free(priv->mediaURI);
73 G_OBJECT_CLASS(webkit_hit_test_result_parent_class)->finalize(object);
76 static void webkit_hit_test_result_dispose(GObject* object)
78 WEBKIT_HIT_TEST_RESULT(object)->priv->~WebKitHitTestResultPrivate();
80 G_OBJECT_CLASS(webkit_hit_test_result_parent_class)->dispose(object);
83 static void webkit_hit_test_result_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* pspec)
85 WebKitHitTestResult* web_hit_test_result = WEBKIT_HIT_TEST_RESULT(object);
86 WebKitHitTestResultPrivate* priv = web_hit_test_result->priv;
90 g_value_set_flags(value, priv->context);
93 g_value_set_string(value, priv->linkURI);
96 g_value_set_string(value, priv->imageURI);
99 g_value_set_string(value, priv->mediaURI);
101 case PROP_INNER_NODE:
102 g_value_set_object(value, priv->innerNode.get());
105 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
109 static void webkit_hit_test_result_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* pspec)
111 WebKitHitTestResult* web_hit_test_result = WEBKIT_HIT_TEST_RESULT(object);
112 WebKitHitTestResultPrivate* priv = web_hit_test_result->priv;
116 priv->context = g_value_get_flags(value);
119 g_free (priv->linkURI);
120 priv->linkURI = g_value_dup_string(value);
123 g_free (priv->imageURI);
124 priv->imageURI = g_value_dup_string(value);
127 g_free (priv->mediaURI);
128 priv->mediaURI = g_value_dup_string(value);
130 case PROP_INNER_NODE:
131 priv->innerNode = static_cast<WebKitDOMNode*>(g_value_get_object(value));
134 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
138 static void webkit_hit_test_result_class_init(WebKitHitTestResultClass* webHitTestResultClass)
140 GObjectClass* objectClass = G_OBJECT_CLASS(webHitTestResultClass);
142 objectClass->finalize = webkit_hit_test_result_finalize;
143 objectClass->dispose = webkit_hit_test_result_dispose;
144 objectClass->get_property = webkit_hit_test_result_get_property;
145 objectClass->set_property = webkit_hit_test_result_set_property;
150 * WebKitHitTestResult:context:
152 * Flags indicating the kind of target that received the event.
156 g_object_class_install_property(objectClass, PROP_CONTEXT,
157 g_param_spec_flags("context",
159 _("Flags indicating the kind of target that received the event."),
160 WEBKIT_TYPE_HIT_TEST_RESULT_CONTEXT,
161 WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
162 static_cast<GParamFlags>((WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY))));
165 * WebKitHitTestResult:link-uri:
167 * The URI to which the target that received the event points, if any.
171 g_object_class_install_property(objectClass, PROP_LINK_URI,
172 g_param_spec_string("link-uri",
174 _("The URI to which the target that received the event points, if any."),
176 static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
179 * WebKitHitTestResult:image-uri:
181 * The URI of the image that is part of the target that received the event, if any.
185 g_object_class_install_property(objectClass, PROP_IMAGE_URI,
186 g_param_spec_string("image-uri",
188 _("The URI of the image that is part of the target that received the event, if any."),
190 static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
193 * WebKitHitTestResult:media-uri:
195 * The URI of the media that is part of the target that received the event, if any.
199 g_object_class_install_property(objectClass, PROP_MEDIA_URI,
200 g_param_spec_string("media-uri",
202 _("The URI of the media that is part of the target that received the event, if any."),
204 static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
207 * WebKitHitTestResult:inner-node:
209 * The DOM node at the coordinates where the hit test
210 * happened. Keep in mind that the node might not be
211 * representative of the information given in the context
212 * property, since WebKit uses a series of heuristics to figure
213 * out that information. One common example is inner-node having
214 * the text node inside the anchor (<a>) tag; WebKit knows the
215 * whole context and will put WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK
216 * in the 'context' property, but the user might be confused by
217 * the lack of any link tag in 'inner-node'.
221 g_object_class_install_property(objectClass, PROP_INNER_NODE,
222 g_param_spec_object("inner-node",
224 _("The inner DOM node associated with the hit test result."),
225 WEBKIT_TYPE_DOM_NODE,
226 static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
228 g_type_class_add_private(webHitTestResultClass, sizeof(WebKitHitTestResultPrivate));
231 static void webkit_hit_test_result_init(WebKitHitTestResult* web_hit_test_result)
233 web_hit_test_result->priv = G_TYPE_INSTANCE_GET_PRIVATE(web_hit_test_result, WEBKIT_TYPE_HIT_TEST_RESULT, WebKitHitTestResultPrivate);
234 new (web_hit_test_result->priv) WebKitHitTestResultPrivate();
239 WebKitHitTestResult* kit(const WebCore::HitTestResult& result)
241 guint context = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT;
242 GOwnPtr<char> linkURI(0);
243 GOwnPtr<char> imageURI(0);
244 GOwnPtr<char> mediaURI(0);
245 WebKitDOMNode* node = 0;
247 if (!result.absoluteLinkURL().isEmpty()) {
248 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK;
249 linkURI.set(g_strdup(result.absoluteLinkURL().string().utf8().data()));
252 if (!result.absoluteImageURL().isEmpty()) {
253 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE;
254 imageURI.set(g_strdup(result.absoluteImageURL().string().utf8().data()));
257 if (!result.absoluteMediaURL().isEmpty()) {
258 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA;
259 mediaURI.set(g_strdup(result.absoluteMediaURL().string().utf8().data()));
262 if (result.isSelected())
263 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION;
265 if (result.isContentEditable())
266 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE;
268 if (result.innerNonSharedNode())
269 node = kit(result.innerNonSharedNode());
271 return WEBKIT_HIT_TEST_RESULT(g_object_new(WEBKIT_TYPE_HIT_TEST_RESULT,
272 "link-uri", linkURI.get(),
273 "image-uri", imageURI.get(),
274 "media-uri", mediaURI.get(),