1 /*
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 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
21 #include "config.h"
22 #include "HitTestResult.h"
23
24 #include "Frame.h"
25 #include "FrameTree.h"
26 #include "HTMLAnchorElement.h"
27 #include "HTMLImageElement.h"
28 #include "HTMLInputElement.h"
29 #include "HTMLNames.h"
30 #include "RenderImage.h"
31 #include "Scrollbar.h"
32 #include "SelectionController.h"
33
34 #if ENABLE(SVG)
35 #include "SVGNames.h"
36 #include "XLinkNames.h"
37 #endif
38
39 #if ENABLE(WML)
40 #include "WMLImageElement.h"
41 #include "WMLNames.h"
42 #endif
43
44 namespace WebCore {
45
46 using namespace HTMLNames;
47
HitTestResult(const IntPoint & point)48 HitTestResult::HitTestResult(const IntPoint& point)
49 : m_point(point)
50 , m_isOverWidget(false)
51 {
52 }
53
HitTestResult(const HitTestResult & other)54 HitTestResult::HitTestResult(const HitTestResult& other)
55 : m_innerNode(other.innerNode())
56 , m_innerNonSharedNode(other.innerNonSharedNode())
57 , m_point(other.point())
58 , m_localPoint(other.localPoint())
59 , m_innerURLElement(other.URLElement())
60 , m_scrollbar(other.scrollbar())
61 , m_isOverWidget(other.isOverWidget())
62 {
63 }
64
~HitTestResult()65 HitTestResult::~HitTestResult()
66 {
67 }
68
operator =(const HitTestResult & other)69 HitTestResult& HitTestResult::operator=(const HitTestResult& other)
70 {
71 m_innerNode = other.innerNode();
72 m_innerNonSharedNode = other.innerNonSharedNode();
73 m_point = other.point();
74 m_localPoint = other.localPoint();
75 m_innerURLElement = other.URLElement();
76 m_scrollbar = other.scrollbar();
77 m_isOverWidget = other.isOverWidget();
78 return *this;
79 }
80
setToNonShadowAncestor()81 void HitTestResult::setToNonShadowAncestor()
82 {
83 Node* node = innerNode();
84 if (node)
85 node = node->shadowAncestorNode();
86 setInnerNode(node);
87 node = innerNonSharedNode();
88 if (node)
89 node = node->shadowAncestorNode();
90 setInnerNonSharedNode(node);
91 }
92
setInnerNode(Node * n)93 void HitTestResult::setInnerNode(Node* n)
94 {
95 m_innerNode = n;
96 }
97
setInnerNonSharedNode(Node * n)98 void HitTestResult::setInnerNonSharedNode(Node* n)
99 {
100 m_innerNonSharedNode = n;
101 }
102
setURLElement(Element * n)103 void HitTestResult::setURLElement(Element* n)
104 {
105 m_innerURLElement = n;
106 }
107
setScrollbar(Scrollbar * s)108 void HitTestResult::setScrollbar(Scrollbar* s)
109 {
110 m_scrollbar = s;
111 }
112
targetFrame() const113 Frame* HitTestResult::targetFrame() const
114 {
115 if (!m_innerURLElement)
116 return 0;
117
118 Frame* frame = m_innerURLElement->document()->frame();
119 if (!frame)
120 return 0;
121
122 return frame->tree()->find(m_innerURLElement->target());
123 }
124
boundingBox() const125 IntRect HitTestResult::boundingBox() const
126 {
127 if (m_innerNonSharedNode) {
128 RenderObject* renderer = m_innerNonSharedNode->renderer();
129 if (renderer)
130 return renderer->absoluteBoundingBoxRect();
131 }
132
133 return IntRect();
134 }
135
isSelected() const136 bool HitTestResult::isSelected() const
137 {
138 if (!m_innerNonSharedNode)
139 return false;
140
141 Frame* frame = m_innerNonSharedNode->document()->frame();
142 if (!frame)
143 return false;
144
145 return frame->selection()->contains(m_point);
146 }
147
spellingToolTip() const148 String HitTestResult::spellingToolTip() const
149 {
150 // Return the tool tip string associated with this point, if any. Only markers associated with bad grammar
151 // currently supply strings, but maybe someday markers associated with misspelled words will also.
152 if (!m_innerNonSharedNode)
153 return String();
154
155 DocumentMarker* marker = m_innerNonSharedNode->document()->markerContainingPoint(m_point, DocumentMarker::Grammar);
156 if (!marker)
157 return String();
158
159 return marker->description;
160 }
161
title() const162 String HitTestResult::title() const
163 {
164 // Find the title in the nearest enclosing DOM node.
165 // For <area> tags in image maps, walk the tree for the <area>, not the <img> using it.
166 for (Node* titleNode = m_innerNode.get(); titleNode; titleNode = titleNode->parentNode()) {
167 if (titleNode->isElementNode()) {
168 String title = static_cast<Element*>(titleNode)->title();
169 if (!title.isEmpty())
170 return title;
171 }
172 }
173 return String();
174 }
175
displayString(const String & string,const Node * node)176 String displayString(const String& string, const Node* node)
177 {
178 if (!node)
179 return string;
180 return node->document()->displayStringModifiedByEncoding(string);
181 }
182
altDisplayString() const183 String HitTestResult::altDisplayString() const
184 {
185 if (!m_innerNonSharedNode)
186 return String();
187
188 if (m_innerNonSharedNode->hasTagName(imgTag)) {
189 HTMLImageElement* image = static_cast<HTMLImageElement*>(m_innerNonSharedNode.get());
190 return displayString(image->alt(), m_innerNonSharedNode.get());
191 }
192
193 if (m_innerNonSharedNode->hasTagName(inputTag)) {
194 HTMLInputElement* input = static_cast<HTMLInputElement*>(m_innerNonSharedNode.get());
195 return displayString(input->alt(), m_innerNonSharedNode.get());
196 }
197
198 #if ENABLE(WML)
199 if (m_innerNonSharedNode->hasTagName(WMLNames::imgTag)) {
200 WMLImageElement* image = static_cast<WMLImageElement*>(m_innerNonSharedNode.get());
201 return displayString(image->altText(), m_innerNonSharedNode.get());
202 }
203 #endif
204
205 return String();
206 }
207
image() const208 Image* HitTestResult::image() const
209 {
210 if (!m_innerNonSharedNode)
211 return 0;
212
213 RenderObject* renderer = m_innerNonSharedNode->renderer();
214 if (renderer && renderer->isImage()) {
215 RenderImage* image = static_cast<WebCore::RenderImage*>(renderer);
216 if (image->cachedImage() && !image->cachedImage()->errorOccurred())
217 return image->cachedImage()->image();
218 }
219
220 return 0;
221 }
222
imageRect() const223 IntRect HitTestResult::imageRect() const
224 {
225 if (!image())
226 return IntRect();
227 return m_innerNonSharedNode->renderBox()->absoluteContentBox();
228 }
229
absoluteImageURL() const230 KURL HitTestResult::absoluteImageURL() const
231 {
232 if (!(m_innerNonSharedNode && m_innerNonSharedNode->document()))
233 return KURL();
234
235 if (!(m_innerNonSharedNode->renderer() && m_innerNonSharedNode->renderer()->isImage()))
236 return KURL();
237
238 AtomicString urlString;
239 if (m_innerNonSharedNode->hasTagName(embedTag)
240 || m_innerNonSharedNode->hasTagName(imgTag)
241 || m_innerNonSharedNode->hasTagName(inputTag)
242 || m_innerNonSharedNode->hasTagName(objectTag)
243 #if ENABLE(SVG)
244 || m_innerNonSharedNode->hasTagName(SVGNames::imageTag)
245 #endif
246 #if ENABLE(WML)
247 || m_innerNonSharedNode->hasTagName(WMLNames::imgTag)
248 #endif
249 ) {
250 Element* element = static_cast<Element*>(m_innerNonSharedNode.get());
251 urlString = element->getAttribute(element->imageSourceAttributeName());
252 } else
253 return KURL();
254
255 return m_innerNonSharedNode->document()->completeURL(parseURL(urlString));
256 }
257
absoluteLinkURL() const258 KURL HitTestResult::absoluteLinkURL() const
259 {
260 if (!(m_innerURLElement && m_innerURLElement->document()))
261 return KURL();
262
263 AtomicString urlString;
264 if (m_innerURLElement->hasTagName(aTag) || m_innerURLElement->hasTagName(areaTag) || m_innerURLElement->hasTagName(linkTag))
265 urlString = m_innerURLElement->getAttribute(hrefAttr);
266 #if ENABLE(SVG)
267 else if (m_innerURLElement->hasTagName(SVGNames::aTag))
268 urlString = m_innerURLElement->getAttribute(XLinkNames::hrefAttr);
269 #endif
270 #if ENABLE(WML)
271 else if (m_innerURLElement->hasTagName(WMLNames::aTag))
272 urlString = m_innerURLElement->getAttribute(hrefAttr);
273 #endif
274 else
275 return KURL();
276
277 return m_innerURLElement->document()->completeURL(parseURL(urlString));
278 }
279
isLiveLink() const280 bool HitTestResult::isLiveLink() const
281 {
282 if (!(m_innerURLElement && m_innerURLElement->document()))
283 return false;
284
285 if (m_innerURLElement->hasTagName(aTag))
286 return static_cast<HTMLAnchorElement*>(m_innerURLElement.get())->isLiveLink();
287 #if ENABLE(SVG)
288 if (m_innerURLElement->hasTagName(SVGNames::aTag))
289 return m_innerURLElement->isLink();
290 #endif
291 #if ENABLE(WML)
292 if (m_innerURLElement->hasTagName(WMLNames::aTag))
293 return m_innerURLElement->isLink();
294 #endif
295
296 return false;
297 }
298
titleDisplayString() const299 String HitTestResult::titleDisplayString() const
300 {
301 if (!m_innerURLElement)
302 return String();
303
304 return displayString(m_innerURLElement->title(), m_innerURLElement.get());
305 }
306
textContent() const307 String HitTestResult::textContent() const
308 {
309 if (!m_innerURLElement)
310 return String();
311 return m_innerURLElement->textContent();
312 }
313
314 // FIXME: This function needs a better name and may belong in a different class. It's not
315 // really isContentEditable(); it's more like needsEditingContextMenu(). In many ways, this
316 // function would make more sense in the ContextMenu class, except that WebElementDictionary
317 // hooks into it. Anyway, we should architect this better.
isContentEditable() const318 bool HitTestResult::isContentEditable() const
319 {
320 if (!m_innerNonSharedNode)
321 return false;
322
323 if (m_innerNonSharedNode->hasTagName(textareaTag) || m_innerNonSharedNode->hasTagName(isindexTag))
324 return true;
325
326 if (m_innerNonSharedNode->hasTagName(inputTag))
327 return static_cast<HTMLInputElement*>(m_innerNonSharedNode.get())->isTextField();
328
329 return m_innerNonSharedNode->isContentEditable();
330 }
331
332 } // namespace WebCore
333