1b15d9c1a5ef3ccab049658bf77ad88758b3efe9
[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     if (!(m_innerNonSharedNode->renderer() && m_innerNonSharedNode->renderer()->isImage()))
283         return KURL();
284
285     AtomicString urlString;
286     if (isHTMLEmbedElement(*m_innerNonSharedNode)
287         || isHTMLImageElement(*m_innerNonSharedNode)
288         || isHTMLInputElement(*m_innerNonSharedNode)
289         || isHTMLObjectElement(*m_innerNonSharedNode)
290         || isSVGImageElement(*m_innerNonSharedNode)
291        ) {
292         urlString = toElement(*m_innerNonSharedNode).imageSourceURL();
293     } else
294         return KURL();
295
296     return m_innerNonSharedNode->document().completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
297 }
298
299 KURL HitTestResult::absoluteMediaURL() const
300 {
301     if (HTMLMediaElement* mediaElt = mediaElement())
302         return mediaElt->currentSrc();
303     return KURL();
304 }
305
306 HTMLMediaElement* HitTestResult::mediaElement() const
307 {
308     if (!m_innerNonSharedNode)
309         return 0;
310
311     if (!(m_innerNonSharedNode->renderer() && m_innerNonSharedNode->renderer()->isMedia()))
312         return 0;
313
314     if (isHTMLMediaElement(*m_innerNonSharedNode))
315         return toHTMLMediaElement(m_innerNonSharedNode);
316     return 0;
317 }
318
319 KURL HitTestResult::absoluteLinkURL() const
320 {
321     if (!m_innerURLElement)
322         return KURL();
323
324     AtomicString urlString;
325     if (isHTMLAnchorElement(*m_innerURLElement) || isHTMLAreaElement(*m_innerURLElement) || isHTMLLinkElement(*m_innerURLElement))
326         urlString = m_innerURLElement->getAttribute(hrefAttr);
327     else if (isSVGAElement(*m_innerURLElement))
328         urlString = m_innerURLElement->getAttribute(XLinkNames::hrefAttr);
329     else
330         return KURL();
331
332     return m_innerURLElement->document().completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
333 }
334
335 bool HitTestResult::isLiveLink() const
336 {
337     if (!m_innerURLElement)
338         return false;
339
340     if (isHTMLAnchorElement(*m_innerURLElement))
341         return toHTMLAnchorElement(m_innerURLElement)->isLiveLink();
342
343     if (isSVGAElement(*m_innerURLElement))
344         return m_innerURLElement->isLink();
345
346     return false;
347 }
348
349 bool HitTestResult::isMisspelled() const
350 {
351     if (!targetNode() || !targetNode()->renderer())
352         return false;
353     VisiblePosition pos(targetNode()->renderer()->positionForPoint(localPoint()));
354     if (pos.isNull())
355         return false;
356     return m_innerNonSharedNode->document().markers().markersInRange(
357         makeRange(pos, pos).get(), DocumentMarker::MisspellingMarkers()).size() > 0;
358 }
359
360 bool HitTestResult::isOverLink() const
361 {
362     return m_innerURLElement && m_innerURLElement->isLink();
363 }
364
365 String HitTestResult::titleDisplayString() const
366 {
367     if (!m_innerURLElement)
368         return String();
369
370     return m_innerURLElement->title();
371 }
372
373 String HitTestResult::textContent() const
374 {
375     if (!m_innerURLElement)
376         return String();
377     return m_innerURLElement->textContent();
378 }
379
380 // FIXME: This function needs a better name and may belong in a different class. It's not
381 // really isContentEditable(); it's more like needsEditingContextMenu(). In many ways, this
382 // function would make more sense in the ContextMenu class, except that WebElementDictionary
383 // hooks into it. Anyway, we should architect this better.
384 bool HitTestResult::isContentEditable() const
385 {
386     if (!m_innerNonSharedNode)
387         return false;
388
389     if (isHTMLTextAreaElement(*m_innerNonSharedNode))
390         return true;
391
392     if (isHTMLInputElement(*m_innerNonSharedNode))
393         return toHTMLInputElement(*m_innerNonSharedNode).isTextField();
394
395     return m_innerNonSharedNode->rendererIsEditable();
396 }
397
398 bool HitTestResult::addNodeToRectBasedTestResult(Node* node, const HitTestRequest& request, const HitTestLocation& locationInContainer, const LayoutRect& rect)
399 {
400     // If it is not a rect-based hit test, this method has to be no-op.
401     // Return false, so the hit test stops.
402     if (!isRectBasedTest())
403         return false;
404
405     // If node is null, return true so the hit test can continue.
406     if (!node)
407         return true;
408
409     if (request.disallowsShadowContent())
410         node = node->document().ancestorInThisScope(node);
411
412     mutableRectBasedTestResult().add(node);
413
414     bool regionFilled = rect.contains(locationInContainer.boundingBox());
415     return !regionFilled;
416 }
417
418 bool HitTestResult::addNodeToRectBasedTestResult(Node* node, const HitTestRequest& request, const HitTestLocation& locationInContainer, const FloatRect& rect)
419 {
420     // If it is not a rect-based hit test, this method has to be no-op.
421     // Return false, so the hit test stops.
422     if (!isRectBasedTest())
423         return false;
424
425     // If node is null, return true so the hit test can continue.
426     if (!node)
427         return true;
428
429     if (request.disallowsShadowContent())
430         node = node->document().ancestorInThisScope(node);
431
432     mutableRectBasedTestResult().add(node);
433
434     bool regionFilled = rect.contains(locationInContainer.boundingBox());
435     return !regionFilled;
436 }
437
438 void HitTestResult::append(const HitTestResult& other)
439 {
440     ASSERT(isRectBasedTest() && other.isRectBasedTest());
441
442     if (!m_scrollbar && other.scrollbar()) {
443         setScrollbar(other.scrollbar());
444     }
445
446     if (!m_innerNode && other.innerNode()) {
447         m_innerNode = other.innerNode();
448         m_innerPossiblyPseudoNode = other.innerPossiblyPseudoNode();
449         m_innerNonSharedNode = other.innerNonSharedNode();
450         m_localPoint = other.localPoint();
451         m_pointInInnerNodeFrame = other.m_pointInInnerNodeFrame;
452         m_innerURLElement = other.URLElement();
453         m_isOverWidget = other.isOverWidget();
454     }
455
456     if (other.m_rectBasedTestResult) {
457         NodeSet& set = mutableRectBasedTestResult();
458         for (NodeSet::const_iterator it = other.m_rectBasedTestResult->begin(), last = other.m_rectBasedTestResult->end(); it != last; ++it)
459             set.add(it->get());
460     }
461 }
462
463 const HitTestResult::NodeSet& HitTestResult::rectBasedTestResult() const
464 {
465     if (!m_rectBasedTestResult)
466         m_rectBasedTestResult = adoptPtr(new NodeSet);
467     return *m_rectBasedTestResult;
468 }
469
470 HitTestResult::NodeSet& HitTestResult::mutableRectBasedTestResult()
471 {
472     if (!m_rectBasedTestResult)
473         m_rectBasedTestResult = adoptPtr(new NodeSet);
474     return *m_rectBasedTestResult;
475 }
476
477 Node* HitTestResult::targetNode() const
478 {
479     Node* node = innerNode();
480     if (!node)
481         return 0;
482     if (node->inDocument())
483         return node;
484
485     Element* element = node->parentElement();
486     if (element && element->inDocument())
487         return element;
488
489     return node;
490 }
491
492 Element* HitTestResult::innerElement() const
493 {
494     for (Node* node = m_innerNode.get(); node; node = NodeRenderingTraversal::parent(node)) {
495         if (node->isElementNode())
496             return toElement(node);
497     }
498
499     return 0;
500 }
501
502 } // namespace WebCore