diff options
Diffstat (limited to 'Source/WebCore/page/FocusController.cpp')
-rw-r--r-- | Source/WebCore/page/FocusController.cpp | 650 |
1 files changed, 434 insertions, 216 deletions
diff --git a/Source/WebCore/page/FocusController.cpp b/Source/WebCore/page/FocusController.cpp index a52b0d720..099cd02fd 100644 --- a/Source/WebCore/page/FocusController.cpp +++ b/Source/WebCore/page/FocusController.cpp @@ -11,10 +11,10 @@ * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR @@ -37,7 +37,6 @@ #include "Event.h" #include "EventHandler.h" #include "EventNames.h" -#include "ExceptionCode.h" #include "FrameSelection.h" #include "FrameTree.h" #include "FrameView.h" @@ -45,11 +44,12 @@ #include "HTMLImageElement.h" #include "HTMLInputElement.h" #include "HTMLNames.h" +#include "HTMLPlugInElement.h" +#include "HTMLSlotElement.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" @@ -60,56 +60,209 @@ #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) +static inline bool hasCustomFocusLogic(const Element& element) { - ASSERT(treeScope); + return is<HTMLElement>(element) && downcast<HTMLElement>(element).hasCustomFocusLogic(); } -ContainerNode* FocusNavigationScope::rootNode() const +static inline bool isFocusScopeOwner(const Element& element) { - return m_rootTreeScope->rootNode(); + if (element.shadowRoot() && !hasCustomFocusLogic(element)) + return true; + if (is<HTMLSlotElement>(element) && downcast<HTMLSlotElement>(element).assignedNodes()) { + ShadowRoot* root = element.containingShadowRoot(); + if (root && root->host() && !hasCustomFocusLogic(*root->host())) + return true; + } + return false; } -Element* FocusNavigationScope::owner() const +class FocusNavigationScope { +public: + Element* owner() const; + WEBCORE_EXPORT static FocusNavigationScope scopeOf(Node&); + static FocusNavigationScope scopeOwnedByScopeOwner(Element&); + static FocusNavigationScope scopeOwnedByIFrame(HTMLFrameOwnerElement&); + + Node* firstNodeInScope() const; + Node* lastNodeInScope() const; + Node* nextInScope(const Node*) const; + Node* previousInScope(const Node*) const; + Node* lastChildInScope(const Node&) const; + +private: + Node* firstChildInScope(const Node&) const; + + Node* parentInScope(const Node&) const; + + Node* nextSiblingInScope(const Node&) const; + Node* previousSiblingInScope(const Node&) const; + + explicit FocusNavigationScope(TreeScope&); + + explicit FocusNavigationScope(HTMLSlotElement&); + + TreeScope* m_rootTreeScope { nullptr }; + HTMLSlotElement* m_slotElement { nullptr }; +}; + +// FIXME: Focus navigation should work with shadow trees that have slots. +Node* FocusNavigationScope::firstChildInScope(const Node& node) const { - ContainerNode* root = rootNode(); - if (root->isShadowRoot()) - return toShadowRoot(root)->hostElement(); - if (Frame* frame = root->document().frame()) - return frame->ownerElement(); - return 0; + if (is<Element>(node) && isFocusScopeOwner(downcast<Element>(node))) + return nullptr; + return node.firstChild(); +} + +Node* FocusNavigationScope::lastChildInScope(const Node& node) const +{ + if (is<Element>(node) && isFocusScopeOwner(downcast<Element>(node))) + return nullptr; + return node.lastChild(); } -FocusNavigationScope FocusNavigationScope::focusNavigationScopeOf(Node* node) +Node* FocusNavigationScope::parentInScope(const Node& node) const +{ + if (m_rootTreeScope && &m_rootTreeScope->rootNode() == &node) + return nullptr; + + if (UNLIKELY(m_slotElement && m_slotElement == node.assignedSlot())) + return nullptr; + + return node.parentNode(); +} + +Node* FocusNavigationScope::nextSiblingInScope(const Node& node) const +{ + if (UNLIKELY(m_slotElement && m_slotElement == node.assignedSlot())) { + for (Node* current = node.nextSibling(); current; current = current->nextSibling()) { + if (current->assignedSlot() == m_slotElement) + return current; + } + return nullptr; + } + return node.nextSibling(); +} + +Node* FocusNavigationScope::previousSiblingInScope(const Node& node) const +{ + if (UNLIKELY(m_slotElement && m_slotElement == node.assignedSlot())) { + for (Node* current = node.previousSibling(); current; current = current->previousSibling()) { + if (current->assignedSlot() == m_slotElement) + return current; + } + return nullptr; + } + return node.previousSibling(); +} + +Node* FocusNavigationScope::firstNodeInScope() const +{ + if (UNLIKELY(m_slotElement)) { + auto* assigneNodes = m_slotElement->assignedNodes(); + ASSERT(assigneNodes); + return assigneNodes->first(); + } + ASSERT(m_rootTreeScope); + return &m_rootTreeScope->rootNode(); +} + +Node* FocusNavigationScope::lastNodeInScope() const +{ + if (UNLIKELY(m_slotElement)) { + auto* assigneNodes = m_slotElement->assignedNodes(); + ASSERT(assigneNodes); + return assigneNodes->last(); + } + ASSERT(m_rootTreeScope); + return &m_rootTreeScope->rootNode(); +} + +Node* FocusNavigationScope::nextInScope(const Node* node) const { ASSERT(node); - Node* root = node; - for (Node* n = node; n; n = NodeRenderingTraversal::parentInScope(n)) - root = n; - // The result is not always a ShadowRoot nor a DocumentNode since - // a starting node is in an orphaned tree in composed shadow tree. - return FocusNavigationScope(&root->treeScope()); + if (Node* next = firstChildInScope(*node)) + return next; + if (Node* next = nextSiblingInScope(*node)) + return next; + const Node* current = node; + while (current && !nextSiblingInScope(*current)) + current = parentInScope(*current); + return current ? nextSiblingInScope(*current) : nullptr; } -FocusNavigationScope FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(Node* node) +Node* FocusNavigationScope::previousInScope(const Node* node) const { ASSERT(node); - ASSERT(toElement(node)->shadowRoot()); - return FocusNavigationScope(toElement(node)->shadowRoot()); + if (Node* current = previousSiblingInScope(*node)) { + while (Node* child = lastChildInScope(*current)) + current = child; + return current; + } + return parentInScope(*node); +} + +FocusNavigationScope::FocusNavigationScope(TreeScope& treeScope) + : m_rootTreeScope(&treeScope) +{ +} + +FocusNavigationScope::FocusNavigationScope(HTMLSlotElement& slotElement) + : m_slotElement(&slotElement) +{ } -FocusNavigationScope FocusNavigationScope::focusNavigationScopeOwnedByIFrame(HTMLFrameOwnerElement* frame) +Element* FocusNavigationScope::owner() const { - ASSERT(frame); - ASSERT(frame->contentFrame()); - return FocusNavigationScope(frame->contentFrame()->document()); + if (m_slotElement) + return m_slotElement; + + ASSERT(m_rootTreeScope); + ContainerNode& root = m_rootTreeScope->rootNode(); + if (is<ShadowRoot>(root)) + return downcast<ShadowRoot>(root).host(); + if (Frame* frame = root.document().frame()) + return frame->ownerElement(); + return nullptr; +} + +FocusNavigationScope FocusNavigationScope::scopeOf(Node& startingNode) +{ + ASSERT(startingNode.isInTreeScope()); + Node* root = nullptr; + for (Node* currentNode = &startingNode; currentNode; currentNode = currentNode->parentNode()) { + root = currentNode; + if (HTMLSlotElement* slot = currentNode->assignedSlot()) { + if (isFocusScopeOwner(*slot)) + return FocusNavigationScope(*slot); + } + if (is<ShadowRoot>(currentNode)) + return FocusNavigationScope(downcast<ShadowRoot>(*currentNode)); + } + ASSERT(root); + return FocusNavigationScope(root->treeScope()); +} + +FocusNavigationScope FocusNavigationScope::scopeOwnedByScopeOwner(Element& element) +{ + ASSERT(element.shadowRoot() || is<HTMLSlotElement>(element)); + if (is<HTMLSlotElement>(element)) + return FocusNavigationScope(downcast<HTMLSlotElement>(element)); + return FocusNavigationScope(*element.shadowRoot()); +} + +FocusNavigationScope FocusNavigationScope::scopeOwnedByIFrame(HTMLFrameOwnerElement& frame) +{ + ASSERT(frame.contentFrame()); + ASSERT(frame.contentFrame()->document()); + return FocusNavigationScope(*frame.contentFrame()->document()); } static inline void dispatchEventsOnWindowAndFocusedElement(Document* document, bool focused) @@ -125,49 +278,45 @@ static inline void dispatchEventsOnWindowAndFocusedElement(Document* document, b } if (!focused && document->focusedElement()) - document->focusedElement()->dispatchBlurEvent(0); + document->focusedElement()->dispatchBlurEvent(nullptr); document->dispatchWindowEvent(Event::create(focused ? eventNames().focusEvent : eventNames().blurEvent, false, false)); if (focused && document->focusedElement()) - document->focusedElement()->dispatchFocusEvent(0, FocusDirectionNone); -} - -static inline bool hasCustomFocusLogic(Element& element) -{ - return element.isHTMLElement() && toHTMLElement(element).hasCustomFocusLogic(); + document->focusedElement()->dispatchFocusEvent(nullptr, FocusDirectionNone); } -static inline bool isNonFocusableShadowHost(Element& element, KeyboardEvent& event) +static inline bool isFocusableElementOrScopeOwner(Element& element, KeyboardEvent& event) { - return !element.isKeyboardFocusable(&event) && element.shadowRoot() && !hasCustomFocusLogic(element); + return element.isKeyboardFocusable(event) || isFocusScopeOwner(element); } -static inline bool isFocusableShadowHost(Node& node, KeyboardEvent& event) +static inline bool isNonFocusableScopeOwner(Element& element, KeyboardEvent& event) { - return node.isElementNode() && toElement(node).isKeyboardFocusable(&event) && toElement(node).shadowRoot() && !hasCustomFocusLogic(toElement(node)); + return !element.isKeyboardFocusable(event) && isFocusScopeOwner(element); } -static inline int adjustedTabIndex(Node& node, KeyboardEvent& event) +static inline bool isFocusableScopeOwner(Element& element, KeyboardEvent& event) { - if (!node.isElementNode()) - return 0; - return isNonFocusableShadowHost(toElement(node), event) ? 0 : toElement(node).tabIndex(); + return element.isKeyboardFocusable(event) && isFocusScopeOwner(element); } -static inline bool shouldVisit(Element& element, KeyboardEvent& event) +static inline int shadowAdjustedTabIndex(Element& element, KeyboardEvent& event) { - return element.isKeyboardFocusable(&event) || isNonFocusableShadowHost(element, event); + if (isNonFocusableScopeOwner(element, event)) { + if (!element.tabIndexSetExplicitly()) + return 0; // Treat a shadow host without tabindex if it has tabindex=0 even though HTMLElement::tabIndex returns -1 on such an element. + } + return element.tabIndex(); } -FocusController::FocusController(Page& page) +FocusController::FocusController(Page& page, ActivityState::Flags activityState) : m_page(page) - , m_isActive(false) - , m_isFocused(false) , m_isChangingFocusedFrame(false) - , m_contentIsVisible(false) + , m_activityState(activityState) + , m_focusRepaintTimer(*this, &FocusController::focusRepaintTimerFired) { } -void FocusController::setFocusedFrame(PassRefPtr<Frame> frame) +void FocusController::setFocusedFrame(Frame* frame) { ASSERT(!frame || frame->page() == &m_page); if (m_focusedFrame == frame || m_isChangingFocusedFrame) @@ -205,12 +354,12 @@ Frame& FocusController::focusedOrMainFrame() const void FocusController::setFocused(bool focused) { - if (isFocused() == focused) - return; - - m_isFocused = focused; + m_page.setActivityState(focused ? m_activityState | ActivityState::IsFocused : m_activityState & ~ActivityState::IsFocused); +} - if (!m_isFocused) +void FocusController::setFocusedInternal(bool focused) +{ + if (!isFocused()) focusedOrMainFrame().eventHandler().stopAutoscrollTimer(); if (!m_focusedFrame) @@ -222,16 +371,17 @@ void FocusController::setFocused(bool focused) } } -Element* FocusController::findFocusableElementDescendingDownIntoFrameDocument(FocusDirection direction, Element* element, KeyboardEvent* event) +Element* FocusController::findFocusableElementDescendingDownIntoFrameDocument(FocusDirection direction, Element* element, KeyboardEvent& event) { // The node we found might be a HTMLFrameOwnerElement, so descend down the tree until we find either: // 1) a focusable node, or // 2) the deepest-nested HTMLFrameOwnerElement. - while (element && element->isFrameOwnerElement()) { - HTMLFrameOwnerElement& owner = toHTMLFrameOwnerElement(*element); - if (!owner.contentFrame()) + while (is<HTMLFrameOwnerElement>(element)) { + HTMLFrameOwnerElement& owner = downcast<HTMLFrameOwnerElement>(*element); + if (!owner.contentFrame() || !owner.contentFrame()->document()) break; - Element* foundElement = findFocusableElement(direction, FocusNavigationScope::focusNavigationScopeOwnedByIFrame(&owner), 0, event); + owner.contentFrame()->document()->updateLayoutIgnorePendingStylesheets(); + Element* foundElement = findFocusableElementWithinScope(direction, FocusNavigationScope::scopeOwnedByIFrame(owner), nullptr, event); if (!foundElement) break; ASSERT(element != foundElement); @@ -240,9 +390,13 @@ Element* FocusController::findFocusableElementDescendingDownIntoFrameDocument(Fo return element; } -bool FocusController::setInitialFocus(FocusDirection direction, KeyboardEvent* event) +bool FocusController::setInitialFocus(FocusDirection direction, KeyboardEvent* providedEvent) { - bool didAdvanceFocus = advanceFocus(direction, event, true); + RefPtr<KeyboardEvent> event = providedEvent; + if (!event) + event = KeyboardEvent::createForDummy(); + + bool didAdvanceFocus = advanceFocus(direction, *event, true); // If focus is being set initially, accessibility needs to be informed that system focus has moved // into the web area again, even if focus did not change within WebCore. PostNotification is called instead @@ -253,7 +407,7 @@ bool FocusController::setInitialFocus(FocusDirection direction, KeyboardEvent* e return didAdvanceFocus; } -bool FocusController::advanceFocus(FocusDirection direction, KeyboardEvent* event, bool initialFocus) +bool FocusController::advanceFocus(FocusDirection direction, KeyboardEvent& event, bool initialFocus) { switch (direction) { case FocusDirectionForward: @@ -271,34 +425,33 @@ bool FocusController::advanceFocus(FocusDirection direction, KeyboardEvent* even return false; } -bool FocusController::advanceFocusInDocumentOrder(FocusDirection direction, KeyboardEvent* event, bool initialFocus) +bool FocusController::advanceFocusInDocumentOrder(FocusDirection direction, KeyboardEvent& event, bool initialFocus) { Frame& frame = focusedOrMainFrame(); Document* document = frame.document(); - Node* currentNode = document->focusedElement(); + Node* currentNode = document->focusNavigationStartingNode(direction); // FIXME: Not quite correct when it comes to focus transitions leaving/entering the WebView itself bool caretBrowsing = frame.settings().caretBrowsingEnabled(); if (caretBrowsing && !currentNode) - currentNode = frame.selection().start().deprecatedNode(); + currentNode = frame.selection().selection().start().deprecatedNode(); document->updateLayoutIgnorePendingStylesheets(); - RefPtr<Element> element = findFocusableElementAcrossFocusScope(direction, FocusNavigationScope::focusNavigationScopeOf(currentNode ? currentNode : document), currentNode, event); + RefPtr<Element> element = findFocusableElementAcrossFocusScope(direction, FocusNavigationScope::scopeOf(currentNode ? *currentNode : *document), currentNode, event); if (!element) { // We didn't find a node to focus, so we should try to pass focus to Chrome. if (!initialFocus && m_page.chrome().canTakeFocus(direction)) { - document->setFocusedElement(0); - setFocusedFrame(0); + document->setFocusedElement(nullptr); + setFocusedFrame(nullptr); m_page.chrome().takeFocus(direction); return true; } // Chrome doesn't want focus, so we should wrap focus. - element = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOf(m_page.mainFrame().document()), 0, event); - element = findFocusableElementDescendingDownIntoFrameDocument(direction, element.get(), event); + element = findFocusableElementAcrossFocusScope(direction, FocusNavigationScope::scopeOf(*m_page.mainFrame().document()), nullptr, event); if (!element) return false; @@ -311,10 +464,10 @@ bool FocusController::advanceFocusInDocumentOrder(FocusDirection direction, Keyb return true; } - if (element->isFrameOwnerElement() && (!element->isPluginElement() || !element->isKeyboardFocusable(event))) { + if (is<HTMLFrameOwnerElement>(*element) && (!is<HTMLPlugInElement>(*element) || !element->isKeyboardFocusable(event))) { // We focus frames rather than frame owners. // FIXME: We should not focus frames that have no scrollbars, as focusing them isn't useful to the user. - HTMLFrameOwnerElement& owner = toHTMLFrameOwnerElement(*element); + HTMLFrameOwnerElement& owner = downcast<HTMLFrameOwnerElement>(*element); if (!owner.contentFrame()) return false; @@ -339,114 +492,133 @@ bool FocusController::advanceFocusInDocumentOrder(FocusDirection direction, Keyb if (caretBrowsing) { Position position = firstPositionInOrBeforeNode(element.get()); VisibleSelection newSelection(position, position, DOWNSTREAM); - if (frame.selection().shouldChangeSelection(newSelection)) - frame.selection().setSelection(newSelection); + 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) +Element* FocusController::findFocusableElementAcrossFocusScope(FocusDirection direction, const FocusNavigationScope& scope, Node* currentNode, KeyboardEvent& event) { - ASSERT(!currentNode || !currentNode->isElementNode() || !isNonFocusableShadowHost(*toElement(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); + ASSERT(!is<Element>(currentNode) || !isNonFocusableScopeOwner(downcast<Element>(*currentNode), event)); + + if (currentNode && direction == FocusDirectionForward && is<Element>(currentNode) && isFocusableScopeOwner(downcast<Element>(*currentNode), event)) { + if (Element* candidateInInnerScope = findFocusableElementWithinScope(direction, FocusNavigationScope::scopeOwnedByScopeOwner(downcast<Element>(*currentNode)), 0, event)) + return candidateInInnerScope; + } + + if (Element* candidateInCurrentScope = findFocusableElementWithinScope(direction, scope, currentNode, event)) + return candidateInCurrentScope; // If there's no focusable node to advance to, move up the focus scopes until we find one. - 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); + Element* owner = scope.owner(); + while (owner) { + if (direction == FocusDirectionBackward && isFocusableScopeOwner(*owner, event)) + return findFocusableElementDescendingDownIntoFrameDocument(direction, owner, event); + + auto outerScope = FocusNavigationScope::scopeOf(*owner); + if (Element* candidateInOuterScope = findFocusableElementWithinScope(direction, outerScope, owner, event)) + return candidateInOuterScope; + owner = outerScope.owner(); } - found = findFocusableElementDescendingDownIntoFrameDocument(direction, found, event); - return found; + return nullptr; } -Element* FocusController::findFocusableElementRecursively(FocusDirection direction, FocusNavigationScope scope, Node* start, KeyboardEvent* event) +Element* FocusController::findFocusableElementWithinScope(FocusDirection direction, const FocusNavigationScope& scope, Node* start, KeyboardEvent& event) { // Starting node is exclusive. - Element* found = findFocusableElement(direction, scope, start, event); + Element* candidate = direction == FocusDirectionForward + ? nextFocusableElementWithinScope(scope, start, event) + : previousFocusableElementWithinScope(scope, start, event); + return findFocusableElementDescendingDownIntoFrameDocument(direction, candidate, event); +} + +Element* FocusController::nextFocusableElementWithinScope(const FocusNavigationScope& scope, Node* start, KeyboardEvent& event) +{ + Element* found = nextFocusableElementOrScopeOwner(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); + if (isNonFocusableScopeOwner(*found, event)) { + if (Element* foundInInnerFocusScope = nextFocusableElementWithinScope(FocusNavigationScope::scopeOwnedByScopeOwner(*found), 0, event)) + return foundInInnerFocusScope; + return nextFocusableElementWithinScope(scope, found, event); } - ASSERT(direction == FocusDirectionBackward); - if (isFocusableShadowHost(*found, *event)) { - Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event); - return foundInInnerFocusScope ? foundInInnerFocusScope : found; + return found; +} + +Element* FocusController::previousFocusableElementWithinScope(const FocusNavigationScope& scope, Node* start, KeyboardEvent& event) +{ + Element* found = previousFocusableElementOrScopeOwner(scope, start, event); + if (!found) + return nullptr; + if (isFocusableScopeOwner(*found, event)) { + // Search an inner focusable element in the shadow tree from the end. + if (Element* foundInInnerFocusScope = previousFocusableElementWithinScope(FocusNavigationScope::scopeOwnedByScopeOwner(*found), 0, event)) + return foundInInnerFocusScope; + return found; } - if (isNonFocusableShadowHost(*found, *event)) { - Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event); - return foundInInnerFocusScope ? foundInInnerFocusScope :findFocusableElementRecursively(direction, scope, found, event); + if (isNonFocusableScopeOwner(*found, event)) { + if (Element* foundInInnerFocusScope = previousFocusableElementWithinScope(FocusNavigationScope::scopeOwnedByScopeOwner(*found), 0, event)) + return foundInInnerFocusScope; + return previousFocusableElementWithinScope(scope, found, event); } return found; } -Element* FocusController::findFocusableElement(FocusDirection direction, FocusNavigationScope scope, Node* node, KeyboardEvent* event) +Element* FocusController::findFocusableElementOrScopeOwner(FocusDirection direction, const FocusNavigationScope& scope, Node* node, KeyboardEvent& event) { return (direction == FocusDirectionForward) - ? nextFocusableElement(scope, node, event) - : previousFocusableElement(scope, node, event); + ? nextFocusableElementOrScopeOwner(scope, node, event) + : previousFocusableElementOrScopeOwner(scope, node, event); } -Element* FocusController::findElementWithExactTabIndex(Node* start, int tabIndex, KeyboardEvent* event, FocusDirection direction) +Element* FocusController::findElementWithExactTabIndex(const FocusNavigationScope& scope, Node* start, int tabIndex, KeyboardEvent& event, FocusDirection direction) { // Search is inclusive of start - using namespace NodeRenderingTraversal; - for (Node* node = start; node; node = direction == FocusDirectionForward ? nextInScope(node) : previousInScope(node)) { - if (!node->isElementNode()) + for (Node* node = start; node; node = direction == FocusDirectionForward ? scope.nextInScope(node) : scope.previousInScope(node)) { + if (!is<Element>(*node)) continue; - Element& element = toElement(*node); - if (shouldVisit(element, *event) && adjustedTabIndex(element, *event) == tabIndex) + Element& element = downcast<Element>(*node); + if (isFocusableElementOrScopeOwner(element, event) && shadowAdjustedTabIndex(element, event) == tabIndex) return &element; } return nullptr; } -static Element* nextElementWithGreaterTabIndex(Node* start, int tabIndex, KeyboardEvent& event) +static Element* nextElementWithGreaterTabIndex(const FocusNavigationScope& scope, int tabIndex, KeyboardEvent& event) { // Search is inclusive of start - int winningTabIndex = std::numeric_limits<short>::max() + 1; + int winningTabIndex = std::numeric_limits<int>::max(); Element* winner = nullptr; - for (Node* node = start; node; node = NodeRenderingTraversal::nextInScope(node)) { - if (!node->isElementNode()) + for (Node* node = scope.firstNodeInScope(); node; node = scope.nextInScope(node)) { + if (!is<Element>(*node)) continue; - Element& element = toElement(*node); - if (shouldVisit(element, event) && element.tabIndex() > tabIndex && element.tabIndex() < winningTabIndex) { - winner = &element; - winningTabIndex = element.tabIndex(); + Element& candidate = downcast<Element>(*node); + int candidateTabIndex = candidate.tabIndex(); + if (isFocusableElementOrScopeOwner(candidate, event) && candidateTabIndex > tabIndex && (!winner || candidateTabIndex < winningTabIndex)) { + winner = &candidate; + winningTabIndex = candidateTabIndex; } } return winner; } -static Element* previousElementWithLowerTabIndex(Node* start, int tabIndex, KeyboardEvent& event) +static Element* previousElementWithLowerTabIndex(const FocusNavigationScope& scope, Node* start, int tabIndex, KeyboardEvent& event) { // Search is inclusive of start int winningTabIndex = 0; Element* winner = nullptr; - for (Node* node = start; node; node = NodeRenderingTraversal::previousInScope(node)) { - if (!node->isElementNode()) + for (Node* node = start; node; node = scope.previousInScope(node)) { + if (!is<Element>(*node)) continue; - Element& element = toElement(*node); - int currentTabIndex = adjustedTabIndex(element, event); - if ((shouldVisit(element, event) || isNonFocusableShadowHost(element, event)) && currentTabIndex < tabIndex && currentTabIndex > winningTabIndex) { + Element& element = downcast<Element>(*node); + int currentTabIndex = shadowAdjustedTabIndex(element, event); + if (isFocusableElementOrScopeOwner(element, event) && currentTabIndex < tabIndex && currentTabIndex > winningTabIndex) { winner = &element; winningTabIndex = currentTabIndex; } @@ -454,83 +626,94 @@ static Element* previousElementWithLowerTabIndex(Node* start, int tabIndex, Keyb return winner; } -Element* FocusController::nextFocusableElement(FocusNavigationScope scope, Node* start, KeyboardEvent* event) +Element* FocusController::nextFocusableElement(Node& start) +{ + // FIXME: This can return a non-focusable shadow host. + Ref<KeyboardEvent> keyEvent = KeyboardEvent::createForDummy(); + return nextFocusableElementOrScopeOwner(FocusNavigationScope::scopeOf(start), &start, keyEvent.get()); +} + +Element* FocusController::previousFocusableElement(Node& start) { - using namespace NodeRenderingTraversal; + // FIXME: This can return a non-focusable shadow host. + Ref<KeyboardEvent> keyEvent = KeyboardEvent::createForDummy(); + return previousFocusableElementOrScopeOwner(FocusNavigationScope::scopeOf(start), &start, keyEvent.get()); +} + +Element* FocusController::nextFocusableElementOrScopeOwner(const FocusNavigationScope& scope, Node* start, KeyboardEvent& event) +{ + int startTabIndex = 0; + if (start && is<Element>(*start)) + startTabIndex = shadowAdjustedTabIndex(downcast<Element>(*start), event); if (start) { - int tabIndex = adjustedTabIndex(*start, *event); // If a node is excluded from the normal tabbing cycle, the next focusable node is determined by tree order - if (tabIndex < 0) { - for (Node* node = nextInScope(start); node; node = nextInScope(node)) { - if (!node->isElementNode()) + if (startTabIndex < 0) { + for (Node* node = scope.nextInScope(start); node; node = scope.nextInScope(node)) { + if (!is<Element>(*node)) continue; - Element& element = toElement(*node); - if (shouldVisit(element, *event) && adjustedTabIndex(element, *event) >= 0) + Element& element = downcast<Element>(*node); + if (isFocusableElementOrScopeOwner(element, event) && shadowAdjustedTabIndex(element, event) >= 0) return &element; } } // First try to find a node with the same tabindex as start that comes after start in the scope. - if (Element* winner = findElementWithExactTabIndex(nextInScope(start), tabIndex, event, FocusDirectionForward)) + if (Element* winner = findElementWithExactTabIndex(scope, scope.nextInScope(start), startTabIndex, event, FocusDirectionForward)) return winner; - if (!tabIndex) - // We've reached the last node in the document with a tabindex of 0. This is the end of the tabbing order. - return 0; + if (!startTabIndex) + return nullptr; // We've reached the last node in the document with a tabindex of 0. This is the end of the tabbing order. } // Look for the first Element in the scope that: // 1) has the lowest tabindex that is higher than start's tabindex (or 0, if start is null), and // 2) comes first in the scope, if there's a tie. - if (Element* winner = nextElementWithGreaterTabIndex(scope.rootNode(), start ? adjustedTabIndex(*start, *event) : 0, *event)) + if (Element* winner = nextElementWithGreaterTabIndex(scope, startTabIndex, event)) return winner; // There are no nodes with a tabindex greater than start's tabindex, // so find the first node with a tabindex of 0. - return findElementWithExactTabIndex(scope.rootNode(), 0, event, FocusDirectionForward); + return findElementWithExactTabIndex(scope, scope.firstNodeInScope(), 0, event, FocusDirectionForward); } -Element* FocusController::previousFocusableElement(FocusNavigationScope scope, Node* start, KeyboardEvent* event) +Element* FocusController::previousFocusableElementOrScopeOwner(const FocusNavigationScope& scope, Node* start, KeyboardEvent& event) { - using namespace NodeRenderingTraversal; - Node* last = nullptr; - for (Node* node = scope.rootNode(); node; node = lastChildInScope(node)) + for (Node* node = scope.lastNodeInScope(); node; node = scope.lastChildInScope(*node)) last = node; ASSERT(last); // First try to find the last node in the scope that comes before start and has the same tabindex as start. // If start is null, find the last node in the scope with a tabindex of 0. Node* startingNode; - int startingTabIndex; + int startingTabIndex = 0; if (start) { - startingNode = previousInScope(start); - startingTabIndex = adjustedTabIndex(*start, *event); - } else { + startingNode = scope.previousInScope(start); + if (is<Element>(*start)) + startingTabIndex = shadowAdjustedTabIndex(downcast<Element>(*start), event); + } else startingNode = last; - startingTabIndex = 0; - } // However, if a node is excluded from the normal tabbing cycle, the previous focusable node is determined by tree order if (startingTabIndex < 0) { - for (Node* node = startingNode; node; node = previousInScope(node)) { - if (!node->isElementNode()) + for (Node* node = startingNode; node; node = scope.previousInScope(node)) { + if (!is<Element>(*node)) continue; - Element& element = toElement(*node); - if (shouldVisit(element, *event) && adjustedTabIndex(element, *event) >= 0) + Element& element = downcast<Element>(*node); + if (isFocusableElementOrScopeOwner(element, event) && shadowAdjustedTabIndex(element, event) >= 0) return &element; } } - if (Element* winner = findElementWithExactTabIndex(startingNode, startingTabIndex, event, FocusDirectionBackward)) + if (Element* winner = findElementWithExactTabIndex(scope, startingNode, startingTabIndex, event, FocusDirectionBackward)) return winner; // There are no nodes before start with the same tabindex as start, so look for a node that: // 1) has the highest non-zero tabindex (that is less than start's tabindex), and // 2) comes last in the scope, if there's a tie. - startingTabIndex = (start && startingTabIndex) ? startingTabIndex : std::numeric_limits<short>::max(); - return previousElementWithLowerTabIndex(last, startingTabIndex, *event); + startingTabIndex = (start && startingTabIndex) ? startingTabIndex : std::numeric_limits<int>::max(); + return previousElementWithLowerTabIndex(scope, last, startingTabIndex, event); } static bool relinquishesEditingFocus(Node *node) @@ -543,7 +726,7 @@ static bool relinquishesEditingFocus(Node *node) if (!frame || !root) return false; - return frame->editor().shouldEndEditing(rangeOfContents(*root).get()); + return frame->editor().shouldEndEditing(rangeOfContents(*root).ptr()); } static void clearSelectionIfNeeded(Frame* oldFocusedFrame, Frame* newFocusedFrame, Node* newFocusedNode) @@ -553,8 +736,8 @@ static void clearSelectionIfNeeded(Frame* oldFocusedFrame, Frame* newFocusedFram if (oldFocusedFrame->document() != newFocusedFrame->document()) return; - - FrameSelection& selection = oldFocusedFrame->selection(); + + const VisibleSelection& selection = oldFocusedFrame->selection().selection(); if (selection.isNone()) return; @@ -562,7 +745,7 @@ static void clearSelectionIfNeeded(Frame* oldFocusedFrame, Frame* newFocusedFram if (caretBrowsing) return; - Node* selectionStartNode = selection.selection().start().deprecatedNode(); + Node* selectionStartNode = selection.start().deprecatedNode(); if (selectionStartNode == newFocusedNode || selectionStartNode->isDescendantOf(newFocusedNode) || selectionStartNode->deprecatedShadowAncestorNode() == newFocusedNode) return; @@ -574,21 +757,22 @@ static void clearSelectionIfNeeded(Frame* oldFocusedFrame, Frame* newFocusedFram return; if (Node* shadowAncestorNode = root->deprecatedShadowAncestorNode()) { - if (!isHTMLInputElement(shadowAncestorNode) && !isHTMLTextAreaElement(shadowAncestorNode)) + if (!is<HTMLInputElement>(*shadowAncestorNode) && !is<HTMLTextAreaElement>(*shadowAncestorNode)) return; } } } - - selection.clear(); + + oldFocusedFrame->selection().clear(); } -bool FocusController::setFocusedElement(Element* element, PassRefPtr<Frame> newFocusedFrame, FocusDirection direction) +bool FocusController::setFocusedElement(Element* element, Frame& newFocusedFrame, FocusDirection direction) { + Ref<Frame> protectedNewFocusedFrame = newFocusedFrame; RefPtr<Frame> oldFocusedFrame = focusedFrame(); - RefPtr<Document> oldDocument = oldFocusedFrame ? oldFocusedFrame->document() : 0; + RefPtr<Document> oldDocument = oldFocusedFrame ? oldFocusedFrame->document() : nullptr; - Element* oldFocusedElement = oldDocument ? oldDocument->focusedElement() : 0; + Element* oldFocusedElement = oldDocument ? oldDocument->focusedElement() : nullptr; if (oldFocusedElement == element) return true; @@ -596,32 +780,32 @@ bool FocusController::setFocusedElement(Element* element, PassRefPtr<Frame> newF if (oldFocusedElement && oldFocusedElement->isRootEditableElement() && !relinquishesEditingFocus(oldFocusedElement)) return false; - m_page.editorClient()->willSetInputMethodState(); + m_page.editorClient().willSetInputMethodState(); - clearSelectionIfNeeded(oldFocusedFrame.get(), newFocusedFrame.get(), element); + clearSelectionIfNeeded(oldFocusedFrame.get(), &newFocusedFrame, element); if (!element) { if (oldDocument) - oldDocument->setFocusedElement(0); - m_page.editorClient()->setInputMethodState(false); + 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()); + m_page.editorClient().setInputMethodState(element->shouldUseInputMethod()); return true; } - if (oldDocument && oldDocument != &newDocument.get()) - oldDocument->setFocusedElement(0); + if (oldDocument && oldDocument != newDocument.ptr()) + oldDocument->setFocusedElement(nullptr); - if (newFocusedFrame && !newFocusedFrame->page()) { - setFocusedFrame(0); + if (!newFocusedFrame.page()) { + setFocusedFrame(nullptr); return false; } - setFocusedFrame(newFocusedFrame); + setFocusedFrame(&newFocusedFrame); Ref<Element> protect(*element); @@ -630,18 +814,35 @@ bool FocusController::setFocusedElement(Element* element, PassRefPtr<Frame> newF return false; if (newDocument->focusedElement() == element) - m_page.editorClient()->setInputMethodState(element->shouldUseInputMethod()); + m_page.editorClient().setInputMethodState(element->shouldUseInputMethod()); + + m_focusSetTime = monotonicallyIncreasingTime(); + m_focusRepaintTimer.stop(); return true; } -void FocusController::setActive(bool active) +void FocusController::setActivityState(ActivityState::Flags activityState) { - if (m_isActive == active) - return; + ActivityState::Flags changed = m_activityState ^ activityState; + m_activityState = activityState; + + if (changed & ActivityState::IsFocused) + setFocusedInternal(activityState & ActivityState::IsFocused); + if (changed & ActivityState::WindowIsActive) { + setActiveInternal(activityState & ActivityState::WindowIsActive); + if (changed & ActivityState::IsVisible) + setIsVisibleAndActiveInternal(activityState & ActivityState::WindowIsActive); + } +} - m_isActive = active; +void FocusController::setActive(bool active) +{ + m_page.setActivityState(active ? m_activityState | ActivityState::WindowIsActive : m_activityState & ~ActivityState::WindowIsActive); +} +void FocusController::setActiveInternal(bool active) +{ if (FrameView* view = m_page.mainFrame().view()) { if (!view->platformWidget()) { view->updateLayoutAndStyleIfNeededRecursive(); @@ -663,13 +864,8 @@ static void contentAreaDidShowOrHide(ScrollableArea* scrollableArea, bool didSho scrollableArea->contentAreaDidHide(); } -void FocusController::setContentIsVisible(bool contentIsVisible) +void FocusController::setIsVisibleAndActiveInternal(bool contentIsVisible) { - if (m_contentIsVisible == contentIsVisible) - return; - - m_contentIsVisible = contentIsVisible; - FrameView* view = m_page.mainFrame().view(); if (!view) return; @@ -685,8 +881,7 @@ void FocusController::setContentIsVisible(bool contentIsVisible) if (!scrollableAreas) continue; - for (HashSet<ScrollableArea*>::const_iterator it = scrollableAreas->begin(), end = scrollableAreas->end(); it != end; ++it) { - ScrollableArea* scrollableArea = *it; + for (auto& scrollableArea : *scrollableAreas) { ASSERT(scrollableArea->scrollbarsCanBeActive() || m_page.shouldSuppressScrollbarAnimations()); contentAreaDidShowOrHide(scrollableArea, contentIsVisible); @@ -724,7 +919,7 @@ static void updateFocusCandidateIfNeeded(FocusDirection direction, const FocusCa // If 2 nodes are intersecting, do hit test to find which node in on top. 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); + HitTestResult result = candidate.visibleNode->document().page()->mainFrame().eventHandler().hitTestResultAtPoint(IntPoint(x, y), HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::IgnoreClipping | HitTestRequest::DisallowUserAgentShadowContent); if (candidate.visibleNode->contains(result.innerNode())) { closest = candidate; return; @@ -743,9 +938,8 @@ static void updateFocusCandidateIfNeeded(FocusDirection direction, const FocusCa closest = candidate; } -void FocusController::findFocusCandidateInContainer(Node* container, const LayoutRect& startingRect, FocusDirection direction, KeyboardEvent* event, FocusCandidate& closest) +void FocusController::findFocusCandidateInContainer(Node& container, const LayoutRect& startingRect, FocusDirection direction, KeyboardEvent& event, FocusCandidate& closest) { - ASSERT(container); Node* focusedNode = (focusedFrame() && focusedFrame()->document()) ? focusedFrame()->document()->focusedElement() : 0; Element* element = ElementTraversal::firstWithin(container); @@ -756,8 +950,8 @@ void FocusController::findFocusCandidateInContainer(Node* container, const Layou unsigned candidateCount = 0; for (; element; element = (element->isFrameOwnerElement() || canScrollInDirection(element, direction)) - ? ElementTraversal::nextSkippingChildren(element, container) - : ElementTraversal::next(element, container)) { + ? ElementTraversal::nextSkippingChildren(*element, &container) + : ElementTraversal::next(*element, &container)) { if (element == focusedNode) continue; @@ -772,7 +966,7 @@ void FocusController::findFocusCandidateInContainer(Node* container, const Layou continue; candidateCount++; - candidate.enclosingScrollableBox = container; + candidate.enclosingScrollableBox = &container; updateFocusCandidateIfNeeded(direction, current, candidate, closest); } @@ -784,7 +978,7 @@ void FocusController::findFocusCandidateInContainer(Node* container, const Layou } } -bool FocusController::advanceFocusDirectionallyInContainer(Node* container, const LayoutRect& startingRect, FocusDirection direction, KeyboardEvent* event) +bool FocusController::advanceFocusDirectionallyInContainer(Node* container, const LayoutRect& startingRect, FocusDirection direction, KeyboardEvent& event) { if (!container) return false; @@ -796,7 +990,7 @@ bool FocusController::advanceFocusDirectionallyInContainer(Node* container, cons // Find the closest node within current container in the direction of the navigation. FocusCandidate focusCandidate; - findFocusCandidateInContainer(container, newStartingRect, direction, event, focusCandidate); + findFocusCandidateInContainer(*container, newStartingRect, direction, event, focusCandidate); if (focusCandidate.isNull()) { // Nothing to focus, scroll if possible. @@ -847,14 +1041,14 @@ bool FocusController::advanceFocusDirectionallyInContainer(Node* container, cons } // We found a new focus node, navigate to it. - Element* element = toElement(focusCandidate.focusableNode); + Element* element = downcast<Element>(focusCandidate.focusableNode); ASSERT(element); element->focus(false, direction); return true; } -bool FocusController::advanceFocusDirectionally(FocusDirection direction, KeyboardEvent* event) +bool FocusController::advanceFocusDirectionally(FocusDirection direction, KeyboardEvent& event) { Document* focusedDocument = focusedOrMainFrame().document(); if (!focusedDocument) @@ -863,8 +1057,8 @@ bool FocusController::advanceFocusDirectionally(FocusDirection direction, Keyboa Element* focusedElement = focusedDocument->focusedElement(); Node* container = focusedDocument; - if (container->isDocumentNode()) - toDocument(container)->updateLayoutIgnorePendingStylesheets(); + if (is<Document>(*container)) + downcast<Document>(*container).updateLayoutIgnorePendingStylesheets(); // Figure out the starting rect. LayoutRect startingRect; @@ -872,10 +1066,10 @@ bool FocusController::advanceFocusDirectionally(FocusDirection direction, Keyboa if (!hasOffscreenRect(focusedElement)) { container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, focusedElement); startingRect = nodeRectInAbsoluteCoordinates(focusedElement, true /* ignore border */); - } else if (isHTMLAreaElement(focusedElement)) { - HTMLAreaElement* area = toHTMLAreaElement(focusedElement); - container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, area->imageElement()); - startingRect = virtualRectForAreaElementAndDirection(area, direction); + } else if (is<HTMLAreaElement>(*focusedElement)) { + HTMLAreaElement& area = downcast<HTMLAreaElement>(*focusedElement); + container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, area.imageElement()); + startingRect = virtualRectForAreaElementAndDirection(&area, direction); } } @@ -887,11 +1081,35 @@ bool FocusController::advanceFocusDirectionally(FocusDirection direction, Keyboa consumed = advanceFocusDirectionallyInContainer(container, startingRect, direction, event); startingRect = nodeRectInAbsoluteCoordinates(container, true /* ignore border */); container = scrollableEnclosingBoxOrParentFrameForNodeInDirection(direction, container); - if (container && container->isDocumentNode()) - toDocument(container)->updateLayoutIgnorePendingStylesheets(); + 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; +} + } // namespace WebCore |