1/*
2 * Copyright (C) 2014 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2,1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitWebHitTestResult.h"
22
23#include "InjectedBundleHitTestResult.h"
24#include "WebKitDOMNodePrivate.h"
25#include "WebKitWebHitTestResultPrivate.h"
26#include <glib/gi18n-lib.h>
27#include <wtf/glib/GRefPtr.h>
28#include <wtf/glib/WTFGType.h>
29#include <wtf/text/CString.h>
30
31using namespace WebKit;
32using namespace WebCore;
33
34/**
35 * SECTION: WebKitWebHitTestResult
36 * @Short_description: Result of a Hit Test (Web Process Extensions)
37 * @Title: WebKitWebHitTestResult
38 * @See_also: #WebKitHitTestResult, #WebKitWebPage
39 *
40 * WebKitWebHitTestResult extends #WebKitHitTestResult to provide information
41 * about the #WebKitDOMNode in the coordinates of the Hit Test.
42 *
43 * Since: 2.8
44 */
45
46enum {
47 PROP_0,
48
49 PROP_NODE
50};
51
52struct _WebKitWebHitTestResultPrivate {
53 GRefPtr<WebKitDOMNode> node;
54};
55
56WEBKIT_DEFINE_TYPE(WebKitWebHitTestResult, webkit_web_hit_test_result, WEBKIT_TYPE_HIT_TEST_RESULT)
57
58static void webkitWebHitTestResultGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
59{
60 WebKitWebHitTestResult* webHitTestResult = WEBKIT_WEB_HIT_TEST_RESULT(object);
61
62 switch (propId) {
63 case PROP_NODE:
64 g_value_set_object(value, webkit_web_hit_test_result_get_node(webHitTestResult));
65 break;
66 default:
67 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
68 }
69}
70
71static void webkitWebHitTestResultSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec)
72{
73 WebKitWebHitTestResult* webHitTestResult = WEBKIT_WEB_HIT_TEST_RESULT(object);
74
75 switch (propId) {
76 case PROP_NODE: {
77 gpointer node = g_value_get_object(value);
78 webHitTestResult->priv->node = node ? WEBKIT_DOM_NODE(node) : nullptr;
79 break;
80 }
81 default:
82 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
83 }
84}
85
86static void webkit_web_hit_test_result_class_init(WebKitWebHitTestResultClass* klass)
87{
88 GObjectClass* gObjectClass = G_OBJECT_CLASS(klass);
89
90 gObjectClass->get_property = webkitWebHitTestResultGetProperty;
91 gObjectClass->set_property = webkitWebHitTestResultSetProperty;
92
93 /**
94 * WebKitWebHitTestResult:node:
95 *
96 * The #WebKitDOMNode
97 */
98 g_object_class_install_property(
99 gObjectClass,
100 PROP_NODE,
101 g_param_spec_object(
102 "node",
103 _("Node"),
104 _("The WebKitDOMNode"),
105 WEBKIT_DOM_TYPE_NODE,
106 static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
107}
108
109WebKitWebHitTestResult* webkitWebHitTestResultCreate(const HitTestResult& hitTestResult)
110{
111 unsigned context = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT;
112 String absoluteLinkURL = hitTestResult.absoluteLinkURL().string();
113 if (!absoluteLinkURL.isEmpty())
114 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK;
115 String absoluteImageURL = hitTestResult.absoluteImageURL().string();
116 if (!absoluteImageURL.isEmpty())
117 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE;
118 String absoluteMediaURL = hitTestResult.absoluteMediaURL().string();
119 if (!absoluteMediaURL.isEmpty())
120 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA;
121 if (hitTestResult.isContentEditable())
122 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE;
123 if (hitTestResult.scrollbar())
124 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR;
125 if (hitTestResult.isSelected())
126 context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION;
127
128 String linkTitle = hitTestResult.titleDisplayString();
129 String linkLabel = hitTestResult.textContent();
130
131 return WEBKIT_WEB_HIT_TEST_RESULT(g_object_new(WEBKIT_TYPE_WEB_HIT_TEST_RESULT,
132 "context", context,
133 "link-uri", context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK ? absoluteLinkURL.utf8().data() : nullptr,
134 "image-uri", context & WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE ? absoluteImageURL.utf8().data() : nullptr,
135 "media-uri", context & WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA ? absoluteMediaURL.utf8().data() : nullptr,
136 "link-title", !linkTitle.isEmpty() ? linkTitle.utf8().data() : nullptr,
137 "link-label", !linkLabel.isEmpty() ? linkLabel.utf8().data() : nullptr,
138 "node", kit(hitTestResult.innerNonSharedNode()),
139 nullptr));
140}
141
142/**
143 * webkit_web_hit_test_result_get_node:
144 * @hit_test_result: a #WebKitWebHitTestResult
145 *
146 * Get the #WebKitDOMNode in the coordinates of the Hit Test.
147 *
148 * Returns: (transfer none): a #WebKitDOMNode
149 *
150 * Since: 2.8
151 */
152WebKitDOMNode* webkit_web_hit_test_result_get_node(WebKitWebHitTestResult* webHitTestResult)
153{
154 g_return_val_if_fail(WEBKIT_IS_WEB_HIT_TEST_RESULT(webHitTestResult), nullptr);
155
156 return webHitTestResult->priv->node.get();
157}
158