1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
/*
* Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
* Copyright (C) 2008 Nuanti Ltd.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* 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
* 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
* 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
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "FocusController.h"
#include "AXObjectCache.h"
#include "Chrome.h"
#include "Document.h"
#include "Editor.h"
#include "EditorClient.h"
#include "Element.h"
#include "Event.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "ExceptionCode.h"
#include "Frame.h"
#include "FrameTree.h"
#include "FrameView.h"
#include "HTMLFrameOwnerElement.h"
#include "HTMLNames.h"
#include "KeyboardEvent.h"
#include "Page.h"
#include "Range.h"
#include "RenderObject.h"
#include "RenderWidget.h"
#include "SelectionController.h"
#include "Settings.h"
#include "SpatialNavigation.h"
#include "Widget.h"
namespace WebCore {
using namespace HTMLNames;
using namespace std;
static inline void dispatchEventsOnWindowAndFocusedNode(Document* document, bool focused)
{
// If we have a focused node we should dispatch blur on it before we blur the window.
// If we have a focused node we should dispatch focus on it after we focus the window.
// https://bugs.webkit.org/show_bug.cgi?id=27105
// Do not fire events while modal dialogs are up. See https://bugs.webkit.org/show_bug.cgi?id=33962
if (Page* page = document->page()) {
if (page->defersLoading())
return;
}
if (!focused && document->focusedNode())
document->focusedNode()->dispatchBlurEvent();
document->dispatchWindowEvent(Event::create(focused ? eventNames().focusEvent : eventNames().blurEvent, false, false));
if (focused && document->focusedNode())
document->focusedNode()->dispatchFocusEvent();
}
FocusController::FocusController(Page* page)
: m_page(page)
, m_isActive(false)
, m_isFocused(false)
, m_isChangingFocusedFrame(false)
{
}
void FocusController::setFocusedFrame(PassRefPtr<Frame> frame)
{
if (m_focusedFrame == frame || m_isChangingFocusedFrame)
return;
m_isChangingFocusedFrame = true;
RefPtr<Frame> oldFrame = m_focusedFrame;
RefPtr<Frame> newFrame = frame;
m_focusedFrame = newFrame;
// Now that the frame is updated, fire events and update the selection focused states of both frames.
if (oldFrame && oldFrame->view()) {
oldFrame->selection()->setFocused(false);
oldFrame->document()->dispatchWindowEvent(Event::create(eventNames().blurEvent, false, false));
}
if (newFrame && newFrame->view() && isFocused()) {
newFrame->selection()->setFocused(true);
newFrame->document()->dispatchWindowEvent(Event::create(eventNames().focusEvent, false, false));
}
m_isChangingFocusedFrame = false;
}
Frame* FocusController::focusedOrMainFrame()
{
if (Frame* frame = focusedFrame())
return frame;
return m_page->mainFrame();
}
void FocusController::setFocused(bool focused)
{
if (isFocused() == focused)
return;
m_isFocused = focused;
if (m_focusedFrame && m_focusedFrame->view()) {
m_focusedFrame->selection()->setFocused(focused);
dispatchEventsOnWindowAndFocusedNode(m_focusedFrame->document(), focused);
}
}
static Node* deepFocusableNode(FocusDirection direction, Node* node, KeyboardEvent* event)
{
// The node we found might be a HTMLFrameOwnerElement, so descend down the frame tree until we find either:
// 1) a focusable node, or
// 2) the deepest-nested HTMLFrameOwnerElement
while (node && node->isFrameOwnerElement()) {
HTMLFrameOwnerElement* owner = static_cast<HTMLFrameOwnerElement*>(node);
if (!owner->contentFrame())
break;
Document* document = owner->contentFrame()->document();
node = (direction == FocusDirectionForward)
? document->nextFocusableNode(0, event)
: document->previousFocusableNode(0, event);
if (!node) {
node = owner;
break;
}
}
return node;
}
bool FocusController::setInitialFocus(FocusDirection direction, KeyboardEvent* event)
{
return advanceFocus(direction, event, true);
}
bool FocusController::advanceFocus(FocusDirection direction, KeyboardEvent* event, bool initialFocus)
{
switch (direction) {
case FocusDirectionForward:
case FocusDirectionBackward:
return advanceFocusInDocumentOrder(direction, event, initialFocus);
case FocusDirectionLeft:
case FocusDirectionRight:
case FocusDirectionUp:
case FocusDirectionDown:
return advanceFocusDirectionally(direction, event);
default:
ASSERT_NOT_REACHED();
}
return false;
}
bool FocusController::advanceFocusInDocumentOrder(FocusDirection direction, KeyboardEvent* event, bool initialFocus)
{
Frame* frame = focusedOrMainFrame();
ASSERT(frame);
Document* document = frame->document();
Node* currentNode = document->focusedNode();
// FIXME: Not quite correct when it comes to focus transitions leaving/entering the WebView itself
bool caretBrowsing = focusedOrMainFrame()->settings()->caretBrowsingEnabled();
if (caretBrowsing && !currentNode)
currentNode = frame->selection()->start().node();
document->updateLayoutIgnorePendingStylesheets();
Node* node = (direction == FocusDirectionForward)
? document->nextFocusableNode(currentNode, event)
: document->previousFocusableNode(currentNode, event);
// If there's no focusable node to advance to, move up the frame tree until we find one.
while (!node && frame) {
Frame* parentFrame = frame->tree()->parent();
if (!parentFrame)
break;
Document* parentDocument = parentFrame->document();
HTMLFrameOwnerElement* owner = frame->ownerElement();
if (!owner)
break;
node = (direction == FocusDirectionForward)
? parentDocument->nextFocusableNode(owner, event)
: parentDocument->previousFocusableNode(owner, event);
frame = parentFrame;
}
node = deepFocusableNode(direction, node, event);
if (!node) {
// 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->setFocusedNode(0);
setFocusedFrame(0);
m_page->chrome()->takeFocus(direction);
return true;
}
// Chrome doesn't want focus, so we should wrap focus.
Document* d = m_page->mainFrame()->document();
node = (direction == FocusDirectionForward)
? d->nextFocusableNode(0, event)
: d->previousFocusableNode(0, event);
node = deepFocusableNode(direction, node, event);
if (!node)
return false;
}
ASSERT(node);
if (node == document->focusedNode())
// Focus wrapped around to the same node.
return true;
if (!node->isElementNode())
// FIXME: May need a way to focus a document here.
return false;
if (node->isFrameOwnerElement()) {
// 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 = static_cast<HTMLFrameOwnerElement*>(node);
if (!owner->contentFrame())
return false;
document->setFocusedNode(0);
setFocusedFrame(owner->contentFrame());
return true;
}
// FIXME: It would be nice to just be able to call setFocusedNode(node) here, but we can't do
// that because some elements (e.g. HTMLInputElement and HTMLTextAreaElement) do extra work in
// their focus() methods.
Document* newDocument = node->document();
if (newDocument != document)
// Focus is going away from this document, so clear the focused node.
document->setFocusedNode(0);
if (newDocument)
setFocusedFrame(newDocument->frame());
if (caretBrowsing) {
VisibleSelection newSelection(Position(node, 0), Position(node, 0), DOWNSTREAM);
if (frame->shouldChangeSelection(newSelection))
frame->selection()->setSelection(newSelection);
}
static_cast<Element*>(node)->focus(false);
return true;
}
bool FocusController::advanceFocusDirectionally(FocusDirection direction, KeyboardEvent* event)
{
Frame* frame = focusedOrMainFrame();
ASSERT(frame);
Document* focusedDocument = frame->document();
if (!focusedDocument)
return false;
Node* focusedNode = focusedDocument->focusedNode();
if (!focusedNode) {
// Just move to the first focusable node.
FocusDirection tabDirection = (direction == FocusDirectionUp || direction == FocusDirectionLeft) ?
FocusDirectionForward : FocusDirectionBackward;
// 'initialFocus' is set to true so the chrome is not focused.
return advanceFocusInDocumentOrder(tabDirection, event, true);
}
// Move up in the chain of nested frames.
frame = frame->tree()->top();
FocusCandidate focusCandidate;
findFocusableNodeInDirection(frame->document(), focusedNode, direction, event, focusCandidate);
Node* node = focusCandidate.node;
if (!node || !node->isElementNode()) {
// FIXME: May need a way to focus a document here.
Frame* frame = focusedOrMainFrame();
scrollInDirection(frame, direction);
return false;
}
// In order to avoid crazy jump between links that are either far away from each other,
// or just not currently visible, lets do a scroll in the given direction and bail out
// if |node| element is not in the viewport.
if (hasOffscreenRect(node)) {
Frame* frame = node->document()->view()->frame();
scrollInDirection(frame, direction);
return true;
}
Document* newDocument = node->document();
if (newDocument != focusedDocument) {
// Focus is going away from the originally focused document, so clear the focused node.
focusedDocument->setFocusedNode(0);
}
if (newDocument)
setFocusedFrame(newDocument->frame());
Element* element = static_cast<Element*>(node);
ASSERT(element);
scrollIntoView(element);
element->focus(false);
return true;
}
static void updateFocusCandidateIfCloser(Node* focusedNode, Node* candidate, long long distance, FocusCandidate& closestFocusCandidate)
{
// Bail out if |distance| is bigger than the current closest candidate.
if (distance >= closestFocusCandidate.distance)
return;
// If |focusedNode| and |candidate| are in the same document AND
// current |closestFocusCandidadte| is not in an {i}frame that is
// preferable to get focused.
if (focusedNode->document() == candidate->document()
&& distance < closestFocusCandidate.parentDistance) {
closestFocusCandidate.node = candidate;
closestFocusCandidate.distance = distance;
closestFocusCandidate.parentDistance = maxDistance();
} else if (focusedNode->document() != candidate->document()) {
// If the |focusedNode| is in an inner document and the |candidate| is
// in a different document, we only consider to change focus if there is
// not another already good focusable candidate in the same document as
// |focusedNode|.
if (!((isInRootDocument(candidate) && !isInRootDocument(focusedNode))
&& closestFocusCandidate.node
&& focusedNode->document() == closestFocusCandidate.node->document())) {
closestFocusCandidate.node = candidate;
closestFocusCandidate.distance = distance;
}
}
}
void FocusController::findFocusableNodeInDirection(Document* document, Node* focusedNode, FocusDirection direction, KeyboardEvent* event, FocusCandidate& closestFocusCandidate)
{
ASSERT(document);
// Walk all the child nodes and update focusCandidate if we find a nearer node.
for (Node* candidate = document->firstChild(); candidate; candidate = candidate->traverseNextNode()) {
// Inner documents case.
if (candidate->isFrameOwnerElement())
deepFindFocusableNodeInDirection(focusedNode, candidate, direction, event, closestFocusCandidate);
else if (candidate != focusedNode && candidate->isKeyboardFocusable(event)) {
long long distance = distanceInDirection(focusedNode, candidate,
direction, closestFocusCandidate);
updateFocusCandidateIfCloser(focusedNode, candidate, distance, closestFocusCandidate);
}
}
}
void FocusController::deepFindFocusableNodeInDirection(Node* focusedNode, Node* candidate, FocusDirection direction, KeyboardEvent* event, FocusCandidate& closestFocusCandidate)
{
HTMLFrameOwnerElement* owner = static_cast<HTMLFrameOwnerElement*>(candidate);
if (!owner->contentFrame())
return;
Document* innerDocument = owner->contentFrame()->document();
if (!innerDocument)
return;
if (innerDocument == focusedNode->document())
findFocusableNodeInDirection(innerDocument, focusedNode, direction, event, closestFocusCandidate);
else {
// Check if the current {i}frame element itself is a good candidate
// to move focus to. If it is, then we traverse its inner nodes.
// Lets pass a copy of the best candidate, to not get fooled by a
// frame without focusable elements.
FocusCandidate focusCandidateCopy = closestFocusCandidate;
long long distance = distanceInDirection(focusedNode, candidate, direction, focusCandidateCopy);
if (distance < focusCandidateCopy.distance) {
focusCandidateCopy.parentAlignment = focusCandidateCopy.alignment;
focusCandidateCopy.parentDistance = distance;
findFocusableNodeInDirection(innerDocument, focusedNode, direction, event, focusCandidateCopy);
// If we really have an inner closer focus candidate node, take it.
if (closestFocusCandidate.node != focusCandidateCopy.node)
closestFocusCandidate = focusCandidateCopy;
}
}
}
static bool relinquishesEditingFocus(Node *node)
{
ASSERT(node);
ASSERT(node->isContentEditable());
Node* root = node->rootEditableElement();
Frame* frame = node->document()->frame();
if (!frame || !root)
return false;
return frame->editor()->shouldEndEditing(rangeOfContents(root).get());
}
static void clearSelectionIfNeeded(Frame* oldFocusedFrame, Frame* newFocusedFrame, Node* newFocusedNode)
{
if (!oldFocusedFrame || !newFocusedFrame)
return;
if (oldFocusedFrame->document() != newFocusedFrame->document())
return;
SelectionController* s = oldFocusedFrame->selection();
if (s->isNone())
return;
bool caretBrowsing = oldFocusedFrame->settings()->caretBrowsingEnabled();
if (caretBrowsing)
return;
Node* selectionStartNode = s->selection().start().node();
if (selectionStartNode == newFocusedNode || selectionStartNode->isDescendantOf(newFocusedNode) || selectionStartNode->shadowAncestorNode() == newFocusedNode)
return;
if (Node* mousePressNode = newFocusedFrame->eventHandler()->mousePressNode())
if (mousePressNode->renderer() && !mousePressNode->canStartSelection())
if (Node* root = s->rootEditableElement())
if (Node* shadowAncestorNode = root->shadowAncestorNode())
// Don't do this for textareas and text fields, when they lose focus their selections should be cleared
// and then restored when they regain focus, to match other browsers.
if (!shadowAncestorNode->hasTagName(inputTag) && !shadowAncestorNode->hasTagName(textareaTag))
return;
s->clear();
}
bool FocusController::setFocusedNode(Node* node, PassRefPtr<Frame> newFocusedFrame)
{
RefPtr<Frame> oldFocusedFrame = focusedFrame();
RefPtr<Document> oldDocument = oldFocusedFrame ? oldFocusedFrame->document() : 0;
Node* oldFocusedNode = oldDocument ? oldDocument->focusedNode() : 0;
if (oldFocusedNode == node)
return true;
// FIXME: Might want to disable this check for caretBrowsing
if (oldFocusedNode && oldFocusedNode->rootEditableElement() == oldFocusedNode && !relinquishesEditingFocus(oldFocusedNode))
return false;
clearSelectionIfNeeded(oldFocusedFrame.get(), newFocusedFrame.get(), node);
if (!node) {
if (oldDocument)
oldDocument->setFocusedNode(0);
m_page->editorClient()->setInputMethodState(false);
return true;
}
RefPtr<Document> newDocument = node->document();
if (newDocument && newDocument->focusedNode() == node) {
m_page->editorClient()->setInputMethodState(node->shouldUseInputMethod());
return true;
}
if (oldDocument && oldDocument != newDocument)
oldDocument->setFocusedNode(0);
setFocusedFrame(newFocusedFrame);
// Setting the focused node can result in losing our last reft to node when JS event handlers fire.
RefPtr<Node> protect = node;
if (newDocument)
newDocument->setFocusedNode(node);
if (newDocument->focusedNode() == node)
m_page->editorClient()->setInputMethodState(node->shouldUseInputMethod());
return true;
}
void FocusController::setActive(bool active)
{
if (m_isActive == active)
return;
m_isActive = active;
if (FrameView* view = m_page->mainFrame()->view()) {
if (!view->platformWidget()) {
view->layoutIfNeededRecursive();
view->updateControlTints();
}
}
focusedOrMainFrame()->selection()->pageActivationChanged();
if (m_focusedFrame && isFocused())
dispatchEventsOnWindowAndFocusedNode(m_focusedFrame->document(), active);
}
} // namespace WebCore
|