Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: Source/core/html/HTMLTextFormControlElement.cpp

Issue 357603003: Add functions searching a word boundary without VisualPosition to HTMLTextFormControlElement. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Update Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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();
Yuta Kitamura 2014/07/04 06:12:35 This assertion fails to ASSERT if the function exi
yoichio 2014/07/04 07:25:26 Done.
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)
Yuta Kitamura 2014/07/04 06:12:35 Prefer enums to bools on function parameters. The
yoichio 2014/07/04 07:25:26 Done.
700 {
701 Vector<UChar> characters;
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 for (unsigned offset = start; offset < end; ++offset)
719 characters.append(text->data()[offset]);
720 const unsigned length = end - start;
721
722 lengthList.append(length);
723 textList.append(text);
724 }
725
726 if (isEndNode)
727 break;
728 }
729
730 if (!characters.size())
731 return startPosition;
732
733 int start, end;
734 if (!findStart && characters.data()[0] == '\n') {
Yuta Kitamura 2014/07/04 06:12:35 ".data()" isn't necessary.
yoichio 2014/07/04 07:25:26 Done.
735 // findWordBoundary("\ntext", 0, &start, &end) assigns 1 to |end| but we expect 0 at the case.
736 start = 0;
737 end = 0;
738 } else {
739 findWordBoundary(characters.data(), characters.size(), findStart ? chara cters.size() : 0, &start, &end);
740 }
741 ASSERT(start >= 0);
742 ASSERT(end >= 0);
743 unsigned remainingOffset = findStart ? start : end;
744 // Find position.
745 for (unsigned i = 0; i < lengthList.size(); ++i) {
746 if (remainingOffset <= lengthList[i])
747 return Position(textList[i], (textList[i] == startPosition.anchorNod e()) ? remainingOffset + startPosition.offsetInContainerNode() : remainingOffset );
748 remainingOffset -= lengthList[i];
749 }
750
751 ASSERT_NOT_REACHED();
752 return Position();
753 }
754
755 Position HTMLTextFormControlElement::startOfWord(const Position& position)
756 {
757 const HTMLTextFormControlElement* textFormControl = enclosingTextFormControl (position);
758 ASSERT(textFormControl);
759 HTMLElement* innerEditor = textFormControl->innerEditorElement();
760
761 const Position startPosition = startOfSentence(position);
762 if (startPosition == position)
763 return position;
764 const Position endPosition = (position.anchorNode() == innerEditor) ? innerN odePosition(position) : position;
765
766 return findWordBoundary(innerEditor, startPosition, endPosition, true);
767 }
768
769 Position HTMLTextFormControlElement::endOfWord(const Position& position)
770 {
771 const HTMLTextFormControlElement* textFormControl = enclosingTextFormControl (position);
772 ASSERT(textFormControl);
773 HTMLElement* innerEditor = textFormControl->innerEditorElement();
774
775
Yuta Kitamura 2014/07/04 06:12:35 nit: Unnecessary blank line.
yoichio 2014/07/04 07:25:26 Done.
776 const Position endPosition = endOfSentence(position);
777 if (endPosition == position)
778 return position;
779 const Position startPosition = (position.anchorNode() == innerEditor) ? inne rNodePosition(position) : position;
780
781 return findWordBoundary(innerEditor, startPosition, endPosition, false);
782 }
783
784 static int findLastLineBreak(const Text& text, int offset)
785 {
786 for (; offset >= 0; --offset) {
787 if (text.data()[offset] == '\n')
788 return offset;
Yuta Kitamura 2014/07/04 06:12:35 Use String::reverseFind().
yoichio 2014/07/04 07:25:26 Done. Removed this function and inlined at call.
789 }
790 return -1;
791 }
792
793 static Position endOfPrevious(const Node& node, HTMLElement* innerEditor)
794 {
795 Node* prev = NodeTraversal::previous(node, innerEditor);
Yuta Kitamura 2014/07/04 06:12:35 Please don't use abbreviated word in names.
yoichio 2014/07/04 07:25:26 Done.
796 if (!prev)
797 return Position();
798
799 if (prev->hasTagName(brTag))
800 return Position(prev, Position::PositionIsAfterAnchor);
801
802 if (prev->isTextNode()) {
803 Text* textNode = toText(prev);
804 return Position(textNode, textNode->length());
805 }
806
807 return Position();
808 }
809
810 static Position previousIfPositionIsAfterLineBreak(const Position& position, HTM LElement* innerEditor)
811 {
812 if (position.isNull())
813 return Position();
814
815 // Move back if position is just after line break.
816 if (isHTMLBRElement(*position.anchorNode())) {
817 switch (position.anchorType()) {
818 case Position::PositionIsAfterAnchor:
819 return Position(position.anchorNode(), Position::PositionIsBeforeAnc hor);
820 case Position::PositionIsBeforeAnchor:
821 return previousIfPositionIsAfterLineBreak(endOfPrevious(*position.an chorNode(), innerEditor), innerEditor);
822 default:
823 ASSERT_NOT_REACHED();
824 }
825 } else if (position.anchorNode()->isTextNode()) {
826 Text* textNode = toText(position.anchorNode());
827 unsigned offset = position.offsetInContainerNode();
828 if (textNode->length() == 0 || offset <= 0) {
829 return previousIfPositionIsAfterLineBreak(endOfPrevious(*position.an chorNode(), innerEditor), innerEditor);
830 }
831
832 if (offset <= textNode->length() && textNode->data()[offset - 1] == '\n' ) {
833 return Position(textNode, offset - 1);
834 }
835 }
836
837 return position;
838 }
839
840 static inline Position startOfInnerText(const HTMLTextFormControlElement* textFo rmControl)
841 {
842 return Position(textFormControl->innerEditorElement(), 0, Position::Position IsOffsetInAnchor);
843 }
844
845 Position HTMLTextFormControlElement::startOfSentence(const Position& position)
846 {
847 HTMLTextFormControlElement* textFormControl = enclosingTextFormControl(posit ion);
848 ASSERT(textFormControl);
849
850 HTMLElement* innerEditor = textFormControl->innerEditorElement();
851 if (!innerEditor->childNodes()->length())
852 return startOfInnerText(textFormControl);
853
854 const Position innerPosition = position.anchorNode() == innerEditor ? innerN odePosition(position) : position;
855 const Position pivotPosition = previousIfPositionIsAfterLineBreak(innerPosit ion, innerEditor);
856 if (pivotPosition.isNull())
857 return startOfInnerText(textFormControl);
858
859 for (Node* node = pivotPosition.anchorNode(); node; node = NodeTraversal::pr evious(*node, innerEditor)) {
860 bool isPivotNode = (node == pivotPosition.anchorNode());
861
862 if (node->isTextNode()) {
863 Text* textNode = toText(node);
864 int lastLineBreak = findLastLineBreak(*textNode, isPivotNode ? pivot Position.offsetInContainerNode() - 1 : (int)textNode->length() - 1);
865 if (lastLineBreak >= 0)
866 return Position(textNode, lastLineBreak + 1);
867 } else if (isHTMLBRElement(node) && (!isPivotNode || pivotPosition.ancho rType() == Position::PositionIsAfterAnchor)) {
868 return Position(node, Position::PositionIsAfterAnchor);
869 }
870 }
871 return startOfInnerText(textFormControl);
872 }
873
874 static int findFirstLineBreak(const Text& text, int offset)
875 {
876 for (; (unsigned)offset < text.length(); ++offset) {
877 if (text.data()[offset] == '\n')
878 return offset;
879 }
880 return -1;
881 }
882
883 static Position endOfInnerText(const HTMLTextFormControlElement* textFormControl )
884 {
885 HTMLElement* innerEditor = textFormControl->innerEditorElement();
886 return Position(innerEditor, innerEditor->childNodes()->length(), Position:: PositionIsOffsetInAnchor);
887 }
888
889 Position HTMLTextFormControlElement::endOfSentence(const Position& position)
890 {
891 HTMLTextFormControlElement* textFormControl = enclosingTextFormControl(posit ion);
892 ASSERT(textFormControl);
893
894 HTMLElement* innerEditor = textFormControl->innerEditorElement();
895 if (!innerEditor->childNodes()->length())
896 return startOfInnerText(textFormControl);
897
898 const Position pivotPosition = position.anchorNode() == innerEditor ? innerN odePosition(position) : position;
899 if (pivotPosition.isNull())
900 return startOfInnerText(textFormControl);
901
902 for (Node* node = pivotPosition.anchorNode(); node; node = NodeTraversal::ne xt(*node, innerEditor)) {
903 bool isPivotNode = (node == pivotPosition.anchorNode());
904
905 if (node->isTextNode()) {
906 Text* textNode = toText(node);
907 int firstLineBreak = findFirstLineBreak(*textNode, isPivotNode ? piv otPosition.offsetInContainerNode() : 0);
908 if (firstLineBreak >= 0)
909 return Position(textNode, firstLineBreak + 1);
910 } else if (isHTMLBRElement(node)) {
911 return Position(node, Position::PositionIsAfterAnchor);
912 }
913 }
914 return endOfInnerText(textFormControl);
915 }
916
662 } // namespace Webcore 917 } // namespace Webcore
OLDNEW
« no previous file with comments | « Source/core/html/HTMLTextFormControlElement.h ('k') | Source/core/html/HTMLTextFormControlElementTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698