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 12 matching lines...) Expand all Loading... |
23 */ | 23 */ |
24 | 24 |
25 #include "config.h" | 25 #include "config.h" |
26 #include "core/html/HTMLTextFormControlElement.h" | 26 #include "core/html/HTMLTextFormControlElement.h" |
27 | 27 |
28 #include "bindings/core/v8/ExceptionState.h" | 28 #include "bindings/core/v8/ExceptionState.h" |
29 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 29 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
30 #include "core/HTMLNames.h" | 30 #include "core/HTMLNames.h" |
31 #include "core/accessibility/AXObjectCache.h" | 31 #include "core/accessibility/AXObjectCache.h" |
32 #include "core/dom/Document.h" | 32 #include "core/dom/Document.h" |
| 33 #include "core/dom/NodeList.h" |
33 #include "core/dom/NodeTraversal.h" | 34 #include "core/dom/NodeTraversal.h" |
34 #include "core/dom/Text.h" | 35 #include "core/dom/Text.h" |
35 #include "core/dom/shadow/ShadowRoot.h" | 36 #include "core/dom/shadow/ShadowRoot.h" |
36 #include "core/editing/FrameSelection.h" | 37 #include "core/editing/FrameSelection.h" |
37 #include "core/editing/TextIterator.h" | 38 #include "core/editing/TextIterator.h" |
38 #include "core/events/Event.h" | 39 #include "core/events/Event.h" |
39 #include "core/frame/LocalFrame.h" | 40 #include "core/frame/LocalFrame.h" |
40 #include "core/frame/UseCounter.h" | 41 #include "core/frame/UseCounter.h" |
41 #include "core/html/HTMLBRElement.h" | 42 #include "core/html/HTMLBRElement.h" |
42 #include "core/html/shadow/ShadowElementNames.h" | 43 #include "core/html/shadow/ShadowElementNames.h" |
43 #include "core/rendering/RenderBlock.h" | 44 #include "core/rendering/RenderBlock.h" |
44 #include "core/rendering/RenderTheme.h" | 45 #include "core/rendering/RenderTheme.h" |
45 #include "platform/heap/Handle.h" | 46 #include "platform/heap/Handle.h" |
| 47 #include "platform/text/TextBoundaries.h" |
46 #include "wtf/text/StringBuilder.h" | 48 #include "wtf/text/StringBuilder.h" |
47 | 49 |
48 namespace WebCore { | 50 namespace WebCore { |
49 | 51 |
50 using namespace HTMLNames; | 52 using namespace HTMLNames; |
51 | 53 |
52 HTMLTextFormControlElement::HTMLTextFormControlElement(const QualifiedName& tagN
ame, Document& doc, HTMLFormElement* form) | 54 HTMLTextFormControlElement::HTMLTextFormControlElement(const QualifiedName& tagN
ame, Document& doc, HTMLFormElement* form) |
53 : HTMLFormControlElementWithState(tagName, doc, form) | 55 : HTMLFormControlElementWithState(tagName, doc, form) |
54 , m_lastChangeWasUserEdit(false) | 56 , m_lastChangeWasUserEdit(false) |
55 , m_cachedSelectionStart(0) | 57 , m_cachedSelectionStart(0) |
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 } | 654 } |
653 | 655 |
654 return "ltr"; | 656 return "ltr"; |
655 } | 657 } |
656 | 658 |
657 HTMLElement* HTMLTextFormControlElement::innerEditorElement() const | 659 HTMLElement* HTMLTextFormControlElement::innerEditorElement() const |
658 { | 660 { |
659 return toHTMLElement(userAgentShadowRoot()->getElementById(ShadowElementName
s::innerEditor())); | 661 return toHTMLElement(userAgentShadowRoot()->getElementById(ShadowElementName
s::innerEditor())); |
660 } | 662 } |
661 | 663 |
| 664 static Position innerNodePosition(const Position& innerPosition) |
| 665 { |
| 666 ASSERT(innerPosition.anchorType() != Position::PositionIsBeforeAnchor); |
| 667 ASSERT(innerPosition.anchorType() != Position::PositionIsAfterAnchor); |
| 668 HTMLElement* element = toHTMLElement(innerPosition.anchorNode()); |
| 669 ASSERT(element); |
| 670 RefPtrWillBeRawPtr<NodeList> childNodes = element->childNodes(); |
| 671 if (!childNodes->length()) |
| 672 return Position(element, 0, Position::PositionIsOffsetInAnchor); |
| 673 |
| 674 unsigned offset = 0; |
| 675 |
| 676 switch (innerPosition.anchorType()) { |
| 677 case Position::PositionIsOffsetInAnchor: |
| 678 offset = std::max(0, std::min(innerPosition.offsetInContainerNode(), sta
tic_cast<int>(childNodes->length()))); |
| 679 break; |
| 680 case Position::PositionIsAfterChildren: |
| 681 offset = childNodes->length(); |
| 682 break; |
| 683 default: |
| 684 break; |
| 685 } |
| 686 |
| 687 if (offset == childNodes->length()) |
| 688 return Position(element->lastChild(), Position::PositionIsAfterAnchor); |
| 689 |
| 690 Node* node = childNodes->item(offset); |
| 691 if (node->isTextNode()) |
| 692 return Position(toText(node), 0); |
| 693 |
| 694 return Position(node, Position::PositionIsBeforeAnchor); |
| 695 } |
| 696 |
| 697 enum FindOption { |
| 698 FindStart, |
| 699 FindEnd |
| 700 }; |
| 701 |
| 702 static Position findWordBoundary(const HTMLElement* innerEditor, const Position&
startPosition, const Position& endPosition, FindOption findOption) |
| 703 { |
| 704 StringBuilder concatTexts; |
| 705 Vector<unsigned> lengthList; |
| 706 Vector<Text*> textList; |
| 707 |
| 708 if (startPosition.anchorNode()->isTextNode()) |
| 709 ASSERT(startPosition.anchorType() == Position::PositionIsOffsetInAnchor)
; |
| 710 if (endPosition.anchorNode()->isTextNode()) |
| 711 ASSERT(endPosition.anchorType() == Position::PositionIsOffsetInAnchor); |
| 712 |
| 713 // Traverse text nodes. |
| 714 for (Node* node = startPosition.anchorNode(); node; node = NodeTraversal::ne
xt(*node, innerEditor)) { |
| 715 bool isStartNode = node == startPosition.anchorNode(); |
| 716 bool isEndNode = node == endPosition.anchorNode(); |
| 717 if (node->isTextNode()) { |
| 718 Text* text = toText(node); |
| 719 const unsigned start = isStartNode ? startPosition.offsetInContainer
Node() : 0; |
| 720 const unsigned end = isEndNode ? endPosition.offsetInContainerNode()
: text->data().length(); |
| 721 const unsigned length = end - start; |
| 722 |
| 723 concatTexts.append(text->data(), start, length); |
| 724 lengthList.append(length); |
| 725 textList.append(text); |
| 726 } |
| 727 |
| 728 if (isEndNode) |
| 729 break; |
| 730 } |
| 731 |
| 732 if (concatTexts.length() == 0) |
| 733 return startPosition; |
| 734 |
| 735 int start, end; |
| 736 if (findOption == FindEnd && concatTexts[0] == '\n') { |
| 737 // findWordBoundary("\ntext", 0, &start, &end) assigns 1 to |end| but |
| 738 // we expect 0 at the case. |
| 739 start = 0; |
| 740 end = 0; |
| 741 } else { |
| 742 Vector<UChar> characters; |
| 743 concatTexts.toString().appendTo(characters); |
| 744 findWordBoundary(characters.data(), characters.size(), findOption == Fin
dStart ? characters.size() : 0, &start, &end); |
| 745 } |
| 746 ASSERT(start >= 0); |
| 747 ASSERT(end >= 0); |
| 748 unsigned remainingOffset = findOption == FindStart ? start : end; |
| 749 // Find position. |
| 750 for (unsigned i = 0; i < lengthList.size(); ++i) { |
| 751 if (remainingOffset <= lengthList[i]) |
| 752 return Position(textList[i], (textList[i] == startPosition.anchorNod
e()) ? remainingOffset + startPosition.offsetInContainerNode() : remainingOffset
); |
| 753 remainingOffset -= lengthList[i]; |
| 754 } |
| 755 |
| 756 ASSERT_NOT_REACHED(); |
| 757 return Position(); |
| 758 } |
| 759 |
| 760 Position HTMLTextFormControlElement::startOfWord(const Position& position) |
| 761 { |
| 762 const HTMLTextFormControlElement* textFormControl = enclosingTextFormControl
(position); |
| 763 ASSERT(textFormControl); |
| 764 HTMLElement* innerEditor = textFormControl->innerEditorElement(); |
| 765 |
| 766 const Position startPosition = startOfSentence(position); |
| 767 if (startPosition == position) |
| 768 return position; |
| 769 const Position endPosition = (position.anchorNode() == innerEditor) ? innerN
odePosition(position) : position; |
| 770 |
| 771 return findWordBoundary(innerEditor, startPosition, endPosition, FindStart); |
| 772 } |
| 773 |
| 774 Position HTMLTextFormControlElement::endOfWord(const Position& position) |
| 775 { |
| 776 const HTMLTextFormControlElement* textFormControl = enclosingTextFormControl
(position); |
| 777 ASSERT(textFormControl); |
| 778 HTMLElement* innerEditor = textFormControl->innerEditorElement(); |
| 779 |
| 780 const Position endPosition = endOfSentence(position); |
| 781 if (endPosition == position) |
| 782 return position; |
| 783 const Position startPosition = (position.anchorNode() == innerEditor) ? inne
rNodePosition(position) : position; |
| 784 |
| 785 return findWordBoundary(innerEditor, startPosition, endPosition, FindEnd); |
| 786 } |
| 787 |
| 788 static Position endOfPrevious(const Node& node, HTMLElement* innerEditor) |
| 789 { |
| 790 Node* previousNode = NodeTraversal::previous(node, innerEditor); |
| 791 if (!previousNode) |
| 792 return Position(); |
| 793 |
| 794 if (isHTMLBRElement(previousNode)) |
| 795 return Position(previousNode, Position::PositionIsAfterAnchor); |
| 796 |
| 797 if (previousNode->isTextNode()) |
| 798 return Position(toText(previousNode), toText(previousNode)->length()); |
| 799 |
| 800 return Position(); |
| 801 } |
| 802 |
| 803 static Position previousIfPositionIsAfterLineBreak(const Position& position, HTM
LElement* innerEditor) |
| 804 { |
| 805 if (position.isNull()) |
| 806 return Position(); |
| 807 |
| 808 // Move back if position is just after line break. |
| 809 if (isHTMLBRElement(*position.anchorNode())) { |
| 810 switch (position.anchorType()) { |
| 811 case Position::PositionIsAfterAnchor: |
| 812 return Position(position.anchorNode(), Position::PositionIsBeforeAnc
hor); |
| 813 case Position::PositionIsBeforeAnchor: |
| 814 return previousIfPositionIsAfterLineBreak(endOfPrevious(*position.an
chorNode(), innerEditor), innerEditor); |
| 815 default: |
| 816 ASSERT_NOT_REACHED(); |
| 817 } |
| 818 } else if (position.anchorNode()->isTextNode()) { |
| 819 Text* textNode = toText(position.anchorNode()); |
| 820 unsigned offset = position.offsetInContainerNode(); |
| 821 if (textNode->length() == 0 || offset <= 0) { |
| 822 return previousIfPositionIsAfterLineBreak(endOfPrevious(*position.an
chorNode(), innerEditor), innerEditor); |
| 823 } |
| 824 |
| 825 if (offset <= textNode->length() && textNode->data()[offset - 1] == '\n'
) { |
| 826 return Position(textNode, offset - 1); |
| 827 } |
| 828 } |
| 829 |
| 830 return position; |
| 831 } |
| 832 |
| 833 static inline Position startOfInnerText(const HTMLTextFormControlElement* textFo
rmControl) |
| 834 { |
| 835 return Position(textFormControl->innerEditorElement(), 0, Position::Position
IsOffsetInAnchor); |
| 836 } |
| 837 |
| 838 Position HTMLTextFormControlElement::startOfSentence(const Position& position) |
| 839 { |
| 840 HTMLTextFormControlElement* textFormControl = enclosingTextFormControl(posit
ion); |
| 841 ASSERT(textFormControl); |
| 842 |
| 843 HTMLElement* innerEditor = textFormControl->innerEditorElement(); |
| 844 if (!innerEditor->childNodes()->length()) |
| 845 return startOfInnerText(textFormControl); |
| 846 |
| 847 const Position innerPosition = position.anchorNode() == innerEditor ? innerN
odePosition(position) : position; |
| 848 const Position pivotPosition = previousIfPositionIsAfterLineBreak(innerPosit
ion, innerEditor); |
| 849 if (pivotPosition.isNull()) |
| 850 return startOfInnerText(textFormControl); |
| 851 |
| 852 for (Node* node = pivotPosition.anchorNode(); node; node = NodeTraversal::pr
evious(*node, innerEditor)) { |
| 853 bool isPivotNode = (node == pivotPosition.anchorNode()); |
| 854 |
| 855 if (isHTMLBRElement(node) && (!isPivotNode || pivotPosition.anchorType()
== Position::PositionIsAfterAnchor)) |
| 856 return Position(node, Position::PositionIsAfterAnchor); |
| 857 |
| 858 if (node->isTextNode()) { |
| 859 Text* textNode = toText(node); |
| 860 size_t lastLineBreak = textNode->data().substring(0, isPivotNode ? p
ivotPosition.offsetInContainerNode() : textNode->length()).reverseFind('\n'); |
| 861 if (lastLineBreak != kNotFound) |
| 862 return Position(textNode, lastLineBreak + 1); |
| 863 } |
| 864 } |
| 865 return startOfInnerText(textFormControl); |
| 866 } |
| 867 |
| 868 static Position endOfInnerText(const HTMLTextFormControlElement* textFormControl
) |
| 869 { |
| 870 HTMLElement* innerEditor = textFormControl->innerEditorElement(); |
| 871 return Position(innerEditor, innerEditor->childNodes()->length(), Position::
PositionIsOffsetInAnchor); |
| 872 } |
| 873 |
| 874 Position HTMLTextFormControlElement::endOfSentence(const Position& position) |
| 875 { |
| 876 HTMLTextFormControlElement* textFormControl = enclosingTextFormControl(posit
ion); |
| 877 ASSERT(textFormControl); |
| 878 |
| 879 HTMLElement* innerEditor = textFormControl->innerEditorElement(); |
| 880 if (innerEditor->childNodes()->length() == 0) |
| 881 return startOfInnerText(textFormControl); |
| 882 |
| 883 const Position pivotPosition = position.anchorNode() == innerEditor ? innerN
odePosition(position) : position; |
| 884 if (pivotPosition.isNull()) |
| 885 return startOfInnerText(textFormControl); |
| 886 |
| 887 for (Node* node = pivotPosition.anchorNode(); node; node = NodeTraversal::ne
xt(*node, innerEditor)) { |
| 888 bool isPivotNode = node == pivotPosition.anchorNode(); |
| 889 |
| 890 if (isHTMLBRElement(node)) |
| 891 return Position(node, Position::PositionIsAfterAnchor); |
| 892 |
| 893 if (node->isTextNode()) { |
| 894 Text* textNode = toText(node); |
| 895 size_t firstLineBreak = textNode->data().find('\n', isPivotNode ? pi
votPosition.offsetInContainerNode() : 0); |
| 896 if (firstLineBreak != kNotFound) |
| 897 return Position(textNode, firstLineBreak + 1); |
| 898 } |
| 899 } |
| 900 return endOfInnerText(textFormControl); |
| 901 } |
| 902 |
662 } // namespace Webcore | 903 } // namespace Webcore |
OLD | NEW |