| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) | 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 4 * (C) 2001 Dirk Mueller (mueller@kde.org) | 4 * (C) 2001 Dirk Mueller (mueller@kde.org) |
| 5 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. | 5 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. |
| 6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com) | 6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com) |
| 7 * | 7 * |
| 8 * This library is free software; you can redistribute it and/or | 8 * This library is free software; you can redistribute it and/or |
| 9 * modify it under the terms of the GNU Library General Public | 9 * modify it under the terms of the GNU Library General Public |
| 10 * License as published by the Free Software Foundation; either | 10 * License as published by the Free Software Foundation; either |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 return setSelectionRange(start, end, direction); | 275 return setSelectionRange(start, end, direction); |
| 276 } | 276 } |
| 277 | 277 |
| 278 void HTMLTextFormControlElement::setSelectionRange(int start, int end, TextField
SelectionDirection direction) | 278 void HTMLTextFormControlElement::setSelectionRange(int start, int end, TextField
SelectionDirection direction) |
| 279 { | 279 { |
| 280 document().updateLayoutIgnorePendingStylesheets(); | 280 document().updateLayoutIgnorePendingStylesheets(); |
| 281 | 281 |
| 282 if (!renderer() || !renderer()->isTextControl()) | 282 if (!renderer() || !renderer()->isTextControl()) |
| 283 return; | 283 return; |
| 284 | 284 |
| 285 end = std::max(end, 0); | 285 int textLength = innerEditorValue().length(); |
| 286 start = std::min(std::max(start, 0), end); | 286 end = std::max(std::min(end, textLength), 0); |
| 287 start = std::max(std::min(start, end), 0); |
| 288 cacheSelection(start, end, direction); |
| 289 bool isCaretSelection = start == end; |
| 290 bool shouldSetSelection = document().focusedElement() == this || (!isCaretSe
lection && start < textLength); |
| 287 | 291 |
| 288 if (!hasVisibleTextArea(renderer(), innerEditorElement())) { | 292 if (!hasVisibleTextArea(renderer(), innerEditorElement())) |
| 289 cacheSelection(start, end, direction); | |
| 290 return; | 293 return; |
| 291 } | 294 |
| 295 LocalFrame* frame = document().frame(); |
| 296 |
| 297 if (!frame || !shouldSetSelection) |
| 298 return; |
| 299 |
| 292 VisiblePosition startPosition = visiblePositionForIndex(start); | 300 VisiblePosition startPosition = visiblePositionForIndex(start); |
| 293 VisiblePosition endPosition; | 301 VisiblePosition endPosition; |
| 294 if (start == end) | 302 if (start == end) |
| 295 endPosition = startPosition; | 303 endPosition = startPosition; |
| 296 else | 304 else |
| 297 endPosition = visiblePositionForIndex(end); | 305 endPosition = visiblePositionForIndex(end); |
| 298 | 306 |
| 299 // startPosition and endPosition can be null position for example when | 307 // startPosition and endPosition can be null position for example when |
| 300 // "-webkit-user-select: none" style attribute is specified. | 308 // "-webkit-user-select: none" style attribute is specified. |
| 301 if (startPosition.isNotNull() && endPosition.isNotNull()) { | 309 if (startPosition.isNotNull() && endPosition.isNotNull()) { |
| 302 ASSERT(startPosition.deepEquivalent().deprecatedNode()->shadowHost() ==
this | 310 ASSERT(startPosition.deepEquivalent().deprecatedNode()->shadowHost() ==
this |
| 303 && endPosition.deepEquivalent().deprecatedNode()->shadowHost() == th
is); | 311 && endPosition.deepEquivalent().deprecatedNode()->shadowHost() == th
is); |
| 304 } | 312 } |
| 305 VisibleSelection newSelection; | 313 VisibleSelection newSelection; |
| 306 if (direction == SelectionHasBackwardDirection) | 314 if (direction == SelectionHasBackwardDirection) |
| 307 newSelection = VisibleSelection(endPosition, startPosition); | 315 newSelection = VisibleSelection(endPosition, startPosition); |
| 308 else | 316 else |
| 309 newSelection = VisibleSelection(startPosition, endPosition); | 317 newSelection = VisibleSelection(startPosition, endPosition); |
| 310 newSelection.setIsDirectional(direction != SelectionHasNoDirection); | 318 newSelection.setIsDirectional(direction != SelectionHasNoDirection); |
| 311 | 319 |
| 312 if (LocalFrame* frame = document().frame()) | 320 frame->selection().setSelection(newSelection); |
| 313 frame->selection().setSelection(newSelection); | |
| 314 } | 321 } |
| 315 | 322 |
| 316 VisiblePosition HTMLTextFormControlElement::visiblePositionForIndex(int index) c
onst | 323 VisiblePosition HTMLTextFormControlElement::visiblePositionForIndex(int index) c
onst |
| 317 { | 324 { |
| 318 if (index <= 0) | 325 if (index <= 0) |
| 319 return VisiblePosition(firstPositionInNode(innerEditorElement()), DOWNST
REAM); | 326 return VisiblePosition(firstPositionInNode(innerEditorElement()), DOWNST
REAM); |
| 320 RefPtrWillBeRawPtr<Range> range = Range::create(document()); | 327 RefPtrWillBeRawPtr<Range> range = Range::create(document()); |
| 321 range->selectNodeContents(innerEditorElement(), ASSERT_NO_EXCEPTION); | 328 range->selectNodeContents(innerEditorElement(), ASSERT_NO_EXCEPTION); |
| 322 CharacterIterator it(range.get()); | 329 CharacterIterator it(range.get()); |
| 323 it.advance(index - 1); | 330 it.advance(index - 1); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 void HTMLTextFormControlElement::restoreCachedSelection() | 477 void HTMLTextFormControlElement::restoreCachedSelection() |
| 471 { | 478 { |
| 472 setSelectionRange(m_cachedSelectionStart, m_cachedSelectionEnd, m_cachedSele
ctionDirection); | 479 setSelectionRange(m_cachedSelectionStart, m_cachedSelectionEnd, m_cachedSele
ctionDirection); |
| 473 } | 480 } |
| 474 | 481 |
| 475 void HTMLTextFormControlElement::selectionChanged(bool userTriggered) | 482 void HTMLTextFormControlElement::selectionChanged(bool userTriggered) |
| 476 { | 483 { |
| 477 if (!renderer() || !isTextFormControl()) | 484 if (!renderer() || !isTextFormControl()) |
| 478 return; | 485 return; |
| 479 | 486 |
| 480 // selectionStart() or selectionEnd() will return cached selection when this
node doesn't have focus | 487 if (document().focusedElement() == this) |
| 481 cacheSelection(computeSelectionStart(), computeSelectionEnd(), computeSelect
ionDirection()); | 488 cacheSelection(computeSelectionStart(), computeSelectionEnd(), computeSe
lectionDirection()); |
| 482 | 489 |
| 483 if (LocalFrame* frame = document().frame()) { | 490 if (LocalFrame* frame = document().frame()) { |
| 484 if (frame->selection().isRange() && userTriggered) | 491 if (frame->selection().isRange() && userTriggered) |
| 485 dispatchEvent(Event::createBubble(EventTypeNames::select)); | 492 dispatchEvent(Event::createBubble(EventTypeNames::select)); |
| 486 } | 493 } |
| 487 } | 494 } |
| 488 | 495 |
| 489 void HTMLTextFormControlElement::parseAttribute(const QualifiedName& name, const
AtomicString& value) | 496 void HTMLTextFormControlElement::parseAttribute(const QualifiedName& name, const
AtomicString& value) |
| 490 { | 497 { |
| 491 if (name == placeholderAttr) { | 498 if (name == placeholderAttr) { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 653 | 660 |
| 654 return "ltr"; | 661 return "ltr"; |
| 655 } | 662 } |
| 656 | 663 |
| 657 HTMLElement* HTMLTextFormControlElement::innerEditorElement() const | 664 HTMLElement* HTMLTextFormControlElement::innerEditorElement() const |
| 658 { | 665 { |
| 659 return toHTMLElement(userAgentShadowRoot()->getElementById(ShadowElementName
s::innerEditor())); | 666 return toHTMLElement(userAgentShadowRoot()->getElementById(ShadowElementName
s::innerEditor())); |
| 660 } | 667 } |
| 661 | 668 |
| 662 } // namespace Webcore | 669 } // namespace Webcore |
| OLD | NEW |