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 HTMLElement* element = toHTMLElement(innerPosition.anchorNode()); | |
667 ASSERT(element); | |
668 RefPtrWillBeRawPtr<NodeList> childNodes = element->childNodes(); | |
669 if (!childNodes->length()) | |
670 return Position(element, 0, Position::PositionIsOffsetInAnchor); | |
671 | |
672 unsigned offset = 0; | |
673 | |
674 switch (innerPosition.anchorType()) { | |
675 case Position::PositionIsOffsetInAnchor: | |
676 offset = std::max(0, std::min(innerPosition.offsetInContainerNode(), sta tic_cast<int>(childNodes->length()))); | |
677 break; | |
678 case Position::PositionIsAfterChildren: | |
679 offset = childNodes->length(); | |
680 break; | |
681 case Position::PositionIsBeforeAnchor: | |
682 case Position::PositionIsAfterAnchor: | |
683 ASSERT_NOT_REACHED(); | |
684 break; | |
685 default: | |
686 break; | |
687 } | |
688 | |
689 if (offset == childNodes->length()) | |
690 return Position(element->lastChild(), Position::PositionIsAfterAnchor); | |
691 | |
692 Node* node = childNodes->item(offset); | |
693 if (node->isTextNode()) | |
694 return Position(toText(node), 0); | |
695 | |
696 return Position(node, Position::PositionIsBeforeAnchor); | |
697 } | |
698 | |
699 static Position findWordBoundary(const HTMLElement* innerEditor, const Position& startPosition, const Position& endPosition, bool findStart) | |
700 { | |
701 StringBuilder concatTexts; | |
702 Vector<unsigned> lengthList; | |
703 Vector<Text*> textList; | |
704 | |
705 if (startPosition.anchorNode()->isTextNode()) | |
706 ASSERT(startPosition.anchorType() == Position::PositionIsOffsetInAnchor) ; | |
707 if (endPosition.anchorNode()->isTextNode()) | |
708 ASSERT(endPosition.anchorType() == Position::PositionIsOffsetInAnchor); | |
709 | |
710 // Trace text nodes. | |
711 for (Node* node = startPosition.anchorNode(); node; node = NodeTraversal::ne xt(*node, innerEditor)) { | |
712 bool isStartNode = node == startPosition.anchorNode(); | |
713 bool isEndNode = node == endPosition.anchorNode(); | |
714 if (node->isTextNode()) { | |
715 Text* text = toText(node); | |
716 const unsigned start = isStartNode ? startPosition.offsetInContainer Node() : 0; | |
717 const unsigned end = isEndNode ? endPosition.offsetInContainerNode() : text->data().length(); | |
718 const unsigned length = end - start; | |
719 | |
720 concatTexts.append(text->data(), start, length); | |
721 lengthList.append(length); | |
722 textList.append(text); | |
723 } | |
724 | |
725 if (isEndNode) | |
726 break; | |
727 } | |
728 | |
729 if (!concatTexts.length()) | |
730 return startPosition; | |
731 | |
732 int start, end; | |
733 if (!findStart && concatTexts[0] == '\n') { | |
734 // findWordBoundary("\ntext", 0, &start, &end) assigns 1 to |end| but we expect 0 at the case. | |
735 start = 0; | |
736 end = 0; | |
737 } else { | |
738 Vector<UChar> characters; | |
739 concatTexts.toString().appendTo(characters); | |
740 findWordBoundary(characters.data(), characters.size(), findStart ? chara cters.size() : 0, &start, &end); | |
741 } | |
742 ASSERT(start >= 0); | |
743 ASSERT(end >= 0); | |
744 unsigned remainingOffset = findStart ? start : end; | |
745 // Find position. | |
746 for (unsigned i = 0; i < lengthList.size(); ++i) { | |
747 if (remainingOffset <= lengthList[i]) | |
748 return Position(textList[i], (textList[i] == startPosition.anchorNod e()) ? remainingOffset + startPosition.offsetInContainerNode() : remainingOffset ); | |
749 remainingOffset -= lengthList[i]; | |
750 } | |
751 | |
752 ASSERT_NOT_REACHED(); | |
753 return Position(); | |
754 } | |
755 | |
756 Position HTMLTextFormControlElement::startOfWord(const Position& position) | |
757 { | |
758 const HTMLTextFormControlElement* textFormControl = enclosingTextFormControl (position); | |
759 ASSERT(textFormControl); | |
760 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
761 | |
762 const Position startPosition = startOfSentence(position); | |
763 if (startPosition == position) | |
764 return position; | |
765 const Position endPosition = (position.anchorNode() == innerEditor) ? innerN odePosition(position) : position; | |
766 | |
767 return findWordBoundary(innerEditor, startPosition, endPosition, true); | |
768 } | |
769 | |
770 Position HTMLTextFormControlElement::endOfWord(const Position& position) | |
771 { | |
772 const HTMLTextFormControlElement* textFormControl = enclosingTextFormControl (position); | |
773 ASSERT(textFormControl); | |
774 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
775 | |
776 | |
777 const Position endPosition = endOfSentence(position); | |
778 if (endPosition == position) | |
779 return position; | |
780 const Position startPosition = (position.anchorNode() == innerEditor) ? inne rNodePosition(position) : position; | |
781 | |
782 return findWordBoundary(innerEditor, startPosition, endPosition, false); | |
783 } | |
784 | |
785 static int findLastLineBreak(const Text& text, int offset) | |
786 { | |
787 for (; offset >= 0; --offset) { | |
788 if (text.data()[offset] == '\n') | |
789 return offset; | |
790 } | |
791 return -1; | |
792 } | |
793 | |
794 static Position endOfPrevious(const Node& node, HTMLElement* innerEditor) | |
795 { | |
796 Node* prev = NodeTraversal::previous(node, innerEditor); | |
797 if (!prev) | |
798 return Position(); | |
799 | |
800 if (prev->hasTagName(brTag)) | |
801 return Position(prev, Position::PositionIsAfterAnchor); | |
802 | |
803 if (prev->isTextNode()) { | |
804 Text* textNode = toText(prev); | |
yosin_UTC9
2014/07/04 06:14:50
nit: Could you remove |textNode| varaible?
yoichio
2014/07/04 07:25:26
Done.
| |
805 return Position(textNode, textNode->length()); | |
806 } | |
807 | |
808 return Position(); | |
809 } | |
810 | |
811 static Position previousIfPositionIsAfterLineBreak(const Position& position, HTM LElement* innerEditor) | |
812 { | |
813 if (position.isNull()) | |
814 return Position(); | |
815 | |
816 // Move back if position is just after line break. | |
817 if (isHTMLBRElement(*position.anchorNode())) { | |
818 switch (position.anchorType()) { | |
819 case Position::PositionIsAfterAnchor: | |
820 return Position(position.anchorNode(), Position::PositionIsBeforeAnc hor); | |
821 case Position::PositionIsBeforeAnchor: | |
822 return previousIfPositionIsAfterLineBreak(endOfPrevious(*position.an chorNode(), innerEditor), innerEditor); | |
823 default: | |
824 ASSERT_NOT_REACHED(); | |
825 } | |
826 } else if (position.anchorNode()->isTextNode()) { | |
827 Text* textNode = toText(position.anchorNode()); | |
828 unsigned offset = position.offsetInContainerNode(); | |
829 if (textNode->length() == 0 || offset <= 0) { | |
830 return previousIfPositionIsAfterLineBreak(endOfPrevious(*position.an chorNode(), innerEditor), innerEditor); | |
831 } | |
832 | |
833 if (offset <= textNode->length() && textNode->data()[offset - 1] == '\n' ) { | |
834 return Position(textNode, offset - 1); | |
835 } | |
836 } | |
837 | |
838 return position; | |
839 } | |
840 | |
841 static inline Position startOfInnerText(const HTMLTextFormControlElement* textFo rmControl) | |
842 { | |
843 return Position(textFormControl->innerEditorElement(), 0, Position::Position IsOffsetInAnchor); | |
844 } | |
845 | |
846 Position HTMLTextFormControlElement::startOfSentence(const Position& position) | |
847 { | |
848 HTMLTextFormControlElement* textFormControl = enclosingTextFormControl(posit ion); | |
849 ASSERT(textFormControl); | |
850 | |
851 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
852 if (!innerEditor->childNodes()->length()) | |
853 return startOfInnerText(textFormControl); | |
854 | |
855 const Position innerPosition = position.anchorNode() == innerEditor ? innerN odePosition(position) : position; | |
856 const Position pivotPosition = previousIfPositionIsAfterLineBreak(innerPosit ion, innerEditor); | |
857 if (pivotPosition.isNull()) | |
858 return startOfInnerText(textFormControl); | |
859 | |
860 for (Node* node = pivotPosition.anchorNode(); node; node = NodeTraversal::pr evious(*node, innerEditor)) { | |
861 bool isPivotNode = (node == pivotPosition.anchorNode()); | |
862 | |
863 if (node->isTextNode()) { | |
864 Text* textNode = toText(node); | |
865 int lastLineBreak = findLastLineBreak(*textNode, isPivotNode ? pivot Position.offsetInContainerNode() - 1 : (int)textNode->length() - 1); | |
866 if (lastLineBreak >= 0) | |
867 return Position(textNode, lastLineBreak + 1); | |
868 } else if (isHTMLBRElement(node) && (!isPivotNode || pivotPosition.ancho rType() == Position::PositionIsAfterAnchor)) { | |
yosin_UTC9
2014/07/04 06:14:50
nit: If we move this if-statement before |if (node
yoichio
2014/07/04 07:25:26
Done.
| |
869 return Position(node, Position::PositionIsAfterAnchor); | |
870 } | |
871 } | |
872 return startOfInnerText(textFormControl); | |
873 } | |
874 | |
875 static int findFirstLineBreak(const Text& text, int offset) | |
876 { | |
877 for (; (unsigned)offset < text.length(); ++offset) { | |
878 if (text.data()[offset] == '\n') | |
879 return offset; | |
880 } | |
881 return -1; | |
882 } | |
883 | |
884 static Position endOfInnerText(const HTMLTextFormControlElement* textFormControl ) | |
885 { | |
886 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
887 return Position(innerEditor, innerEditor->childNodes()->length(), Position:: PositionIsOffsetInAnchor); | |
888 } | |
889 | |
890 Position HTMLTextFormControlElement::endOfSentence(const Position& position) | |
891 { | |
892 HTMLTextFormControlElement* textFormControl = enclosingTextFormControl(posit ion); | |
893 ASSERT(textFormControl); | |
894 | |
895 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
896 if (!innerEditor->childNodes()->length()) | |
897 return startOfInnerText(textFormControl); | |
898 | |
899 const Position pivotPosition = position.anchorNode() == innerEditor ? innerN odePosition(position) : position; | |
900 if (pivotPosition.isNull()) | |
901 return startOfInnerText(textFormControl); | |
902 | |
903 for (Node* node = pivotPosition.anchorNode(); node; node = NodeTraversal::ne xt(*node, innerEditor)) { | |
904 bool isPivotNode = (node == pivotPosition.anchorNode()); | |
yosin_UTC9
2014/07/04 06:14:50
nit: We don't have "()".
yoichio
2014/07/04 07:25:26
Done.
| |
905 | |
906 if (node->isTextNode()) { | |
907 Text* textNode = toText(node); | |
908 int firstLineBreak = findFirstLineBreak(*textNode, isPivotNode ? piv otPosition.offsetInContainerNode() : 0); | |
909 if (firstLineBreak >= 0) | |
910 return Position(textNode, firstLineBreak + 1); | |
911 } else if (isHTMLBRElement(node)) { | |
yosin_UTC9
2014/07/04 06:14:50
nit: If we move this if-statement before |if (node
yoichio
2014/07/04 07:25:26
Done.
| |
912 return Position(node, Position::PositionIsAfterAnchor); | |
913 } | |
914 } | |
915 return endOfInnerText(textFormControl); | |
916 } | |
917 | |
662 } // namespace Webcore | 918 } // namespace Webcore |
OLD | NEW |