Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / HitTestResult.cpp
1 /*
2  * Copyright (C) 2006, 2008, 2011 Apple Inc. All rights reserved.
3  * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
4  *
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.
9  *
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.
14  *
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.
19  *
20 */
21
22 #include "config.h"
23 #include "core/rendering/HitTestResult.h"
24
25 #include "HTMLNames.h"
26 #include "SVGNames.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"
44
45 namespace WebCore {
46
47 using namespace HTMLNames;
48
49 HitTestResult::HitTestResult()
50     : m_isOverWidget(false)
51     , m_isFirstLetter(false)
52 {
53 }
54
55 HitTestResult::HitTestResult(const LayoutPoint& point)
56     : m_hitTestLocation(point)
57     , m_pointInInnerNodeFrame(point)
58     , m_isOverWidget(false)
59     , m_isFirstLetter(false)
60 {
61 }
62
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)
68 {
69 }
70
71 HitTestResult::HitTestResult(const HitTestLocation& other)
72     : m_hitTestLocation(other)
73     , m_pointInInnerNodeFrame(m_hitTestLocation.point())
74     , m_isOverWidget(false)
75     , m_isFirstLetter(false)
76 {
77 }
78
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)
90 {
91     // Only copy the NodeSet in case of rect hit test.
92     m_rectBasedTestResult = adoptPtr(other.m_rectBasedTestResult ? new NodeSet(*other.m_rectBasedTestResult) : 0);
93 }
94
95 HitTestResult::~HitTestResult()
96 {
97 }
98
99 HitTestResult& HitTestResult::operator=(const HitTestResult& other)
100 {
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();
111
112     // Only copy the NodeSet in case of rect hit test.
113     m_rectBasedTestResult = adoptPtr(other.m_rectBasedTestResult ? new NodeSet(*other.m_rectBasedTestResult) : 0);
114
115     return *this;
116 }
117
118 RenderObject* HitTestResult::renderer() const
119 {
120     if (!m_innerNode)
121         return 0;
122     RenderObject* renderer = m_innerNode->renderer();
123     if (!m_isFirstLetter || !renderer || !renderer->isText() || !toRenderText(renderer)->isTextFragment())
124         return renderer;
125     return toRenderTextFragment(renderer)->firstRenderTextInFirstLetter();
126 }
127
128 void HitTestResult::setToNodesInDocumentTreeScope()
129 {
130     if (Node* node = innerNode()) {
131         node = node->document().ancestorInThisScope(node);
132         setInnerNode(node);
133     }
134
135     if (Node* node = innerNonSharedNode()) {
136         node = node->document().ancestorInThisScope(node);
137         setInnerNonSharedNode(node);
138     }
139 }
140
141 void HitTestResult::setToShadowHostIfInUserAgentShadowRoot()
142 {
143     if (Node* node = innerNode()) {
144         if (ShadowRoot* containingShadowRoot = node->containingShadowRoot()) {
145             if (containingShadowRoot->type() == ShadowRoot::UserAgentShadowRoot)
146                 setInnerNode(node->shadowHost());
147         }
148     }
149
150     if (Node* node = innerNonSharedNode()) {
151         if (ShadowRoot* containingShadowRoot = node->containingShadowRoot()) {
152             if (containingShadowRoot->type() == ShadowRoot::UserAgentShadowRoot)
153                 setInnerNonSharedNode(node->shadowHost());
154         }
155     }
156 }
157
158 void HitTestResult::setInnerNode(Node* n)
159 {
160     m_innerPossiblyPseudoNode = n;
161     if (n && n->isPseudoElement())
162         n = n->parentOrShadowHostNode();
163     m_innerNode = n;
164 }
165
166 void HitTestResult::setInnerNonSharedNode(Node* n)
167 {
168     if (n && n->isPseudoElement())
169         n = n->parentOrShadowHostNode();
170     m_innerNonSharedNode = n;
171 }
172
173 void HitTestResult::setURLElement(Element* n)
174 {
175     m_innerURLElement = n;
176 }
177
178 void HitTestResult::setScrollbar(Scrollbar* s)
179 {
180     m_scrollbar = s;
181 }
182
183 LocalFrame* HitTestResult::innerNodeFrame() const
184 {
185     if (m_innerNonSharedNode)
186         return m_innerNonSharedNode->document().frame();
187     if (m_innerNode)
188         return m_innerNode->document().frame();
189     return 0;
190 }
191
192 bool HitTestResult::isSelected() const
193 {
194     if (!m_innerNonSharedNode)
195         return false;
196
197     if (LocalFrame* frame = m_innerNonSharedNode->document().frame())
198         return frame->selection().contains(m_hitTestLocation.point());
199     return false;
200 }
201
202 String HitTestResult::spellingToolTip(TextDirection& dir) const
203 {
204     dir = LTR;
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)
208         return String();
209
210     DocumentMarker* marker = m_innerNonSharedNode->document().markers().markerContainingPoint(m_hitTestLocation.point(), DocumentMarker::Grammar);
211     if (!marker)
212         return String();
213
214     if (RenderObject* renderer = m_innerNonSharedNode->renderer())
215         dir = renderer->style()->direction();
216     return marker->description();
217 }
218
219 String HitTestResult::title(TextDirection& dir) const
220 {
221     dir = LTR;
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();
230                 return title;
231             }
232         }
233     }
234     return String();
235 }
236
237 const AtomicString& HitTestResult::altDisplayString() const
238 {
239     if (!m_innerNonSharedNode)
240         return nullAtom;
241
242     if (isHTMLImageElement(*m_innerNonSharedNode)) {
243         HTMLImageElement& image = toHTMLImageElement(*m_innerNonSharedNode);
244         return image.getAttribute(altAttr);
245     }
246
247     if (isHTMLInputElement(*m_innerNonSharedNode)) {
248         HTMLInputElement& input = toHTMLInputElement(*m_innerNonSharedNode);
249         return input.alt();
250     }
251
252     return nullAtom;
253 }
254
255 Image* HitTestResult::image() const
256 {
257     if (!m_innerNonSharedNode)
258         return 0;
259
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);
265     }
266
267     return 0;
268 }
269
270 IntRect HitTestResult::imageRect() const
271 {
272     if (!image())
273         return IntRect();
274     return m_innerNonSharedNode->renderBox()->absoluteContentQuad().enclosingBoundingBox();
275 }
276
277 KURL HitTestResult::absoluteImageURL() const
278 {
279     if (!m_innerNonSharedNode)
280         return KURL();
281
282     RenderObject* renderer = m_innerNonSharedNode->renderer();
283     if (!(renderer && (renderer->isImage() || renderer->isCanvas())))
284         return KURL();
285
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)
293        ) {
294         urlString = toElement(*m_innerNonSharedNode).imageSourceURL();
295     } else
296         return KURL();
297
298     return m_innerNonSharedNode->document().completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
299 }
300
301 KURL HitTestResult::absoluteMediaURL() const
302 {
303     if (HTMLMediaElement* mediaElt = mediaElement())
304         return mediaElt->currentSrc();
305     return KURL();
306 }
307
308 HTMLMediaElement* HitTestResult::mediaElement() const
309 {
310     if (!m_innerNonSharedNode)
311         return 0;
312
313     if (!(m_innerNonSharedNode->renderer() && m_innerNonSharedNode->renderer()->isMedia()))
314         return 0;
315
316     if (isHTMLMediaElement(*m_innerNonSharedNode))
317         return toHTMLMediaElement(m_innerNonSharedNode);
318     return 0;
319 }
320
321 KURL HitTestResult::absoluteLinkURL() const
322 {
323     if (!m_innerURLElement)
324         return KURL();
325
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);
331     else
332         return KURL();
333
334     return m_innerURLElement->document().completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
335 }
336
337 bool HitTestResult::isLiveLink() const
338 {
339     if (!m_innerURLElement)
340         return false;
341
342     if (isHTMLAnchorElement(*m_innerURLElement))
343         return toHTMLAnchorElement(m_innerURLElement)->isLiveLink();
344
345     if (isSVGAElement(*m_innerURLElement))
346         return m_innerURLElement->isLink();
347
348     return false;
349 }
350
351 bool HitTestResult::isMisspelled() const
352 {
353     if (!targetNode() || !targetNode()->renderer())
354         return false;
355     VisiblePosition pos(targetNode()->renderer()->positionForPoint(localPoint()));
356     if (pos.isNull())
357         return false;
358     return m_innerNonSharedNode->document().markers().markersInRange(
359         makeRange(pos, pos).get(), DocumentMarker::MisspellingMarkers()).size() > 0;
360 }
361
362 bool HitTestResult::isOverLink() const
363 {
364     return m_innerURLElement && m_innerURLElement->isLink();
365 }
366
367 String HitTestResult::textContent() const
368 {
369     if (!m_innerURLElement)
370         return String();
371     return m_innerURLElement->textContent();
372 }
373
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
379 {
380     if (!m_innerNonSharedNode)
381         return false;
382
383     if (isHTMLTextAreaElement(*m_innerNonSharedNode))
384         return true;
385
386     if (isHTMLInputElement(*m_innerNonSharedNode))
387         return toHTMLInputElement(*m_innerNonSharedNode).isTextField();
388
389     return m_innerNonSharedNode->rendererIsEditable();
390 }
391
392 bool HitTestResult::addNodeToRectBasedTestResult(Node* node, const HitTestRequest& request, const HitTestLocation& locationInContainer, const LayoutRect& rect)
393 {
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())
397         return false;
398
399     // If node is null, return true so the hit test can continue.
400     if (!node)
401         return true;
402
403     if (request.disallowsShadowContent())
404         node = node->document().ancestorInThisScope(node);
405
406     mutableRectBasedTestResult().add(node);
407
408     bool regionFilled = rect.contains(locationInContainer.boundingBox());
409     return !regionFilled;
410 }
411
412 bool HitTestResult::addNodeToRectBasedTestResult(Node* node, const HitTestRequest& request, const HitTestLocation& locationInContainer, const FloatRect& rect)
413 {
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())
417         return false;
418
419     // If node is null, return true so the hit test can continue.
420     if (!node)
421         return true;
422
423     if (request.disallowsShadowContent())
424         node = node->document().ancestorInThisScope(node);
425
426     mutableRectBasedTestResult().add(node);
427
428     bool regionFilled = rect.contains(locationInContainer.boundingBox());
429     return !regionFilled;
430 }
431
432 void HitTestResult::append(const HitTestResult& other)
433 {
434     ASSERT(isRectBasedTest() && other.isRectBasedTest());
435
436     if (!m_scrollbar && other.scrollbar()) {
437         setScrollbar(other.scrollbar());
438     }
439
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();
448     }
449
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)
453             set.add(it->get());
454     }
455 }
456
457 const HitTestResult::NodeSet& HitTestResult::rectBasedTestResult() const
458 {
459     if (!m_rectBasedTestResult)
460         m_rectBasedTestResult = adoptPtr(new NodeSet);
461     return *m_rectBasedTestResult;
462 }
463
464 HitTestResult::NodeSet& HitTestResult::mutableRectBasedTestResult()
465 {
466     if (!m_rectBasedTestResult)
467         m_rectBasedTestResult = adoptPtr(new NodeSet);
468     return *m_rectBasedTestResult;
469 }
470
471 Node* HitTestResult::targetNode() const
472 {
473     Node* node = innerNode();
474     if (!node)
475         return 0;
476     if (node->inDocument())
477         return node;
478
479     Element* element = node->parentElement();
480     if (element && element->inDocument())
481         return element;
482
483     return node;
484 }
485
486 Element* HitTestResult::innerElement() const
487 {
488     for (Node* node = m_innerNode.get(); node; node = NodeRenderingTraversal::parent(node)) {
489         if (node->isElementNode())
490             return toElement(node);
491     }
492
493     return 0;
494 }
495
496 } // namespace WebCore