Remove InsertionPoint and ContentDistributor
[WebKit-https.git] / Source / WebCore / page / FocusController.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2013 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Nuanti Ltd.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
26
27 #include "config.h"
28 #include "FocusController.h"
29
30 #include "AXObjectCache.h"
31 #include "Chrome.h"
32 #include "Document.h"
33 #include "Editor.h"
34 #include "EditorClient.h"
35 #include "Element.h"
36 #include "ElementTraversal.h"
37 #include "Event.h"
38 #include "EventHandler.h"
39 #include "EventNames.h"
40 #include "ExceptionCode.h"
41 #include "FrameSelection.h"
42 #include "FrameTree.h"
43 #include "FrameView.h"
44 #include "HTMLAreaElement.h"
45 #include "HTMLImageElement.h"
46 #include "HTMLInputElement.h"
47 #include "HTMLNames.h"
48 #include "HTMLPlugInElement.h"
49 #include "HTMLTextAreaElement.h"
50 #include "HitTestResult.h"
51 #include "KeyboardEvent.h"
52 #include "MainFrame.h"
53 #include "NodeRenderingTraversal.h"
54 #include "Page.h"
55 #include "Range.h"
56 #include "RenderWidget.h"
57 #include "ScrollAnimator.h"
58 #include "Settings.h"
59 #include "ShadowRoot.h"
60 #include "SpatialNavigation.h"
61 #include "Widget.h"
62 #include "htmlediting.h" // For firstPositionInOrBeforeNode
63 #include <limits>
64 #include <wtf/CurrentTime.h>
65 #include <wtf/Ref.h>
66
67 namespace WebCore {
68
69 using namespace HTMLNames;
70
71 FocusNavigationScope::FocusNavigationScope(TreeScope* treeScope)
72     : m_rootTreeScope(treeScope)
73 {
74     ASSERT(treeScope);
75 }
76
77 ContainerNode* FocusNavigationScope::rootNode() const
78 {
79     return &m_rootTreeScope->rootNode();
80 }
81
82 Element* FocusNavigationScope::owner() const
83 {
84     ContainerNode* root = rootNode();
85     if (is<ShadowRoot>(*root))
86         return downcast<ShadowRoot>(*root).host();
87     if (Frame* frame = root->document().frame())
88         return frame->ownerElement();
89     return nullptr;
90 }
91
92 FocusNavigationScope FocusNavigationScope::focusNavigationScopeOf(Node* node)
93 {
94     ASSERT(node);
95     Node* root = node;
96     for (Node* n = node; n; n = NodeRenderingTraversal::parentInScope(n))
97         root = n;
98     // The result is not always a ShadowRoot nor a DocumentNode since
99     // a starting node is in an orphaned tree in composed shadow tree.
100     return FocusNavigationScope(&root->treeScope());
101 }
102
103 FocusNavigationScope FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(Node* node)
104 {
105     ASSERT(node);
106     ASSERT(downcast<Element>(*node).shadowRoot());
107     return FocusNavigationScope(downcast<Element>(*node).shadowRoot());
108 }
109
110 FocusNavigationScope FocusNavigationScope::focusNavigationScopeOwnedByIFrame(HTMLFrameOwnerElement* frame)
111 {
112     ASSERT(frame);
113     ASSERT(frame->contentFrame());
114     return FocusNavigationScope(frame->contentFrame()->document());
115 }
116
117 static inline void dispatchEventsOnWindowAndFocusedElement(Document* document, bool focused)
118 {
119     // If we have a focused node we should dispatch blur on it before we blur the window.
120     // If we have a focused node we should dispatch focus on it after we focus the window.
121     // https://bugs.webkit.org/show_bug.cgi?id=27105
122
123     // Do not fire events while modal dialogs are up.  See https://bugs.webkit.org/show_bug.cgi?id=33962
124     if (Page* page = document->page()) {
125         if (page->defersLoading())
126             return;
127     }
128
129     if (!focused && document->focusedElement())
130         document->focusedElement()->dispatchBlurEvent(nullptr);
131     document->dispatchWindowEvent(Event::create(focused ? eventNames().focusEvent : eventNames().blurEvent, false, false));
132     if (focused && document->focusedElement())
133         document->focusedElement()->dispatchFocusEvent(nullptr, FocusDirectionNone);
134 }
135
136 static inline bool hasCustomFocusLogic(Element& element)
137 {
138     return is<HTMLElement>(element) && downcast<HTMLElement>(element).hasCustomFocusLogic();
139 }
140
141 static inline bool isNonFocusableShadowHost(Element& element, KeyboardEvent& event)
142 {
143     return !element.isKeyboardFocusable(&event) && element.shadowRoot() && !hasCustomFocusLogic(element);
144 }
145
146 static inline bool isFocusableShadowHost(Node& node, KeyboardEvent& event)
147 {
148     return is<Element>(node) && downcast<Element>(node).isKeyboardFocusable(&event) && downcast<Element>(node).shadowRoot() && !hasCustomFocusLogic(downcast<Element>(node));
149 }
150
151 static inline int adjustedTabIndex(Node& node, KeyboardEvent& event)
152 {
153     if (!is<Element>(node))
154         return 0;
155     return isNonFocusableShadowHost(downcast<Element>(node), event) ? 0 : downcast<Element>(node).tabIndex();
156 }
157
158 static inline bool shouldVisit(Element& element, KeyboardEvent& event)
159 {
160     return element.isKeyboardFocusable(&event) || isNonFocusableShadowHost(element, event);
161 }
162
163 FocusController::FocusController(Page& page, ViewState::Flags viewState)
164     : m_page(page)
165     , m_isChangingFocusedFrame(false)
166     , m_viewState(viewState)
167     , m_focusRepaintTimer(*this, &FocusController::focusRepaintTimerFired)
168 {
169 }
170
171 void FocusController::setFocusedFrame(PassRefPtr<Frame> frame)
172 {
173     ASSERT(!frame || frame->page() == &m_page);
174     if (m_focusedFrame == frame || m_isChangingFocusedFrame)
175         return;
176
177     m_isChangingFocusedFrame = true;
178
179     RefPtr<Frame> oldFrame = m_focusedFrame;
180     RefPtr<Frame> newFrame = frame;
181
182     m_focusedFrame = newFrame;
183
184     // Now that the frame is updated, fire events and update the selection focused states of both frames.
185     if (oldFrame && oldFrame->view()) {
186         oldFrame->selection().setFocused(false);
187         oldFrame->document()->dispatchWindowEvent(Event::create(eventNames().blurEvent, false, false));
188     }
189
190     if (newFrame && newFrame->view() && isFocused()) {
191         newFrame->selection().setFocused(true);
192         newFrame->document()->dispatchWindowEvent(Event::create(eventNames().focusEvent, false, false));
193     }
194
195     m_page.chrome().focusedFrameChanged(newFrame.get());
196
197     m_isChangingFocusedFrame = false;
198 }
199
200 Frame& FocusController::focusedOrMainFrame() const
201 {
202     if (Frame* frame = focusedFrame())
203         return *frame;
204     return m_page.mainFrame();
205 }
206
207 void FocusController::setFocused(bool focused)
208 {
209     m_page.setViewState(focused ? m_viewState | ViewState::IsFocused : m_viewState & ~ViewState::IsFocused);
210 }
211
212 void FocusController::setFocusedInternal(bool focused)
213 {
214     if (!isFocused())
215         focusedOrMainFrame().eventHandler().stopAutoscrollTimer();
216
217     if (!m_focusedFrame)
218         setFocusedFrame(&m_page.mainFrame());
219
220     if (m_focusedFrame->view()) {
221         m_focusedFrame->selection().setFocused(focused);
222         dispatchEventsOnWindowAndFocusedElement(m_focusedFrame->document(), focused);
223     }
224 }
225
226 Element* FocusController::findFocusableElementDescendingDownIntoFrameDocument(FocusDirection direction, Element* element, KeyboardEvent* event)
227 {
228     // The node we found might be a HTMLFrameOwnerElement, so descend down the tree until we find either:
229     // 1) a focusable node, or
230     // 2) the deepest-nested HTMLFrameOwnerElement.
231     while (is<HTMLFrameOwnerElement>(element)) {
232         HTMLFrameOwnerElement& owner = downcast<HTMLFrameOwnerElement>(*element);
233         if (!owner.contentFrame())
234             break;
235         Element* foundElement = findFocusableElement(direction, FocusNavigationScope::focusNavigationScopeOwnedByIFrame(&owner), 0, event);
236         if (!foundElement)
237             break;
238         ASSERT(element != foundElement);
239         element = foundElement;
240     }
241     return element;
242 }
243
244 bool FocusController::setInitialFocus(FocusDirection direction, KeyboardEvent* event)
245 {
246     bool didAdvanceFocus = advanceFocus(direction, event, true);
247     
248     // If focus is being set initially, accessibility needs to be informed that system focus has moved 
249     // into the web area again, even if focus did not change within WebCore. PostNotification is called instead
250     // of handleFocusedUIElementChanged, because this will send the notification even if the element is the same.
251     if (AXObjectCache* cache = focusedOrMainFrame().document()->existingAXObjectCache())
252         cache->postNotification(focusedOrMainFrame().document(), AXObjectCache::AXFocusedUIElementChanged);
253
254     return didAdvanceFocus;
255 }
256
257 bool FocusController::advanceFocus(FocusDirection direction, KeyboardEvent* event, bool initialFocus)
258 {
259     switch (direction) {
260     case FocusDirectionForward:
261     case FocusDirectionBackward:
262         return advanceFocusInDocumentOrder(direction, event, initialFocus);
263     case FocusDirectionLeft:
264     case FocusDirectionRight:
265     case FocusDirectionUp:
266     case FocusDirectionDown:
267         return advanceFocusDirectionally(direction, event);
268     default:
269         ASSERT_NOT_REACHED();
270     }
271
272     return false;
273 }
274
275 bool FocusController::advanceFocusInDocumentOrder(FocusDirection direction, KeyboardEvent* event, bool initialFocus)
276 {
277     Frame& frame = focusedOrMainFrame();
278     Document* document = frame.document();
279
280     Node* currentNode = document->focusedElement();
281     // FIXME: Not quite correct when it comes to focus transitions leaving/entering the WebView itself
282     bool caretBrowsing = frame.settings().caretBrowsingEnabled();
283
284     if (caretBrowsing && !currentNode)
285         currentNode = frame.selection().selection().start().deprecatedNode();
286
287     document->updateLayoutIgnorePendingStylesheets();
288
289     RefPtr<Element> element = findFocusableElementAcrossFocusScope(direction, FocusNavigationScope::focusNavigationScopeOf(currentNode ? currentNode : document), currentNode, event);
290
291     if (!element) {
292         // We didn't find a node to focus, so we should try to pass focus to Chrome.
293         if (!initialFocus && m_page.chrome().canTakeFocus(direction)) {
294             document->setFocusedElement(nullptr);
295             setFocusedFrame(nullptr);
296             m_page.chrome().takeFocus(direction);
297             return true;
298         }
299
300         // Chrome doesn't want focus, so we should wrap focus.
301         element = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOf(m_page.mainFrame().document()), 0, event);
302         element = findFocusableElementDescendingDownIntoFrameDocument(direction, element.get(), event);
303
304         if (!element)
305             return false;
306     }
307
308     ASSERT(element);
309
310     if (element == document->focusedElement()) {
311         // Focus wrapped around to the same element.
312         return true;
313     }
314
315     if (is<HTMLFrameOwnerElement>(*element) && (!is<HTMLPlugInElement>(*element) || !element->isKeyboardFocusable(event))) {
316         // We focus frames rather than frame owners.
317         // FIXME: We should not focus frames that have no scrollbars, as focusing them isn't useful to the user.
318         HTMLFrameOwnerElement& owner = downcast<HTMLFrameOwnerElement>(*element);
319         if (!owner.contentFrame())
320             return false;
321
322         document->setFocusedElement(nullptr);
323         setFocusedFrame(owner.contentFrame());
324         return true;
325     }
326     
327     // FIXME: It would be nice to just be able to call setFocusedElement(node) here, but we can't do
328     // that because some elements (e.g. HTMLInputElement and HTMLTextAreaElement) do extra work in
329     // their focus() methods.
330
331     Document& newDocument = element->document();
332
333     if (&newDocument != document) {
334         // Focus is going away from this document, so clear the focused node.
335         document->setFocusedElement(nullptr);
336     }
337
338     setFocusedFrame(newDocument.frame());
339
340     if (caretBrowsing) {
341         Position position = firstPositionInOrBeforeNode(element.get());
342         VisibleSelection newSelection(position, position, DOWNSTREAM);
343         if (frame.selection().shouldChangeSelection(newSelection)) {
344             AXTextStateChangeIntent intent(AXTextStateChangeTypeSelectionMove, AXTextSelection { AXTextSelectionDirectionDiscontiguous, AXTextSelectionGranularityUnknown, true });
345             frame.selection().setSelection(newSelection, FrameSelection::defaultSetSelectionOptions(UserTriggered), intent);
346         }
347     }
348
349     element->focus(false, direction);
350     return true;
351 }
352
353 Element* FocusController::findFocusableElementAcrossFocusScope(FocusDirection direction, FocusNavigationScope scope, Node* currentNode, KeyboardEvent* event)
354 {
355     ASSERT(!is<Element>(currentNode) || !isNonFocusableShadowHost(*downcast<Element>(currentNode), *event));
356     Element* found;
357     if (currentNode && direction == FocusDirectionForward && isFocusableShadowHost(*currentNode, *event)) {
358         Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(currentNode), 0, event);
359         found = foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableElementRecursively(direction, scope, currentNode, event);
360     } else
361         found = findFocusableElementRecursively(direction, scope, currentNode, event);
362
363     // If there's no focusable node to advance to, move up the focus scopes until we find one.
364     while (!found) {
365         Element* owner = scope.owner();
366         if (!owner)
367             break;
368         scope = FocusNavigationScope::focusNavigationScopeOf(owner);
369         if (direction == FocusDirectionBackward && isFocusableShadowHost(*owner, *event)) {
370             found = owner;
371             break;
372         }
373         found = findFocusableElementRecursively(direction, scope, owner, event);
374     }
375     found = findFocusableElementDescendingDownIntoFrameDocument(direction, found, event);
376     return found;
377 }
378
379 Element* FocusController::findFocusableElementRecursively(FocusDirection direction, FocusNavigationScope scope, Node* start, KeyboardEvent* event)
380 {
381     // Starting node is exclusive.
382     Element* found = findFocusableElement(direction, scope, start, event);
383     if (!found)
384         return nullptr;
385     if (direction == FocusDirectionForward) {
386         if (!isNonFocusableShadowHost(*found, *event))
387             return found;
388         Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
389         return foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableElementRecursively(direction, scope, found, event);
390     }
391     ASSERT(direction == FocusDirectionBackward);
392     if (isFocusableShadowHost(*found, *event)) {
393         Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
394         return foundInInnerFocusScope ? foundInInnerFocusScope : found;
395     }
396     if (isNonFocusableShadowHost(*found, *event)) {
397         Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
398         return foundInInnerFocusScope ? foundInInnerFocusScope :findFocusableElementRecursively(direction, scope, found, event);
399     }
400     return found;
401 }
402
403 Element* FocusController::findFocusableElement(FocusDirection direction, FocusNavigationScope scope, Node* node, KeyboardEvent* event)
404 {
405     return (direction == FocusDirectionForward)
406         ? nextFocusableElement(scope, node, event)
407         : previousFocusableElement(scope, node, event);
408 }
409
410 Element* FocusController::findElementWithExactTabIndex(Node* start, int tabIndex, KeyboardEvent* event, FocusDirection direction)
411 {
412     // Search is inclusive of start
413     using namespace NodeRenderingTraversal;
414     for (Node* node = start; node; node = direction == FocusDirectionForward ? nextInScope(node) : previousInScope(node)) {
415         if (!is<Element>(*node))
416             continue;
417         Element& element = downcast<Element>(*node);
418         if (shouldVisit(element, *event) && adjustedTabIndex(element, *event) == tabIndex)
419             return &element;
420     }
421     return nullptr;
422 }
423
424 static Element* nextElementWithGreaterTabIndex(Node* start, int tabIndex, KeyboardEvent& event)
425 {
426     // Search is inclusive of start
427     int winningTabIndex = std::numeric_limits<short>::max() + 1;
428     Element* winner = nullptr;
429     for (Node* node = start; node; node = NodeRenderingTraversal::nextInScope(node)) {
430         if (!is<Element>(*node))
431             continue;
432         Element& element = downcast<Element>(*node);
433         if (shouldVisit(element, event) && element.tabIndex() > tabIndex && element.tabIndex() < winningTabIndex) {
434             winner = &element;
435             winningTabIndex = element.tabIndex();
436         }
437     }
438
439     return winner;
440 }
441
442 static Element* previousElementWithLowerTabIndex(Node* start, int tabIndex, KeyboardEvent& event)
443 {
444     // Search is inclusive of start
445     int winningTabIndex = 0;
446     Element* winner = nullptr;
447     for (Node* node = start; node; node = NodeRenderingTraversal::previousInScope(node)) {
448         if (!is<Element>(*node))
449             continue;
450         Element& element = downcast<Element>(*node);
451         int currentTabIndex = adjustedTabIndex(element, event);
452         if ((shouldVisit(element, event) || isNonFocusableShadowHost(element, event)) && currentTabIndex < tabIndex && currentTabIndex > winningTabIndex) {
453             winner = &element;
454             winningTabIndex = currentTabIndex;
455         }
456     }
457     return winner;
458 }
459
460 Element* FocusController::nextFocusableElement(FocusNavigationScope scope, Node* start, KeyboardEvent* event)
461 {
462     using namespace NodeRenderingTraversal;
463
464     if (start) {
465         int tabIndex = adjustedTabIndex(*start, *event);
466         // If a node is excluded from the normal tabbing cycle, the next focusable node is determined by tree order
467         if (tabIndex < 0) {
468             for (Node* node = nextInScope(start); node; node = nextInScope(node)) {
469                 if (!is<Element>(*node))
470                     continue;
471                 Element& element = downcast<Element>(*node);
472                 if (shouldVisit(element, *event) && adjustedTabIndex(element, *event) >= 0)
473                     return &element;
474             }
475         }
476
477         // First try to find a node with the same tabindex as start that comes after start in the scope.
478         if (Element* winner = findElementWithExactTabIndex(nextInScope(start), tabIndex, event, FocusDirectionForward))
479             return winner;
480
481         if (!tabIndex)
482             // We've reached the last node in the document with a tabindex of 0. This is the end of the tabbing order.
483             return 0;
484     }
485
486     // Look for the first Element in the scope that:
487     // 1) has the lowest tabindex that is higher than start's tabindex (or 0, if start is null), and
488     // 2) comes first in the scope, if there's a tie.
489     if (Element* winner = nextElementWithGreaterTabIndex(scope.rootNode(), start ? adjustedTabIndex(*start, *event) : 0, *event))
490         return winner;
491
492     // There are no nodes with a tabindex greater than start's tabindex,
493     // so find the first node with a tabindex of 0.
494     return findElementWithExactTabIndex(scope.rootNode(), 0, event, FocusDirectionForward);
495 }
496
497 Element* FocusController::previousFocusableElement(FocusNavigationScope scope, Node* start, KeyboardEvent* event)
498 {
499     Node* last = nullptr;
500     for (Node* node = scope.rootNode(); node; node = NodeRenderingTraversal::lastChildInScope(node))
501         last = node;
502     ASSERT(last);
503
504     // First try to find the last node in the scope that comes before start and has the same tabindex as start.
505     // If start is null, find the last node in the scope with a tabindex of 0.
506     Node* startingNode;
507     int startingTabIndex;
508     if (start) {
509         startingNode = NodeRenderingTraversal::previousInScope(start);
510         startingTabIndex = adjustedTabIndex(*start, *event);
511     } else {
512         startingNode = last;
513         startingTabIndex = 0;
514     }
515
516     // However, if a node is excluded from the normal tabbing cycle, the previous focusable node is determined by tree order
517     if (startingTabIndex < 0) {
518         for (Node* node = startingNode; node; node = NodeRenderingTraversal::previousInScope(node)) {
519             if (!is<Element>(*node))
520                 continue;
521             Element& element = downcast<Element>(*node);
522             if (shouldVisit(element, *event) && adjustedTabIndex(element, *event) >= 0)
523                 return &element;
524         }
525     }
526
527     if (Element* winner = findElementWithExactTabIndex(startingNode, startingTabIndex, event, FocusDirectionBackward))
528         return winner;
529
530     // There are no nodes before start with the same tabindex as start, so look for a node that:
531     // 1) has the highest non-zero tabindex (that is less than start's tabindex), and
532     // 2) comes last in the scope, if there's a tie.
533     startingTabIndex = (start && startingTabIndex) ? startingTabIndex : std::numeric_limits<short>::max();
534     return previousElementWithLowerTabIndex(last, startingTabIndex, *event);
535 }
536
537 static bool relinquishesEditingFocus(Node *node)
538 {
539     ASSERT(node);
540     ASSERT(node->hasEditableStyle());
541
542     Node* root = node->rootEditableElement();
543     Frame* frame = node->document().frame();
544     if (!frame || !root)
545         return false;
546
547     return frame->editor().shouldEndEditing(rangeOfContents(*root).ptr());
548 }
549
550 static void clearSelectionIfNeeded(Frame* oldFocusedFrame, Frame* newFocusedFrame, Node* newFocusedNode)
551 {
552     if (!oldFocusedFrame || !newFocusedFrame)
553         return;
554         
555     if (oldFocusedFrame->document() != newFocusedFrame->document())
556         return;
557
558     const VisibleSelection& selection = oldFocusedFrame->selection().selection();
559     if (selection.isNone())
560         return;
561
562     bool caretBrowsing = oldFocusedFrame->settings().caretBrowsingEnabled();
563     if (caretBrowsing)
564         return;
565
566     Node* selectionStartNode = selection.start().deprecatedNode();
567     if (selectionStartNode == newFocusedNode || selectionStartNode->isDescendantOf(newFocusedNode) || selectionStartNode->deprecatedShadowAncestorNode() == newFocusedNode)
568         return;
569         
570     if (Node* mousePressNode = newFocusedFrame->eventHandler().mousePressNode()) {
571         if (mousePressNode->renderer() && !mousePressNode->canStartSelection()) {
572             // Don't clear the selection for contentEditable elements, but do clear it for input and textarea. See bug 38696.
573             Node * root = selection.rootEditableElement();
574             if (!root)
575                 return;
576
577             if (Node* shadowAncestorNode = root->deprecatedShadowAncestorNode()) {
578                 if (!is<HTMLInputElement>(*shadowAncestorNode) && !is<HTMLTextAreaElement>(*shadowAncestorNode))
579                     return;
580             }
581         }
582     }
583
584     oldFocusedFrame->selection().clear();
585 }
586
587 bool FocusController::setFocusedElement(Element* element, PassRefPtr<Frame> newFocusedFrame, FocusDirection direction)
588 {
589     RefPtr<Frame> oldFocusedFrame = focusedFrame();
590     RefPtr<Document> oldDocument = oldFocusedFrame ? oldFocusedFrame->document() : nullptr;
591     
592     Element* oldFocusedElement = oldDocument ? oldDocument->focusedElement() : nullptr;
593     if (oldFocusedElement == element)
594         return true;
595
596     // FIXME: Might want to disable this check for caretBrowsing
597     if (oldFocusedElement && oldFocusedElement->isRootEditableElement() && !relinquishesEditingFocus(oldFocusedElement))
598         return false;
599
600     m_page.editorClient().willSetInputMethodState();
601
602     clearSelectionIfNeeded(oldFocusedFrame.get(), newFocusedFrame.get(), element);
603
604     if (!element) {
605         if (oldDocument)
606             oldDocument->setFocusedElement(nullptr);
607         m_page.editorClient().setInputMethodState(false);
608         return true;
609     }
610
611     Ref<Document> newDocument(element->document());
612
613     if (newDocument->focusedElement() == element) {
614         m_page.editorClient().setInputMethodState(element->shouldUseInputMethod());
615         return true;
616     }
617     
618     if (oldDocument && oldDocument != newDocument.ptr())
619         oldDocument->setFocusedElement(nullptr);
620
621     if (newFocusedFrame && !newFocusedFrame->page()) {
622         setFocusedFrame(nullptr);
623         return false;
624     }
625     setFocusedFrame(newFocusedFrame);
626
627     Ref<Element> protect(*element);
628
629     bool successfullyFocused = newDocument->setFocusedElement(element, direction);
630     if (!successfullyFocused)
631         return false;
632
633     if (newDocument->focusedElement() == element)
634         m_page.editorClient().setInputMethodState(element->shouldUseInputMethod());
635
636     m_focusSetTime = monotonicallyIncreasingTime();
637     m_focusRepaintTimer.stop();
638
639     return true;
640 }
641
642 void FocusController::setViewState(ViewState::Flags viewState)
643 {
644     ViewState::Flags changed = m_viewState ^ viewState;
645     m_viewState = viewState;
646
647     if (changed & ViewState::IsFocused)
648         setFocusedInternal(viewState & ViewState::IsFocused);
649     if (changed & ViewState::WindowIsActive) {
650         setActiveInternal(viewState & ViewState::WindowIsActive);
651         if (changed & ViewState::IsVisible)
652             setIsVisibleAndActiveInternal(viewState & ViewState::WindowIsActive);
653     }
654 }
655
656 void FocusController::setActive(bool active)
657 {
658     m_page.setViewState(active ? m_viewState | ViewState::WindowIsActive : m_viewState & ~ViewState::WindowIsActive);
659 }
660
661 void FocusController::setActiveInternal(bool active)
662 {
663     if (FrameView* view = m_page.mainFrame().view()) {
664         if (!view->platformWidget()) {
665             view->updateLayoutAndStyleIfNeededRecursive();
666             view->updateControlTints();
667         }
668     }
669
670     focusedOrMainFrame().selection().pageActivationChanged();
671     
672     if (m_focusedFrame && isFocused())
673         dispatchEventsOnWindowAndFocusedElement(m_focusedFrame->document(), active);
674 }
675
676 static void contentAreaDidShowOrHide(ScrollableArea* scrollableArea, bool didShow)
677 {
678     if (didShow)
679         scrollableArea->contentAreaDidShow();
680     else
681         scrollableArea->contentAreaDidHide();
682 }
683
684 void FocusController::setIsVisibleAndActiveInternal(bool contentIsVisible)
685 {
686     FrameView* view = m_page.mainFrame().view();
687     if (!view)
688         return;
689
690     contentAreaDidShowOrHide(view, contentIsVisible);
691
692     for (Frame* frame = &m_page.mainFrame(); frame; frame = frame->tree().traverseNext()) {
693         FrameView* frameView = frame->view();
694         if (!frameView)
695             continue;
696
697         const HashSet<ScrollableArea*>* scrollableAreas = frameView->scrollableAreas();
698         if (!scrollableAreas)
699             continue;
700
701         for (auto& scrollableArea : *scrollableAreas) {
702             ASSERT(scrollableArea->scrollbarsCanBeActive() || m_page.shouldSuppressScrollbarAnimations());
703
704             contentAreaDidShowOrHide(scrollableArea, contentIsVisible);
705         }
706     }
707 }
708
709 static void updateFocusCandidateIfNeeded(FocusDirection direction, const FocusCandidate& current, FocusCandidate& candidate, FocusCandidate& closest)
710 {
711     ASSERT(candidate.visibleNode->isElementNode());
712     ASSERT(candidate.visibleNode->renderer());
713
714     // Ignore iframes that don't have a src attribute
715     if (frameOwnerElement(candidate) && (!frameOwnerElement(candidate)->contentFrame() || candidate.rect.isEmpty()))
716         return;
717
718     // Ignore off screen child nodes of containers that do not scroll (overflow:hidden)
719     if (candidate.isOffscreen && !canBeScrolledIntoView(direction, candidate))
720         return;
721
722     distanceDataForNode(direction, current, candidate);
723     if (candidate.distance == maxDistance())
724         return;
725
726     if (candidate.isOffscreenAfterScrolling && candidate.alignment < Full)
727         return;
728
729     if (closest.isNull()) {
730         closest = candidate;
731         return;
732     }
733
734     LayoutRect intersectionRect = intersection(candidate.rect, closest.rect);
735     if (!intersectionRect.isEmpty() && !areElementsOnSameLine(closest, candidate)) {
736         // If 2 nodes are intersecting, do hit test to find which node in on top.
737         LayoutUnit x = intersectionRect.x() + intersectionRect.width() / 2;
738         LayoutUnit y = intersectionRect.y() + intersectionRect.height() / 2;
739         HitTestResult result = candidate.visibleNode->document().page()->mainFrame().eventHandler().hitTestResultAtPoint(IntPoint(x, y), HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::IgnoreClipping | HitTestRequest::DisallowShadowContent);
740         if (candidate.visibleNode->contains(result.innerNode())) {
741             closest = candidate;
742             return;
743         }
744         if (closest.visibleNode->contains(result.innerNode()))
745             return;
746     }
747
748     if (candidate.alignment == closest.alignment) {
749         if (candidate.distance < closest.distance)
750             closest = candidate;
751         return;
752     }
753
754     if (candidate.alignment > closest.alignment)
755         closest = candidate;
756 }
757
758 void FocusController::findFocusCandidateInContainer(Node& container, const LayoutRect& startingRect, FocusDirection direction, KeyboardEvent* event, FocusCandidate& closest)
759 {
760     Node* focusedNode = (focusedFrame() && focusedFrame()->document()) ? focusedFrame()->document()->focusedElement() : 0;
761
762     Element* element = ElementTraversal::firstWithin(container);
763     FocusCandidate current;
764     current.rect = startingRect;
765     current.focusableNode = focusedNode;
766     current.visibleNode = focusedNode;
767
768     unsigned candidateCount = 0;
769     for (; element; element = (element->isFrameOwnerElement() || canScrollInDirection(element, direction))
770         ? ElementTraversal::nextSkippingChildren(*element, &container)
771         : ElementTraversal::next(*element, &container)) {
772         if (element == focusedNode)
773             continue;
774
775         if (!element->isKeyboardFocusable(event) && !element->isFrameOwnerElement() && !canScrollInDirection(element, direction))
776             continue;
777
778         FocusCandidate candidate = FocusCandidate(element, direction);
779         if (candidate.isNull())
780             continue;
781
782         if (!isValidCandidate(direction, current, candidate))
783             continue;
784
785         candidateCount++;
786         candidate.enclosingScrollableBox = &container;
787         updateFocusCandidateIfNeeded(direction, current, candidate, closest);
788     }
789
790     // The variable 'candidateCount' keeps track of the number of nodes traversed in a given container.
791     // If we have more than one container in a page then the total number of nodes traversed is equal to the sum of nodes traversed in each container.
792     if (focusedFrame() && focusedFrame()->document()) {
793         candidateCount += focusedFrame()->document()->page()->lastSpatialNavigationCandidateCount();
794         focusedFrame()->document()->page()->setLastSpatialNavigationCandidateCount(candidateCount);
795     }
796 }
797
798 bool FocusController::advanceFocusDirectionallyInContainer(Node* container, const LayoutRect& startingRect, FocusDirection direction, KeyboardEvent* event)
799 {
800     if (!container)
801         return false;
802
803     LayoutRect newStartingRect = startingRect;
804
805     if (startingRect.isEmpty())
806         newStartingRect = virtualRectForDirection(direction, nodeRectInAbsoluteCoordinates(container));
807
808     // Find the closest node within current container in the direction of the navigation.
809     FocusCandidate focusCandidate;
810     findFocusCandidateInContainer(*container, newStartingRect, direction, event, focusCandidate);
811
812     if (focusCandidate.isNull()) {
813         // Nothing to focus, scroll if possible.
814         // NOTE: If no scrolling is performed (i.e. scrollInDirection returns false), the
815         // spatial navigation algorithm will skip this container.
816         return scrollInDirection(container, direction);
817     }
818
819     if (HTMLFrameOwnerElement* frameElement = frameOwnerElement(focusCandidate)) {
820         // If we have an iframe without the src attribute, it will not have a contentFrame().
821         // We ASSERT here to make sure that
822         // updateFocusCandidateIfNeeded() will never consider such an iframe as a candidate.
823         ASSERT(frameElement->contentFrame());
824
825         if (focusCandidate.isOffscreenAfterScrolling) {
826             scrollInDirection(&focusCandidate.visibleNode->document(), direction);
827             return true;
828         }
829         // Navigate into a new frame.
830         LayoutRect rect;
831         Element* focusedElement = focusedOrMainFrame().document()->focusedElement();
832         if (focusedElement && !hasOffscreenRect(focusedElement))
833             rect = nodeRectInAbsoluteCoordinates(focusedElement, true /* ignore border */);
834         frameElement->contentFrame()->document()->updateLayoutIgnorePendingStylesheets();
835         if (!advanceFocusDirectionallyInContainer(frameElement->contentFrame()->document(), rect, direction, event)) {
836             // The new frame had nothing interesting, need to find another candidate.
837             return advanceFocusDirectionallyInContainer(container, nodeRectInAbsoluteCoordinates(focusCandidate.visibleNode, true), direction, event);
838         }
839         return true;
840     }
841
842     if (canScrollInDirection(focusCandidate.visibleNode, direction)) {
843         if (focusCandidate.isOffscreenAfterScrolling) {
844             scrollInDirection(focusCandidate.visibleNode, direction);
845             return true;
846         }
847         // Navigate into a new scrollable container.
848         LayoutRect startingRect;
849         Element* focusedElement = focusedOrMainFrame().document()->focusedElement();
850         if (focusedElement && !hasOffscreenRect(focusedElement))
851             startingRect = nodeRectInAbsoluteCoordinates(focusedElement, true);
852         return advanceFocusDirectionallyInContainer(focusCandidate.visibleNode, startingRect, direction, event);
853     }
854     if (focusCandidate.isOffscreenAfterScrolling) {
855         Node* container = focusCandidate.enclosingScrollableBox;
856         scrollInDirection(container, direction);
857         return true;
858     }
859
860     // We found a new focus node, navigate to it.
861     Element* element = downcast<Element>(focusCandidate.focusableNode);
862     ASSERT(element);
863
864     element->focus(false, direction);
865     return true;
866 }
867
868 bool FocusController::advanceFocusDirectionally(FocusDirection direction, KeyboardEvent* event)
869 {
870     Document* focusedDocument = focusedOrMainFrame().document();
871     if (!focusedDocument)
872         return false;
873
874     Element* focusedElement = focusedDocument->focusedElement();
875     Node* container = focusedDocument;
876
877     if (is<Document>(*container))
878         downcast<Document>(*container).updateLayoutIgnorePendingStylesheets();
879
880     // Figure out the starting rect.
881     LayoutRect startingRect;
882     if (focusedElement) {
883         if (!hasOffscreenRect(focusedElement)) {
884             container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, focusedElement);
885             startingRect = nodeRectInAbsoluteCoordinates(focusedElement, true /* ignore border */);
886         } else if (is<HTMLAreaElement>(*focusedElement)) {
887             HTMLAreaElement& area = downcast<HTMLAreaElement>(*focusedElement);
888             container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, area.imageElement());
889             startingRect = virtualRectForAreaElementAndDirection(&area, direction);
890         }
891     }
892
893     if (focusedFrame() && focusedFrame()->document())
894         focusedDocument->page()->setLastSpatialNavigationCandidateCount(0);
895
896     bool consumed = false;
897     do {
898         consumed = advanceFocusDirectionallyInContainer(container, startingRect, direction, event);
899         startingRect = nodeRectInAbsoluteCoordinates(container, true /* ignore border */);
900         container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, container);
901         if (is<Document>(container))
902             downcast<Document>(*container).updateLayoutIgnorePendingStylesheets();
903     } while (!consumed && container);
904
905     return consumed;
906 }
907
908 void FocusController::setFocusedElementNeedsRepaint()
909 {
910     m_focusRepaintTimer.startOneShot(0.033);
911 }
912
913 void FocusController::focusRepaintTimerFired()
914 {
915     Document* focusedDocument = focusedOrMainFrame().document();
916     if (!focusedDocument)
917         return;
918
919     Element* focusedElement = focusedDocument->focusedElement();
920     if (!focusedElement)
921         return;
922
923     if (focusedElement->renderer())
924         focusedElement->renderer()->repaint();
925 }
926
927 double FocusController::timeSinceFocusWasSet() const
928 {
929     return monotonicallyIncreasingTime() - m_focusSetTime;
930 }
931
932 } // namespace WebCore