2 * Copyright (C) 2006, 2008, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
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.
23 #include "core/rendering/HitTestResult.h"
25 #include "HTMLNames.h"
27 #include "XLinkNames.h"
28 #include "core/dom/DocumentMarkerController.h"
29 #include "core/dom/NodeRenderingTraversal.h"
30 #include "core/dom/shadow/ShadowRoot.h"
31 #include "core/editing/FrameSelection.h"
32 #include "core/fetch/ImageResource.h"
33 #include "core/frame/LocalFrame.h"
34 #include "core/html/HTMLAnchorElement.h"
35 #include "core/html/HTMLImageElement.h"
36 #include "core/html/HTMLInputElement.h"
37 #include "core/html/HTMLMediaElement.h"
38 #include "core/html/parser/HTMLParserIdioms.h"
39 #include "core/page/FrameTree.h"
40 #include "core/rendering/RenderImage.h"
41 #include "core/rendering/RenderTextFragment.h"
42 #include "core/svg/SVGElement.h"
43 #include "platform/scroll/Scrollbar.h"
47 using namespace HTMLNames;
49 HitTestResult::HitTestResult()
50 : m_isOverWidget(false)
51 , m_isFirstLetter(false)
55 HitTestResult::HitTestResult(const LayoutPoint& point)
56 : m_hitTestLocation(point)
57 , m_pointInInnerNodeFrame(point)
58 , m_isOverWidget(false)
59 , m_isFirstLetter(false)
63 HitTestResult::HitTestResult(const LayoutPoint& centerPoint, unsigned topPadding, unsigned rightPadding, unsigned bottomPadding, unsigned leftPadding)
64 : m_hitTestLocation(centerPoint, topPadding, rightPadding, bottomPadding, leftPadding)
65 , m_pointInInnerNodeFrame(centerPoint)
66 , m_isOverWidget(false)
67 , m_isFirstLetter(false)
71 HitTestResult::HitTestResult(const HitTestLocation& other)
72 : m_hitTestLocation(other)
73 , m_pointInInnerNodeFrame(m_hitTestLocation.point())
74 , m_isOverWidget(false)
75 , m_isFirstLetter(false)
79 HitTestResult::HitTestResult(const HitTestResult& other)
80 : m_hitTestLocation(other.m_hitTestLocation)
81 , m_innerNode(other.innerNode())
82 , m_innerPossiblyPseudoNode(other.m_innerPossiblyPseudoNode)
83 , m_innerNonSharedNode(other.innerNonSharedNode())
84 , m_pointInInnerNodeFrame(other.m_pointInInnerNodeFrame)
85 , m_localPoint(other.localPoint())
86 , m_innerURLElement(other.URLElement())
87 , m_scrollbar(other.scrollbar())
88 , m_isOverWidget(other.isOverWidget())
89 , m_isFirstLetter(other.m_isFirstLetter)
91 // Only copy the NodeSet in case of rect hit test.
92 m_rectBasedTestResult = adoptPtr(other.m_rectBasedTestResult ? new NodeSet(*other.m_rectBasedTestResult) : 0);
95 HitTestResult::~HitTestResult()
99 HitTestResult& HitTestResult::operator=(const HitTestResult& other)
101 m_hitTestLocation = other.m_hitTestLocation;
102 m_innerNode = other.innerNode();
103 m_innerPossiblyPseudoNode = other.innerPossiblyPseudoNode();
104 m_innerNonSharedNode = other.innerNonSharedNode();
105 m_pointInInnerNodeFrame = other.m_pointInInnerNodeFrame;
106 m_localPoint = other.localPoint();
107 m_innerURLElement = other.URLElement();
108 m_scrollbar = other.scrollbar();
109 m_isFirstLetter = other.m_isFirstLetter;
110 m_isOverWidget = other.isOverWidget();
112 // Only copy the NodeSet in case of rect hit test.
113 m_rectBasedTestResult = adoptPtr(other.m_rectBasedTestResult ? new NodeSet(*other.m_rectBasedTestResult) : 0);
118 RenderObject* HitTestResult::renderer() const
122 RenderObject* renderer = m_innerNode->renderer();
123 if (!m_isFirstLetter || !renderer || !renderer->isText() || !toRenderText(renderer)->isTextFragment())
125 return toRenderTextFragment(renderer)->firstRenderTextInFirstLetter();
128 void HitTestResult::setToNodesInDocumentTreeScope()
130 if (Node* node = innerNode()) {
131 node = node->document().ancestorInThisScope(node);
135 if (Node* node = innerNonSharedNode()) {
136 node = node->document().ancestorInThisScope(node);
137 setInnerNonSharedNode(node);
141 void HitTestResult::setToShadowHostIfInUserAgentShadowRoot()
143 if (Node* node = innerNode()) {
144 if (ShadowRoot* containingShadowRoot = node->containingShadowRoot()) {
145 if (containingShadowRoot->type() == ShadowRoot::UserAgentShadowRoot)
146 setInnerNode(node->shadowHost());
150 if (Node* node = innerNonSharedNode()) {
151 if (ShadowRoot* containingShadowRoot = node->containingShadowRoot()) {
152 if (containingShadowRoot->type() == ShadowRoot::UserAgentShadowRoot)
153 setInnerNonSharedNode(node->shadowHost());
158 void HitTestResult::setInnerNode(Node* n)
160 m_innerPossiblyPseudoNode = n;
161 if (n && n->isPseudoElement())
162 n = n->parentOrShadowHostNode();
166 void HitTestResult::setInnerNonSharedNode(Node* n)
168 if (n && n->isPseudoElement())
169 n = n->parentOrShadowHostNode();
170 m_innerNonSharedNode = n;
173 void HitTestResult::setURLElement(Element* n)
175 m_innerURLElement = n;
178 void HitTestResult::setScrollbar(Scrollbar* s)
183 LocalFrame* HitTestResult::innerNodeFrame() const
185 if (m_innerNonSharedNode)
186 return m_innerNonSharedNode->document().frame();
188 return m_innerNode->document().frame();
192 bool HitTestResult::isSelected() const
194 if (!m_innerNonSharedNode)
197 if (LocalFrame* frame = m_innerNonSharedNode->document().frame())
198 return frame->selection().contains(m_hitTestLocation.point());
202 String HitTestResult::spellingToolTip(TextDirection& dir) const
205 // Return the tool tip string associated with this point, if any. Only markers associated with bad grammar
206 // currently supply strings, but maybe someday markers associated with misspelled words will also.
207 if (!m_innerNonSharedNode)
210 DocumentMarker* marker = m_innerNonSharedNode->document().markers().markerContainingPoint(m_hitTestLocation.point(), DocumentMarker::Grammar);
214 if (RenderObject* renderer = m_innerNonSharedNode->renderer())
215 dir = renderer->style()->direction();
216 return marker->description();
219 String HitTestResult::title(TextDirection& dir) const
222 // Find the title in the nearest enclosing DOM node.
223 // For <area> tags in image maps, walk the tree for the <area>, not the <img> using it.
224 for (Node* titleNode = m_innerNode.get(); titleNode; titleNode = titleNode->parentNode()) {
225 if (titleNode->isElementNode()) {
226 String title = toElement(titleNode)->title();
227 if (!title.isEmpty()) {
228 if (RenderObject* renderer = titleNode->renderer())
229 dir = renderer->style()->direction();
237 const AtomicString& HitTestResult::altDisplayString() const
239 if (!m_innerNonSharedNode)
242 if (isHTMLImageElement(*m_innerNonSharedNode)) {
243 HTMLImageElement& image = toHTMLImageElement(*m_innerNonSharedNode);
244 return image.getAttribute(altAttr);
247 if (isHTMLInputElement(*m_innerNonSharedNode)) {
248 HTMLInputElement& input = toHTMLInputElement(*m_innerNonSharedNode);
255 Image* HitTestResult::image() const
257 if (!m_innerNonSharedNode)
260 RenderObject* renderer = m_innerNonSharedNode->renderer();
261 if (renderer && renderer->isImage()) {
262 RenderImage* image = toRenderImage(renderer);
263 if (image->cachedImage() && !image->cachedImage()->errorOccurred())
264 return image->cachedImage()->imageForRenderer(image);
270 IntRect HitTestResult::imageRect() const
274 return m_innerNonSharedNode->renderBox()->absoluteContentQuad().enclosingBoundingBox();
277 KURL HitTestResult::absoluteImageURL() const
279 if (!m_innerNonSharedNode)
282 RenderObject* renderer = m_innerNonSharedNode->renderer();
283 if (!(renderer && (renderer->isImage() || renderer->isCanvas())))
286 AtomicString urlString;
287 if (isHTMLCanvasElement(*m_innerNonSharedNode)
288 || isHTMLEmbedElement(*m_innerNonSharedNode)
289 || isHTMLImageElement(*m_innerNonSharedNode)
290 || isHTMLInputElement(*m_innerNonSharedNode)
291 || isHTMLObjectElement(*m_innerNonSharedNode)
292 || isSVGImageElement(*m_innerNonSharedNode)
294 urlString = toElement(*m_innerNonSharedNode).imageSourceURL();
298 return m_innerNonSharedNode->document().completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
301 KURL HitTestResult::absoluteMediaURL() const
303 if (HTMLMediaElement* mediaElt = mediaElement())
304 return mediaElt->currentSrc();
308 HTMLMediaElement* HitTestResult::mediaElement() const
310 if (!m_innerNonSharedNode)
313 if (!(m_innerNonSharedNode->renderer() && m_innerNonSharedNode->renderer()->isMedia()))
316 if (isHTMLMediaElement(*m_innerNonSharedNode))
317 return toHTMLMediaElement(m_innerNonSharedNode);
321 KURL HitTestResult::absoluteLinkURL() const
323 if (!m_innerURLElement)
326 AtomicString urlString;
327 if (isHTMLAnchorElement(*m_innerURLElement) || isHTMLAreaElement(*m_innerURLElement) || isHTMLLinkElement(*m_innerURLElement))
328 urlString = m_innerURLElement->getAttribute(hrefAttr);
329 else if (isSVGAElement(*m_innerURLElement))
330 urlString = m_innerURLElement->getAttribute(XLinkNames::hrefAttr);
334 return m_innerURLElement->document().completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
337 bool HitTestResult::isLiveLink() const
339 if (!m_innerURLElement)
342 if (isHTMLAnchorElement(*m_innerURLElement))
343 return toHTMLAnchorElement(m_innerURLElement)->isLiveLink();
345 if (isSVGAElement(*m_innerURLElement))
346 return m_innerURLElement->isLink();
351 bool HitTestResult::isMisspelled() const
353 if (!targetNode() || !targetNode()->renderer())
355 VisiblePosition pos(targetNode()->renderer()->positionForPoint(localPoint()));
358 return m_innerNonSharedNode->document().markers().markersInRange(
359 makeRange(pos, pos).get(), DocumentMarker::MisspellingMarkers()).size() > 0;
362 bool HitTestResult::isOverLink() const
364 return m_innerURLElement && m_innerURLElement->isLink();
367 String HitTestResult::textContent() const
369 if (!m_innerURLElement)
371 return m_innerURLElement->textContent();
374 // FIXME: This function needs a better name and may belong in a different class. It's not
375 // really isContentEditable(); it's more like needsEditingContextMenu(). In many ways, this
376 // function would make more sense in the ContextMenu class, except that WebElementDictionary
377 // hooks into it. Anyway, we should architect this better.
378 bool HitTestResult::isContentEditable() const
380 if (!m_innerNonSharedNode)
383 if (isHTMLTextAreaElement(*m_innerNonSharedNode))
386 if (isHTMLInputElement(*m_innerNonSharedNode))
387 return toHTMLInputElement(*m_innerNonSharedNode).isTextField();
389 return m_innerNonSharedNode->rendererIsEditable();
392 bool HitTestResult::addNodeToRectBasedTestResult(Node* node, const HitTestRequest& request, const HitTestLocation& locationInContainer, const LayoutRect& rect)
394 // If it is not a rect-based hit test, this method has to be no-op.
395 // Return false, so the hit test stops.
396 if (!isRectBasedTest())
399 // If node is null, return true so the hit test can continue.
403 if (request.disallowsShadowContent())
404 node = node->document().ancestorInThisScope(node);
406 mutableRectBasedTestResult().add(node);
408 bool regionFilled = rect.contains(locationInContainer.boundingBox());
409 return !regionFilled;
412 bool HitTestResult::addNodeToRectBasedTestResult(Node* node, const HitTestRequest& request, const HitTestLocation& locationInContainer, const FloatRect& rect)
414 // If it is not a rect-based hit test, this method has to be no-op.
415 // Return false, so the hit test stops.
416 if (!isRectBasedTest())
419 // If node is null, return true so the hit test can continue.
423 if (request.disallowsShadowContent())
424 node = node->document().ancestorInThisScope(node);
426 mutableRectBasedTestResult().add(node);
428 bool regionFilled = rect.contains(locationInContainer.boundingBox());
429 return !regionFilled;
432 void HitTestResult::append(const HitTestResult& other)
434 ASSERT(isRectBasedTest() && other.isRectBasedTest());
436 if (!m_scrollbar && other.scrollbar()) {
437 setScrollbar(other.scrollbar());
440 if (!m_innerNode && other.innerNode()) {
441 m_innerNode = other.innerNode();
442 m_innerPossiblyPseudoNode = other.innerPossiblyPseudoNode();
443 m_innerNonSharedNode = other.innerNonSharedNode();
444 m_localPoint = other.localPoint();
445 m_pointInInnerNodeFrame = other.m_pointInInnerNodeFrame;
446 m_innerURLElement = other.URLElement();
447 m_isOverWidget = other.isOverWidget();
450 if (other.m_rectBasedTestResult) {
451 NodeSet& set = mutableRectBasedTestResult();
452 for (NodeSet::const_iterator it = other.m_rectBasedTestResult->begin(), last = other.m_rectBasedTestResult->end(); it != last; ++it)
457 const HitTestResult::NodeSet& HitTestResult::rectBasedTestResult() const
459 if (!m_rectBasedTestResult)
460 m_rectBasedTestResult = adoptPtr(new NodeSet);
461 return *m_rectBasedTestResult;
464 HitTestResult::NodeSet& HitTestResult::mutableRectBasedTestResult()
466 if (!m_rectBasedTestResult)
467 m_rectBasedTestResult = adoptPtr(new NodeSet);
468 return *m_rectBasedTestResult;
471 Node* HitTestResult::targetNode() const
473 Node* node = innerNode();
476 if (node->inDocument())
479 Element* element = node->parentElement();
480 if (element && element->inDocument())
486 Element* HitTestResult::innerElement() const
488 for (Node* node = m_innerNode.get(); node; node = NodeRenderingTraversal::parent(node)) {
489 if (node->isElementNode())
490 return toElement(node);
496 } // namespace WebCore