FocusController.cpp [plain text]
#include "config.h"
#include "FocusController.h"
#include "AXObjectCache.h"
#include "Chrome.h"
#include "ComposedShadowTreeWalker.h"
#include "Document.h"
#include "Editor.h"
#include "EditorClient.h"
#include "Element.h"
#include "Event.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "ExceptionCode.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "FrameTree.h"
#include "FrameView.h"
#include "HTMLAreaElement.h"
#include "HTMLImageElement.h"
#include "HTMLNames.h"
#include "HitTestResult.h"
#include "KeyboardEvent.h"
#include "Page.h"
#include "Range.h"
#include "RenderObject.h"
#include "RenderWidget.h"
#include "ScrollAnimator.h"
#include "Settings.h"
#include "ShadowRoot.h"
#include "ShadowTree.h"
#include "SpatialNavigation.h"
#include "Widget.h"
#include "htmlediting.h" // For firstPositionInOrBeforeNode
#include <limits>
namespace WebCore {
using namespace HTMLNames;
using namespace std;
static inline bool isShadowHost(const Node* node)
{
return node && node->isElementNode() && toElement(node)->hasShadowRoot();
}
static inline ComposedShadowTreeWalker walkerFrom(const Node* node)
{
return ComposedShadowTreeWalker(node, ComposedShadowTreeWalker::DoNotCrossUpperBoundary);
}
static inline ComposedShadowTreeWalker walkerFromNext(const Node* node)
{
ComposedShadowTreeWalker walker = ComposedShadowTreeWalker(node, ComposedShadowTreeWalker::DoNotCrossUpperBoundary);
walker.next();
return walker;
}
static inline ComposedShadowTreeWalker walkerFromPrevious(const Node* node)
{
ComposedShadowTreeWalker walker = ComposedShadowTreeWalker(node, ComposedShadowTreeWalker::DoNotCrossUpperBoundary);
walker.previous();
return walker;
}
static inline Node* nextNode(const Node* node)
{
return walkerFromNext(node).get();
}
static inline Node* previousNode(const Node* node)
{
return walkerFromPrevious(node).get();
}
FocusScope::FocusScope(TreeScope* treeScope)
: m_rootTreeScope(treeScope)
{
ASSERT(treeScope);
ASSERT(!treeScope->rootNode()->isShadowRoot() || toShadowRoot(treeScope->rootNode())->isYoungest());
}
Node* FocusScope::rootNode() const
{
return m_rootTreeScope->rootNode();
}
Element* FocusScope::owner() const
{
Node* root = rootNode();
if (root->isShadowRoot())
return root->shadowHost();
if (Frame* frame = root->document()->frame())
return frame->ownerElement();
return 0;
}
FocusScope FocusScope::focusScopeOf(Node* node)
{
ASSERT(node);
TreeScope* scope = node->treeScope();
if (scope->rootNode()->isShadowRoot())
return FocusScope(toShadowRoot(scope->rootNode())->tree()->youngestShadowRoot());
return FocusScope(scope);
}
FocusScope FocusScope::focusScopeOwnedByShadowHost(Node* node)
{
ASSERT(isShadowHost(node));
return FocusScope(toElement(node)->shadowTree()->youngestShadowRoot());
}
FocusScope FocusScope::focusScopeOwnedByIFrame(HTMLFrameOwnerElement* frame)
{
ASSERT(frame && frame->contentFrame());
return FocusScope(frame->contentFrame()->document());
}
static inline void dispatchEventsOnWindowAndFocusedNode(Document* document, bool focused)
{
if (Page* page = document->page()) {
if (page->defersLoading())
return;
}
if (!focused && document->focusedNode())
document->focusedNode()->dispatchBlurEvent(0);
document->dispatchWindowEvent(Event::create(focused ? eventNames().focusEvent : eventNames().blurEvent, false, false));
if (focused && document->focusedNode())
document->focusedNode()->dispatchFocusEvent(0);
}
static inline bool hasCustomFocusLogic(Node* node)
{
return node->hasTagName(inputTag) || node->hasTagName(textareaTag) || node->hasTagName(videoTag) || node->hasTagName(audioTag);
}
static inline bool isNonFocusableShadowHost(Node* node, KeyboardEvent* event)
{
ASSERT(node);
return !node->isKeyboardFocusable(event) && isShadowHost(node) && !hasCustomFocusLogic(node);
}
static inline bool isFocusableShadowHost(Node* node, KeyboardEvent* event)
{
ASSERT(node);
return node->isKeyboardFocusable(event) && isShadowHost(node) && !hasCustomFocusLogic(node);
}
static inline int adjustedTabIndex(Node* node, KeyboardEvent* event)
{
ASSERT(node);
return isNonFocusableShadowHost(node, event) ? 0 : node->tabIndex();
}
static inline bool shouldVisit(Node* node, KeyboardEvent* event)
{
ASSERT(node);
return node->isKeyboardFocusable(event) || isNonFocusableShadowHost(node, event);
}
FocusController::FocusController(Page* page)
: m_page(page)
, m_isActive(false)
, m_isFocused(false)
, m_isChangingFocusedFrame(false)
, m_containingWindowIsVisible(false)
{
}
PassOwnPtr<FocusController> FocusController::create(Page* page)
{
return adoptPtr(new FocusController(page));
}
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)
{
if (isFocused() == focused)
return;
m_isFocused = focused;
if (!m_isFocused)
focusedOrMainFrame()->eventHandler()->stopAutoscrollTimer();
if (!m_focusedFrame)
setFocusedFrame(m_page->mainFrame());
if (m_focusedFrame->view()) {
m_focusedFrame->selection()->setFocused(focused);
dispatchEventsOnWindowAndFocusedNode(m_focusedFrame->document(), focused);
}
}
Node* FocusController::findFocusableNodeDecendingDownIntoFrameDocument(FocusDirection direction, Node* node, KeyboardEvent* event)
{
while (node && node->isFrameOwnerElement()) {
HTMLFrameOwnerElement* owner = static_cast<HTMLFrameOwnerElement*>(node);
if (!owner->contentFrame())
break;
Node* foundNode = findFocusableNode(direction, FocusScope::focusScopeOwnedByIFrame(owner), 0, event);
if (!foundNode)
break;
ASSERT(node != foundNode);
node = foundNode;
}
return node;
}
bool FocusController::setInitialFocus(FocusDirection direction, KeyboardEvent* event)
{
bool didAdvanceFocus = advanceFocus(direction, event, true);
if (AXObjectCache::accessibilityEnabled())
focusedOrMainFrame()->document()->axObjectCache()->postNotification(focusedOrMainFrame()->document()->renderer(), AXObjectCache::AXFocusedUIElementChanged, true);
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();
ASSERT(frame);
Document* document = frame->document();
Node* currentNode = document->focusedNode();
bool caretBrowsing = frame->settings() && frame->settings()->caretBrowsingEnabled();
if (caretBrowsing && !currentNode)
currentNode = frame->selection()->start().deprecatedNode();
document->updateLayoutIgnorePendingStylesheets();
RefPtr<Node> node = findFocusableNodeAcrossFocusScope(direction, FocusScope::focusScopeOf(currentNode ? currentNode : document), currentNode, event);
if (!node) {
if (!initialFocus && m_page->chrome()->canTakeFocus(direction)) {
document->setFocusedNode(0);
setFocusedFrame(0);
m_page->chrome()->takeFocus(direction);
return true;
}
node = findFocusableNodeRecursively(direction, FocusScope::focusScopeOf(m_page->mainFrame()->document()), 0, event);
node = findFocusableNodeDecendingDownIntoFrameDocument(direction, node.get(), event);
if (!node)
return false;
}
ASSERT(node);
if (node == document->focusedNode())
return true;
if (!node->isElementNode())
return false;
if (node->isFrameOwnerElement()) {
HTMLFrameOwnerElement* owner = static_cast<HTMLFrameOwnerElement*>(node.get());
if (!owner->contentFrame())
return false;
document->setFocusedNode(0);
setFocusedFrame(owner->contentFrame());
return true;
}
Document* newDocument = node->document();
if (newDocument != document)
document->setFocusedNode(0);
if (newDocument)
setFocusedFrame(newDocument->frame());
if (caretBrowsing) {
Position position = firstPositionInOrBeforeNode(node.get());
VisibleSelection newSelection(position, position, DOWNSTREAM);
if (frame->selection()->shouldChangeSelection(newSelection))
frame->selection()->setSelection(newSelection);
}
static_cast<Element*>(node.get())->focus(false);
return true;
}
Node* FocusController::findFocusableNodeAcrossFocusScope(FocusDirection direction, FocusScope scope, Node* currentNode, KeyboardEvent* event)
{
ASSERT(!currentNode || !isNonFocusableShadowHost(currentNode, event));
Node* found;
if (currentNode && direction == FocusDirectionForward && isFocusableShadowHost(currentNode, event)) {
Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusScope::focusScopeOwnedByShadowHost(currentNode), 0, event);
found = foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableNodeRecursively(direction, scope, currentNode, event);
} else
found = findFocusableNodeRecursively(direction, scope, currentNode, event);
while (!found) {
Node* owner = scope.owner();
if (!owner)
break;
scope = FocusScope::focusScopeOf(owner);
if (direction == FocusDirectionBackward && isFocusableShadowHost(owner, event)) {
found = owner;
break;
}
found = findFocusableNodeRecursively(direction, scope, owner, event);
}
found = findFocusableNodeDecendingDownIntoFrameDocument(direction, found, event);
return found;
}
Node* FocusController::findFocusableNodeRecursively(FocusDirection direction, FocusScope scope, Node* start, KeyboardEvent* event)
{
Node* found = findFocusableNode(direction, scope, start, event);
if (!found)
return 0;
if (direction == FocusDirectionForward) {
if (!isNonFocusableShadowHost(found, event))
return found;
Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusScope::focusScopeOwnedByShadowHost(found), 0, event);
return foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableNodeRecursively(direction, scope, found, event);
}
ASSERT(direction == FocusDirectionBackward);
if (isFocusableShadowHost(found, event)) {
Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusScope::focusScopeOwnedByShadowHost(found), 0, event);
return foundInInnerFocusScope ? foundInInnerFocusScope : found;
}
if (isNonFocusableShadowHost(found, event)) {
Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusScope::focusScopeOwnedByShadowHost(found), 0, event);
return foundInInnerFocusScope ? foundInInnerFocusScope :findFocusableNodeRecursively(direction, scope, found, event);
}
return found;
}
Node* FocusController::findFocusableNode(FocusDirection direction, FocusScope scope, Node* node, KeyboardEvent* event)
{
return (direction == FocusDirectionForward)
? nextFocusableNode(scope, node, event)
: previousFocusableNode(scope, node, event);
}
Node* FocusController::findNodeWithExactTabIndex(Node* start, int tabIndex, KeyboardEvent* event, FocusDirection direction)
{
for (ComposedShadowTreeWalker walker = walkerFrom(start); walker.get(); direction == FocusDirectionForward ? walker.next() : walker.previous()) {
if (shouldVisit(walker.get(), event) && adjustedTabIndex(walker.get(), event) == tabIndex)
return walker.get();
}
return 0;
}
static Node* nextNodeWithGreaterTabIndex(Node* start, int tabIndex, KeyboardEvent* event)
{
int winningTabIndex = std::numeric_limits<short>::max() + 1;
Node* winner = 0;
for (ComposedShadowTreeWalker walker = walkerFrom(start); walker.get(); walker.next()) {
Node* node = walker.get();
if (shouldVisit(node, event) && node->tabIndex() > tabIndex && node->tabIndex() < winningTabIndex) {
winner = node;
winningTabIndex = node->tabIndex();
}
}
return winner;
}
static Node* previousNodeWithLowerTabIndex(Node* start, int tabIndex, KeyboardEvent* event)
{
int winningTabIndex = 0;
Node* winner = 0;
for (ComposedShadowTreeWalker walker = walkerFrom(start); walker.get(); walker.previous()) {
Node* node = walker.get();
int currentTabIndex = adjustedTabIndex(node, event);
if ((shouldVisit(node, event) || isNonFocusableShadowHost(node, event)) && currentTabIndex < tabIndex && currentTabIndex > winningTabIndex) {
winner = node;
winningTabIndex = currentTabIndex;
}
}
return winner;
}
Node* FocusController::nextFocusableNode(FocusScope scope, Node* start, KeyboardEvent* event)
{
if (start) {
int tabIndex = adjustedTabIndex(start, event);
if (tabIndex < 0) {
for (ComposedShadowTreeWalker walker = walkerFromNext(start); walker.get(); walker.next()) {
if (shouldVisit(walker.get(), event) && adjustedTabIndex(walker.get(), event) >= 0)
return walker.get();
}
}
if (Node* winner = findNodeWithExactTabIndex(nextNode(start), tabIndex, event, FocusDirectionForward))
return winner;
if (!tabIndex)
return 0;
}
if (Node* winner = nextNodeWithGreaterTabIndex(scope.rootNode(), start ? adjustedTabIndex(start, event) : 0, event))
return winner;
return findNodeWithExactTabIndex(scope.rootNode(), 0, event, FocusDirectionForward);
}
Node* FocusController::previousFocusableNode(FocusScope scope, Node* start, KeyboardEvent* event)
{
Node* last = 0;
for (ComposedShadowTreeWalker walker = walkerFrom(scope.rootNode()); walker.get(); walker.lastChild())
last = walker.get();
ASSERT(last);
Node* startingNode;
int startingTabIndex;
if (start) {
startingNode = previousNode(start);
startingTabIndex = adjustedTabIndex(start, event);
} else {
startingNode = last;
startingTabIndex = 0;
}
if (startingTabIndex < 0) {
for (ComposedShadowTreeWalker walker = walkerFrom(startingNode); walker.get(); walker.previous()) {
if (shouldVisit(walker.get(), event) && adjustedTabIndex(walker.get(), event) >= 0)
return walker.get();
}
}
if (Node* winner = findNodeWithExactTabIndex(startingNode, startingTabIndex, event, FocusDirectionBackward))
return winner;
startingTabIndex = (start && startingTabIndex) ? startingTabIndex : std::numeric_limits<short>::max();
return previousNodeWithLowerTabIndex(last, startingTabIndex, event);
}
static bool relinquishesEditingFocus(Node *node)
{
ASSERT(node);
ASSERT(node->rendererIsEditable());
Node* root = node->rootEditableElement();
Frame* frame = node->document()->frame();
if (!frame || !root)
return false;
return frame->editor()->shouldEndEditing(rangeOfContents(root).get());
}
static void clearSelectionIfNeeded(Frame* oldFocusedFrame, Frame* newFocusedFrame, Node* newFocusedNode)
{
if (!oldFocusedFrame || !newFocusedFrame)
return;
if (oldFocusedFrame->document() != newFocusedFrame->document())
return;
FrameSelection* s = oldFocusedFrame->selection();
if (s->isNone())
return;
bool caretBrowsing = oldFocusedFrame->settings()->caretBrowsingEnabled();
if (caretBrowsing)
return;
Node* selectionStartNode = s->selection().start().deprecatedNode();
if (selectionStartNode == newFocusedNode || selectionStartNode->isDescendantOf(newFocusedNode) || selectionStartNode->shadowAncestorNode() == newFocusedNode)
return;
if (Node* mousePressNode = newFocusedFrame->eventHandler()->mousePressNode()) {
if (mousePressNode->renderer() && !mousePressNode->canStartSelection()) {
Node * root = s->rootEditableElement();
if (!root)
return;
if (Node* shadowAncestorNode = root->shadowAncestorNode()) {
if (!shadowAncestorNode->hasTagName(inputTag) && !shadowAncestorNode->hasTagName(textareaTag))
return;
}
}
}
s->clear();
}
bool FocusController::setFocusedNode(Node* node, PassRefPtr<Frame> newFocusedFrame)
{
RefPtr<Frame> oldFocusedFrame = focusedFrame();
RefPtr<Document> oldDocument = oldFocusedFrame ? oldFocusedFrame->document() : 0;
Node* oldFocusedNode = oldDocument ? oldDocument->focusedNode() : 0;
if (oldFocusedNode == node)
return true;
if (oldFocusedNode && oldFocusedNode->rootEditableElement() == oldFocusedNode && !relinquishesEditingFocus(oldFocusedNode))
return false;
m_page->editorClient()->willSetInputMethodState();
clearSelectionIfNeeded(oldFocusedFrame.get(), newFocusedFrame.get(), node);
if (!node) {
if (oldDocument)
oldDocument->setFocusedNode(0);
m_page->editorClient()->setInputMethodState(false);
return true;
}
RefPtr<Document> newDocument = node->document();
if (newDocument && newDocument->focusedNode() == node) {
m_page->editorClient()->setInputMethodState(node->shouldUseInputMethod());
return true;
}
if (oldDocument && oldDocument != newDocument)
oldDocument->setFocusedNode(0);
setFocusedFrame(newFocusedFrame);
RefPtr<Node> protect = node;
if (newDocument) {
bool successfullyFocused = newDocument->setFocusedNode(node);
if (!successfullyFocused)
return false;
}
if (newDocument->focusedNode() == node)
m_page->editorClient()->setInputMethodState(node->shouldUseInputMethod());
return true;
}
void FocusController::setActive(bool active)
{
if (m_isActive == active)
return;
m_isActive = active;
if (FrameView* view = m_page->mainFrame()->view()) {
if (!view->platformWidget()) {
view->updateLayoutAndStyleIfNeededRecursive();
view->updateControlTints();
}
}
focusedOrMainFrame()->selection()->pageActivationChanged();
if (m_focusedFrame && isFocused())
dispatchEventsOnWindowAndFocusedNode(m_focusedFrame->document(), active);
}
static void contentAreaDidShowOrHide(ScrollableArea* scrollableArea, bool didShow)
{
if (didShow)
scrollableArea->contentAreaDidShow();
else
scrollableArea->contentAreaDidHide();
}
void FocusController::setContainingWindowIsVisible(bool containingWindowIsVisible)
{
if (m_containingWindowIsVisible == containingWindowIsVisible)
return;
m_containingWindowIsVisible = containingWindowIsVisible;
FrameView* view = m_page->mainFrame()->view();
if (!view)
return;
contentAreaDidShowOrHide(view, containingWindowIsVisible);
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 (HashSet<ScrollableArea*>::const_iterator it = scrollableAreas->begin(), end = scrollableAreas->end(); it != end; ++it) {
ScrollableArea* scrollableArea = *it;
ASSERT(scrollableArea->isOnActivePage());
contentAreaDidShowOrHide(scrollableArea, containingWindowIsVisible);
}
}
}
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), false, true);
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)
{
ASSERT(container);
Node* focusedNode = (focusedFrame() && focusedFrame()->document()) ? focusedFrame()->document()->focusedNode() : 0;
Node* node = container->firstChild();
FocusCandidate current;
current.rect = startingRect;
current.focusableNode = focusedNode;
current.visibleNode = focusedNode;
for (; node; node = (node->isFrameOwnerElement() || canScrollInDirection(node, direction)) ? node->traverseNextSibling(container) : node->traverseNextNode(container)) {
if (node == focusedNode)
continue;
if (!node->isElementNode())
continue;
if (!node->isKeyboardFocusable(event) && !node->isFrameOwnerElement() && !canScrollInDirection(node, direction))
continue;
FocusCandidate candidate = FocusCandidate(node, direction);
if (candidate.isNull())
continue;
candidate.enclosingScrollableBox = container;
updateFocusCandidateIfNeeded(direction, current, candidate, closest);
}
}
bool FocusController::advanceFocusDirectionallyInContainer(Node* container, const LayoutRect& startingRect, FocusDirection direction, KeyboardEvent* event)
{
if (!container || !container->document())
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;
Node* focusedNode = focusedOrMainFrame()->document()->focusedNode();
if (focusedNode && !hasOffscreenRect(focusedNode))
rect = nodeRectInAbsoluteCoordinates(focusedNode, 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;
Node* focusedNode = focusedOrMainFrame()->document()->focusedNode();
if (focusedNode && !hasOffscreenRect(focusedNode))
startingRect = nodeRectInAbsoluteCoordinates(focusedNode, true);
return advanceFocusDirectionallyInContainer(focusCandidate.visibleNode, startingRect, direction, event);
}
if (focusCandidate.isOffscreenAfterScrolling) {
Node* container = focusCandidate.enclosingScrollableBox;
scrollInDirection(container, direction);
return true;
}
Element* element = toElement(focusCandidate.focusableNode);
ASSERT(element);
element->focus(false);
return true;
}
bool FocusController::advanceFocusDirectionally(FocusDirection direction, KeyboardEvent* event)
{
Frame* curFrame = focusedOrMainFrame();
ASSERT(curFrame);
Document* focusedDocument = curFrame->document();
if (!focusedDocument)
return false;
Node* focusedNode = focusedDocument->focusedNode();
Node* container = focusedDocument;
if (container->isDocumentNode())
static_cast<Document*>(container)->updateLayoutIgnorePendingStylesheets();
LayoutRect startingRect;
if (focusedNode) {
if (!hasOffscreenRect(focusedNode)) {
container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, focusedNode);
startingRect = nodeRectInAbsoluteCoordinates(focusedNode, true );
} else if (focusedNode->hasTagName(areaTag)) {
HTMLAreaElement* area = static_cast<HTMLAreaElement*>(focusedNode);
container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, area->imageElement());
startingRect = virtualRectForAreaElementAndDirection(area, direction);
}
}
bool consumed = false;
do {
consumed = advanceFocusDirectionallyInContainer(container, startingRect, direction, event);
startingRect = nodeRectInAbsoluteCoordinates(container, true );
container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, container);
if (container && container->isDocumentNode())
static_cast<Document*>(container)->updateLayoutIgnorePendingStylesheets();
} while (!consumed && container);
return consumed;
}
}