Chromium Code Reviews| 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()); | |
|
Yuta Kitamura
2014/07/03 08:39:27
What if the anchor node isn't an HTMLElement?
yoichio
2014/07/04 01:29:44
This function is called with only innerEditorEleme
Yuta Kitamura
2014/07/04 06:12:34
Then that should be ASSERTed.
| |
| 667 RefPtrWillBeRawPtr<NodeList> childNodes = element->childNodes(); | |
| 668 if (!childNodes->length()) | |
| 669 return Position(element, 0, Position::PositionIsOffsetInAnchor); | |
| 670 | |
| 671 unsigned offset = 0; | |
| 672 | |
| 673 switch (innerPosition.anchorType()) { | |
| 674 case Position::PositionIsOffsetInAnchor: | |
| 675 offset = std::max(0, std::min(innerPosition.computeOffsetInContainerNode (), (int)childNodes->length())); | |
|
Yuta Kitamura
2014/07/03 08:39:27
1. Can the second argument of max() become negativ
yoichio
2014/07/04 01:29:43
Sorry, This must be offsetInContainerNode(), which
| |
| 676 break; | |
| 677 case Position::PositionIsAfterChildren: | |
| 678 offset = childNodes->length(); | |
| 679 break; | |
| 680 default: | |
|
Yuta Kitamura
2014/07/03 08:39:27
What about other Position types?
yoichio
2014/07/04 01:29:43
PositionIsBeforeChildren means offset is 0.
In ou
Yuta Kitamura
2014/07/04 05:23:34
Then you should ASSERT that. It's not okay to have
yoichio
2014/07/04 05:44:41
Done.
| |
| 681 break; | |
| 682 } | |
| 683 | |
| 684 if (offset == childNodes->length()) | |
| 685 return Position(childNodes->item(childNodes->length() - 1), Position::Po sitionIsAfterAnchor); | |
|
Yuta Kitamura
2014/07/03 08:39:27
item(...) can be lastChild().
yoichio
2014/07/04 01:29:44
Done.
| |
| 686 | |
| 687 Node* node = childNodes->item(offset); | |
| 688 if (node->isTextNode()) | |
| 689 return Position(toText(node), 0); | |
| 690 | |
| 691 return Position(node, Position::PositionIsBeforeAnchor); | |
| 692 } | |
| 693 | |
| 694 static Position findWordBoundary(const HTMLElement* innerEditor, const Position& startPosition, const Position endPosition, bool findStart) | |
|
Yuta Kitamura
2014/07/03 08:39:27
The type of the third argument should be "const Po
yoichio
2014/07/04 01:29:43
Done.
| |
| 695 { | |
| 696 Vector<UChar> characters; | |
| 697 Vector<unsigned> lengthList; | |
| 698 Vector<Text*> textList; | |
| 699 // Trace text nodes. | |
| 700 for (Node* node = startPosition.anchorNode(); node; node = NodeTraversal::ne xt(*node, innerEditor)) { | |
|
Yuta Kitamura
2014/07/03 08:39:27
If startPosition is of type AfterAnchor or AfterCh
yoichio
2014/07/04 01:29:44
A goal of this loop is to collect all strings in t
| |
| 701 bool isStartNode = node == startPosition.anchorNode(); | |
| 702 bool isEndNode = node == endPosition.anchorNode(); | |
|
Yuta Kitamura
2014/07/03 08:39:27
These booleans are smelly, too, if they are of typ
yoichio
2014/07/04 01:29:44
ditto
| |
| 703 if (node->isTextNode()) { | |
| 704 Text* text = toText(node); | |
| 705 const unsigned start = isStartNode ? startPosition.computeOffsetInCo ntainerNode() : 0; | |
| 706 const unsigned end = isEndNode ? endPosition.computeOffsetInContaine rNode() : text->data().length(); | |
| 707 for (unsigned offset = start; offset < end; ++offset) | |
| 708 characters.append(text->data()[offset]); | |
|
Yuta Kitamura
2014/07/03 08:39:27
This loop looks error-prone.
If you use StringBui
yoichio
2014/07/04 01:29:44
Since findWordBoundary takes UChar*, we need to co
Yuta Kitamura
2014/07/04 05:23:34
It's silly to write this kind of loop by hand. You
yoichio
2014/07/04 05:44:41
Done.
Thanks! I have not gotten the way :)
| |
| 709 const unsigned length = end - start; | |
| 710 | |
| 711 lengthList.append(length); | |
| 712 textList.append(text); | |
| 713 } | |
| 714 | |
| 715 if (isEndNode) | |
| 716 break; | |
| 717 } | |
| 718 | |
| 719 if (!characters.size()) | |
| 720 return startPosition; | |
| 721 | |
| 722 int start, end; | |
| 723 if (!findStart && characters.data()[0] == '\n') { | |
| 724 // findWordBoundary("\ntext", 0, &start, &end) assigns 1 to |end| but we expect 0 at the case. | |
| 725 start = 0; | |
| 726 end = 0; | |
| 727 } else { | |
| 728 findWordBoundary(characters.data(), characters.size(), findStart ? chara cters.size() : 0, &start, &end); | |
| 729 } | |
| 730 ASSERT(start >= 0); | |
| 731 ASSERT(end >= 0); | |
| 732 unsigned restOffset = findStart ? start : end; | |
|
Yuta Kitamura
2014/07/03 08:39:27
nit: "rest" isn't an adjective. It's a noun. You p
yoichio
2014/07/04 01:29:44
Done.
| |
| 733 // Find position. | |
| 734 for (unsigned i = 0; i < lengthList.size(); ++i) { | |
| 735 if (restOffset <= lengthList[i]) | |
| 736 return Position(textList[i], (textList[i] == startPosition.anchorNod e()) ? restOffset + startPosition.computeOffsetInContainerNode() : restOffset); | |
| 737 restOffset -= lengthList[i]; | |
| 738 } | |
| 739 | |
| 740 ASSERT_NOT_REACHED(); | |
| 741 return Position(); | |
| 742 } | |
| 743 | |
| 744 Position HTMLTextFormControlElement::startOfWord(const Position& position) | |
| 745 { | |
| 746 const HTMLTextFormControlElement* textFormControl = enclosingTextFormControl (position); | |
| 747 ASSERT(textFormControl); | |
| 748 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
| 749 | |
| 750 const Position startPosition = startOfSentence(position); | |
| 751 if (startPosition == position) | |
| 752 return position; | |
| 753 const Position endPosition = (position.anchorNode() == innerEditor) ? innerN odePosition(position) : position; | |
| 754 | |
| 755 return findWordBoundary(innerEditor, startPosition, endPosition, true); | |
| 756 } | |
| 757 | |
| 758 Position HTMLTextFormControlElement::endOfWord(const Position& position) | |
| 759 { | |
| 760 const HTMLTextFormControlElement* textFormControl = enclosingTextFormControl (position); | |
| 761 ASSERT(textFormControl); | |
| 762 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
| 763 | |
| 764 | |
| 765 const Position endPosition = endOfSentence(position); | |
| 766 if (endPosition == position) | |
| 767 return position; | |
| 768 const Position startPosition = (position.anchorNode() == innerEditor) ? inne rNodePosition(position) : position; | |
| 769 | |
| 770 return findWordBoundary(innerEditor, startPosition, endPosition, false); | |
| 771 } | |
| 772 | |
| 773 static int findLastLineBreak(const Text& text, int offset) | |
| 774 { | |
| 775 for (; offset >= 0; --offset) { | |
| 776 if (text.data()[offset] == '\n') | |
| 777 return offset; | |
| 778 } | |
| 779 return -1; | |
| 780 } | |
| 781 | |
| 782 static Position endOfPrevious(const Node& node, HTMLElement* innerEditor) | |
| 783 { | |
| 784 Node* prev = NodeTraversal::previous(node, innerEditor); | |
| 785 if (!prev) | |
| 786 return Position(); | |
| 787 | |
| 788 if (prev->hasTagName(brTag)) | |
| 789 return Position(prev, Position::PositionIsAfterAnchor); | |
| 790 | |
| 791 if (prev->isTextNode()) { | |
| 792 Text* textNode = toText(prev); | |
| 793 return Position(textNode, textNode->length()); | |
| 794 } | |
| 795 | |
| 796 return Position(); | |
| 797 } | |
| 798 | |
| 799 static Position previousIfPositionIsAfterLineBreak(const Position& position, HTM LElement* innerEditor) | |
| 800 { | |
| 801 if (position.isNull()) | |
| 802 return Position(); | |
| 803 | |
| 804 // Move back if position is just after line break. | |
| 805 if (isHTMLBRElement(*position.anchorNode())) { | |
| 806 switch (position.anchorType()) { | |
| 807 case Position::PositionIsAfterAnchor: | |
| 808 return Position(position.anchorNode(), Position::PositionIsBeforeAnc hor); | |
| 809 case Position::PositionIsBeforeAnchor: | |
| 810 return previousIfPositionIsAfterLineBreak(endOfPrevious(*position.an chorNode(), innerEditor), innerEditor); | |
| 811 default: | |
| 812 ASSERT_NOT_REACHED(); | |
| 813 } | |
| 814 } else if (position.anchorNode()->isTextNode()) { | |
| 815 Text* textNode = toText(position.anchorNode()); | |
| 816 unsigned offset = position.offsetInContainerNode(); | |
| 817 if (textNode->length() == 0 || offset <= 0) { | |
| 818 return previousIfPositionIsAfterLineBreak(endOfPrevious(*position.an chorNode(), innerEditor), innerEditor); | |
| 819 } | |
| 820 | |
| 821 if (offset <= textNode->length() && textNode->data()[offset - 1] == '\n' ) { | |
| 822 return Position(textNode, offset - 1); | |
| 823 } | |
| 824 } | |
| 825 | |
| 826 return position; | |
| 827 } | |
| 828 | |
| 829 static inline Position startOfInnerText(const HTMLTextFormControlElement* textFo rmControl) | |
| 830 { | |
| 831 return Position(textFormControl->innerEditorElement(), 0, Position::Position IsOffsetInAnchor); | |
| 832 } | |
| 833 | |
| 834 Position HTMLTextFormControlElement::startOfSentence(const Position& position) | |
| 835 { | |
| 836 HTMLTextFormControlElement* textFormControl = enclosingTextFormControl(posit ion); | |
| 837 ASSERT(textFormControl); | |
| 838 | |
| 839 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
| 840 if (!innerEditor->childNodes()->length()) | |
| 841 return startOfInnerText(textFormControl); | |
| 842 | |
| 843 const Position innerPosition = position.anchorNode() == innerEditor ? innerN odePosition(position) : position; | |
| 844 const Position pivotPosition = previousIfPositionIsAfterLineBreak(innerPosit ion, innerEditor); | |
| 845 if (pivotPosition.isNull()) | |
| 846 return startOfInnerText(textFormControl); | |
| 847 | |
| 848 for (Node* node = pivotPosition.anchorNode(); node; node = NodeTraversal::pr evious(*node, innerEditor)) { | |
| 849 bool isPivotNode = (node == pivotPosition.anchorNode()); | |
| 850 | |
| 851 if (node->isTextNode()) { | |
| 852 Text* textNode = toText(node); | |
| 853 int lastLineBreak = findLastLineBreak(*textNode, isPivotNode ? pivot Position.offsetInContainerNode() - 1 : (int)textNode->length() - 1); | |
| 854 if (lastLineBreak >= 0) | |
| 855 return Position(textNode, lastLineBreak + 1); | |
| 856 } else if (isHTMLBRElement(node) && (!isPivotNode || pivotPosition.ancho rType() == Position::PositionIsAfterAnchor)) { | |
| 857 return Position(node, Position::PositionIsAfterAnchor); | |
| 858 } | |
| 859 } | |
| 860 return startOfInnerText(textFormControl); | |
| 861 } | |
| 862 | |
| 863 static int findFirstLineBreak(const Text& text, int offset) | |
| 864 { | |
| 865 for (; (unsigned)offset < text.length(); ++offset) { | |
| 866 if (text.data()[offset] == '\n') | |
| 867 return offset; | |
| 868 } | |
| 869 return -1; | |
| 870 } | |
| 871 | |
| 872 static Position endOfInnerText(const HTMLTextFormControlElement* textFormControl ) | |
| 873 { | |
| 874 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
| 875 return Position(innerEditor, innerEditor->childNodes()->length(), Position:: PositionIsOffsetInAnchor); | |
| 876 } | |
| 877 | |
| 878 Position HTMLTextFormControlElement::endOfSentence(const Position& position) | |
| 879 { | |
| 880 HTMLTextFormControlElement* textFormControl = enclosingTextFormControl(posit ion); | |
| 881 ASSERT(textFormControl); | |
| 882 | |
| 883 HTMLElement* innerEditor = textFormControl->innerEditorElement(); | |
| 884 if (!innerEditor->childNodes()->length()) | |
| 885 return startOfInnerText(textFormControl); | |
| 886 | |
| 887 const Position pivotPosition = position.anchorNode() == innerEditor ? innerN odePosition(position) : position; | |
| 888 if (pivotPosition.isNull()) | |
| 889 return startOfInnerText(textFormControl); | |
| 890 | |
| 891 for (Node* node = pivotPosition.anchorNode(); node; node = NodeTraversal::ne xt(*node, innerEditor)) { | |
| 892 bool isPivotNode = (node == pivotPosition.anchorNode()); | |
| 893 | |
| 894 if (node->isTextNode()) { | |
| 895 Text* textNode = toText(node); | |
| 896 int firstLineBreak = findFirstLineBreak(*textNode, isPivotNode ? piv otPosition.offsetInContainerNode() : 0); | |
| 897 if (firstLineBreak >= 0) | |
| 898 return Position(textNode, firstLineBreak + 1); | |
| 899 } else if (isHTMLBRElement(node)) { | |
| 900 return Position(node, Position::PositionIsAfterAnchor); | |
| 901 } | |
| 902 } | |
| 903 return endOfInnerText(textFormControl); | |
| 904 } | |
| 905 | |
| 662 } // namespace Webcore | 906 } // namespace Webcore |
| OLD | NEW |