| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserv
ed. |
| 3 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org) |
| 4 * Copyright (C) 2012 Digia Plc. and/or its subsidiary(-ies) |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions |
| 8 * are met: |
| 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. |
| 11 * 2. Redistributions in binary form must reproduce the above copyright |
| 12 * notice, this list of conditions and the following disclaimer in the |
| 13 * documentation and/or other materials provided with the distribution. |
| 14 * |
| 15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ |
| 27 |
| 28 #include "config.h" |
| 29 #include "core/editing/SelectionController.h" |
| 30 |
| 31 #include "core/HTMLNames.h" |
| 32 #include "core/dom/Document.h" |
| 33 #include "core/dom/DocumentMarkerController.h" |
| 34 #include "core/editing/Editor.h" |
| 35 #include "core/editing/FrameSelection.h" |
| 36 #include "core/editing/htmlediting.h" |
| 37 #include "core/editing/iterators/TextIterator.h" |
| 38 #include "core/events/Event.h" |
| 39 #include "core/frame/FrameView.h" |
| 40 #include "core/frame/LocalFrame.h" |
| 41 #include "core/frame/Settings.h" |
| 42 #include "core/layout/LayoutView.h" |
| 43 #include "core/page/FocusController.h" |
| 44 #include "core/page/Page.h" |
| 45 #include "platform/RuntimeEnabledFeatures.h" |
| 46 |
| 47 namespace blink { |
| 48 |
| 49 namespace { |
| 50 |
| 51 void setSelectionIfNeeded(FrameSelection& selection, const VisibleSelection& new
Selection) |
| 52 { |
| 53 if (selection.selection() == newSelection) |
| 54 return; |
| 55 selection.setSelection(newSelection); |
| 56 } |
| 57 |
| 58 bool dispatchSelectStart(Node* node) |
| 59 { |
| 60 if (!node || !node->layoutObject()) |
| 61 return true; |
| 62 |
| 63 return node->dispatchEvent(Event::createCancelableBubble(EventTypeNames::sel
ectstart)); |
| 64 } |
| 65 |
| 66 VisibleSelection expandSelectionToRespectUserSelectAll(Node* targetNode, const V
isibleSelection& selection) |
| 67 { |
| 68 Node* rootUserSelectAll = Position::rootUserSelectAllForNode(targetNode); |
| 69 if (!rootUserSelectAll) |
| 70 return selection; |
| 71 |
| 72 VisibleSelection newSelection(selection); |
| 73 newSelection.setBase(positionBeforeNode(rootUserSelectAll).upstream(CanCross
EditingBoundary)); |
| 74 newSelection.setExtent(positionAfterNode(rootUserSelectAll).downstream(CanCr
ossEditingBoundary)); |
| 75 |
| 76 return newSelection; |
| 77 } |
| 78 |
| 79 bool canMouseDownStartSelect(Node* node) |
| 80 { |
| 81 if (!node || !node->layoutObject()) |
| 82 return true; |
| 83 |
| 84 if (!node->canStartSelection()) |
| 85 return false; |
| 86 |
| 87 return true; |
| 88 } |
| 89 |
| 90 int textDistance(const Position& start, const Position& end) |
| 91 { |
| 92 RefPtrWillBeRawPtr<Range> range = Range::create(*start.document(), start, en
d); |
| 93 return TextIterator::rangeLength(range->startPosition(), range->endPosition(
), true); |
| 94 } |
| 95 |
| 96 } // anonymous namespace |
| 97 |
| 98 PassOwnPtrWillBeRawPtr<SelectionController> SelectionController::create(LocalFra
me* frame) |
| 99 { |
| 100 return adoptPtrWillBeNoop(new SelectionController(frame)); |
| 101 } |
| 102 |
| 103 SelectionController::SelectionController(LocalFrame* frame) |
| 104 : m_frame(frame) |
| 105 , m_mouseDownMayStartSelect(false) |
| 106 , m_singleClickInSelection(false) |
| 107 , m_selectionState(SelectionState::HaveNotStartedSelection) |
| 108 { |
| 109 } |
| 110 |
| 111 DEFINE_TRACE(SelectionController) |
| 112 { |
| 113 visitor->trace(m_frame); |
| 114 } |
| 115 |
| 116 bool SelectionController::updateSelectionForMouseDownDispatchingSelectStart(Node
* targetNode, const VisibleSelection& visibleSelection, TextGranularity granular
ity) |
| 117 { |
| 118 if (Position::nodeIsUserSelectNone(targetNode)) |
| 119 return false; |
| 120 |
| 121 if (!dispatchSelectStart(targetNode)) |
| 122 return false; |
| 123 |
| 124 if (visibleSelection.isRange()) { |
| 125 m_selectionState = SelectionState::ExtendedSelection; |
| 126 } else { |
| 127 granularity = CharacterGranularity; |
| 128 m_selectionState = SelectionState::PlacedCaret; |
| 129 } |
| 130 |
| 131 selection().setNonDirectionalSelectionIfNeeded(visibleSelection, granularity
); |
| 132 |
| 133 return true; |
| 134 } |
| 135 |
| 136 void SelectionController::selectClosestWordFromHitTestResult(const HitTestResult
& result, AppendTrailingWhitespace appendTrailingWhitespace) |
| 137 { |
| 138 Node* innerNode = result.innerNode(); |
| 139 VisibleSelection newSelection; |
| 140 |
| 141 if (innerNode && innerNode->layoutObject()) { |
| 142 VisiblePosition pos(innerNode->layoutObject()->positionForPoint(result.l
ocalPoint())); |
| 143 if (pos.isNotNull()) { |
| 144 newSelection = VisibleSelection(pos); |
| 145 newSelection.expandUsingGranularity(WordGranularity); |
| 146 } |
| 147 |
| 148 if (appendTrailingWhitespace == AppendTrailingWhitespace::ShouldAppend &
& newSelection.isRange()) |
| 149 newSelection.appendTrailingWhitespace(); |
| 150 |
| 151 updateSelectionForMouseDownDispatchingSelectStart(innerNode, expandSelec
tionToRespectUserSelectAll(innerNode, newSelection), WordGranularity); |
| 152 } |
| 153 } |
| 154 |
| 155 void SelectionController::selectClosestMisspellingFromHitTestResult(const HitTes
tResult& result, AppendTrailingWhitespace appendTrailingWhitespace) |
| 156 { |
| 157 Node* innerNode = result.innerNode(); |
| 158 VisibleSelection newSelection; |
| 159 |
| 160 if (!innerNode || !innerNode->layoutObject()) |
| 161 return; |
| 162 |
| 163 VisiblePosition pos(innerNode->layoutObject()->positionForPoint(result.local
Point())); |
| 164 Position start = pos.deepEquivalent(); |
| 165 Position end = pos.deepEquivalent(); |
| 166 if (pos.isNotNull()) { |
| 167 DocumentMarkerVector markers = innerNode->document().markers().markersIn
Range(makeRange(pos, pos).get(), DocumentMarker::MisspellingMarkers()); |
| 168 if (markers.size() == 1) { |
| 169 start.moveToOffset(markers[0]->startOffset()); |
| 170 end.moveToOffset(markers[0]->endOffset()); |
| 171 newSelection = VisibleSelection(start, end); |
| 172 } |
| 173 } |
| 174 |
| 175 if (appendTrailingWhitespace == AppendTrailingWhitespace::ShouldAppend && ne
wSelection.isRange()) |
| 176 newSelection.appendTrailingWhitespace(); |
| 177 |
| 178 updateSelectionForMouseDownDispatchingSelectStart(innerNode, expandSelection
ToRespectUserSelectAll(innerNode, newSelection), WordGranularity); |
| 179 } |
| 180 |
| 181 void SelectionController::selectClosestWordFromMouseEvent(const MouseEventWithHi
tTestResults& result) |
| 182 { |
| 183 if (!m_mouseDownMayStartSelect) |
| 184 return; |
| 185 |
| 186 selectClosestWordFromHitTestResult(result.hitTestResult(), |
| 187 (result.event().clickCount() == 2 && m_frame->editor().isSelectTrailingW
hitespaceEnabled()) ? AppendTrailingWhitespace::ShouldAppend : AppendTrailingWhi
tespace::DontAppend); |
| 188 } |
| 189 |
| 190 void SelectionController::selectClosestMisspellingFromMouseEvent(const MouseEven
tWithHitTestResults& result) |
| 191 { |
| 192 if (!m_mouseDownMayStartSelect) |
| 193 return; |
| 194 |
| 195 selectClosestMisspellingFromHitTestResult(result.hitTestResult(), |
| 196 (result.event().clickCount() == 2 && m_frame->editor().isSelectTrailingW
hitespaceEnabled()) ? AppendTrailingWhitespace::ShouldAppend : AppendTrailingWhi
tespace::DontAppend); |
| 197 } |
| 198 |
| 199 void SelectionController::selectClosestWordOrLinkFromMouseEvent(const MouseEvent
WithHitTestResults& result) |
| 200 { |
| 201 if (!result.hitTestResult().isLiveLink()) |
| 202 return selectClosestWordFromMouseEvent(result); |
| 203 |
| 204 Node* innerNode = result.innerNode(); |
| 205 |
| 206 if (!innerNode || !innerNode->layoutObject() || !m_mouseDownMayStartSelect) |
| 207 return; |
| 208 |
| 209 VisibleSelection newSelection; |
| 210 Element* URLElement = result.hitTestResult().URLElement(); |
| 211 VisiblePosition pos(innerNode->layoutObject()->positionForPoint(result.local
Point())); |
| 212 if (pos.isNotNull() && pos.deepEquivalent().deprecatedNode()->isDescendantOf
(URLElement)) |
| 213 newSelection = VisibleSelection::selectionFromContentsOfNode(URLElement)
; |
| 214 |
| 215 updateSelectionForMouseDownDispatchingSelectStart(innerNode, expandSelection
ToRespectUserSelectAll(innerNode, newSelection), WordGranularity); |
| 216 } |
| 217 |
| 218 void SelectionController::handleMousePressEvent(const MouseEventWithHitTestResul
ts& event) |
| 219 { |
| 220 // If we got the event back, that must mean it wasn't prevented, |
| 221 // so it's allowed to start a drag or selection if it wasn't in a scrollbar. |
| 222 m_mouseDownMayStartSelect = canMouseDownStartSelect(event.innerNode()) && !e
vent.scrollbar(); |
| 223 m_singleClickInSelection = false; |
| 224 } |
| 225 |
| 226 bool SelectionController::handleMousePressEventDoubleClick(const MouseEventWithH
itTestResults& event) |
| 227 { |
| 228 TRACE_EVENT0("blink", "SelectionController::handleMousePressEventDoubleClick
"); |
| 229 |
| 230 if (event.event().button() != LeftButton) |
| 231 return false; |
| 232 |
| 233 if (selection().isRange()) { |
| 234 // A double-click when range is already selected |
| 235 // should not change the selection. So, do not call |
| 236 // selectClosestWordFromMouseEvent, but do set |
| 237 // m_beganSelectingText to prevent handleMouseReleaseEvent |
| 238 // from setting caret selection. |
| 239 m_selectionState = SelectionState::ExtendedSelection; |
| 240 } else { |
| 241 selectClosestWordFromMouseEvent(event); |
| 242 } |
| 243 return true; |
| 244 } |
| 245 |
| 246 bool SelectionController::handleMousePressEventTripleClick(const MouseEventWithH
itTestResults& event) |
| 247 { |
| 248 TRACE_EVENT0("blink", "SelectionController::handleMousePressEventTripleClick
"); |
| 249 |
| 250 if (event.event().button() != LeftButton) |
| 251 return false; |
| 252 |
| 253 Node* innerNode = event.innerNode(); |
| 254 if (!(innerNode && innerNode->layoutObject() && m_mouseDownMayStartSelect)) |
| 255 return false; |
| 256 |
| 257 VisibleSelection newSelection; |
| 258 VisiblePosition pos(innerNode->layoutObject()->positionForPoint(event.localP
oint())); |
| 259 if (pos.isNotNull()) { |
| 260 newSelection = VisibleSelection(pos); |
| 261 newSelection.expandUsingGranularity(ParagraphGranularity); |
| 262 } |
| 263 |
| 264 return updateSelectionForMouseDownDispatchingSelectStart(innerNode, expandSe
lectionToRespectUserSelectAll(innerNode, newSelection), ParagraphGranularity); |
| 265 } |
| 266 |
| 267 bool SelectionController::handleMousePressEventSingleClick(const MouseEventWithH
itTestResults& event) |
| 268 { |
| 269 TRACE_EVENT0("blink", "SelectionController::handleMousePressEventSingleClick
"); |
| 270 |
| 271 m_frame->document()->updateLayoutIgnorePendingStylesheets(); |
| 272 Node* innerNode = event.innerNode(); |
| 273 if (!(innerNode && innerNode->layoutObject() && m_mouseDownMayStartSelect)) |
| 274 return false; |
| 275 |
| 276 // Extend the selection if the Shift key is down, unless the click is in a l
ink. |
| 277 bool extendSelection = event.event().shiftKey() && !event.isOverLink(); |
| 278 |
| 279 // Don't restart the selection when the mouse is pressed on an |
| 280 // existing selection so we can allow for text dragging. |
| 281 if (FrameView* view = m_frame->view()) { |
| 282 LayoutPoint vPoint = view->rootFrameToContents(event.event().position())
; |
| 283 if (!extendSelection && selection().contains(vPoint)) { |
| 284 m_singleClickInSelection = true; |
| 285 return false; |
| 286 } |
| 287 } |
| 288 |
| 289 VisiblePosition visiblePos(innerNode->layoutObject()->positionForPoint(event
.localPoint())); |
| 290 if (visiblePos.isNull()) |
| 291 visiblePos = VisiblePosition(firstPositionInOrBeforeNode(innerNode), DOW
NSTREAM); |
| 292 Position pos = visiblePos.deepEquivalent(); |
| 293 |
| 294 VisibleSelection newSelection = selection().selection(); |
| 295 TextGranularity granularity = CharacterGranularity; |
| 296 |
| 297 if (extendSelection && newSelection.isCaretOrRange()) { |
| 298 VisibleSelection selectionInUserSelectAll(expandSelectionToRespectUserSe
lectAll(innerNode, VisibleSelection(VisiblePosition(pos)))); |
| 299 if (selectionInUserSelectAll.isRange()) { |
| 300 if (comparePositions(selectionInUserSelectAll.start(), newSelection.
start()) < 0) |
| 301 pos = selectionInUserSelectAll.start(); |
| 302 else if (comparePositions(newSelection.end(), selectionInUserSelectA
ll.end()) < 0) |
| 303 pos = selectionInUserSelectAll.end(); |
| 304 } |
| 305 |
| 306 if (!m_frame->editor().behavior().shouldConsiderSelectionAsDirectional()
) { |
| 307 if (pos.isNotNull()) { |
| 308 // See <rdar://problem/3668157> REGRESSION (Mail): shift-click d
eselects when selection |
| 309 // was created right-to-left |
| 310 Position start = newSelection.start(); |
| 311 Position end = newSelection.end(); |
| 312 int distanceToStart = textDistance(start, pos); |
| 313 int distanceToEnd = textDistance(pos, end); |
| 314 if (distanceToStart <= distanceToEnd) |
| 315 newSelection = VisibleSelection(end, pos); |
| 316 else |
| 317 newSelection = VisibleSelection(start, pos); |
| 318 } |
| 319 } else { |
| 320 newSelection.setExtent(pos); |
| 321 } |
| 322 |
| 323 if (selection().granularity() != CharacterGranularity) { |
| 324 granularity = selection().granularity(); |
| 325 newSelection.expandUsingGranularity(selection().granularity()); |
| 326 } |
| 327 } else if (m_selectionState != SelectionState::ExtendedSelection) { |
| 328 newSelection = expandSelectionToRespectUserSelectAll(innerNode, VisibleS
election(visiblePos)); |
| 329 } |
| 330 |
| 331 // Updating the selection is considered side-effect of the event and so it d
oesn't impact the handled state. |
| 332 updateSelectionForMouseDownDispatchingSelectStart(innerNode, newSelection, g
ranularity); |
| 333 return false; |
| 334 } |
| 335 |
| 336 void SelectionController::handleMouseDraggedEvent(const MouseEventWithHitTestRes
ults& event, const IntPoint& mouseDownPos, const LayoutPoint& dragStartPos, Node
* mousePressNode, const IntPoint& lastKnownMousePosition) |
| 337 { |
| 338 if (m_selectionState != SelectionState::ExtendedSelection) { |
| 339 HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active
); |
| 340 HitTestResult result(request, mouseDownPos); |
| 341 m_frame->document()->layoutView()->hitTest(result); |
| 342 |
| 343 updateSelectionForMouseDrag(result, mousePressNode, dragStartPos, lastKn
ownMousePosition); |
| 344 } |
| 345 updateSelectionForMouseDrag(event.hitTestResult(), mousePressNode, dragStart
Pos, lastKnownMousePosition); |
| 346 } |
| 347 |
| 348 bool SelectionController::handleMouseReleaseEvent(const MouseEventWithHitTestRes
ults& event, const LayoutPoint& dragStartPos) |
| 349 { |
| 350 bool handled = false; |
| 351 m_mouseDownMayStartSelect = false; |
| 352 // Clear the selection if the mouse didn't move after the last mouse |
| 353 // press and it's not a context menu click. We do this so when clicking |
| 354 // on the selection, the selection goes away. However, if we are |
| 355 // editing, place the caret. |
| 356 if (m_singleClickInSelection && m_selectionState != SelectionState::Extended
Selection |
| 357 && dragStartPos == event.event().position() |
| 358 && selection().isRange() |
| 359 && event.event().button() != RightButton) { |
| 360 VisibleSelection newSelection; |
| 361 Node* node = event.innerNode(); |
| 362 bool caretBrowsing = m_frame->settings() && m_frame->settings()->caretBr
owsingEnabled(); |
| 363 if (node && node->layoutObject() && (caretBrowsing || node->hasEditableS
tyle())) { |
| 364 VisiblePosition pos = VisiblePosition(node->layoutObject()->position
ForPoint(event.localPoint())); |
| 365 newSelection = VisibleSelection(pos); |
| 366 } |
| 367 |
| 368 setSelectionIfNeeded(selection(), newSelection); |
| 369 |
| 370 handled = true; |
| 371 } |
| 372 |
| 373 selection().notifyLayoutObjectOfSelectionChange(UserTriggered); |
| 374 |
| 375 selection().selectFrameElementInParentIfFullySelected(); |
| 376 |
| 377 if (event.event().button() == MiddleButton && !event.isOverLink()) { |
| 378 // Ignore handled, since we want to paste to where the caret was placed
anyway. |
| 379 handled = handlePasteGlobalSelection(event.event()) || handled; |
| 380 } |
| 381 |
| 382 return handled; |
| 383 } |
| 384 |
| 385 bool SelectionController::handlePasteGlobalSelection(const PlatformMouseEvent& m
ouseEvent) |
| 386 { |
| 387 // If the event was a middle click, attempt to copy global selection in afte
r |
| 388 // the newly set caret position. |
| 389 // |
| 390 // This code is called from either the mouse up or mouse down handling. Ther
e |
| 391 // is some debate about when the global selection is pasted: |
| 392 // xterm: pastes on up. |
| 393 // GTK: pastes on down. |
| 394 // Qt: pastes on up. |
| 395 // Firefox: pastes on up. |
| 396 // Chromium: pastes on up. |
| 397 // |
| 398 // There is something of a webcompat angle to this well, as highlighted by |
| 399 // crbug.com/14608. Pages can clear text boxes 'onclick' and, if we paste on |
| 400 // down then the text is pasted just before the onclick handler runs and |
| 401 // clears the text box. So it's important this happens after the event |
| 402 // handlers have been fired. |
| 403 if (mouseEvent.type() != PlatformEvent::MouseReleased) |
| 404 return false; |
| 405 |
| 406 if (!m_frame->page()) |
| 407 return false; |
| 408 Frame* focusFrame = m_frame->page()->focusController().focusedOrMainFrame(); |
| 409 // Do not paste here if the focus was moved somewhere else. |
| 410 if (m_frame == focusFrame && m_frame->editor().behavior().supportsGlobalSele
ction()) |
| 411 return m_frame->editor().command("PasteGlobalSelection").execute(); |
| 412 |
| 413 return false; |
| 414 } |
| 415 |
| 416 bool SelectionController::handleGestureLongPress(const PlatformGestureEvent& ges
tureEvent, const HitTestResult& hitTestResult) |
| 417 { |
| 418 #if OS(ANDROID) |
| 419 bool shouldLongPressSelectWord = true; |
| 420 #else |
| 421 bool shouldLongPressSelectWord = m_frame->settings() && m_frame->settings()-
>touchEditingEnabled(); |
| 422 #endif |
| 423 if (!shouldLongPressSelectWord) |
| 424 return false; |
| 425 |
| 426 Node* innerNode = hitTestResult.innerNode(); |
| 427 if (!hitTestResult.isLiveLink() && innerNode && (innerNode->isContentEditabl
e() || innerNode->isTextNode() |
| 428 #if OS(ANDROID) |
| 429 || innerNode->canStartSelection() |
| 430 #endif |
| 431 )) { |
| 432 selectClosestWordFromHitTestResult(hitTestResult, AppendTrailingWhitespa
ce::DontAppend); |
| 433 if (m_frame->selection().isRange()) { |
| 434 Page* page = m_frame->page(); |
| 435 if (page) |
| 436 page->focusController().focusDocumentView(m_frame); |
| 437 |
| 438 return true; |
| 439 } |
| 440 } |
| 441 return false; |
| 442 } |
| 443 |
| 444 void SelectionController::updateSelectionForMouseDrag(Node* mousePressNode, cons
t LayoutPoint& dragStartPos, const IntPoint& lastKnownMousePosition) |
| 445 { |
| 446 FrameView* view = m_frame->view(); |
| 447 if (!view) |
| 448 return; |
| 449 LayoutView* layoutObject = m_frame->contentLayoutObject(); |
| 450 if (!layoutObject) |
| 451 return; |
| 452 |
| 453 HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active | H
itTestRequest::Move); |
| 454 HitTestResult result(request, view->rootFrameToContents(lastKnownMousePositi
on)); |
| 455 layoutObject->hitTest(result); |
| 456 updateSelectionForMouseDrag(result, mousePressNode, dragStartPos, lastKnownM
ousePosition); |
| 457 } |
| 458 |
| 459 void SelectionController::updateSelectionForMouseDrag(const HitTestResult& hitTe
stResult, Node* mousePressNode, const LayoutPoint& dragStartPos, const IntPoint&
lastKnownMousePosition) |
| 460 { |
| 461 if (!m_mouseDownMayStartSelect) |
| 462 return; |
| 463 |
| 464 Node* target = hitTestResult.innerNode(); |
| 465 if (!target) |
| 466 return; |
| 467 |
| 468 VisiblePosition targetPosition = selection().selection().visiblePositionResp
ectingEditingBoundary(hitTestResult.localPoint(), target); |
| 469 // Don't modify the selection if we're not on a node. |
| 470 if (targetPosition.isNull()) |
| 471 return; |
| 472 |
| 473 // Restart the selection if this is the first mouse move. This work is usual
ly |
| 474 // done in handleMousePressEvent, but not if the mouse press was on an exist
ing selection. |
| 475 VisibleSelection newSelection = selection().selection(); |
| 476 |
| 477 // Special case to limit selection to the containing block for SVG text. |
| 478 // FIXME: Isn't there a better non-SVG-specific way to do this? |
| 479 if (Node* selectionBaseNode = newSelection.base().deprecatedNode()) { |
| 480 if (LayoutObject* selectionBaseLayoutObject = selectionBaseNode->layoutO
bject()) { |
| 481 if (selectionBaseLayoutObject->isSVGText()) { |
| 482 if (target->layoutObject()->containingBlock() != selectionBaseLa
youtObject->containingBlock()) |
| 483 return; |
| 484 } |
| 485 } |
| 486 } |
| 487 |
| 488 if (m_selectionState == SelectionState::HaveNotStartedSelection && !dispatch
SelectStart(target)) |
| 489 return; |
| 490 |
| 491 if (m_selectionState != SelectionState::ExtendedSelection) { |
| 492 // Always extend selection here because it's caused by a mouse drag |
| 493 m_selectionState = SelectionState::ExtendedSelection; |
| 494 newSelection = VisibleSelection(targetPosition); |
| 495 } |
| 496 |
| 497 if (RuntimeEnabledFeatures::userSelectAllEnabled()) { |
| 498 Node* rootUserSelectAllForMousePressNode = Position::rootUserSelectAllFo
rNode(mousePressNode); |
| 499 if (rootUserSelectAllForMousePressNode && rootUserSelectAllForMousePress
Node == Position::rootUserSelectAllForNode(target)) { |
| 500 newSelection.setBase(positionBeforeNode(rootUserSelectAllForMousePre
ssNode).upstream(CanCrossEditingBoundary)); |
| 501 newSelection.setExtent(positionAfterNode(rootUserSelectAllForMousePr
essNode).downstream(CanCrossEditingBoundary)); |
| 502 } else { |
| 503 // Reset base for user select all when base is inside user-select-al
l area and extent < base. |
| 504 if (rootUserSelectAllForMousePressNode && comparePositions(target->l
ayoutObject()->positionForPoint(hitTestResult.localPoint()), mousePressNode->lay
outObject()->positionForPoint(dragStartPos)) < 0) |
| 505 newSelection.setBase(positionAfterNode(rootUserSelectAllForMouse
PressNode).downstream(CanCrossEditingBoundary)); |
| 506 |
| 507 Node* rootUserSelectAllForTarget = Position::rootUserSelectAllForNod
e(target); |
| 508 if (rootUserSelectAllForTarget && mousePressNode->layoutObject() &&
comparePositions(target->layoutObject()->positionForPoint(hitTestResult.localPoi
nt()), mousePressNode->layoutObject()->positionForPoint(dragStartPos)) < 0) |
| 509 newSelection.setExtent(positionBeforeNode(rootUserSelectAllForTa
rget).upstream(CanCrossEditingBoundary)); |
| 510 else if (rootUserSelectAllForTarget && mousePressNode->layoutObject(
)) |
| 511 newSelection.setExtent(positionAfterNode(rootUserSelectAllForTar
get).downstream(CanCrossEditingBoundary)); |
| 512 else |
| 513 newSelection.setExtent(targetPosition); |
| 514 } |
| 515 } else { |
| 516 newSelection.setExtent(targetPosition); |
| 517 } |
| 518 |
| 519 if (selection().granularity() != CharacterGranularity) |
| 520 newSelection.expandUsingGranularity(selection().granularity()); |
| 521 |
| 522 selection().setNonDirectionalSelectionIfNeeded(newSelection, selection().gra
nularity(), |
| 523 FrameSelection::AdjustEndpointsAtBidiBoundary); |
| 524 } |
| 525 |
| 526 |
| 527 void SelectionController::prepareForContextMenu(const MouseEventWithHitTestResul
ts& mev, const LayoutPoint& position) |
| 528 { |
| 529 if (selection().contains(position) |
| 530 || mev.scrollbar() |
| 531 // FIXME: In the editable case, word selection sometimes selects content
that isn't underneath the mouse. |
| 532 // If the selection is non-editable, we do word selection to make it eas
ier to use the contextual menu items |
| 533 // available for text selections. But only if we're above text. |
| 534 || !(selection().isContentEditable() || (mev.innerNode() && mev.innerNod
e()->isTextNode()))) |
| 535 return; |
| 536 |
| 537 m_mouseDownMayStartSelect = true; // context menu events are always allowed
to perform a selection |
| 538 |
| 539 if (mev.hitTestResult().isMisspelled()) { |
| 540 selectClosestMisspellingFromMouseEvent(mev); |
| 541 return; |
| 542 } |
| 543 |
| 544 if (m_frame->editor().behavior().shouldSelectOnContextualMenuClick()) |
| 545 selectClosestWordOrLinkFromMouseEvent(mev); |
| 546 } |
| 547 |
| 548 void SelectionController::preparePassMousePressEventToSubframe(MouseEventWithHit
TestResults& mev) |
| 549 { |
| 550 // If we're clicking into a frame that is selected, the frame will appear |
| 551 // greyed out even though we're clicking on the selection. This looks |
| 552 // really strange (having the whole frame be greyed out), so we deselect the |
| 553 // selection. |
| 554 IntPoint p = m_frame->view()->rootFrameToContents(mev.event().position()); |
| 555 if (!selection().contains(p)) |
| 556 return; |
| 557 |
| 558 VisiblePosition visiblePos( |
| 559 mev.innerNode()->layoutObject()->positionForPoint(mev.localPoint())); |
| 560 VisibleSelection newSelection(visiblePos); |
| 561 selection().setSelection(newSelection); |
| 562 } |
| 563 |
| 564 void SelectionController::initializeSelectionState() |
| 565 { |
| 566 m_selectionState = SelectionState::HaveNotStartedSelection; |
| 567 } |
| 568 |
| 569 void SelectionController::setMouseDownMayStartSelect(bool mayStartSelect) |
| 570 { |
| 571 m_mouseDownMayStartSelect = mayStartSelect; |
| 572 } |
| 573 |
| 574 bool SelectionController::mouseDownMayStartSelect() const |
| 575 { |
| 576 return m_mouseDownMayStartSelect; |
| 577 } |
| 578 |
| 579 bool SelectionController::singleClickInSelection() const |
| 580 { |
| 581 return m_singleClickInSelection; |
| 582 } |
| 583 |
| 584 FrameSelection& SelectionController::selection() const |
| 585 { |
| 586 return m_frame->selection(); |
| 587 } |
| 588 |
| 589 } // namespace blink |
| OLD | NEW |