2
* This file is part of the HTML rendering engine for KDE.
4
* Copyright (C) 2006 Apple Computer, Inc.
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
16
* You should have received a copy of the GNU Library General Public License
17
* along with this library; see the file COPYING.LIB. If not, write to
18
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
* Boston, MA 02111-1307, USA.
24
#include "HitTestResult.h"
26
#include "CSSHelper.h"
29
#include "FrameTree.h"
30
#include "HTMLAnchorElement.h"
31
#include "HTMLElement.h"
32
#include "HTMLImageElement.h"
33
#include "HTMLInputElement.h"
34
#include "HTMLNames.h"
36
#include "PlatformScrollBar.h"
37
#include "RenderImage.h"
38
#include "RenderObject.h"
39
#include "SelectionController.h"
43
#include "XLinkNames.h"
48
using namespace HTMLNames;
50
HitTestResult::HitTestResult(const IntPoint& point)
55
HitTestResult::HitTestResult(const HitTestResult& other)
56
: m_innerNode(other.innerNode())
57
, m_innerNonSharedNode(other.innerNonSharedNode())
58
, m_point(other.point())
59
, m_localPoint(other.localPoint())
60
, m_innerURLElement(other.URLElement())
61
, m_scrollbar(other.scrollbar())
65
HitTestResult::~HitTestResult()
69
HitTestResult& HitTestResult::operator=(const HitTestResult& other)
71
m_innerNode = other.innerNode();
72
m_innerNonSharedNode = other.innerNonSharedNode();
73
m_point = other.point();
74
m_innerURLElement = other.URLElement();
75
m_scrollbar = other.scrollbar();
79
void HitTestResult::setToNonShadowAncestor()
81
Node* node = innerNode();
83
node = node->shadowAncestorNode();
85
node = innerNonSharedNode();
87
node = node->shadowAncestorNode();
88
setInnerNonSharedNode(node);
91
void HitTestResult::setInnerNode(Node* n)
96
void HitTestResult::setInnerNonSharedNode(Node* n)
98
m_innerNonSharedNode = n;
101
void HitTestResult::setURLElement(Element* n)
103
m_innerURLElement = n;
106
void HitTestResult::setScrollbar(PlatformScrollbar* s)
111
Frame* HitTestResult::targetFrame() const
113
if (!m_innerURLElement)
116
Frame* frame = m_innerURLElement->document()->frame();
120
return frame->tree()->find(m_innerURLElement->target());
123
IntRect HitTestResult::boundingBox() const
125
if (m_innerNonSharedNode) {
126
RenderObject* renderer = m_innerNonSharedNode->renderer();
128
return renderer->absoluteBoundingBoxRect();
134
bool HitTestResult::isSelected() const
136
if (!m_innerNonSharedNode)
139
Frame* frame = m_innerNonSharedNode->document()->frame();
143
return frame->selectionController()->contains(m_point);
146
String HitTestResult::spellingToolTip() const
148
// Return the tool tip string associated with this point, if any. Only markers associated with bad grammar
149
// currently supply strings, but maybe someday markers associated with misspelled words will also.
150
if (!m_innerNonSharedNode)
153
DocumentMarker* marker = m_innerNonSharedNode->document()->markerContainingPoint(m_point, DocumentMarker::Grammar);
157
return marker->description;
160
String HitTestResult::title() const
162
// Find the title in the nearest enclosing DOM node.
163
// For <area> tags in image maps, walk the tree for the <area>, not the <img> using it.
164
for (Node* titleNode = m_innerNode.get(); titleNode; titleNode = titleNode->parentNode()) {
165
if (titleNode->isElementNode()) {
166
String title = static_cast<Element*>(titleNode)->title();
167
if (!title.isEmpty())
174
String displayString(const String& string, const Node* node)
179
copy.replace('\\', node->document()->backslashAsCurrencySymbol());
183
String HitTestResult::altDisplayString() const
185
if (!m_innerNonSharedNode)
188
if (m_innerNonSharedNode->hasTagName(imgTag)) {
189
HTMLImageElement* image = static_cast<HTMLImageElement*>(m_innerNonSharedNode.get());
190
return displayString(image->alt(), m_innerNonSharedNode.get());
193
if (m_innerNonSharedNode->hasTagName(inputTag)) {
194
HTMLInputElement* input = static_cast<HTMLInputElement*>(m_innerNonSharedNode.get());
195
return displayString(input->alt(), m_innerNonSharedNode.get());
201
Image* HitTestResult::image() const
203
if (!m_innerNonSharedNode)
206
RenderObject* renderer = m_innerNonSharedNode->renderer();
207
if (renderer && renderer->isImage()) {
208
RenderImage* image = static_cast<WebCore::RenderImage*>(renderer);
209
if (image->cachedImage() && !image->cachedImage()->errorOccurred())
210
return image->cachedImage()->image();
216
IntRect HitTestResult::imageRect() const
220
return m_innerNonSharedNode->renderer()->absoluteContentBox();
223
KURL HitTestResult::absoluteImageURL() const
225
if (!(m_innerNonSharedNode && m_innerNonSharedNode->document()))
228
if (!(m_innerNonSharedNode->renderer() && m_innerNonSharedNode->renderer()->isImage()))
231
AtomicString urlString;
232
if (m_innerNonSharedNode->hasTagName(imgTag) || m_innerNonSharedNode->hasTagName(inputTag))
233
urlString = static_cast<Element*>(m_innerNonSharedNode.get())->getAttribute(srcAttr);
235
else if (m_innerNonSharedNode->hasTagName(SVGNames::imageTag))
236
urlString = static_cast<Element*>(m_innerNonSharedNode.get())->getAttribute(XLinkNames::hrefAttr);
238
else if (m_innerNonSharedNode->hasTagName(objectTag))
239
urlString = static_cast<Element*>(m_innerNonSharedNode.get())->getAttribute(dataAttr);
243
return KURL(m_innerNonSharedNode->document()->completeURL(parseURL(urlString).deprecatedString()));
246
KURL HitTestResult::absoluteLinkURL() const
248
if (!(m_innerURLElement && m_innerURLElement->document()))
251
AtomicString urlString;
252
if (m_innerURLElement->hasTagName(aTag) || m_innerURLElement->hasTagName(areaTag) || m_innerURLElement->hasTagName(linkTag))
253
urlString = m_innerURLElement->getAttribute(hrefAttr);
255
else if (m_innerURLElement->hasTagName(SVGNames::aTag))
256
urlString = m_innerURLElement->getAttribute(XLinkNames::hrefAttr);
261
return KURL(m_innerURLElement->document()->completeURL(parseURL(urlString).deprecatedString()));
264
bool HitTestResult::isLiveLink() const
266
if (!(m_innerURLElement && m_innerURLElement->document()))
269
if (m_innerURLElement->hasTagName(aTag))
270
return static_cast<HTMLAnchorElement*>(m_innerURLElement.get())->isLiveLink();
272
if (m_innerURLElement->hasTagName(SVGNames::aTag))
273
return m_innerURLElement->isLink();
279
String HitTestResult::titleDisplayString() const
281
if (!m_innerURLElement)
284
return displayString(m_innerURLElement->title(), m_innerURLElement.get());
287
String HitTestResult::textContent() const
289
if (!m_innerURLElement)
291
return m_innerURLElement->textContent();
294
// FIXME: This function needs a better name and may belong in a different class. It's not
295
// really isContentEditable(); it's more like needsEditingContextMenu(). In many ways, this
296
// function would make more sense in the ContextMenu class, except that WebElementDictionary
297
// hooks into it. Anyway, we should architect this better.
298
bool HitTestResult::isContentEditable() const
300
if (!m_innerNonSharedNode)
303
if (m_innerNonSharedNode->hasTagName(textareaTag) || m_innerNonSharedNode->hasTagName(isindexTag))
306
if (m_innerNonSharedNode->hasTagName(inputTag))
307
return static_cast<HTMLInputElement*>(m_innerNonSharedNode.get())->isTextField();
309
return m_innerNonSharedNode->isContentEditable();
312
} // namespace WebCore