| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/editing/FrameSelection.h" | 5 #include "core/editing/FrameSelection.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 7 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/Element.h" | 9 #include "core/dom/Element.h" |
| 10 #include "core/dom/Text.h" | 10 #include "core/dom/Text.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 namespace blink { | 26 namespace blink { |
| 27 | 27 |
| 28 class FrameSelectionTest : public EditingTestBase { | 28 class FrameSelectionTest : public EditingTestBase { |
| 29 protected: | 29 protected: |
| 30 void setSelection(const VisibleSelection&); | 30 void setSelection(const VisibleSelection&); |
| 31 FrameSelection& selection() const; | 31 FrameSelection& selection() const; |
| 32 const VisibleSelection& visibleSelectionInDOMTree() const { return selection
().selection(); } | 32 const VisibleSelection& visibleSelectionInDOMTree() const { return selection
().selection(); } |
| 33 const VisibleSelectionInFlatTree& visibleSelectionInFlatTree() const { retur
n selection().selectionInFlatTree(); } | 33 const VisibleSelectionInFlatTree& visibleSelectionInFlatTree() const { retur
n selection().selectionInFlatTree(); } |
| 34 | 34 |
| 35 PassRefPtrWillBeRawPtr<Text> appendTextNode(const String& data); | 35 RawPtr<Text> appendTextNode(const String& data); |
| 36 int layoutCount() const { return dummyPageHolder().frameView().layoutCount()
; } | 36 int layoutCount() const { return dummyPageHolder().frameView().layoutCount()
; } |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 RefPtrWillBePersistent<Text> m_textNode; | 39 Persistent<Text> m_textNode; |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 void FrameSelectionTest::setSelection(const VisibleSelection& newSelection) | 42 void FrameSelectionTest::setSelection(const VisibleSelection& newSelection) |
| 43 { | 43 { |
| 44 dummyPageHolder().frame().selection().setSelection(newSelection); | 44 dummyPageHolder().frame().selection().setSelection(newSelection); |
| 45 } | 45 } |
| 46 | 46 |
| 47 FrameSelection& FrameSelectionTest::selection() const | 47 FrameSelection& FrameSelectionTest::selection() const |
| 48 { | 48 { |
| 49 return dummyPageHolder().frame().selection(); | 49 return dummyPageHolder().frame().selection(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 PassRefPtrWillBeRawPtr<Text> FrameSelectionTest::appendTextNode(const String& da
ta) | 52 RawPtr<Text> FrameSelectionTest::appendTextNode(const String& data) |
| 53 { | 53 { |
| 54 RefPtrWillBeRawPtr<Text> text = document().createTextNode(data); | 54 RawPtr<Text> text = document().createTextNode(data); |
| 55 document().body()->appendChild(text); | 55 document().body()->appendChild(text); |
| 56 return text.release(); | 56 return text.release(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 TEST_F(FrameSelectionTest, SetValidSelection) | 59 TEST_F(FrameSelectionTest, SetValidSelection) |
| 60 { | 60 { |
| 61 RefPtrWillBeRawPtr<Text> text = appendTextNode("Hello, World!"); | 61 RawPtr<Text> text = appendTextNode("Hello, World!"); |
| 62 VisibleSelection validSelection(Position(text, 0), Position(text, 5)); | 62 VisibleSelection validSelection(Position(text, 0), Position(text, 5)); |
| 63 EXPECT_FALSE(validSelection.isNone()); | 63 EXPECT_FALSE(validSelection.isNone()); |
| 64 setSelection(validSelection); | 64 setSelection(validSelection); |
| 65 EXPECT_FALSE(selection().isNone()); | 65 EXPECT_FALSE(selection().isNone()); |
| 66 } | 66 } |
| 67 | 67 |
| 68 TEST_F(FrameSelectionTest, SetInvalidSelection) | 68 TEST_F(FrameSelectionTest, SetInvalidSelection) |
| 69 { | 69 { |
| 70 // Create a new document without frame by using DOMImplementation. | 70 // Create a new document without frame by using DOMImplementation. |
| 71 DocumentInit dummy; | 71 DocumentInit dummy; |
| 72 RefPtrWillBeRawPtr<Document> documentWithoutFrame = Document::create(); | 72 RawPtr<Document> documentWithoutFrame = Document::create(); |
| 73 RefPtrWillBeRawPtr<Element> body = HTMLBodyElement::create(*documentWithoutF
rame); | 73 RawPtr<Element> body = HTMLBodyElement::create(*documentWithoutFrame); |
| 74 documentWithoutFrame->appendChild(body); | 74 documentWithoutFrame->appendChild(body); |
| 75 RefPtrWillBeRawPtr<Text> anotherText = documentWithoutFrame->createTextNode(
"Hello, another world"); | 75 RawPtr<Text> anotherText = documentWithoutFrame->createTextNode("Hello, anot
her world"); |
| 76 body->appendChild(anotherText); | 76 body->appendChild(anotherText); |
| 77 | 77 |
| 78 // Create a new VisibleSelection for the new document without frame and | 78 // Create a new VisibleSelection for the new document without frame and |
| 79 // update FrameSelection with the selection. | 79 // update FrameSelection with the selection. |
| 80 VisibleSelection invalidSelection; | 80 VisibleSelection invalidSelection; |
| 81 invalidSelection.setWithoutValidation(Position(anotherText, 0), Position(ano
therText, 5)); | 81 invalidSelection.setWithoutValidation(Position(anotherText, 0), Position(ano
therText, 5)); |
| 82 setSelection(invalidSelection); | 82 setSelection(invalidSelection); |
| 83 | 83 |
| 84 EXPECT_TRUE(selection().isNone()); | 84 EXPECT_TRUE(selection().isNone()); |
| 85 } | 85 } |
| 86 | 86 |
| 87 TEST_F(FrameSelectionTest, InvalidateCaretRect) | 87 TEST_F(FrameSelectionTest, InvalidateCaretRect) |
| 88 { | 88 { |
| 89 RefPtrWillBeRawPtr<Text> text = appendTextNode("Hello, World!"); | 89 RawPtr<Text> text = appendTextNode("Hello, World!"); |
| 90 document().view()->updateAllLifecyclePhases(); | 90 document().view()->updateAllLifecyclePhases(); |
| 91 | 91 |
| 92 VisibleSelection validSelection(Position(text, 0), Position(text, 0)); | 92 VisibleSelection validSelection(Position(text, 0), Position(text, 0)); |
| 93 setSelection(validSelection); | 93 setSelection(validSelection); |
| 94 selection().setCaretRectNeedsUpdate(); | 94 selection().setCaretRectNeedsUpdate(); |
| 95 EXPECT_TRUE(selection().isCaretBoundsDirty()); | 95 EXPECT_TRUE(selection().isCaretBoundsDirty()); |
| 96 selection().invalidateCaretRect(); | 96 selection().invalidateCaretRect(); |
| 97 EXPECT_FALSE(selection().isCaretBoundsDirty()); | 97 EXPECT_FALSE(selection().isCaretBoundsDirty()); |
| 98 | 98 |
| 99 document().body()->removeChild(text); | 99 document().body()->removeChild(text); |
| 100 document().updateLayoutIgnorePendingStylesheets(); | 100 document().updateLayoutIgnorePendingStylesheets(); |
| 101 selection().setCaretRectNeedsUpdate(); | 101 selection().setCaretRectNeedsUpdate(); |
| 102 EXPECT_TRUE(selection().isCaretBoundsDirty()); | 102 EXPECT_TRUE(selection().isCaretBoundsDirty()); |
| 103 selection().invalidateCaretRect(); | 103 selection().invalidateCaretRect(); |
| 104 EXPECT_FALSE(selection().isCaretBoundsDirty()); | 104 EXPECT_FALSE(selection().isCaretBoundsDirty()); |
| 105 } | 105 } |
| 106 | 106 |
| 107 TEST_F(FrameSelectionTest, PaintCaretShouldNotLayout) | 107 TEST_F(FrameSelectionTest, PaintCaretShouldNotLayout) |
| 108 { | 108 { |
| 109 RefPtrWillBeRawPtr<Text> text = appendTextNode("Hello, World!"); | 109 RawPtr<Text> text = appendTextNode("Hello, World!"); |
| 110 document().view()->updateAllLifecyclePhases(); | 110 document().view()->updateAllLifecyclePhases(); |
| 111 | 111 |
| 112 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); | 112 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); |
| 113 document().body()->focus(); | 113 document().body()->focus(); |
| 114 EXPECT_TRUE(document().body()->focused()); | 114 EXPECT_TRUE(document().body()->focused()); |
| 115 | 115 |
| 116 VisibleSelection validSelection(Position(text, 0), Position(text, 0)); | 116 VisibleSelection validSelection(Position(text, 0), Position(text, 0)); |
| 117 selection().setCaretVisible(true); | 117 selection().setCaretVisible(true); |
| 118 setSelection(validSelection); | 118 setSelection(validSelection); |
| 119 EXPECT_TRUE(selection().isCaret()); | 119 EXPECT_TRUE(selection().isCaret()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 134 selection().paintCaret(context, LayoutPoint()); | 134 selection().paintCaret(context, LayoutPoint()); |
| 135 EXPECT_EQ(startCount, layoutCount()); | 135 EXPECT_EQ(startCount, layoutCount()); |
| 136 } | 136 } |
| 137 | 137 |
| 138 #define EXPECT_EQ_SELECTED_TEXT(text) \ | 138 #define EXPECT_EQ_SELECTED_TEXT(text) \ |
| 139 EXPECT_EQ(text, WebString(selection().selectedText()).utf8()) | 139 EXPECT_EQ(text, WebString(selection().selectedText()).utf8()) |
| 140 | 140 |
| 141 TEST_F(FrameSelectionTest, SelectWordAroundPosition) | 141 TEST_F(FrameSelectionTest, SelectWordAroundPosition) |
| 142 { | 142 { |
| 143 // "Foo Bar Baz," | 143 // "Foo Bar Baz," |
| 144 RefPtrWillBeRawPtr<Text> text = appendTextNode("Foo Bar Baz,"); | 144 RawPtr<Text> text = appendTextNode("Foo Bar Baz,"); |
| 145 // "Fo|o Bar Baz," | 145 // "Fo|o Bar Baz," |
| 146 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePosition(Posit
ion(text, 2)))); | 146 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePosition(Posit
ion(text, 2)))); |
| 147 EXPECT_EQ_SELECTED_TEXT("Foo"); | 147 EXPECT_EQ_SELECTED_TEXT("Foo"); |
| 148 // "Foo| Bar Baz," | 148 // "Foo| Bar Baz," |
| 149 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePosition(Posit
ion(text, 3)))); | 149 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePosition(Posit
ion(text, 3)))); |
| 150 EXPECT_EQ_SELECTED_TEXT("Foo"); | 150 EXPECT_EQ_SELECTED_TEXT("Foo"); |
| 151 // "Foo Bar | Baz," | 151 // "Foo Bar | Baz," |
| 152 EXPECT_FALSE(selection().selectWordAroundPosition(createVisiblePosition(Posi
tion(text, 13)))); | 152 EXPECT_FALSE(selection().selectWordAroundPosition(createVisiblePosition(Posi
tion(text, 13)))); |
| 153 // "Foo Bar Baz|," | 153 // "Foo Bar Baz|," |
| 154 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePosition(Posit
ion(text, 22)))); | 154 EXPECT_TRUE(selection().selectWordAroundPosition(createVisiblePosition(Posit
ion(text, 22)))); |
| 155 EXPECT_EQ_SELECTED_TEXT("Baz"); | 155 EXPECT_EQ_SELECTED_TEXT("Baz"); |
| 156 } | 156 } |
| 157 | 157 |
| 158 TEST_F(FrameSelectionTest, MoveRangeSelectionTest) | 158 TEST_F(FrameSelectionTest, MoveRangeSelectionTest) |
| 159 { | 159 { |
| 160 // "Foo Bar Baz," | 160 // "Foo Bar Baz," |
| 161 RefPtrWillBeRawPtr<Text> text = appendTextNode("Foo Bar Baz,"); | 161 RawPtr<Text> text = appendTextNode("Foo Bar Baz,"); |
| 162 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end). | 162 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end). |
| 163 selection().setSelection(VisibleSelection(Position(text, 5), Position(text,
6))); | 163 selection().setSelection(VisibleSelection(Position(text, 5), Position(text,
6))); |
| 164 EXPECT_EQ_SELECTED_TEXT("a"); | 164 EXPECT_EQ_SELECTED_TEXT("a"); |
| 165 | 165 |
| 166 // "Foo B|ar B>az," with the Character granularity. | 166 // "Foo B|ar B>az," with the Character granularity. |
| 167 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 9)), CharacterGranularity); | 167 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 9)), CharacterGranularity); |
| 168 EXPECT_EQ_SELECTED_TEXT("ar B"); | 168 EXPECT_EQ_SELECTED_TEXT("ar B"); |
| 169 // "Foo B|ar B>az," with the Word granularity. | 169 // "Foo B|ar B>az," with the Word granularity. |
| 170 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 9)), WordGranularity); | 170 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 9)), WordGranularity); |
| 171 EXPECT_EQ_SELECTED_TEXT("Bar Baz"); | 171 EXPECT_EQ_SELECTED_TEXT("Bar Baz"); |
| 172 // "Fo<o B|ar Baz," with the Character granularity. | 172 // "Fo<o B|ar Baz," with the Character granularity. |
| 173 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 2)), CharacterGranularity); | 173 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 2)), CharacterGranularity); |
| 174 EXPECT_EQ_SELECTED_TEXT("o B"); | 174 EXPECT_EQ_SELECTED_TEXT("o B"); |
| 175 // "Fo<o B|ar Baz," with the Word granularity. | 175 // "Fo<o B|ar Baz," with the Word granularity. |
| 176 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 2)), WordGranularity); | 176 selection().moveRangeSelection(createVisiblePosition(Position(text, 5)), cre
ateVisiblePosition(Position(text, 2)), WordGranularity); |
| 177 EXPECT_EQ_SELECTED_TEXT("Foo Bar"); | 177 EXPECT_EQ_SELECTED_TEXT("Foo Bar"); |
| 178 } | 178 } |
| 179 | 179 |
| 180 TEST_F(FrameSelectionTest, setNonDirectionalSelectionIfNeeded) | 180 TEST_F(FrameSelectionTest, setNonDirectionalSelectionIfNeeded) |
| 181 { | 181 { |
| 182 const char* bodyContent = "<span id=top>top</span><span id=host></span>"; | 182 const char* bodyContent = "<span id=top>top</span><span id=host></span>"; |
| 183 const char* shadowContent = "<span id=bottom>bottom</span>"; | 183 const char* shadowContent = "<span id=bottom>bottom</span>"; |
| 184 setBodyContent(bodyContent); | 184 setBodyContent(bodyContent); |
| 185 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent,
"host"); | 185 RawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent, "host"); |
| 186 updateLayoutAndStyleForPainting(); | 186 updateLayoutAndStyleForPainting(); |
| 187 | 187 |
| 188 Node* top = document().getElementById("top")->firstChild(); | 188 Node* top = document().getElementById("top")->firstChild(); |
| 189 Node* bottom = shadowRoot->getElementById("bottom")->firstChild(); | 189 Node* bottom = shadowRoot->getElementById("bottom")->firstChild(); |
| 190 Node* host = document().getElementById("host"); | 190 Node* host = document().getElementById("host"); |
| 191 | 191 |
| 192 // top to bottom | 192 // top to bottom |
| 193 selection().setNonDirectionalSelectionIfNeeded(VisibleSelectionInFlatTree(Po
sitionInFlatTree(top, 1), PositionInFlatTree(bottom, 3)), CharacterGranularity); | 193 selection().setNonDirectionalSelectionIfNeeded(VisibleSelectionInFlatTree(Po
sitionInFlatTree(top, 1), PositionInFlatTree(bottom, 3)), CharacterGranularity); |
| 194 EXPECT_EQ(Position(top, 1), visibleSelectionInDOMTree().base()); | 194 EXPECT_EQ(Position(top, 1), visibleSelectionInDOMTree().base()); |
| 195 EXPECT_EQ(Position::beforeNode(host), visibleSelectionInDOMTree().extent()); | 195 EXPECT_EQ(Position::beforeNode(host), visibleSelectionInDOMTree().extent()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 209 EXPECT_EQ(Position(bottom, 3), visibleSelectionInDOMTree().end()); | 209 EXPECT_EQ(Position(bottom, 3), visibleSelectionInDOMTree().end()); |
| 210 | 210 |
| 211 EXPECT_EQ(PositionInFlatTree(bottom, 3), visibleSelectionInFlatTree().base()
); | 211 EXPECT_EQ(PositionInFlatTree(bottom, 3), visibleSelectionInFlatTree().base()
); |
| 212 EXPECT_EQ(PositionInFlatTree(top, 1), visibleSelectionInFlatTree().extent())
; | 212 EXPECT_EQ(PositionInFlatTree(top, 1), visibleSelectionInFlatTree().extent())
; |
| 213 EXPECT_EQ(PositionInFlatTree(top, 1), visibleSelectionInFlatTree().start()); | 213 EXPECT_EQ(PositionInFlatTree(top, 1), visibleSelectionInFlatTree().start()); |
| 214 EXPECT_EQ(PositionInFlatTree(bottom, 3), visibleSelectionInFlatTree().end())
; | 214 EXPECT_EQ(PositionInFlatTree(bottom, 3), visibleSelectionInFlatTree().end())
; |
| 215 } | 215 } |
| 216 | 216 |
| 217 TEST_F(FrameSelectionTest, SelectAllWithUnselectableRoot) | 217 TEST_F(FrameSelectionTest, SelectAllWithUnselectableRoot) |
| 218 { | 218 { |
| 219 RefPtrWillBeRawPtr<Element> select = document().createElement("select", ASSE
RT_NO_EXCEPTION); | 219 RawPtr<Element> select = document().createElement("select", ASSERT_NO_EXCEPT
ION); |
| 220 document().replaceChild(select.get(), document().documentElement()); | 220 document().replaceChild(select.get(), document().documentElement()); |
| 221 selection().selectAll(); | 221 selection().selectAll(); |
| 222 EXPECT_TRUE(selection().isNone()) << "Nothing should be selected if the cont
ent of the documentElement is not selctable."; | 222 EXPECT_TRUE(selection().isNone()) << "Nothing should be selected if the cont
ent of the documentElement is not selctable."; |
| 223 } | 223 } |
| 224 | 224 |
| 225 } // namespace blink | 225 } // namespace blink |
| OLD | NEW |