Index: third_party/WebKit/Source/core/page/FocusController.cpp |
diff --git a/third_party/WebKit/Source/core/page/FocusController.cpp b/third_party/WebKit/Source/core/page/FocusController.cpp |
index 8065b70f63dbbbd9537c1f1609e78ddce3ec31af..0b5aa3331fc4ac12396ea84b1c09af7ceae97fd5 100644 |
--- a/third_party/WebKit/Source/core/page/FocusController.cpp |
+++ b/third_party/WebKit/Source/core/page/FocusController.cpp |
@@ -1040,10 +1040,6 @@ |
setFocusedFrame(newDocument.frame()); |
- if (!element->isTextControl() && !hasEditableStyle(*element->toNode())) { |
- // TODO(editing-dev): Remove this clear(), crbug.com/692898. |
- focusedFrame()->selection().clear(); |
- } |
element->focus( |
FocusParams(SelectionBehaviorOnFocus::Reset, type, sourceCapabilities)); |
return true; |
@@ -1070,6 +1066,35 @@ |
return element.document().frame() && rootEditableElement(element); |
} |
+static void clearSelectionIfNeeded(LocalFrame* oldFocusedFrame, |
+ LocalFrame* newFocusedFrame, |
+ Element* newFocusedElement) { |
+ if (!oldFocusedFrame || !newFocusedFrame) |
+ return; |
+ |
+ if (oldFocusedFrame->document() != newFocusedFrame->document()) |
+ return; |
+ |
+ FrameSelection& selection = oldFocusedFrame->selection(); |
+ const SelectionInDOMTree& selectionInDOMTree = selection.selectionInDOMTree(); |
+ if (selectionInDOMTree.isNone()) |
+ return; |
+ |
+ Node* selectionStartNode = selectionInDOMTree.base().anchorNode(); |
+ if (selectionStartNode == newFocusedElement || |
+ selectionStartNode->isDescendantOf(newFocusedElement)) |
+ return; |
+ |
+ if (!enclosingTextControl(selectionStartNode)) |
+ return; |
+ |
+ if (selectionStartNode->isInShadowTree() && |
+ selectionStartNode->ownerShadowHost() == newFocusedElement) |
+ return; |
+ |
+ selection.clear(); |
+} |
+ |
bool FocusController::setFocusedElement(Element* element, |
Frame* newFocusedFrame) { |
return setFocusedElement( |
@@ -1105,6 +1130,10 @@ |
if (newDocument && oldDocument == newDocument && |
newDocument->focusedElement() == element) |
return true; |
+ |
+ if (newFocusedFrame && newFocusedFrame->isLocalFrame()) |
+ clearSelectionIfNeeded(oldFocusedFrame, toLocalFrame(newFocusedFrame), |
+ element); |
if (oldDocument && oldDocument != newDocument) |
oldDocument->clearFocusedElement(); |
@@ -1326,10 +1355,6 @@ |
Element* element = toElement(focusCandidate.focusableNode); |
DCHECK(element); |
- if (!element->isTextControl() && !hasEditableStyle(*element->toNode())) { |
- // TODO(editing-dev): Remove this clear(), crbug.com/692898. |
- focusedFrame()->selection().clear(); |
- } |
element->focus(FocusParams(SelectionBehaviorOnFocus::Reset, type, nullptr)); |
return true; |
} |