Changeset 150869 in webkit
- Timestamp:
- May 29, 2013 12:36:29 AM (11 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r150868 r150869 1 2013-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 1 27 2013-05-29 Radu Stavila <stavila@adobe.com> 2 28 -
trunk/Source/WebCore/page/FocusController.cpp
r150796 r150869 1 1 /* 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.2 * Copyright (C) 2006, 2007, 2013 Apple Inc. All rights reserved. 3 3 * Copyright (C) 2008 Nuanti Ltd. 4 4 * … … 111 111 } 112 112 113 static inline void dispatchEventsOnWindowAndFocused Node(Document* document, bool focused)113 static inline void dispatchEventsOnWindowAndFocusedElement(Document* document, bool focused) 114 114 { 115 115 // If we have a focused node we should dispatch blur on it before we blur the window. … … 135 135 } 136 136 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);137 static inline bool isNonFocusableShadowHost(Element* element, KeyboardEvent* event) 138 { 139 ASSERT(element); 140 return !element->isKeyboardFocusable(event) && isShadowHost(element) && !hasCustomFocusLogic(element); 141 141 } 142 142 … … 152 152 if (!node->isElementNode()) 153 153 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 157 static inline bool shouldVisit(Element* element, KeyboardEvent* event) 158 { 159 ASSERT(element); 160 return element->isKeyboardFocusable(event) || isNonFocusableShadowHost(element, event); 161 161 } 162 162 … … 226 226 if (m_focusedFrame->view()) { 227 227 m_focusedFrame->selection()->setFocused(focused); 228 dispatchEventsOnWindowAndFocused Node(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 232 Element* FocusController::findFocusableElementDescendingDownIntoFrameDocument(FocusDirection direction, Element* element, KeyboardEvent* event) 233 233 { 234 234 // The node we found might be a HTMLFrameOwnerElement, so descend down the tree until we find either: 235 235 // 1) a focusable node, or 236 236 // 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); 239 239 if (!owner->contentFrame()) 240 240 break; 241 Node* foundNode = findFocusableNode(direction, FocusNavigationScope::focusNavigationScopeOwnedByIFrame(owner), 0, event);242 if (!found Node)241 Element* foundElement = findFocusableElement(direction, FocusNavigationScope::focusNavigationScopeOwnedByIFrame(owner), 0, event); 242 if (!foundElement) 243 243 break; 244 ASSERT( node != foundNode);245 node = foundNode;246 } 247 return node;244 ASSERT(element != foundElement); 245 element = foundElement; 246 } 247 return element; 248 248 } 249 249 … … 294 294 document->updateLayoutIgnorePendingStylesheets(); 295 295 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) { 299 299 // We didn't find a node to focus, so we should try to pass focus to Chrome. 300 300 if (!initialFocus && m_page->chrome().canTakeFocus(direction)) { … … 306 306 307 307 // 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) 312 312 return false; 313 313 } 314 314 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. 319 319 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 } 326 321 327 322 if (element->isFrameOwnerElement() && (!element->isPluginElement() || !element->isKeyboardFocusable(event))) { 328 323 // We focus frames rather than frame owners. 329 324 // 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()); 331 326 if (!owner->contentFrame()) 332 327 return false; … … 341 336 // their focus() methods. 342 337 343 Document* newDocument = node->document();344 345 if (newDocument != document) 338 Document* newDocument = element->document(); 339 340 if (newDocument != document) { 346 341 // Focus is going away from this document, so clear the focused node. 347 342 document->setFocusedElement(0); 343 } 348 344 349 345 if (newDocument) … … 351 347 352 348 if (caretBrowsing) { 353 Position position = firstPositionInOrBeforeNode( node.get());349 Position position = firstPositionInOrBeforeNode(element.get()); 354 350 VisibleSelection newSelection(position, position, DOWNSTREAM); 355 351 if (frame->selection()->shouldChangeSelection(newSelection)) … … 357 353 } 358 354 359 toElement(node.get())->focus(false, direction);355 element->focus(false, direction); 360 356 return true; 361 357 } 362 358 363 Node* FocusController::findFocusableNodeAcrossFocusScope(FocusDirection direction, FocusNavigationScope scope, Node* currentNode, KeyboardEvent* event)364 { 365 ASSERT(!currentNode || ! isNonFocusableShadowHost(currentNode, event));366 Node* found;359 Element* FocusController::findFocusableElementAcrossFocusScope(FocusDirection direction, FocusNavigationScope scope, Node* currentNode, KeyboardEvent* event) 360 { 361 ASSERT(!currentNode || !currentNode->isElementNode() || !isNonFocusableShadowHost(toElement(currentNode), event)); 362 Element* found; 367 363 if (currentNode && direction == FocusDirectionForward && isFocusableShadowHost(currentNode, event)) { 368 Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(currentNode), 0, event);369 found = foundInInnerFocusScope ? foundInInnerFocusScope : findFocusable NodeRecursively(direction, scope, currentNode, event);364 Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(currentNode), 0, event); 365 found = foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableElementRecursively(direction, scope, currentNode, event); 370 366 } else 371 found = findFocusable NodeRecursively(direction, scope, currentNode, event);367 found = findFocusableElementRecursively(direction, scope, currentNode, event); 372 368 373 369 // If there's no focusable node to advance to, move up the focus scopes until we find one. 374 370 while (!found) { 375 Node* owner = scope.owner();371 Element* owner = scope.owner(); 376 372 if (!owner) 377 373 break; … … 381 377 break; 382 378 } 383 found = findFocusable NodeRecursively(direction, scope, owner, event);384 } 385 found = findFocusable NodeDecendingDownIntoFrameDocument(direction, found, event);379 found = findFocusableElementRecursively(direction, scope, owner, event); 380 } 381 found = findFocusableElementDescendingDownIntoFrameDocument(direction, found, event); 386 382 return found; 387 383 } 388 384 389 Node* FocusController::findFocusableNodeRecursively(FocusDirection direction, FocusNavigationScope scope, Node* start, KeyboardEvent* event)385 Element* FocusController::findFocusableElementRecursively(FocusDirection direction, FocusNavigationScope scope, Node* start, KeyboardEvent* event) 390 386 { 391 387 // Starting node is exclusive. 392 Node* found = findFocusableNode(direction, scope, start, event);388 Element* found = findFocusableElement(direction, scope, start, event); 393 389 if (!found) 394 390 return 0; … … 396 392 if (!isNonFocusableShadowHost(found, event)) 397 393 return found; 398 Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);399 return foundInInnerFocusScope ? foundInInnerFocusScope : findFocusable NodeRecursively(direction, scope, found, event);394 Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event); 395 return foundInInnerFocusScope ? foundInInnerFocusScope : findFocusableElementRecursively(direction, scope, found, event); 400 396 } 401 397 ASSERT(direction == FocusDirectionBackward); 402 398 if (isFocusableShadowHost(found, event)) { 403 Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);399 Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event); 404 400 return foundInInnerFocusScope ? foundInInnerFocusScope : found; 405 401 } 406 402 if (isNonFocusableShadowHost(found, event)) { 407 Node* foundInInnerFocusScope = findFocusableNodeRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event);408 return foundInInnerFocusScope ? foundInInnerFocusScope :findFocusable NodeRecursively(direction, scope, found, event);403 Element* foundInInnerFocusScope = findFocusableElementRecursively(direction, FocusNavigationScope::focusNavigationScopeOwnedByShadowHost(found), 0, event); 404 return foundInInnerFocusScope ? foundInInnerFocusScope :findFocusableElementRecursively(direction, scope, found, event); 409 405 } 410 406 return found; 411 407 } 412 408 413 Node* FocusController::findFocusableNode(FocusDirection direction, FocusNavigationScope scope, Node* node, KeyboardEvent* event)409 Element* FocusController::findFocusableElement(FocusDirection direction, FocusNavigationScope scope, Node* node, KeyboardEvent* event) 414 410 { 415 411 return (direction == FocusDirectionForward) 416 ? nextFocusable Node(scope, node, event)417 : previousFocusable Node(scope, node, event);412 ? nextFocusableElement(scope, node, event) 413 : previousFocusableElement(scope, node, event); 418 414 } 419 415 … … 468 464 } 469 465 470 Node* FocusController::nextFocusableNode(FocusNavigationScope scope, Node* start, KeyboardEvent* event)466 Element* FocusController::nextFocusableElement(FocusNavigationScope scope, Node* start, KeyboardEvent* event) 471 467 { 472 468 using namespace NodeRenderingTraversal; … … 477 473 if (tabIndex < 0) { 478 474 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; 481 480 } 482 481 } … … 502 501 } 503 502 504 Node* FocusController::previousFocusableNode(FocusNavigationScope scope, Node* start, KeyboardEvent* event)503 Element* FocusController::previousFocusableElement(FocusNavigationScope scope, Node* start, KeyboardEvent* event) 505 504 { 506 505 using namespace NodeRenderingTraversal; … … 526 525 if (startingTabIndex < 0) { 527 526 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; 530 532 } 531 533 } … … 661 663 662 664 if (m_focusedFrame && isFocused()) 663 dispatchEventsOnWindowAndFocused Node(m_focusedFrame->document(), active);665 dispatchEventsOnWindowAndFocusedElement(m_focusedFrame->document(), active); 664 666 } 665 667 -
trunk/Source/WebCore/page/FocusController.h
r150713 r150869 89 89 bool advanceFocusInDocumentOrder(FocusDirection, KeyboardEvent*, bool initialFocus); 90 90 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*); 94 94 95 95 // Searches through the given tree scope, starting from start node, for the next/previous selectable element that comes after/before start node. … … 102 102 // 103 103 // 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*); 105 105 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*); 108 108 109 109 Element* findElementWithExactTabIndex(Node* start, int tabIndex, KeyboardEvent*, FocusDirection);
Note: See TracChangeset
for help on using the changeset viewer.