FocusController.cpp [plain text]
#include "config.h"
#include "FocusController.h"
#include "AXObjectCache.h"
#include "Chrome.h"
#include "Document.h"
#include "Editor.h"
#include "EditorClient.h"
#include "Element.h"
#include "ElementTraversal.h"
#include "Event.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "ExceptionCode.h"
#include "FrameSelection.h"
#include "FrameTree.h"
#include "FrameView.h"
#include "HTMLAreaElement.h"
#include "HTMLImageElement.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HTMLPlugInElement.h"
#include "HTMLTextAreaElement.h"
#include "HitTestResult.h"
#include "KeyboardEvent.h"
#include "MainFrame.h"
#include "NodeRenderingTraversal.h"
#include "Page.h"
#include "Range.h"
#include "RenderWidget.h"
#include "ScrollAnimator.h"
#include "Settings.h"
#include "ShadowRoot.h"
#include "SpatialNavigation.h"
#include "Widget.h"
#include "htmlediting.h" // For firstPositionInOrBeforeNode
#include <limits>
#include <wtf/CurrentTime.h>
#include <wtf/Ref.h>
namespace WebCore {
using namespace HTMLNames;
FocusNavigationScope::FocusNavigationScope(TreeScope* treeScope)
: m_rootTreeScope(treeScope)
{
ASSERT(treeScope);
}
ContainerNode* FocusNavigationScope::rootNode() const
{
return &m_rootTreeScope->rootNode();
}
Element* FocusNavigationScope::owner() const
{
ContainerNode* root = rootNode();
if (is<ShadowRoot>(*root))
return downcast<ShadowRoot>(*root).hostElement();
if (Frame* frame = root->document().frame())
return frame->ownerElement();
return nullptr;
}
FocusNavigationScope FocusNavigationScope::focusNavigationScopeOf(Node* node)
{
ASSERT(node);
Node* root = node;
for (Node* n = node; n; n = NodeRenderingTraversal::parentInScope(n))
root = n;
return FocusNavigationScope(&root->treeScope());
}
FocusNavigationScope FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(Node* node)
{
ASSERT(node);
ASSERT(downcast<Element>(*node).shadowRoot());
return FocusNavigationScope(downcast<Element>(*node).shadowRoot());
}
FocusNavigationScope FocusNavigationScope::focusNavigationScopeOwnedByIFrame(HTMLFrameOwnerElement* frame)
{
ASSERT(frame);
ASSERT(frame->contentFrame());
return FocusNavigationScope(frame->contentFrame()->document());
}
static inline void dispatchEventsOnWindowAndFocusedElement(Document* document, bool focused)
{
if (Page* page = document->page()) {
if (page->defersLoading())
return;
}
if (!focused && document->focusedElement())
document->focusedElement()->dispatchBlurEvent(nullptr);
document->dispatchWindowEvent(Event::create(focused ? eventNames().focusEvent : eventNames().blurEvent, false, false));
if (focused && document->focusedElement())
document->focusedElement()->dispatchFocusEvent(nullptr, FocusDirectionNone);
}
static inline bool hasCustomFocusLogic(Element& element)
{
return is<HTMLElement>(element) && downcast<HTMLElement>(element).hasCustomFocusLogic();
}
static inline bool isNonFocusableShadowHost(Element& element, KeyboardEvent& event)
{
return !element.isKeyboardFocusable(&event) && element.shadowRoot() && !hasCustomFocusLogic(element);
}
static inline bool isFocusableShadowHost(Node& node, KeyboardEvent& event)
{
return is<Element>(node) && downcast<Element>(node).isKeyboardFocusable(&event) && downcast<Element>(node).shadowRoot() && !hasCustomFocusLogic(downcast<Element>(node));
}
static inline int adjustedTabIndex(Node& node, KeyboardEvent& event)
{
if (!is<Element>(node))
return 0;
return isNonFocusableShadowHost(downcast<Element>(node), event) ? 0 : downcast<Element>(node).tabIndex();
}
static inline bool shouldVisit(Element& element, KeyboardEvent& event)
{
return element.isKeyboardFocusable(&event) || isNonFocusableShadowHost(element, event);
}
FocusController::FocusController(Page& page, ViewState::Flags viewState)
: m_page(page)
, m_isChangingFocusedFrame(false)
, m_viewState(viewState)
, m_focusRepaintTimer(*this, &FocusController::focusRepaintTimerFired)
{
}
void FocusController::setFocusedFrame(PassRefPtr<Frame> frame)
{
ASSERT(!frame || frame->page() == &m_page);
if (m_focusedFrame == frame || m_isChangingFocusedFrame)
return;
m_isChangingFocusedFrame = true;
RefPtr<Frame> oldFrame = m_focusedFrame;
RefPtr<Frame> newFrame = frame;
m_focusedFrame = newFrame;
if (oldFrame && oldFrame->view()) {
oldFrame->selection().setFocused(false);
oldFrame->document()->dispatchWindowEvent(Event::create(eventNames().blurEvent, false, false));
}
if (newFrame && newFrame->view() && isFocused()) {
newFrame->selection().setFocused(true);
newFrame->document()->dispatchWindowEvent(Event::create(eventNames().focusEvent, false, false));
}
m_page.chrome().focusedFrameChanged(newFrame.get());
m_isChangingFocusedFrame = false;
}
Frame& FocusController::focusedOrMainFrame() const
{
if (Frame* frame = focusedFrame())
return *frame;
return m_page.mainFrame();
}
void FocusController::setFocused(bool focused)
{
m_page.setViewState(focused ? m_viewState | ViewState::IsFocused : m_viewState & ~ViewState::IsFocused);
}
void FocusController::setFocusedInternal(bool focused)
{
if (!isFocused())
focusedOrMainFrame().eventHandler().stopAutoscrollTimer();
if (!m_focusedFrame)
setFocusedFrame(&m_page.mainFrame());
if (m_focusedFrame->view()) {
m_focusedFrame->selection().setFocused(focused);
dispatchEventsOnWindowAndFocusedElement(m_focusedFrame->document(), focused);
}
}
Element* FocusController::findFocusableElementDescendingDownIntoFrameDocument(FocusDirection direction, Element* element, KeyboardEvent* event)
{
while (is<HTMLFrameOwnerElement>(element)) {
HTMLFrameOwnerElement& owner = downcast<HTMLFrameOwnerElement>(*element);
if (!owner.contentFrame())
break;
Element* foundElement = findFocusableElement(direction, FocusNavigationScope::focusNavigationScopeOwnedByIFrame(&owner), 0, event);
if (!foundElement)
break;
ASSERT(element != foundElement);
element = foundElement;
}
return element;
}
bool FocusController::setInitialFocus(FocusDirection direction, KeyboardEvent* event)
{
bool didAdvanceFocus = advanceFocus(direction, event, true);
if (AXObjectCache* cache = focusedOrMainFrame().document()->existingAXObjectCache())
cache->postNotification(focusedOrMainFrame().document(), AXObjectCache::AXFocusedUIElementChanged);
return didAdvanceFocus;
}
bool FocusController::advanceFocus(FocusDirection direction, KeyboardEvent* event, bool initialFocus)
{
switch (direction) {
case FocusDirectionForward:
case FocusDirectionBackward:
return advanceFocusInDocumentOrder(direction, event, initialFocus);
case FocusDirectionLeft:
case FocusDirectionRight:
case FocusDirectionUp:
case FocusDirectionDown:
return advanceFocusDirectionally(direction, event);
default:
ASSERT_NOT_REACHED();
}
return false;
}
bool FocusController::advanceFocusInDocumentOrder(FocusDirection direction, KeyboardEvent* event, bool initialFocus)
{
Frame& frame = focusedOrMainFrame();
Document* document = frame.document();
Node* currentNode = document->focusedElement();
bool caretBrowsing = frame.settings().caretBrowsingEnabled();
if (caretBrowsing && !currentNode)
currentNode = frame.selection().selection().start().deprecatedNode();
document->updateLayoutIgnorePendingStylesheets();
RefPtr<Element> element = findFocusableElementAcrossFocusScope(direction, FocusNavigationScope::focusNavigationScopeOf(currentNode ? currentNode : document), currentNode, event);
if (!element) {
if (!initialFocus && m_page.chrome().canTakeFocus(direction)) {
document->setFocusedElement(nullptr);
setFocusedFrame(nullptr);
m_page.chrome().takeFocus(direction);
return true;
}
element = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOf(m_page.mainFrame().document()), 0, event);
element = findFocusableElementDescendingDownIntoFrameDocument(direction, element.get(), event);
if (!element)
return false;
}
ASSERT(element);
if (element == document->focusedElement()) {
return true;
}
if (is<HTMLFrameOwnerElement>(*element) && (!is<HTMLPlugInElement>(*element) || !element->isKeyboardFocusable(event))) {
HTMLFrameOwnerElement& owner = downcast<HTMLFrameOwnerElement>(*element);
if (!owner.contentFrame())
return false;
document->setFocusedElement(nullptr);
setFocusedFrame(owner.contentFrame());
return true;
}
Document& newDocument = element->document();
if (&newDocument != document) {
document->setFocusedElement(nullptr);
}
setFocusedFrame(newDocument.frame());
if (caretBrowsing) {
Position position = firstPositionInOrBeforeNode(element.get());
VisibleSelection newSelection(position, position, DOWNSTREAM);
if (frame.selection().shouldChangeSelection(newSelection)) {
AXTextStateChangeIntent intent(AXTextStateChangeTypeSelectionMove, AXTextSelection { AXTextSelectionDirectionDiscontiguous, AXTextSelectionGranularityUnknown, true });
frame.selection().setSelection(newSelection, FrameSelection::defaultSetSelectionOptions(UserTriggered), intent);
}
}
element->focus(false, direction);
return true;
}
Element* FocusController::findFocusableElementAcrossFocusScope(FocusDirection direction, FocusNavigationScope scope, Node* currentNode, KeyboardEvent* event)
{
ASSERT(!is<Element>(currentNode) || !isNonFocusableShadowHost(*downcast<Element>(currentNode), *event));
Element* found;
if (currentNode && direction == FocusDirectionForward && isFocusableShadowHost(*currentNode, *event)) {
Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(currentNode), 0, event);
found = foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableElementRecursively(direction, scope, currentNode, event);
} else
found = findFocusableElementRecursively(direction, scope, currentNode, event);
while (!found) {
Element* owner = scope.owner();
if (!owner)
break;
scope = FocusNavigationScope::focusNavigationScopeOf(owner);
if (direction == FocusDirectionBackward && isFocusableShadowHost(*owner, *event)) {
found = owner;
break;
}
found = findFocusableElementRecursively(direction, scope, owner, event);
}
found = findFocusableElementDescendingDownIntoFrameDocument(direction, found, event);
return found;
}
Element* FocusController::findFocusableElementRecursively(FocusDirection direction, FocusNavigationScope scope, Node* start, KeyboardEvent* event)
{
Element* found = findFocusableElement(direction, scope, start, event);
if (!found)
return nullptr;
if (direction == FocusDirectionForward) {
if (!isNonFocusableShadowHost(*found, *event))
return found;
Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
return foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableElementRecursively(direction, scope, found, event);
}
ASSERT(direction == FocusDirectionBackward);
if (isFocusableShadowHost(*found, *event)) {
Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
return foundInInnerFocusScope ? foundInInnerFocusScope : found;
}
if (isNonFocusableShadowHost(*found, *event)) {
Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
return foundInInnerFocusScope ? foundInInnerFocusScope :findFocusableElementRecursively(direction, scope, found, event);
}
return found;
}
Element* FocusController::findFocusableElement(FocusDirection direction, FocusNavigationScope scope, Node* node, KeyboardEvent* event)
{
return (direction == FocusDirectionForward)
? nextFocusableElement(scope, node, event)
: previousFocusableElement(scope, node, event);
}
Element* FocusController::findElementWithExactTabIndex(Node* start, int tabIndex, KeyboardEvent* event, FocusDirection direction)
{
using namespace NodeRenderingTraversal;
for (Node* node = start; node; node = direction == FocusDirectionForward ? nextInScope(node) : previousInScope(node)) {
if (!is<Element>(*node))
continue;
Element& element = downcast<Element>(*node);
if (shouldVisit(element, *event) && adjustedTabIndex(element, *event) == tabIndex)
return &element;
}
return nullptr;
}
static Element* nextElementWithGreaterTabIndex(Node* start, int tabIndex, KeyboardEvent& event)
{
int winningTabIndex = std::numeric_limits<short>::max() + 1;
Element* winner = nullptr;
for (Node* node = start; node; node = NodeRenderingTraversal::nextInScope(node)) {
if (!is<Element>(*node))
continue;
Element& element = downcast<Element>(*node);
if (shouldVisit(element, event) && element.tabIndex() > tabIndex && element.tabIndex() < winningTabIndex) {
winner = &element;
winningTabIndex = element.tabIndex();
}
}
return winner;
}
static Element* previousElementWithLowerTabIndex(Node* start, int tabIndex, KeyboardEvent& event)
{
int winningTabIndex = 0;
Element* winner = nullptr;
for (Node* node = start; node; node = NodeRenderingTraversal::previousInScope(node)) {
if (!is<Element>(*node))
continue;
Element& element = downcast<Element>(*node);
int currentTabIndex = adjustedTabIndex(element, event);
if ((shouldVisit(element, event) || isNonFocusableShadowHost(element, event)) && currentTabIndex < tabIndex && currentTabIndex > winningTabIndex) {
winner = &element;
winningTabIndex = currentTabIndex;
}
}
return winner;
}
Element* FocusController::nextFocusableElement(FocusNavigationScope scope, Node* start, KeyboardEvent* event)
{
using namespace NodeRenderingTraversal;
if (start) {
int tabIndex = adjustedTabIndex(*start, *event);
if (tabIndex < 0) {
for (Node* node = nextInScope(start); node; node = nextInScope(node)) {
if (!is<Element>(*node))
continue;
Element& element = downcast<Element>(*node);
if (shouldVisit(element, *event) && adjustedTabIndex(element, *event) >= 0)
return &element;
}
}
if (Element* winner = findElementWithExactTabIndex(nextInScope(start), tabIndex, event, FocusDirectionForward))
return winner;
if (!tabIndex)
return 0;
}
if (Element* winner = nextElementWithGreaterTabIndex(scope.rootNode(), start ? adjustedTabIndex(*start, *event) : 0, *event))
return winner;
return findElementWithExactTabIndex(scope.rootNode(), 0, event, FocusDirectionForward);
}
Element* FocusController::previousFocusableElement(FocusNavigationScope scope, Node* start, KeyboardEvent* event)
{
using namespace NodeRenderingTraversal;
Node* last = nullptr;
for (Node* node = scope.rootNode(); node; node = lastChildInScope(node))
last = node;
ASSERT(last);
Node* startingNode;
int startingTabIndex;
if (start) {
startingNode = previousInScope(start);
startingTabIndex = adjustedTabIndex(*start, *event);
} else {
startingNode = last;
startingTabIndex = 0;
}
if (startingTabIndex < 0) {
for (Node* node = startingNode; node; node = previousInScope(node)) {
if (!is<Element>(*node))
continue;
Element& element = downcast<Element>(*node);
if (shouldVisit(element, *event) && adjustedTabIndex(element, *event) >= 0)
return &element;
}
}
if (Element* winner = findElementWithExactTabIndex(startingNode, startingTabIndex, event, FocusDirectionBackward))
return winner;
startingTabIndex = (start && startingTabIndex) ? startingTabIndex : std::numeric_limits<short>::max();
return previousElementWithLowerTabIndex(last, startingTabIndex, *event);
}
static bool relinquishesEditingFocus(Node *node)
{
ASSERT(node);
ASSERT(node->hasEditableStyle());
Node* root = node->rootEditableElement();
Frame* frame = node->document().frame();
if (!frame || !root)
return false;
return frame->editor().shouldEndEditing(rangeOfContents(*root).ptr());
}
static void clearSelectionIfNeeded(Frame* oldFocusedFrame, Frame* newFocusedFrame, Node* newFocusedNode)
{
if (!oldFocusedFrame || !newFocusedFrame)
return;
if (oldFocusedFrame->document() != newFocusedFrame->document())
return;
const VisibleSelection& selection = oldFocusedFrame->selection().selection();
if (selection.isNone())
return;
bool caretBrowsing = oldFocusedFrame->settings().caretBrowsingEnabled();
if (caretBrowsing)
return;
Node* selectionStartNode = selection.start().deprecatedNode();
if (selectionStartNode == newFocusedNode || selectionStartNode->isDescendantOf(newFocusedNode) || selectionStartNode->deprecatedShadowAncestorNode() == newFocusedNode)
return;
if (Node* mousePressNode = newFocusedFrame->eventHandler().mousePressNode()) {
if (mousePressNode->renderer() && !mousePressNode->canStartSelection()) {
Node * root = selection.rootEditableElement();
if (!root)
return;
if (Node* shadowAncestorNode = root->deprecatedShadowAncestorNode()) {
if (!is<HTMLInputElement>(*shadowAncestorNode) && !is<HTMLTextAreaElement>(*shadowAncestorNode))
return;
}
}
}
oldFocusedFrame->selection().clear();
}
bool FocusController::setFocusedElement(Element* element, PassRefPtr<Frame> newFocusedFrame, FocusDirection direction)
{
RefPtr<Frame> oldFocusedFrame = focusedFrame();
RefPtr<Document> oldDocument = oldFocusedFrame ? oldFocusedFrame->document() : nullptr;
Element* oldFocusedElement = oldDocument ? oldDocument->focusedElement() : nullptr;
if (oldFocusedElement == element)
return true;
if (oldFocusedElement && oldFocusedElement->isRootEditableElement() && !relinquishesEditingFocus(oldFocusedElement))
return false;
m_page.editorClient().willSetInputMethodState();
clearSelectionIfNeeded(oldFocusedFrame.get(), newFocusedFrame.get(), element);
if (!element) {
if (oldDocument)
oldDocument->setFocusedElement(nullptr);
m_page.editorClient().setInputMethodState(false);
return true;
}
Ref<Document> newDocument(element->document());
if (newDocument->focusedElement() == element) {
m_page.editorClient().setInputMethodState(element->shouldUseInputMethod());
return true;
}
if (oldDocument && oldDocument != newDocument.ptr())
oldDocument->setFocusedElement(nullptr);
if (newFocusedFrame && !newFocusedFrame->page()) {
setFocusedFrame(nullptr);
return false;
}
setFocusedFrame(newFocusedFrame);
Ref<Element> protect(*element);
bool successfullyFocused = newDocument->setFocusedElement(element, direction);
if (!successfullyFocused)
return false;
if (newDocument->focusedElement() == element)
m_page.editorClient().setInputMethodState(element->shouldUseInputMethod());
m_focusSetTime = monotonicallyIncreasingTime();
m_focusRepaintTimer.stop();
return true;
}
void FocusController::setViewState(ViewState::Flags viewState)
{
ViewState::Flags changed = m_viewState ^ viewState;
m_viewState = viewState;
if (changed & ViewState::IsFocused)
setFocusedInternal(viewState & ViewState::IsFocused);
if (changed & ViewState::WindowIsActive) {
setActiveInternal(viewState & ViewState::WindowIsActive);
if (changed & ViewState::IsVisible)
setIsVisibleAndActiveInternal(viewState & ViewState::WindowIsActive);
}
}
void FocusController::setActive(bool active)
{
m_page.setViewState(active ? m_viewState | ViewState::WindowIsActive : m_viewState & ~ViewState::WindowIsActive);
}
void FocusController::setActiveInternal(bool active)
{
if (FrameView* view = m_page.mainFrame().view()) {
if (!view->platformWidget()) {
view->updateLayoutAndStyleIfNeededRecursive();
view->updateControlTints();
}
}
focusedOrMainFrame().selection().pageActivationChanged();
if (m_focusedFrame && isFocused())
dispatchEventsOnWindowAndFocusedElement(m_focusedFrame->document(), active);
}
static void contentAreaDidShowOrHide(ScrollableArea* scrollableArea, bool didShow)
{
if (didShow)
scrollableArea->contentAreaDidShow();
else
scrollableArea->contentAreaDidHide();
}
void FocusController::setIsVisibleAndActiveInternal(bool contentIsVisible)
{
FrameView* view = m_page.mainFrame().view();
if (!view)
return;
contentAreaDidShowOrHide(view, contentIsVisible);
for (Frame* frame = &m_page.mainFrame(); frame; frame = frame->tree().traverseNext()) {
FrameView* frameView = frame->view();
if (!frameView)
continue;
const HashSet<ScrollableArea*>* scrollableAreas = frameView->scrollableAreas();
if (!scrollableAreas)
continue;
for (auto& scrollableArea : *scrollableAreas) {
ASSERT(scrollableArea->scrollbarsCanBeActive() || m_page.shouldSuppressScrollbarAnimations());
contentAreaDidShowOrHide(scrollableArea, contentIsVisible);
}
}
}
static void updateFocusCandidateIfNeeded(FocusDirection direction, const FocusCandidate& current, FocusCandidate& candidate, FocusCandidate& closest)
{
ASSERT(candidate.visibleNode->isElementNode());
ASSERT(candidate.visibleNode->renderer());
if (frameOwnerElement(candidate) && (!frameOwnerElement(candidate)->contentFrame() || candidate.rect.isEmpty()))
return;
if (candidate.isOffscreen && !canBeScrolledIntoView(direction, candidate))
return;
distanceDataForNode(direction, current, candidate);
if (candidate.distance == maxDistance())
return;
if (candidate.isOffscreenAfterScrolling && candidate.alignment < Full)
return;
if (closest.isNull()) {
closest = candidate;
return;
}
LayoutRect intersectionRect = intersection(candidate.rect, closest.rect);
if (!intersectionRect.isEmpty() && !areElementsOnSameLine(closest, candidate)) {
LayoutUnit x = intersectionRect.x() + intersectionRect.width() / 2;
LayoutUnit y = intersectionRect.y() + intersectionRect.height() / 2;
HitTestResult result = candidate.visibleNode->document().page()->mainFrame().eventHandler().hitTestResultAtPoint(IntPoint(x, y), HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::IgnoreClipping | HitTestRequest::DisallowShadowContent);
if (candidate.visibleNode->contains(result.innerNode())) {
closest = candidate;
return;
}
if (closest.visibleNode->contains(result.innerNode()))
return;
}
if (candidate.alignment == closest.alignment) {
if (candidate.distance < closest.distance)
closest = candidate;
return;
}
if (candidate.alignment > closest.alignment)
closest = candidate;
}
void FocusController::findFocusCandidateInContainer(Node& container, const LayoutRect& startingRect, FocusDirection direction, KeyboardEvent* event, FocusCandidate& closest)
{
Node* focusedNode = (focusedFrame() && focusedFrame()->document()) ? focusedFrame()->document()->focusedElement() : 0;
Element* element = ElementTraversal::firstWithin(container);
FocusCandidate current;
current.rect = startingRect;
current.focusableNode = focusedNode;
current.visibleNode = focusedNode;
unsigned candidateCount = 0;
for (; element; element = (element->isFrameOwnerElement() || canScrollInDirection(element, direction))
? ElementTraversal::nextSkippingChildren(*element, &container)
: ElementTraversal::next(*element, &container)) {
if (element == focusedNode)
continue;
if (!element->isKeyboardFocusable(event) && !element->isFrameOwnerElement() && !canScrollInDirection(element, direction))
continue;
FocusCandidate candidate = FocusCandidate(element, direction);
if (candidate.isNull())
continue;
if (!isValidCandidate(direction, current, candidate))
continue;
candidateCount++;
candidate.enclosingScrollableBox = &container;
updateFocusCandidateIfNeeded(direction, current, candidate, closest);
}
if (focusedFrame() && focusedFrame()->document()) {
candidateCount += focusedFrame()->document()->page()->lastSpatialNavigationCandidateCount();
focusedFrame()->document()->page()->setLastSpatialNavigationCandidateCount(candidateCount);
}
}
bool FocusController::advanceFocusDirectionallyInContainer(Node* container, const LayoutRect& startingRect, FocusDirection direction, KeyboardEvent* event)
{
if (!container)
return false;
LayoutRect newStartingRect = startingRect;
if (startingRect.isEmpty())
newStartingRect = virtualRectForDirection(direction, nodeRectInAbsoluteCoordinates(container));
FocusCandidate focusCandidate;
findFocusCandidateInContainer(*container, newStartingRect, direction, event, focusCandidate);
if (focusCandidate.isNull()) {
return scrollInDirection(container, direction);
}
if (HTMLFrameOwnerElement* frameElement = frameOwnerElement(focusCandidate)) {
ASSERT(frameElement->contentFrame());
if (focusCandidate.isOffscreenAfterScrolling) {
scrollInDirection(&focusCandidate.visibleNode->document(), direction);
return true;
}
LayoutRect rect;
Element* focusedElement = focusedOrMainFrame().document()->focusedElement();
if (focusedElement && !hasOffscreenRect(focusedElement))
rect = nodeRectInAbsoluteCoordinates(focusedElement, true );
frameElement->contentFrame()->document()->updateLayoutIgnorePendingStylesheets();
if (!advanceFocusDirectionallyInContainer(frameElement->contentFrame()->document(), rect, direction, event)) {
return advanceFocusDirectionallyInContainer(container, nodeRectInAbsoluteCoordinates(focusCandidate.visibleNode, true), direction, event);
}
return true;
}
if (canScrollInDirection(focusCandidate.visibleNode, direction)) {
if (focusCandidate.isOffscreenAfterScrolling) {
scrollInDirection(focusCandidate.visibleNode, direction);
return true;
}
LayoutRect startingRect;
Element* focusedElement = focusedOrMainFrame().document()->focusedElement();
if (focusedElement && !hasOffscreenRect(focusedElement))
startingRect = nodeRectInAbsoluteCoordinates(focusedElement, true);
return advanceFocusDirectionallyInContainer(focusCandidate.visibleNode, startingRect, direction, event);
}
if (focusCandidate.isOffscreenAfterScrolling) {
Node* container = focusCandidate.enclosingScrollableBox;
scrollInDirection(container, direction);
return true;
}
Element* element = downcast<Element>(focusCandidate.focusableNode);
ASSERT(element);
element->focus(false, direction);
return true;
}
bool FocusController::advanceFocusDirectionally(FocusDirection direction, KeyboardEvent* event)
{
Document* focusedDocument = focusedOrMainFrame().document();
if (!focusedDocument)
return false;
Element* focusedElement = focusedDocument->focusedElement();
Node* container = focusedDocument;
if (is<Document>(*container))
downcast<Document>(*container).updateLayoutIgnorePendingStylesheets();
LayoutRect startingRect;
if (focusedElement) {
if (!hasOffscreenRect(focusedElement)) {
container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, focusedElement);
startingRect = nodeRectInAbsoluteCoordinates(focusedElement, true );
} else if (is<HTMLAreaElement>(*focusedElement)) {
HTMLAreaElement& area = downcast<HTMLAreaElement>(*focusedElement);
container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, area.imageElement());
startingRect = virtualRectForAreaElementAndDirection(&area, direction);
}
}
if (focusedFrame() && focusedFrame()->document())
focusedDocument->page()->setLastSpatialNavigationCandidateCount(0);
bool consumed = false;
do {
consumed = advanceFocusDirectionallyInContainer(container, startingRect, direction, event);
startingRect = nodeRectInAbsoluteCoordinates(container, true );
container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, container);
if (is<Document>(container))
downcast<Document>(*container).updateLayoutIgnorePendingStylesheets();
} while (!consumed && container);
return consumed;
}
void FocusController::setFocusedElementNeedsRepaint()
{
m_focusRepaintTimer.startOneShot(0.033);
}
void FocusController::focusRepaintTimerFired()
{
Document* focusedDocument = focusedOrMainFrame().document();
if (!focusedDocument)
return;
Element* focusedElement = focusedDocument->focusedElement();
if (!focusedElement)
return;
if (focusedElement->renderer())
focusedElement->renderer()->repaint();
}
double FocusController::timeSinceFocusWasSet() const
{
return monotonicallyIncreasingTime() - m_focusSetTime;
}
}