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 // Trace text nodes. | |
tkent
2014/07/07 23:54:18
nit: We don't use the word 'trace' for such operat
yoichio
2014/07/08 01:46:13
Done.
| |
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()) | |
tkent
2014/07/07 23:54:18
nit: You may write |if (concatTexts.length() == 0)
yoichio
2014/07/08 01:46:13
Done.
| |
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 we expect 0 at the case. | |
738 start = 0; | |
739 end = 0; | |
740 } else { | |
741 Vector<UChar> characters; | |
742 concatTexts.toString().appendTo(characters); | |
743 findWordBoundary(characters.data(), characters.size(), findOption == Fin dStart ? characters.size() : 0, &start, &end); | |
744 } | |
745 ASSERT(start >= 0); | |
746 ASSERT(end >= 0); | |
747 unsigned remainingOffset = findOption == FindStart ? start : end; | |
748 // Find position. | |
749 for (unsigned i = 0; i < lengthList.size(); ++i) { | |
750 if (remainingOffset <= lengthList[i]) | |
751 return Position(textList[i], (textList[i] == startPosition.anchorNod e()) ? remainingOffset + startPosition.offsetInContainerNode() : remainingOffset ); | |
752 remainingOffset -= lengthList[i]; | |
753 } | |
754 | |
755 ASSERT_NOT_REACHED(); | |
756 return Position(); | |
757 } | |
758 | |
759 Position HTMLTextFormControlElement::startOfWord(const Position& position) | |
760 { | |
761 const HTMLTextFormControlElement* textFormControl = enclosingTextFormControl (position); | |
762 ASSERT(textFormControl); | |
763 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
764 | |
765 const Position startPosition = startOfSentence(position); | |
766 if (startPosition == position) | |
767 return position; | |
768 const Position endPosition = (position.anchorNode() == innerEditor) ? innerN odePosition(position) : position; | |
769 | |
770 return findWordBoundary(innerEditor, startPosition, endPosition, FindStart); | |
771 } | |
772 | |
773 Position HTMLTextFormControlElement::endOfWord(const Position& position) | |
774 { | |
775 const HTMLTextFormControlElement* textFormControl = enclosingTextFormControl (position); | |
776 ASSERT(textFormControl); | |
777 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
778 | |
779 const Position endPosition = endOfSentence(position); | |
780 if (endPosition == position) | |
781 return position; | |
782 const Position startPosition = (position.anchorNode() == innerEditor) ? inne rNodePosition(position) : position; | |
783 | |
784 return findWordBoundary(innerEditor, startPosition, endPosition, FindEnd); | |
785 } | |
786 | |
787 static Position endOfPrevious(const Node& node, HTMLElement* innerEditor) | |
788 { | |
789 Node* previousNode = NodeTraversal::previous(node, innerEditor); | |
790 if (!previousNode) | |
791 return Position(); | |
792 | |
793 if (previousNode->hasTagName(brTag)) | |
tkent
2014/07/07 23:54:18
This is inconsistent with other functions. They u
yoichio
2014/07/08 01:46:13
Done.
| |
794 return Position(previousNode, Position::PositionIsAfterAnchor); | |
795 | |
796 if (previousNode->isTextNode()) | |
797 return Position(toText(previousNode), toText(previousNode)->length()); | |
798 | |
799 return Position(); | |
800 } | |
801 | |
802 static Position previousIfPositionIsAfterLineBreak(const Position& position, HTM LElement* innerEditor) | |
803 { | |
804 if (position.isNull()) | |
805 return Position(); | |
806 | |
807 // Move back if position is just after line break. | |
808 if (isHTMLBRElement(*position.anchorNode())) { | |
809 switch (position.anchorType()) { | |
810 case Position::PositionIsAfterAnchor: | |
811 return Position(position.anchorNode(), Position::PositionIsBeforeAnc hor); | |
812 case Position::PositionIsBeforeAnchor: | |
813 return previousIfPositionIsAfterLineBreak(endOfPrevious(*position.an chorNode(), innerEditor), innerEditor); | |
814 default: | |
815 ASSERT_NOT_REACHED(); | |
816 } | |
817 } else if (position.anchorNode()->isTextNode()) { | |
818 Text* textNode = toText(position.anchorNode()); | |
819 unsigned offset = position.offsetInContainerNode(); | |
820 if (textNode->length() == 0 || offset <= 0) { | |
821 return previousIfPositionIsAfterLineBreak(endOfPrevious(*position.an chorNode(), innerEditor), innerEditor); | |
822 } | |
823 | |
824 if (offset <= textNode->length() && textNode->data()[offset - 1] == '\n' ) { | |
825 return Position(textNode, offset - 1); | |
826 } | |
827 } | |
828 | |
829 return position; | |
830 } | |
831 | |
832 static inline Position startOfInnerText(const HTMLTextFormControlElement* textFo rmControl) | |
833 { | |
834 return Position(textFormControl->innerEditorElement(), 0, Position::Position IsOffsetInAnchor); | |
835 } | |
836 | |
837 Position HTMLTextFormControlElement::startOfSentence(const Position& position) | |
838 { | |
839 HTMLTextFormControlElement* textFormControl = enclosingTextFormControl(posit ion); | |
840 ASSERT(textFormControl); | |
841 | |
842 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
843 if (!innerEditor->childNodes()->length()) | |
844 return startOfInnerText(textFormControl); | |
845 | |
846 const Position innerPosition = position.anchorNode() == innerEditor ? innerN odePosition(position) : position; | |
847 const Position pivotPosition = previousIfPositionIsAfterLineBreak(innerPosit ion, innerEditor); | |
848 if (pivotPosition.isNull()) | |
849 return startOfInnerText(textFormControl); | |
850 | |
851 for (Node* node = pivotPosition.anchorNode(); node; node = NodeTraversal::pr evious(*node, innerEditor)) { | |
852 bool isPivotNode = (node == pivotPosition.anchorNode()); | |
853 | |
854 if (isHTMLBRElement(node) && (!isPivotNode || pivotPosition.anchorType() == Position::PositionIsAfterAnchor)) | |
855 return Position(node, Position::PositionIsAfterAnchor); | |
856 | |
857 if (node->isTextNode()) { | |
858 Text* textNode = toText(node); | |
859 size_t lastLineBreak = textNode->data().substring(0, isPivotNode ? p ivotPosition.offsetInContainerNode() : textNode->length()).reverseFind('\n'); | |
860 if (lastLineBreak != kNotFound) | |
861 return Position(textNode, lastLineBreak + 1); | |
862 } | |
863 } | |
864 return startOfInnerText(textFormControl); | |
865 } | |
866 | |
867 static Position endOfInnerText(const HTMLTextFormControlElement* textFormControl ) | |
868 { | |
869 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
870 return Position(innerEditor, innerEditor->childNodes()->length(), Position:: PositionIsOffsetInAnchor); | |
871 } | |
872 | |
873 Position HTMLTextFormControlElement::endOfSentence(const Position& position) | |
874 { | |
875 HTMLTextFormControlElement* textFormControl = enclosingTextFormControl(posit ion); | |
876 ASSERT(textFormControl); | |
877 | |
878 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
879 if (!innerEditor->childNodes()->length()) | |
880 return startOfInnerText(textFormControl); | |
881 | |
882 const Position pivotPosition = position.anchorNode() == innerEditor ? innerN odePosition(position) : position; | |
883 if (pivotPosition.isNull()) | |
884 return startOfInnerText(textFormControl); | |
885 | |
886 for (Node* node = pivotPosition.anchorNode(); node; node = NodeTraversal::ne xt(*node, innerEditor)) { | |
887 bool isPivotNode = node == pivotPosition.anchorNode(); | |
888 | |
889 if (isHTMLBRElement(node)) | |
890 return Position(node, Position::PositionIsAfterAnchor); | |
891 | |
892 if (node->isTextNode()) { | |
893 Text* textNode = toText(node); | |
894 size_t firstLineBreak = textNode->data().find('\n', isPivotNode ? pi votPosition.offsetInContainerNode() : 0); | |
895 if (firstLineBreak != kNotFound) | |
896 return Position(textNode, firstLineBreak + 1); | |
897 } | |
898 } | |
899 return endOfInnerText(textFormControl); | |
900 } | |
901 | |
662 } // namespace Webcore | 902 } // namespace Webcore |
OLD | NEW |