Changeset 150869 in webkit


Ignore:
Timestamp:
May 29, 2013 12:36:29 AM (11 years ago)
Author:
akling@apple.com
Message:

FocusController should operate on Elements internally.
<http://webkit.org/b/116781>

Reviewed by Antti Koivisto.

Switch from Node* to Element* as much as possible inside FocusController.
Mostly mechanical, gets rid of some unnecessary isElementNode() checks and casts.

  • page/FocusController.h:
  • page/FocusController.cpp:

(WebCore::dispatchEventsOnWindowAndFocusedElement):
(WebCore::isNonFocusableShadowHost):
(WebCore::adjustedTabIndex):
(WebCore::shouldVisit):
(WebCore::FocusController::setFocused):
(WebCore::FocusController::findFocusableElementDescendingDownIntoFrameDocument):
(WebCore::FocusController::advanceFocusInDocumentOrder):
(WebCore::FocusController::findFocusableElementAcrossFocusScope):
(WebCore::FocusController::findFocusableElementRecursively):
(WebCore::FocusController::findFocusableElement):
(WebCore::FocusController::nextFocusableElement):
(WebCore::FocusController::previousFocusableElement):
(WebCore::FocusController::setActive):

Location:
trunk/Source/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r150868 r150869  
     12013-05-28  Andreas Kling  <akling@apple.com>
     2
     3        FocusController should operate on Elements internally.
     4        <http://webkit.org/b/116781>
     5
     6        Reviewed by Antti Koivisto.
     7
     8        Switch from Node* to Element* as much as possible inside FocusController.
     9        Mostly mechanical, gets rid of some unnecessary isElementNode() checks and casts.
     10
     11        * page/FocusController.h:
     12        * page/FocusController.cpp:
     13        (WebCore::dispatchEventsOnWindowAndFocusedElement):
     14        (WebCore::isNonFocusableShadowHost):
     15        (WebCore::adjustedTabIndex):
     16        (WebCore::shouldVisit):
     17        (WebCore::FocusController::setFocused):
     18        (WebCore::FocusController::findFocusableElementDescendingDownIntoFrameDocument):
     19        (WebCore::FocusController::advanceFocusInDocumentOrder):
     20        (WebCore::FocusController::findFocusableElementAcrossFocusScope):
     21        (WebCore::FocusController::findFocusableElementRecursively):
     22        (WebCore::FocusController::findFocusableElement):
     23        (WebCore::FocusController::nextFocusableElement):
     24        (WebCore::FocusController::previousFocusableElement):
     25        (WebCore::FocusController::setActive):
     26
    1272013-05-29  Radu Stavila  <stavila@adobe.com>
    228
  • trunk/Source/WebCore/page/FocusController.cpp

    r150796 r150869  
    11/*
    2  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
     2 * Copyright (C) 2006, 2007, 2013 Apple Inc. All rights reserved.
    33 * Copyright (C) 2008 Nuanti Ltd.
    44 *
     
    111111}
    112112
    113 static inline void dispatchEventsOnWindowAndFocusedNode(Document* document, bool focused)
     113static inline void dispatchEventsOnWindowAndFocusedElement(Document* document, bool focused)
    114114{
    115115    // If we have a focused node we should dispatch blur on it before we blur the window.
     
    135135}
    136136
    137 static inline bool isNonFocusableShadowHost(Node* node, KeyboardEvent* event)
    138 {
    139     ASSERT(node);
    140     return node->isElementNode() && !toElement(node)->isKeyboardFocusable(event) && isShadowHost(node) && !hasCustomFocusLogic(node);
     137static inline bool isNonFocusableShadowHost(Element* element, KeyboardEvent* event)
     138{
     139    ASSERT(element);
     140    return !element->isKeyboardFocusable(event) && isShadowHost(element) && !hasCustomFocusLogic(element);
    141141}
    142142
     
    152152    if (!node->isElementNode())
    153153        return 0;
    154     return isNonFocusableShadowHost(node, event) ? 0 : toElement(node)->tabIndex();
    155 }
    156 
    157 static inline bool shouldVisit(Node* node, KeyboardEvent* event)
    158 {
    159     ASSERT(node);
    160     return (node->isElementNode() && toElement(node)->isKeyboardFocusable(event)) || isNonFocusableShadowHost(node, event);
     154    return isNonFocusableShadowHost(toElement(node), event) ? 0 : toElement(node)->tabIndex();
     155}
     156
     157static inline bool shouldVisit(Element* element, KeyboardEvent* event)
     158{
     159    ASSERT(element);
     160    return element->isKeyboardFocusable(event) || isNonFocusableShadowHost(element, event);
    161161}
    162162
     
    226226    if (m_focusedFrame->view()) {
    227227        m_focusedFrame->selection()->setFocused(focused);
    228         dispatchEventsOnWindowAndFocusedNode(m_focusedFrame->document(), focused);
    229     }
    230 }
    231 
    232 Node* FocusController::findFocusableNodeDecendingDownIntoFrameDocument(FocusDirection direction, Node* node, KeyboardEvent* event)
     228        dispatchEventsOnWindowAndFocusedElement(m_focusedFrame->document(), focused);
     229    }
     230}
     231
     232Element* FocusController::findFocusableElementDescendingDownIntoFrameDocument(FocusDirection direction, Element* element, KeyboardEvent* event)
    233233{
    234234    // The node we found might be a HTMLFrameOwnerElement, so descend down the tree until we find either:
    235235    // 1) a focusable node, or
    236236    // 2) the deepest-nested HTMLFrameOwnerElement.
    237     while (node && node->isFrameOwnerElement()) {
    238         HTMLFrameOwnerElement* owner = static_cast<HTMLFrameOwnerElement*>(node);
     237    while (element && element->isFrameOwnerElement()) {
     238        HTMLFrameOwnerElement* owner = toFrameOwnerElement(element);
    239239        if (!owner->contentFrame())
    240240            break;
    241         Node* foundNode = findFocusableNode(direction, FocusNavigationScope::focusNavigationScopeOwnedByIFrame(owner), 0, event);
    242         if (!foundNode)
     241        Element* foundElement = findFocusableElement(direction, FocusNavigationScope::focusNavigationScopeOwnedByIFrame(owner), 0, event);
     242        if (!foundElement)
    243243            break;
    244         ASSERT(node != foundNode);
    245         node = foundNode;
    246     }
    247     return node;
     244        ASSERT(element != foundElement);
     245        element = foundElement;
     246    }
     247    return element;
    248248}
    249249
     
    294294    document->updateLayoutIgnorePendingStylesheets();
    295295
    296     RefPtr<Node> node = findFocusableNodeAcrossFocusScope(direction, FocusNavigationScope::focusNavigationScopeOf(currentNode ? currentNode : document), currentNode, event);
    297 
    298     if (!node) {
     296    RefPtr<Element> element = findFocusableElementAcrossFocusScope(direction, FocusNavigationScope::focusNavigationScopeOf(currentNode ? currentNode : document), currentNode, event);
     297
     298    if (!element) {
    299299        // We didn't find a node to focus, so we should try to pass focus to Chrome.
    300300        if (!initialFocus && m_page->chrome().canTakeFocus(direction)) {
     
    306306
    307307        // Chrome doesn't want focus, so we should wrap focus.
    308         node = findFocusableNodeRecursively(direction, FocusNavigationScope::focusNavigationScopeOf(m_page->mainFrame()->document()), 0, event);
    309         node = findFocusableNodeDecendingDownIntoFrameDocument(direction, node.get(), event);
    310 
    311         if (!node)
     308        element = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOf(m_page->mainFrame()->document()), 0, event);
     309        element = findFocusableElementDescendingDownIntoFrameDocument(direction, element.get(), event);
     310
     311        if (!element)
    312312            return false;
    313313    }
    314314
    315     ASSERT(node);
    316 
    317     if (node == document->focusedElement())
    318         // Focus wrapped around to the same node.
     315    ASSERT(element);
     316
     317    if (element == document->focusedElement()) {
     318        // Focus wrapped around to the same Element.
    319319        return true;
    320 
    321     if (!node->isElementNode())
    322         // FIXME: May need a way to focus a document here.
    323         return false;
    324 
    325     Element* element = toElement(node.get());
     320    }
    326321
    327322    if (element->isFrameOwnerElement() && (!element->isPluginElement() || !element->isKeyboardFocusable(event))) {
    328323        // We focus frames rather than frame owners.
    329324        // FIXME: We should not focus frames that have no scrollbars, as focusing them isn't useful to the user.
    330         HTMLFrameOwnerElement* owner = toFrameOwnerElement(element);
     325        HTMLFrameOwnerElement* owner = toFrameOwnerElement(element.get());
    331326        if (!owner->contentFrame())
    332327            return false;
     
    341336    // their focus() methods.
    342337
    343     Document* newDocument = node->document();
    344 
    345     if (newDocument != document)
     338    Document* newDocument = element->document();
     339
     340    if (newDocument != document) {
    346341        // Focus is going away from this document, so clear the focused node.
    347342        document->setFocusedElement(0);
     343    }
    348344
    349345    if (newDocument)
     
    351347
    352348    if (caretBrowsing) {
    353         Position position = firstPositionInOrBeforeNode(node.get());
     349        Position position = firstPositionInOrBeforeNode(element.get());
    354350        VisibleSelection newSelection(position, position, DOWNSTREAM);
    355351        if (frame->selection()->shouldChangeSelection(newSelection))
     
    357353    }
    358354
    359     toElement(node.get())->focus(false, direction);
     355    element->focus(false, direction);
    360356    return true;
    361357}
    362358
    363 Node* FocusController::findFocusableNodeAcrossFocusScope(FocusDirection direction, FocusNavigationScope scope, Node* currentNode, KeyboardEvent* event)
    364 {
    365     ASSERT(!currentNode || !isNonFocusableShadowHost(currentNode, event));
    366     Node* found;
     359Element* FocusController::findFocusableElementAcrossFocusScope(FocusDirection direction, FocusNavigationScope scope, Node* currentNode, KeyboardEvent* event)
     360{
     361    ASSERT(!currentNode || !currentNode->isElementNode() || !isNonFocusableShadowHost(toElement(currentNode), event));
     362    Element* found;
    367363    if (currentNode && direction == FocusDirectionForward && isFocusableShadowHost(currentNode, event)) {
    368         Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(currentNode), 0, event);
    369         found = foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableNodeRecursively(direction, scope, currentNode, event);
     364        Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(currentNode), 0, event);
     365        found = foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableElementRecursively(direction, scope, currentNode, event);
    370366    } else
    371         found = findFocusableNodeRecursively(direction, scope, currentNode, event);
     367        found = findFocusableElementRecursively(direction, scope, currentNode, event);
    372368
    373369    // If there's no focusable node to advance to, move up the focus scopes until we find one.
    374370    while (!found) {
    375         Node* owner = scope.owner();
     371        Element* owner = scope.owner();
    376372        if (!owner)
    377373            break;
     
    381377            break;
    382378        }
    383         found = findFocusableNodeRecursively(direction, scope, owner, event);
    384     }
    385     found = findFocusableNodeDecendingDownIntoFrameDocument(direction, found, event);
     379        found = findFocusableElementRecursively(direction, scope, owner, event);
     380    }
     381    found = findFocusableElementDescendingDownIntoFrameDocument(direction, found, event);
    386382    return found;
    387383}
    388384
    389 Node* FocusController::findFocusableNodeRecursively(FocusDirection direction, FocusNavigationScope scope, Node* start, KeyboardEvent* event)
     385Element* FocusController::findFocusableElementRecursively(FocusDirection direction, FocusNavigationScope scope, Node* start, KeyboardEvent* event)
    390386{
    391387    // Starting node is exclusive.
    392     Node* found = findFocusableNode(direction, scope, start, event);
     388    Element* found = findFocusableElement(direction, scope, start, event);
    393389    if (!found)
    394390        return 0;
     
    396392        if (!isNonFocusableShadowHost(found, event))
    397393            return found;
    398         Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
    399         return foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableNodeRecursively(direction, scope, found, event);
     394        Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
     395        return foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableElementRecursively(direction, scope, found, event);
    400396    }
    401397    ASSERT(direction == FocusDirectionBackward);
    402398    if (isFocusableShadowHost(found, event)) {
    403         Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
     399        Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
    404400        return foundInInnerFocusScope ? foundInInnerFocusScope : found;
    405401    }
    406402    if (isNonFocusableShadowHost(found, event)) {
    407         Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
    408         return foundInInnerFocusScope ? foundInInnerFocusScope :findFocusableNodeRecursively(direction, scope, found, event);
     403        Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);
     404        return foundInInnerFocusScope ? foundInInnerFocusScope :findFocusableElementRecursively(direction, scope, found, event);
    409405    }
    410406    return found;
    411407}
    412408
    413 Node* FocusController::findFocusableNode(FocusDirection direction, FocusNavigationScope scope, Node* node, KeyboardEvent* event)
     409Element* FocusController::findFocusableElement(FocusDirection direction, FocusNavigationScope scope, Node* node, KeyboardEvent* event)
    414410{
    415411    return (direction == FocusDirectionForward)
    416         ? nextFocusableNode(scope, node, event)
    417         : previousFocusableNode(scope, node, event);
     412        ? nextFocusableElement(scope, node, event)
     413        : previousFocusableElement(scope, node, event);
    418414}
    419415
     
    468464}
    469465
    470 Node* FocusController::nextFocusableNode(FocusNavigationScope scope, Node* start, KeyboardEvent* event)
     466Element* FocusController::nextFocusableElement(FocusNavigationScope scope, Node* start, KeyboardEvent* event)
    471467{
    472468    using namespace NodeRenderingTraversal;
     
    477473        if (tabIndex < 0) {
    478474            for (Node* node = nextInScope(start); node; node = nextInScope(node)) {
    479                 if (shouldVisit(node, event) && adjustedTabIndex(node, event) >= 0)
    480                     return node;
     475                if (!node->isElementNode())
     476                    continue;
     477                Element* element = toElement(node);
     478                if (shouldVisit(element, event) && adjustedTabIndex(element, event) >= 0)
     479                    return element;
    481480            }
    482481        }
     
    502501}
    503502
    504 Node* FocusController::previousFocusableNode(FocusNavigationScope scope, Node* start, KeyboardEvent* event)
     503Element* FocusController::previousFocusableElement(FocusNavigationScope scope, Node* start, KeyboardEvent* event)
    505504{
    506505    using namespace NodeRenderingTraversal;
     
    526525    if (startingTabIndex < 0) {
    527526        for (Node* node = startingNode; node; node = previousInScope(node)) {
    528             if (shouldVisit(node, event) && adjustedTabIndex(node, event) >= 0)
    529                 return node;
     527            if (!node->isElementNode())
     528                continue;
     529            Element* element = toElement(node);
     530            if (shouldVisit(element, event) && adjustedTabIndex(element, event) >= 0)
     531                return element;
    530532        }
    531533    }
     
    661663   
    662664    if (m_focusedFrame && isFocused())
    663         dispatchEventsOnWindowAndFocusedNode(m_focusedFrame->document(), active);
     665        dispatchEventsOnWindowAndFocusedElement(m_focusedFrame->document(), active);
    664666}
    665667
  • trunk/Source/WebCore/page/FocusController.h

    r150713 r150869  
    8989    bool advanceFocusInDocumentOrder(FocusDirection, KeyboardEvent*, bool initialFocus);
    9090
    91     Node* findFocusableNodeAcrossFocusScope(FocusDirection, FocusNavigationScope startScope, Node* start, KeyboardEvent*);
    92     Node* findFocusableNodeRecursively(FocusDirection, FocusNavigationScope, Node* start, KeyboardEvent*);
    93     Node* findFocusableNodeDecendingDownIntoFrameDocument(FocusDirection, Node*, KeyboardEvent*);
     91    Element* findFocusableElementAcrossFocusScope(FocusDirection, FocusNavigationScope startScope, Node* start, KeyboardEvent*);
     92    Element* findFocusableElementRecursively(FocusDirection, FocusNavigationScope, Node* start, KeyboardEvent*);
     93    Element* findFocusableElementDescendingDownIntoFrameDocument(FocusDirection, Element*, KeyboardEvent*);
    9494
    9595    // Searches through the given tree scope, starting from start node, for the next/previous selectable element that comes after/before start node.
     
    102102    //
    103103    // See http://www.w3.org/TR/html4/interact/forms.html#h-17.11.1
    104     inline Node* findFocusableNode(FocusDirection, FocusNavigationScope, Node* start, KeyboardEvent*);
     104    Element* findFocusableElement(FocusDirection, FocusNavigationScope, Node* start, KeyboardEvent*);
    105105
    106     Node* nextFocusableNode(FocusNavigationScope, Node* start, KeyboardEvent*);
    107     Node* previousFocusableNode(FocusNavigationScope, Node* start, KeyboardEvent*);
     106    Element* nextFocusableElement(FocusNavigationScope, Node* start, KeyboardEvent*);
     107    Element* previousFocusableElement(FocusNavigationScope, Node* start, KeyboardEvent*);
    108108
    109109    Element* findElementWithExactTabIndex(Node* start, int tabIndex, KeyboardEvent*, FocusDirection);
Note: See TracChangeset for help on using the changeset viewer.