This source file includes following definitions.
- isShadowInsertionPointFocusScopeOwner
- rootNode
- owner
- focusNavigationScopeOf
- ownedByNonFocusableFocusScopeOwner
- ownedByShadowHost
- ownedByIFrame
- ownedByShadowInsertionPoint
- dispatchEventsOnWindowAndFocusedNode
- hasCustomFocusLogic
- isNonFocusableShadowHost
- isNonKeyboardFocusableShadowHost
- isKeyboardFocusableShadowHost
- isNonFocusableFocusScopeOwner
- adjustedTabIndex
- shouldVisit
- m_containingWindowIsVisible
- create
- setFocusedFrame
- focusedOrMainFrame
- setFocused
- findFocusableNodeDecendingDownIntoFrameDocument
- setInitialFocus
- advanceFocus
- advanceFocusInDocumentOrder
- findFocusableNodeAcrossFocusScope
- findFocusableNodeRecursively
- findFocusableNode
- findNodeWithExactTabIndex
- nextNodeWithGreaterTabIndex
- previousNodeWithLowerTabIndex
- nextFocusableNode
- previousFocusableNode
- relinquishesEditingFocus
- clearSelectionIfNeeded
- setFocusedElement
- setActive
- contentAreaDidShowOrHide
- setContainingWindowIsVisible
- updateFocusCandidateIfNeeded
- findFocusCandidateInContainer
- advanceFocusDirectionallyInContainer
- advanceFocusDirectionally
#include "config.h"
#include "core/page/FocusController.h"
#include <limits>
#include "HTMLNames.h"
#include "core/accessibility/AXObjectCache.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/NodeTraversal.h"
#include "core/dom/Range.h"
#include "core/dom/shadow/ElementShadow.h"
#include "core/dom/shadow/ShadowRoot.h"
#include "core/editing/Editor.h"
#include "core/editing/FrameSelection.h"
#include "core/editing/htmlediting.h"
#include "core/events/Event.h"
#include "core/frame/DOMWindow.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLAreaElement.h"
#include "core/html/HTMLImageElement.h"
#include "core/html/HTMLPlugInElement.h"
#include "core/html/HTMLShadowElement.h"
#include "core/page/Chrome.h"
#include "core/page/ChromeClient.h"
#include "core/page/EventHandler.h"
#include "core/page/FrameTree.h"
#include "core/page/Page.h"
#include "core/frame/Settings.h"
#include "core/page/SpatialNavigation.h"
#include "core/rendering/HitTestResult.h"
#include "core/rendering/RenderLayer.h"
namespace WebCore {
using namespace HTMLNames;
static inline bool isShadowInsertionPointFocusScopeOwner(Node& node)
{
return isActiveShadowInsertionPoint(node) && toHTMLShadowElement(node).olderShadowRoot();
}
FocusNavigationScope::FocusNavigationScope(TreeScope* treeScope)
: m_rootTreeScope(treeScope)
{
ASSERT(treeScope);
}
Node* FocusNavigationScope::rootNode() const
{
return &m_rootTreeScope->rootNode();
}
Element* FocusNavigationScope::owner() const
{
Node* root = rootNode();
if (root->isShadowRoot()) {
ShadowRoot* shadowRoot = toShadowRoot(root);
return shadowRoot->isYoungest() ? shadowRoot->host() : shadowRoot->shadowInsertionPointOfYoungerShadowRoot();
}
if (Frame* frame = root->document().frame())
return frame->ownerElement();
return 0;
}
FocusNavigationScope FocusNavigationScope::focusNavigationScopeOf(Node* node)
{
ASSERT(node);
Node* root = node;
for (Node* n = node; n; n = n->parentNode())
root = n;
return FocusNavigationScope(&root->treeScope());
}
FocusNavigationScope FocusNavigationScope::ownedByNonFocusableFocusScopeOwner(Node* node)
{
ASSERT(node);
if (isShadowHost(node))
return FocusNavigationScope::ownedByShadowHost(node);
ASSERT(isShadowInsertionPointFocusScopeOwner(*node));
return FocusNavigationScope::ownedByShadowInsertionPoint(toHTMLShadowElement(node));
}
FocusNavigationScope FocusNavigationScope::ownedByShadowHost(Node* node)
{
ASSERT(isShadowHost(node));
return FocusNavigationScope(toElement(node)->shadow()->youngestShadowRoot());
}
FocusNavigationScope FocusNavigationScope::ownedByIFrame(HTMLFrameOwnerElement* frame)
{
ASSERT(frame && frame->contentFrame());
return FocusNavigationScope(frame->contentFrame()->document());
}
FocusNavigationScope FocusNavigationScope::ownedByShadowInsertionPoint(HTMLShadowElement* shadowInsertionPoint)
{
ASSERT(isShadowInsertionPointFocusScopeOwner(*shadowInsertionPoint));
return FocusNavigationScope(shadowInsertionPoint->olderShadowRoot());
}
static inline void dispatchEventsOnWindowAndFocusedNode(Document* document, bool focused)
{
if (Page* page = document->page()) {
if (page->defersLoading())
return;
}
if (!focused && document->focusedElement()) {
RefPtr<Element> focusedElement(document->focusedElement());
focusedElement->setFocus(false);
focusedElement->dispatchBlurEvent(0);
if (focusedElement == document->focusedElement()) {
focusedElement->dispatchFocusOutEvent(EventTypeNames::focusout, 0);
if (focusedElement == document->focusedElement())
focusedElement->dispatchFocusOutEvent(EventTypeNames::DOMFocusOut, 0);
}
}
if (DOMWindow* window = document->domWindow())
window->dispatchEvent(Event::create(focused ? EventTypeNames::focus : EventTypeNames::blur));
if (focused && document->focusedElement()) {
RefPtr<Element> focusedElement(document->focusedElement());
focusedElement->setFocus(true);
focusedElement->dispatchFocusEvent(0, FocusTypePage);
if (focusedElement == document->focusedElement()) {
document->focusedElement()->dispatchFocusInEvent(EventTypeNames::focusin, 0);
if (focusedElement == document->focusedElement())
document->focusedElement()->dispatchFocusInEvent(EventTypeNames::DOMFocusIn, 0);
}
}
}
static inline bool hasCustomFocusLogic(Element* element)
{
return element->isHTMLElement() && toHTMLElement(element)->hasCustomFocusLogic();
}
#if !ASSERT_DISABLED
static inline bool isNonFocusableShadowHost(Node* node)
{
ASSERT(node);
if (!node->isElementNode())
return false;
Element* element = toElement(node);
return !element->isFocusable() && isShadowHost(element) && !hasCustomFocusLogic(element);
}
#endif
static inline bool isNonKeyboardFocusableShadowHost(Node* node)
{
ASSERT(node);
if (!node->isElementNode())
return false;
Element* element = toElement(node);
return !element->isKeyboardFocusable() && isShadowHost(element) && !hasCustomFocusLogic(element);
}
static inline bool isKeyboardFocusableShadowHost(Node* node)
{
ASSERT(node);
if (!node->isElementNode())
return false;
Element* element = toElement(node);
return element->isKeyboardFocusable() && isShadowHost(element) && !hasCustomFocusLogic(element);
}
static inline bool isNonFocusableFocusScopeOwner(Node* node)
{
ASSERT(node);
return isNonKeyboardFocusableShadowHost(node) || isShadowInsertionPointFocusScopeOwner(*node);
}
static inline int adjustedTabIndex(Node* node)
{
ASSERT(node);
return isNonFocusableFocusScopeOwner(node) ? 0 : node->tabIndex();
}
static inline bool shouldVisit(Node* node)
{
ASSERT(node);
return (node->isElementNode() && toElement(node)->isKeyboardFocusable()) || isNonFocusableFocusScopeOwner(node);
}
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<LocalFrame> oldFrame = (m_focusedFrame && m_focusedFrame->isLocalFrame()) ? toLocalFrame(m_focusedFrame.get()) : 0;
RefPtr<LocalFrame> newFrame = (frame && frame->isLocalFrame()) ? toLocalFrame(frame.get()) : 0;
m_focusedFrame = frame.get();
if (oldFrame && oldFrame->view()) {
oldFrame->selection().setFocused(false);
oldFrame->domWindow()->dispatchEvent(Event::create(EventTypeNames::blur));
}
if (newFrame && newFrame->view() && isFocused()) {
newFrame->selection().setFocused(true);
newFrame->domWindow()->dispatchEvent(Event::create(EventTypeNames::focus));
}
m_isChangingFocusedFrame = false;
m_page->chrome().client().focusedFrameChanged(newFrame.get());
}
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()->isLocalFrame())
toLocalFrame(focusedOrMainFrame())->eventHandler().stopAutoscroll();
if (!m_focusedFrame)
setFocusedFrame(m_page->mainFrame());
if (m_focusedFrame && m_focusedFrame->isLocalFrame() && toLocalFrame(m_focusedFrame.get())->view()) {
toLocalFrame(m_focusedFrame.get())->selection().setFocused(focused);
dispatchEventsOnWindowAndFocusedNode(toLocalFrame(m_focusedFrame.get())->document(), focused);
}
}
Node* FocusController::findFocusableNodeDecendingDownIntoFrameDocument(FocusType type, Node* node)
{
while (node && node->isFrameOwnerElement()) {
HTMLFrameOwnerElement* owner = toHTMLFrameOwnerElement(node);
if (!owner->contentFrame())
break;
Node* foundNode = findFocusableNode(type, FocusNavigationScope::ownedByIFrame(owner), 0);
if (!foundNode)
break;
ASSERT(node != foundNode);
node = foundNode;
}
return node;
}
bool FocusController::setInitialFocus(FocusType type)
{
bool didAdvanceFocus = advanceFocus(type, true);
if (AXObjectCache* cache = focusedOrMainFrame()->document()->existingAXObjectCache())
cache->postNotification(focusedOrMainFrame()->document(), AXObjectCache::AXFocusedUIElementChanged, true);
return didAdvanceFocus;
}
bool FocusController::advanceFocus(FocusType type, bool initialFocus)
{
switch (type) {
case FocusTypeForward:
case FocusTypeBackward:
return advanceFocusInDocumentOrder(type, initialFocus);
case FocusTypeLeft:
case FocusTypeRight:
case FocusTypeUp:
case FocusTypeDown:
return advanceFocusDirectionally(type);
default:
ASSERT_NOT_REACHED();
}
return false;
}
bool FocusController::advanceFocusInDocumentOrder(FocusType type, bool initialFocus)
{
if (!focusedOrMainFrame()->isLocalFrame())
return false;
LocalFrame* frame = toLocalFrame(focusedOrMainFrame());
ASSERT(frame);
Document* document = frame->document();
Node* currentNode = document->focusedElement();
bool caretBrowsing = frame->settings() && frame->settings()->caretBrowsingEnabled();
if (caretBrowsing && !currentNode)
currentNode = frame->selection().start().deprecatedNode();
document->updateLayoutIgnorePendingStylesheets();
RefPtr<Node> node = findFocusableNodeAcrossFocusScope(type, FocusNavigationScope::focusNavigationScopeOf(currentNode ? currentNode : document), currentNode);
if (!node) {
if (!initialFocus && m_page->chrome().canTakeFocus(type)) {
document->setFocusedElement(nullptr);
setFocusedFrame(nullptr);
m_page->chrome().takeFocus(type);
return true;
}
node = findFocusableNodeRecursively(type, FocusNavigationScope::focusNavigationScopeOf(m_page->mainFrame()->document()), 0);
node = findFocusableNodeDecendingDownIntoFrameDocument(type, node.get());
if (!node)
return false;
}
ASSERT(node);
if (node == document->focusedElement())
return true;
if (!node->isElementNode())
return false;
Element* element = toElement(node);
if (element->isFrameOwnerElement() && (!isHTMLPlugInElement(*element) || !element->isKeyboardFocusable())) {
HTMLFrameOwnerElement* owner = toHTMLFrameOwnerElement(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);
VisibleSelection newSelection(position, position, DOWNSTREAM);
frame->selection().setSelection(newSelection);
}
element->focus(false, type);
return true;
}
Node* FocusController::findFocusableNodeAcrossFocusScope(FocusType type, FocusNavigationScope scope, Node* currentNode)
{
ASSERT(!currentNode || !isNonFocusableShadowHost(currentNode));
Node* found;
if (currentNode && type == FocusTypeForward && isKeyboardFocusableShadowHost(currentNode)) {
Node* foundInInnerFocusScope = findFocusableNodeRecursively(type, FocusNavigationScope::ownedByShadowHost(currentNode), 0);
found = foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableNodeRecursively(type, scope, currentNode);
} else {
found = findFocusableNodeRecursively(type, scope, currentNode);
}
while (!found) {
Node* owner = scope.owner();
if (!owner)
break;
scope = FocusNavigationScope::focusNavigationScopeOf(owner);
if (type == FocusTypeBackward && isKeyboardFocusableShadowHost(owner)) {
found = owner;
break;
}
found = findFocusableNodeRecursively(type, scope, owner);
}
found = findFocusableNodeDecendingDownIntoFrameDocument(type, found);
return found;
}
Node* FocusController::findFocusableNodeRecursively(FocusType type, FocusNavigationScope scope, Node* start)
{
Node* found = findFocusableNode(type, scope, start);
if (!found)
return 0;
if (type == FocusTypeForward) {
if (!isNonFocusableFocusScopeOwner(found))
return found;
Node* foundInInnerFocusScope = findFocusableNodeRecursively(type, FocusNavigationScope::ownedByNonFocusableFocusScopeOwner(found), 0);
return foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableNodeRecursively(type, scope, found);
}
ASSERT(type == FocusTypeBackward);
if (isKeyboardFocusableShadowHost(found)) {
Node* foundInInnerFocusScope = findFocusableNodeRecursively(type, FocusNavigationScope::ownedByShadowHost(found), 0);
return foundInInnerFocusScope ? foundInInnerFocusScope : found;
}
if (isNonFocusableFocusScopeOwner(found)) {
Node* foundInInnerFocusScope = findFocusableNodeRecursively(type, FocusNavigationScope::ownedByNonFocusableFocusScopeOwner(found), 0);
return foundInInnerFocusScope ? foundInInnerFocusScope :findFocusableNodeRecursively(type, scope, found);
}
return found;
}
Node* FocusController::findFocusableNode(FocusType type, FocusNavigationScope scope, Node* node)
{
return type == FocusTypeForward ? nextFocusableNode(scope, node) : previousFocusableNode(scope, node);
}
Node* FocusController::findNodeWithExactTabIndex(Node* start, int tabIndex, FocusType type)
{
for (Node* node = start; node; node = type == FocusTypeForward ? NodeTraversal::next(*node) : NodeTraversal::previous(*node)) {
if (shouldVisit(node) && adjustedTabIndex(node) == tabIndex)
return node;
}
return 0;
}
static Node* nextNodeWithGreaterTabIndex(Node* start, int tabIndex)
{
int winningTabIndex = std::numeric_limits<short>::max() + 1;
Node* winner = 0;
for (Node* node = start; node; node = NodeTraversal::next(*node)) {
if (shouldVisit(node) && node->tabIndex() > tabIndex && node->tabIndex() < winningTabIndex) {
winner = node;
winningTabIndex = node->tabIndex();
}
}
return winner;
}
static Node* previousNodeWithLowerTabIndex(Node* start, int tabIndex)
{
int winningTabIndex = 0;
Node* winner = 0;
for (Node* node = start; node; node = NodeTraversal::previous(*node)) {
int currentTabIndex = adjustedTabIndex(node);
if ((shouldVisit(node) || isNonKeyboardFocusableShadowHost(node)) && currentTabIndex < tabIndex && currentTabIndex > winningTabIndex) {
winner = node;
winningTabIndex = currentTabIndex;
}
}
return winner;
}
Node* FocusController::nextFocusableNode(FocusNavigationScope scope, Node* start)
{
if (start) {
int tabIndex = adjustedTabIndex(start);
if (tabIndex < 0) {
for (Node* node = NodeTraversal::next(*start); node; node = NodeTraversal::next(*node)) {
if (shouldVisit(node) && adjustedTabIndex(node) >= 0)
return node;
}
}
if (Node* winner = findNodeWithExactTabIndex(NodeTraversal::next(*start), tabIndex, FocusTypeForward))
return winner;
if (!tabIndex)
return 0;
}
if (Node* winner = nextNodeWithGreaterTabIndex(scope.rootNode(), start ? adjustedTabIndex(start) : 0))
return winner;
return findNodeWithExactTabIndex(scope.rootNode(), 0, FocusTypeForward);
}
Node* FocusController::previousFocusableNode(FocusNavigationScope scope, Node* start)
{
Node* last = 0;
for (Node* node = scope.rootNode(); node; node = node->lastChild())
last = node;
ASSERT(last);
Node* startingNode;
int startingTabIndex;
if (start) {
startingNode = NodeTraversal::previous(*start);
startingTabIndex = adjustedTabIndex(start);
} else {
startingNode = last;
startingTabIndex = 0;
}
if (startingTabIndex < 0) {
for (Node* node = startingNode; node; node = NodeTraversal::previous(*node)) {
if (shouldVisit(node) && adjustedTabIndex(node) >= 0)
return node;
}
}
if (Node* winner = findNodeWithExactTabIndex(startingNode, startingTabIndex, FocusTypeBackward))
return winner;
startingTabIndex = (start && startingTabIndex) ? startingTabIndex : std::numeric_limits<short>::max();
return previousNodeWithLowerTabIndex(last, startingTabIndex);
}
static bool relinquishesEditingFocus(Node *node)
{
ASSERT(node);
ASSERT(node->rendererIsEditable());
return node->document().frame() && node->rootEditableElement();
}
static void clearSelectionIfNeeded(LocalFrame* oldFocusedFrame, LocalFrame* newFocusedFrame, Node* newFocusedNode)
{
if (!oldFocusedFrame || !newFocusedFrame)
return;
if (oldFocusedFrame->document() != newFocusedFrame->document())
return;
FrameSelection& selection = oldFocusedFrame->selection();
if (selection.isNone())
return;
bool caretBrowsing = oldFocusedFrame->settings()->caretBrowsingEnabled();
if (caretBrowsing)
return;
Node* selectionStartNode = selection.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 (!isHTMLInputElement(*shadowAncestorNode) && !isHTMLTextAreaElement(*shadowAncestorNode))
return;
}
}
}
selection.clear();
}
bool FocusController::setFocusedElement(Element* element, PassRefPtr<Frame> newFocusedFrame, FocusType type)
{
RefPtr<LocalFrame> oldFocusedFrame = toLocalFrame(focusedFrame());
RefPtr<Document> oldDocument = oldFocusedFrame ? oldFocusedFrame->document() : 0;
Element* oldFocusedElement = oldDocument ? oldDocument->focusedElement() : 0;
if (element && oldFocusedElement == element)
return true;
if (oldFocusedElement && oldFocusedElement->isRootEditableElement() && !relinquishesEditingFocus(oldFocusedElement))
return false;
m_page->chrome().client().willSetInputMethodState();
RefPtr<Document> newDocument;
if (element)
newDocument = &element->document();
else if (newFocusedFrame)
newDocument = newFocusedFrame->document();
if (newDocument && oldDocument == newDocument && newDocument->focusedElement() == element)
return true;
clearSelectionIfNeeded(oldFocusedFrame.get(), toLocalFrame(newFocusedFrame.get()), element);
if (oldDocument && oldDocument != newDocument)
oldDocument->setFocusedElement(nullptr);
if (newFocusedFrame && !newFocusedFrame->page()) {
setFocusedFrame(nullptr);
return false;
}
setFocusedFrame(newFocusedFrame);
RefPtr<Element> protect = element;
if (newDocument) {
bool successfullyFocused = newDocument->setFocusedElement(element, type);
if (!successfullyFocused)
return false;
}
return true;
}
void FocusController::setActive(bool active)
{
if (m_isActive == active)
return;
m_isActive = active;
if (FrameView* view = m_page->mainFrame()->view())
view->updateControlTints();
toLocalFrame(focusedOrMainFrame())->selection().pageActivationChanged();
}
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 (LocalFrame* 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->scrollbarsCanBeActive());
contentAreaDidShowOrHide(scrollableArea, containingWindowIsVisible);
}
}
}
static void updateFocusCandidateIfNeeded(FocusType type, 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(type, candidate))
return;
distanceDataForNode(type, 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::ConfusingAndOftenMisusedDisallowShadowContent);
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, FocusType type, FocusCandidate& closest)
{
Element* focusedElement = (focusedFrame() && focusedFrame()->document()) ? focusedFrame()->document()->focusedElement() : 0;
Element* element = ElementTraversal::firstWithin(container);
FocusCandidate current;
current.rect = startingRect;
current.focusableNode = focusedElement;
current.visibleNode = focusedElement;
for (; element; element = (element->isFrameOwnerElement() || canScrollInDirection(element, type))
? ElementTraversal::nextSkippingChildren(*element, &container)
: ElementTraversal::next(*element, &container)) {
if (element == focusedElement)
continue;
if (!element->isKeyboardFocusable() && !element->isFrameOwnerElement() && !canScrollInDirection(element, type))
continue;
FocusCandidate candidate = FocusCandidate(element, type);
if (candidate.isNull())
continue;
candidate.enclosingScrollableBox = &container;
updateFocusCandidateIfNeeded(type, current, candidate, closest);
}
}
bool FocusController::advanceFocusDirectionallyInContainer(Node* container, const LayoutRect& startingRect, FocusType type)
{
if (!container)
return false;
LayoutRect newStartingRect = startingRect;
if (startingRect.isEmpty())
newStartingRect = virtualRectForDirection(type, nodeRectInAbsoluteCoordinates(container));
FocusCandidate focusCandidate;
findFocusCandidateInContainer(*container, newStartingRect, type, focusCandidate);
if (focusCandidate.isNull()) {
return scrollInDirection(container, type);
}
if (HTMLFrameOwnerElement* frameElement = frameOwnerElement(focusCandidate)) {
ASSERT(frameElement->contentFrame());
if (focusCandidate.isOffscreenAfterScrolling) {
scrollInDirection(&focusCandidate.visibleNode->document(), type);
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, type)) {
return advanceFocusDirectionallyInContainer(container, nodeRectInAbsoluteCoordinates(focusCandidate.visibleNode, true), type);
}
return true;
}
if (canScrollInDirection(focusCandidate.visibleNode, type)) {
if (focusCandidate.isOffscreenAfterScrolling) {
scrollInDirection(focusCandidate.visibleNode, type);
return true;
}
LayoutRect startingRect;
Element* focusedElement = focusedOrMainFrame()->document()->focusedElement();
if (focusedElement && !hasOffscreenRect(focusedElement))
startingRect = nodeRectInAbsoluteCoordinates(focusedElement, true);
return advanceFocusDirectionallyInContainer(focusCandidate.visibleNode, startingRect, type);
}
if (focusCandidate.isOffscreenAfterScrolling) {
Node* container = focusCandidate.enclosingScrollableBox;
scrollInDirection(container, type);
return true;
}
Element* element = toElement(focusCandidate.focusableNode);
ASSERT(element);
element->focus(false, type);
return true;
}
bool FocusController::advanceFocusDirectionally(FocusType type)
{
if (!focusedOrMainFrame()->isLocalFrame())
return false;
LocalFrame* curFrame = toLocalFrame(focusedOrMainFrame());
ASSERT(curFrame);
Document* focusedDocument = curFrame->document();
if (!focusedDocument)
return false;
Element* focusedElement = focusedDocument->focusedElement();
Node* container = focusedDocument;
if (container->isDocumentNode())
toDocument(container)->updateLayoutIgnorePendingStylesheets();
LayoutRect startingRect;
if (focusedElement) {
if (!hasOffscreenRect(focusedElement)) {
container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(type, focusedElement);
startingRect = nodeRectInAbsoluteCoordinates(focusedElement, true );
} else if (isHTMLAreaElement(*focusedElement)) {
HTMLAreaElement& area = toHTMLAreaElement(*focusedElement);
container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(type, area.imageElement());
startingRect = virtualRectForAreaElementAndDirection(area, type);
}
}
bool consumed = false;
do {
consumed = advanceFocusDirectionallyInContainer(container, startingRect, type);
startingRect = nodeRectInAbsoluteCoordinates(container, true );
container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(type, container);
if (container && container->isDocumentNode())
toDocument(container)->updateLayoutIgnorePendingStylesheets();
} while (!consumed && container);
return consumed;
}
}