| Differences between
and this patch
- Source/WebCore/ChangeLog +36 lines
Lines 1-3 Source/WebCore/ChangeLog_sec1
1
2016-05-05  Ryosuke Niwa  <rniwa@webkit.org>
2
3
        Refactor FocusController::findFocusableElementRecursively
4
        https://bugs.webkit.org/show_bug.cgi?id=157415
5
6
        Reviewed by NOBODY (OOPS!).
7
8
        Refactor FocusController::findFocusableElementRecursively and related functions. Extracted two functions:
9
        nextFocusableElementWithinScope and previousFocusableElementWithinScope out of it since they didn't really share
10
        any code other than calling findFocusableElement at the beginning.
11
12
        Also renamed internal variant of nextFocusableElement and previousFocusableElement to nextFocusableElementOrScopeOwner
13
        and previousFocusableElementOrScopeOwner. It was confusing to have these internal functions in addition to public
14
        member functions that are used in Objective-C DOM API.
15
16
        No new tests are added since there should be no behavioral change.
17
18
        * page/FocusController.cpp:
19
        (WebCore::FocusController::findFocusableElementDescendingDownIntoFrameDocument): Added a FIXME.
20
        (WebCore::FocusController::advanceFocusInDocumentOrder): Use findFocusableElementAcrossFocusScope instead of manually
21
        calling findFocusableElementRecursively and findFocusableElementDescendingDownIntoFrameDocument separately.
22
        (WebCore::FocusController::findFocusableElementAcrossFocusScope): Now that findFocusableElementWithinScope calls
23
        findFocusableElementDescendingDownIntoFrameDocument internally, there is no need to keep around "found" local variable.
24
        Introduce a few early exists for a better clarity.
25
        (WebCore::FocusController::findFocusableElementWithinScope): Renamed from findFocusableElementRecursively. Also call
26
        findFocusableElementDescendingDownIntoFrameDocument here instead of findFocusableElementAcrossFocusScope.
27
        (WebCore::FocusController::nextFocusableElementWithinScope): Extracted from findFocusableElementRecursively.
28
        (WebCore::FocusController::previousFocusableElementWithinScope): Ditto.
29
        (WebCore::FocusController::findFocusableElement):
30
        (WebCore::FocusController::nextFocusableElement): Added a FIXME.
31
        (WebCore::FocusController::previousFocusableElement): Ditto.
32
        (WebCore::FocusController::findFocusableElementOrScopeOwner): Renamed from findFocusableElement for clarity.
33
        (WebCore::FocusController::nextFocusableElementOrScopeOwner): Ditto from nextFocusableElement.
34
        (WebCore::FocusController::previousFocusableElementOrScopeOwner): Ditto from nextFocusableElement.
35
        * page/FocusController.h:
36
1
2016-05-05  David Kilzer  <ddkilzer@apple.com>
37
2016-05-05  David Kilzer  <ddkilzer@apple.com>
2
38
3
        [iOS] WebCore does not need to link to CoreAudio, GraphicsServices, SystemConfiguration frameworks
39
        [iOS] WebCore does not need to link to CoreAudio, GraphicsServices, SystemConfiguration frameworks
- Source/WebCore/page/FocusController.cpp -37 / +56 lines
Lines 300-306 Element* FocusController::findFocusableE Source/WebCore/page/FocusController.cpp_sec1
300
        HTMLFrameOwnerElement& owner = downcast<HTMLFrameOwnerElement>(*element);
300
        HTMLFrameOwnerElement& owner = downcast<HTMLFrameOwnerElement>(*element);
301
        if (!owner.contentFrame())
301
        if (!owner.contentFrame())
302
            break;
302
            break;
303
        Element* foundElement = findFocusableElement(direction, FocusNavigationScope::scopeOwnedByIFrame(owner), 0, event);
303
        // FIXME: This can return a non-focusable shadow root.
304
        Element* foundElement = findFocusableElementOrScopeOwner(direction, FocusNavigationScope::scopeOwnedByIFrame(owner), 0, event);
304
        if (!foundElement)
305
        if (!foundElement)
305
            break;
306
            break;
306
        ASSERT(element != foundElement);
307
        ASSERT(element != foundElement);
Lines 366-373 bool FocusController::advanceFocusInDocu Source/WebCore/page/FocusController.cpp_sec2
366
        }
367
        }
367
368
368
        // Chrome doesn't want focus, so we should wrap focus.
369
        // Chrome doesn't want focus, so we should wrap focus.
369
        element = findFocusableElementRecursively(direction, FocusNavigationScope::scopeOf(*m_page.mainFrame().document()), 0, event);
370
        element = findFocusableElementAcrossFocusScope(direction, FocusNavigationScope::scopeOf(*m_page.mainFrame().document()), nullptr, event);
370
        element = findFocusableElementDescendingDownIntoFrameDocument(direction, element.get(), event);
371
371
372
        if (!element)
372
        if (!element)
373
            return false;
373
            return false;
Lines 421-477 bool FocusController::advanceFocusInDocu Source/WebCore/page/FocusController.cpp_sec3
421
Element* FocusController::findFocusableElementAcrossFocusScope(FocusDirection direction, const FocusNavigationScope& scope, Node* currentNode, KeyboardEvent* event)
421
Element* FocusController::findFocusableElementAcrossFocusScope(FocusDirection direction, const FocusNavigationScope& scope, Node* currentNode, KeyboardEvent* event)
422
{
422
{
423
    ASSERT(!is<Element>(currentNode) || !isNonFocusableShadowHost(*downcast<Element>(currentNode), *event));
423
    ASSERT(!is<Element>(currentNode) || !isNonFocusableShadowHost(*downcast<Element>(currentNode), *event));
424
    Element* found;
424
425
    if (currentNode && direction == FocusDirectionForward && isFocusableShadowHost(*currentNode, *event)) {
425
    if (currentNode && direction == FocusDirectionForward && isFocusableShadowHost(*currentNode, *event)) {
426
        Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::scopeOwnedByShadowHost(downcast<Element>(*currentNode)), 0, event);
426
        if (Element* candidateInInnerScope = findFocusableElementWithinScope(direction, FocusNavigationScope::scopeOwnedByShadowHost(downcast<Element>(*currentNode)), 0, event))
427
        found = foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableElementRecursively(direction, scope, currentNode, event);
427
            return candidateInInnerScope;
428
    } else
428
    }
429
        found = findFocusableElementRecursively(direction, scope, currentNode, event);
429
430
    if (Element* candidateInCurrentScope = findFocusableElementWithinScope(direction, scope, currentNode, event))
431
        return candidateInCurrentScope;
430
432
431
    // If there's no focusable node to advance to, move up the focus scopes until we find one.
433
    // If there's no focusable node to advance to, move up the focus scopes until we find one.
432
    Element* owner = scope.owner();
434
    Element* owner = scope.owner();
433
    while (!found && owner) {
435
    while (owner) {
434
        FocusNavigationScope currentScope = FocusNavigationScope::scopeOf(*owner);
436
        FocusNavigationScope outerScope = FocusNavigationScope::scopeOf(*owner);
435
        if (direction == FocusDirectionBackward && isFocusableShadowHost(*owner, *event)) {
437
        if (direction == FocusDirectionBackward && isFocusableShadowHost(*owner, *event))
436
            found = owner;
438
            return findFocusableElementDescendingDownIntoFrameDocument(direction, owner, event);
437
            break;
439
        if (Element* candidateInOuterScope = findFocusableElementWithinScope(direction, outerScope, owner, event))
438
        }
440
            return candidateInOuterScope;
439
        found = findFocusableElementRecursively(direction, currentScope, owner, event);
441
        owner = outerScope.owner();
440
        owner = currentScope.owner();
441
    }
442
    }
442
    found = findFocusableElementDescendingDownIntoFrameDocument(direction, found, event);
443
    return nullptr;
443
    return found;
444
}
444
}
445
445
446
Element* FocusController::findFocusableElementRecursively(FocusDirection direction, const FocusNavigationScope& scope, Node* start, KeyboardEvent* event)
446
Element* FocusController::findFocusableElementWithinScope(FocusDirection direction, const FocusNavigationScope& scope, Node* start, KeyboardEvent* event)
447
{
447
{
448
    // Starting node is exclusive.
448
    // Starting node is exclusive.
449
    Element* found = findFocusableElement(direction, scope, start, event);
449
    Element* candidate = direction == FocusDirectionForward
450
        ? nextFocusableElementWithinScope(scope, start, event)
451
        : previousFocusableElementWithinScope(scope, start, event);
452
    return findFocusableElementDescendingDownIntoFrameDocument(direction, candidate, event);
453
}
454
455
Element* FocusController::nextFocusableElementWithinScope(const FocusNavigationScope& scope, Node* start, KeyboardEvent* event)
456
{
457
    Element* found = nextFocusableElementOrScopeOwner(scope, start, event);
450
    if (!found)
458
    if (!found)
451
        return nullptr;
459
        return nullptr;
452
    if (direction == FocusDirectionForward) {
460
    if (isNonFocusableShadowHost(*found, *event)) {
453
        if (!isNonFocusableShadowHost(*found, *event))
461
        if (Element* foundInInnerFocusScope = nextFocusableElementWithinScope(FocusNavigationScope::scopeOwnedByShadowHost(*found), 0, event))
454
            return found;
462
            return foundInInnerFocusScope;
455
        Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::scopeOwnedByShadowHost(*found), 0, event);
463
        return nextFocusableElementWithinScope(scope, found, event);
456
        return foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableElementRecursively(direction, scope, found, event);
457
    }
464
    }
458
    ASSERT(direction == FocusDirectionBackward);
465
    return found;
466
}
467
468
Element* FocusController::previousFocusableElementWithinScope(const FocusNavigationScope& scope, Node* start, KeyboardEvent* event)
469
{
470
    Element* found = previousFocusableElementOrScopeOwner(scope, start, event);
471
    if (!found)
472
        return nullptr;
459
    if (isFocusableShadowHost(*found, *event)) {
473
    if (isFocusableShadowHost(*found, *event)) {
460
        Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::scopeOwnedByShadowHost(*found), 0, event);
474
        // Search an inner focusable element in the shadow tree from the end.
461
        return foundInInnerFocusScope ? foundInInnerFocusScope : found;
475
        if (Element* foundInInnerFocusScope = previousFocusableElementWithinScope(FocusNavigationScope::scopeOwnedByShadowHost(*found), 0, event))
476
            return foundInInnerFocusScope;
477
        return found;
462
    }
478
    }
463
    if (isNonFocusableShadowHost(*found, *event)) {
479
    if (isNonFocusableShadowHost(*found, *event)) {
464
        Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::scopeOwnedByShadowHost(*found), 0, event);
480
        if (Element* foundInInnerFocusScope = previousFocusableElementWithinScope(FocusNavigationScope::scopeOwnedByShadowHost(*found), 0, event))
465
        return foundInInnerFocusScope ? foundInInnerFocusScope :findFocusableElementRecursively(direction, scope, found, event);
481
            return foundInInnerFocusScope;
482
        return previousFocusableElementWithinScope(scope, found, event);
466
    }
483
    }
467
    return found;
484
    return found;
468
}
485
}
469
486
470
Element* FocusController::findFocusableElement(FocusDirection direction, const FocusNavigationScope& scope, Node* node, KeyboardEvent* event)
487
Element* FocusController::findFocusableElementOrScopeOwner(FocusDirection direction, const FocusNavigationScope& scope, Node* node, KeyboardEvent* event)
471
{
488
{
472
    return (direction == FocusDirectionForward)
489
    return (direction == FocusDirectionForward)
473
        ? nextFocusableElement(scope, node, event)
490
        ? nextFocusableElementOrScopeOwner(scope, node, event)
474
        : previousFocusableElement(scope, node, event);
491
        : previousFocusableElementOrScopeOwner(scope, node, event);
475
}
492
}
476
493
477
Element* FocusController::findElementWithExactTabIndex(const FocusNavigationScope& scope, Node* start, int tabIndex, KeyboardEvent* event, FocusDirection direction)
494
Element* FocusController::findElementWithExactTabIndex(const FocusNavigationScope& scope, Node* start, int tabIndex, KeyboardEvent* event, FocusDirection direction)
Lines 528-544 static Element* previousElementWithLower Source/WebCore/page/FocusController.cpp_sec4
528
545
529
Element* FocusController::nextFocusableElement(Node& start)
546
Element* FocusController::nextFocusableElement(Node& start)
530
{
547
{
548
    // FIXME: This can return a non-focusable shadow host.
531
    Ref<KeyboardEvent> keyEvent = KeyboardEvent::createForDummy();
549
    Ref<KeyboardEvent> keyEvent = KeyboardEvent::createForDummy();
532
    return nextFocusableElement(FocusNavigationScope::scopeOf(start), &start, keyEvent.ptr());
550
    return nextFocusableElementOrScopeOwner(FocusNavigationScope::scopeOf(start), &start, keyEvent.ptr());
533
}
551
}
534
552
535
Element* FocusController::previousFocusableElement(Node& start)
553
Element* FocusController::previousFocusableElement(Node& start)
536
{
554
{
555
    // FIXME: This can return a non-focusable shadow host.
537
    Ref<KeyboardEvent> keyEvent = KeyboardEvent::createForDummy();
556
    Ref<KeyboardEvent> keyEvent = KeyboardEvent::createForDummy();
538
    return previousFocusableElement(FocusNavigationScope::scopeOf(start), &start, keyEvent.ptr());
557
    return previousFocusableElementOrScopeOwner(FocusNavigationScope::scopeOf(start), &start, keyEvent.ptr());
539
}
558
}
540
559
541
Element* FocusController::nextFocusableElement(const FocusNavigationScope& scope, Node* start, KeyboardEvent* event)
560
Element* FocusController::nextFocusableElementOrScopeOwner(const FocusNavigationScope& scope, Node* start, KeyboardEvent* event)
542
{
561
{
543
    int startTabIndex = 0;
562
    int startTabIndex = 0;
544
    if (start && is<Element>(*start))
563
    if (start && is<Element>(*start))
Lines 575-581 Element* FocusController::nextFocusableE Source/WebCore/page/FocusController.cpp_sec5
575
    return findElementWithExactTabIndex(scope, &scope.rootNode(), 0, event, FocusDirectionForward);
594
    return findElementWithExactTabIndex(scope, &scope.rootNode(), 0, event, FocusDirectionForward);
576
}
595
}
577
596
578
Element* FocusController::previousFocusableElement(const FocusNavigationScope& scope, Node* start, KeyboardEvent* event)
597
Element* FocusController::previousFocusableElementOrScopeOwner(const FocusNavigationScope& scope, Node* start, KeyboardEvent* event)
579
{
598
{
580
    Node* last = nullptr;
599
    Node* last = nullptr;
581
    for (Node* node = &scope.rootNode(); node; node = scope.lastChildInScope(node))
600
    for (Node* node = &scope.rootNode(); node; node = scope.lastChildInScope(node))
- Source/WebCore/page/FocusController.h -4 / +8 lines
Lines 89-95 private: Source/WebCore/page/FocusController.h_sec1
89
    bool advanceFocusInDocumentOrder(FocusDirection, KeyboardEvent*, bool initialFocus);
89
    bool advanceFocusInDocumentOrder(FocusDirection, KeyboardEvent*, bool initialFocus);
90
90
91
    Element* findFocusableElementAcrossFocusScope(FocusDirection, const FocusNavigationScope& startScope, Node* start, KeyboardEvent*);
91
    Element* findFocusableElementAcrossFocusScope(FocusDirection, const FocusNavigationScope& startScope, Node* start, KeyboardEvent*);
92
    Element* findFocusableElementRecursively(FocusDirection, const FocusNavigationScope&, Node* start, KeyboardEvent*);
92
93
    Element* findFocusableElementWithinScope(FocusDirection, const FocusNavigationScope&, Node* start, KeyboardEvent*);
94
    Element* nextFocusableElementWithinScope(const FocusNavigationScope&, Node* start, KeyboardEvent*);
95
    Element* previousFocusableElementWithinScope(const FocusNavigationScope&, Node* start, KeyboardEvent*);
96
93
    Element* findFocusableElementDescendingDownIntoFrameDocument(FocusDirection, Element*, KeyboardEvent*);
97
    Element* findFocusableElementDescendingDownIntoFrameDocument(FocusDirection, Element*, KeyboardEvent*);
94
98
95
    // Searches through the given tree scope, starting from start node, for the next/previous selectable element that comes after/before start node.
99
    // Searches through the given tree scope, starting from start node, for the next/previous selectable element that comes after/before start node.
Lines 101-112 private: Source/WebCore/page/FocusController.h_sec2
101
    // @return The focus node that comes after/before start node.
105
    // @return The focus node that comes after/before start node.
102
    //
106
    //
103
    // See http://www.w3.org/TR/html4/interact/forms.html#h-17.11.1
107
    // See http://www.w3.org/TR/html4/interact/forms.html#h-17.11.1
104
    Element* findFocusableElement(FocusDirection, const FocusNavigationScope&, Node* start, KeyboardEvent*);
108
    Element* findFocusableElementOrScopeOwner(FocusDirection, const FocusNavigationScope&, Node* start, KeyboardEvent*);
105
109
106
    Element* findElementWithExactTabIndex(const FocusNavigationScope&, Node* start, int tabIndex, KeyboardEvent*, FocusDirection);
110
    Element* findElementWithExactTabIndex(const FocusNavigationScope&, Node* start, int tabIndex, KeyboardEvent*, FocusDirection);
107
    
111
    
108
    Element* nextFocusableElement(const FocusNavigationScope&, Node* start, KeyboardEvent*);
112
    Element* nextFocusableElementOrScopeOwner(const FocusNavigationScope&, Node* start, KeyboardEvent*);
109
    Element* previousFocusableElement(const FocusNavigationScope&, Node* start, KeyboardEvent*);
113
    Element* previousFocusableElementOrScopeOwner(const FocusNavigationScope&, Node* start, KeyboardEvent*);
110
114
111
    bool advanceFocusDirectionallyInContainer(Node* container, const LayoutRect& startingRect, FocusDirection, KeyboardEvent*);
115
    bool advanceFocusDirectionallyInContainer(Node* container, const LayoutRect& startingRect, FocusDirection, KeyboardEvent*);
112
    void findFocusCandidateInContainer(Node& container, const LayoutRect& startingRect, FocusDirection, KeyboardEvent*, FocusCandidate& closest);
116
    void findFocusCandidateInContainer(Node& container, const LayoutRect& startingRect, FocusDirection, KeyboardEvent*, FocusCandidate& closest);

Return to Bug 157415