| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/dom/StaticRange.h" | 5 #include "core/dom/StaticRange.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "core/dom/Element.h" | 8 #include "core/dom/Element.h" |
| 9 #include "core/dom/NodeList.h" | 9 #include "core/dom/NodeList.h" |
| 10 #include "core/dom/Range.h" | 10 #include "core/dom/Range.h" |
| 11 #include "core/dom/Text.h" | 11 #include "core/dom/Text.h" |
| 12 #include "core/editing/EditingTestBase.h" |
| 12 #include "core/html/HTMLBodyElement.h" | 13 #include "core/html/HTMLBodyElement.h" |
| 13 #include "core/html/HTMLDocument.h" | 14 #include "core/html/HTMLDocument.h" |
| 14 #include "core/html/HTMLElement.h" | 15 #include "core/html/HTMLElement.h" |
| 15 #include "core/html/HTMLHtmlElement.h" | 16 #include "core/html/HTMLHtmlElement.h" |
| 16 #include "platform/heap/Handle.h" | 17 #include "platform/heap/Handle.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "wtf/Compiler.h" | 19 #include "wtf/Compiler.h" |
| 19 #include "wtf/RefPtr.h" | 20 #include "wtf/RefPtr.h" |
| 20 #include "wtf/text/AtomicString.h" | 21 #include "wtf/text/AtomicString.h" |
| 21 | 22 |
| 22 namespace blink { | 23 namespace blink { |
| 23 | 24 |
| 24 class StaticRangeTest : public testing::Test { | 25 class StaticRangeTest : public EditingTestBase {}; |
| 25 protected: | |
| 26 void SetUp() override; | |
| 27 | |
| 28 HTMLDocument& document() const; | |
| 29 | |
| 30 private: | |
| 31 Persistent<HTMLDocument> m_document; | |
| 32 }; | |
| 33 | |
| 34 void StaticRangeTest::SetUp() { | |
| 35 m_document = HTMLDocument::create(); | |
| 36 HTMLHtmlElement* html = HTMLHtmlElement::create(*m_document); | |
| 37 html->appendChild(HTMLBodyElement::create(*m_document)); | |
| 38 m_document->appendChild(html); | |
| 39 } | |
| 40 | |
| 41 HTMLDocument& StaticRangeTest::document() const { | |
| 42 return *m_document; | |
| 43 } | |
| 44 | 26 |
| 45 TEST_F(StaticRangeTest, SplitTextNodeRangeWithinText) { | 27 TEST_F(StaticRangeTest, SplitTextNodeRangeWithinText) { |
| 28 ScriptState* scriptState = ScriptState::forMainWorld(document().frame()); |
| 29 ScriptState::Scope scope(scriptState); |
| 30 |
| 46 document().body()->setInnerHTML("1234"); | 31 document().body()->setInnerHTML("1234"); |
| 47 Text* oldText = toText(document().body()->firstChild()); | 32 Text* oldText = toText(document().body()->firstChild()); |
| 48 | 33 |
| 49 StaticRange* staticRange04 = | 34 StaticRange* staticRange04 = |
| 50 StaticRange::create(document(), oldText, 0, oldText, 4); | 35 StaticRange::create(document(), oldText, 0, oldText, 4); |
| 51 StaticRange* staticRange02 = | 36 StaticRange* staticRange02 = |
| 52 StaticRange::create(document(), oldText, 0, oldText, 2); | 37 StaticRange::create(document(), oldText, 0, oldText, 2); |
| 53 StaticRange* staticRange22 = | 38 StaticRange* staticRange22 = |
| 54 StaticRange::create(document(), oldText, 2, oldText, 2); | 39 StaticRange::create(document(), oldText, 2, oldText, 2); |
| 55 StaticRange* staticRange24 = | 40 StaticRange* staticRange24 = |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 EXPECT_EQ(oldText, staticRange22->endContainer()); | 91 EXPECT_EQ(oldText, staticRange22->endContainer()); |
| 107 EXPECT_EQ(2, staticRange22->endOffset()); | 92 EXPECT_EQ(2, staticRange22->endOffset()); |
| 108 | 93 |
| 109 EXPECT_EQ(oldText, staticRange24->startContainer()); | 94 EXPECT_EQ(oldText, staticRange24->startContainer()); |
| 110 EXPECT_EQ(2, staticRange24->startOffset()); | 95 EXPECT_EQ(2, staticRange24->startOffset()); |
| 111 EXPECT_EQ(oldText, staticRange24->endContainer()); | 96 EXPECT_EQ(oldText, staticRange24->endContainer()); |
| 112 EXPECT_EQ(4, staticRange24->endOffset()); | 97 EXPECT_EQ(4, staticRange24->endOffset()); |
| 113 } | 98 } |
| 114 | 99 |
| 115 TEST_F(StaticRangeTest, SplitTextNodeRangeOutsideText) { | 100 TEST_F(StaticRangeTest, SplitTextNodeRangeOutsideText) { |
| 101 ScriptState* scriptState = ScriptState::forMainWorld(document().frame()); |
| 102 ScriptState::Scope scope(scriptState); |
| 103 |
| 116 document().body()->setInnerHTML( | 104 document().body()->setInnerHTML( |
| 117 "<span id=\"outer\">0<span id=\"inner-left\">1</span>SPLITME<span " | 105 "<span id=\"outer\">0<span id=\"inner-left\">1</span>SPLITME<span " |
| 118 "id=\"inner-right\">2</span>3</span>"); | 106 "id=\"inner-right\">2</span>3</span>"); |
| 119 | 107 |
| 120 Element* outer = document().getElementById(AtomicString::fromUTF8("outer")); | 108 Element* outer = document().getElementById(AtomicString::fromUTF8("outer")); |
| 121 Element* innerLeft = | 109 Element* innerLeft = |
| 122 document().getElementById(AtomicString::fromUTF8("inner-left")); | 110 document().getElementById(AtomicString::fromUTF8("inner-left")); |
| 123 Element* innerRight = | 111 Element* innerRight = |
| 124 document().getElementById(AtomicString::fromUTF8("inner-right")); | 112 document().getElementById(AtomicString::fromUTF8("inner-right")); |
| 125 Text* oldText = toText(outer->childNodes()->item(2)); | 113 Text* oldText = toText(outer->childNodes()->item(2)); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 EXPECT_EQ(innerRight, staticRangeInnerRight->endContainer()); | 204 EXPECT_EQ(innerRight, staticRangeInnerRight->endContainer()); |
| 217 EXPECT_EQ(1, staticRangeInnerRight->endOffset()); | 205 EXPECT_EQ(1, staticRangeInnerRight->endOffset()); |
| 218 | 206 |
| 219 EXPECT_EQ(oldText, staticRangeFromTextToMiddleOfElement->startContainer()); | 207 EXPECT_EQ(oldText, staticRangeFromTextToMiddleOfElement->startContainer()); |
| 220 EXPECT_EQ(6, staticRangeFromTextToMiddleOfElement->startOffset()); | 208 EXPECT_EQ(6, staticRangeFromTextToMiddleOfElement->startOffset()); |
| 221 EXPECT_EQ(outer, staticRangeFromTextToMiddleOfElement->endContainer()); | 209 EXPECT_EQ(outer, staticRangeFromTextToMiddleOfElement->endContainer()); |
| 222 EXPECT_EQ(3, staticRangeFromTextToMiddleOfElement->endOffset()); | 210 EXPECT_EQ(3, staticRangeFromTextToMiddleOfElement->endOffset()); |
| 223 } | 211 } |
| 224 | 212 |
| 225 TEST_F(StaticRangeTest, InvalidToRange) { | 213 TEST_F(StaticRangeTest, InvalidToRange) { |
| 214 ScriptState* scriptState = ScriptState::forMainWorld(document().frame()); |
| 215 ScriptState::Scope scope(scriptState); |
| 216 |
| 226 document().body()->setInnerHTML("1234"); | 217 document().body()->setInnerHTML("1234"); |
| 227 Text* oldText = toText(document().body()->firstChild()); | 218 Text* oldText = toText(document().body()->firstChild()); |
| 228 | 219 |
| 229 StaticRange* staticRange04 = | 220 StaticRange* staticRange04 = |
| 230 StaticRange::create(document(), oldText, 0, oldText, 4); | 221 StaticRange::create(document(), oldText, 0, oldText, 4); |
| 231 | 222 |
| 232 // Valid StaticRange. | 223 // Valid StaticRange. |
| 233 staticRange04->toRange(ASSERT_NO_EXCEPTION); | 224 staticRange04->toRange(ASSERT_NO_EXCEPTION); |
| 234 | 225 |
| 235 oldText->splitText(2, ASSERT_NO_EXCEPTION); | 226 oldText->splitText(2, ASSERT_NO_EXCEPTION); |
| 236 // StaticRange shouldn't mutate, endOffset() become invalid after splitText(). | 227 // StaticRange shouldn't mutate, endOffset() become invalid after splitText(). |
| 237 EXPECT_EQ(oldText, staticRange04->startContainer()); | 228 EXPECT_EQ(oldText, staticRange04->startContainer()); |
| 238 EXPECT_EQ(0, staticRange04->startOffset()); | 229 EXPECT_EQ(0, staticRange04->startOffset()); |
| 239 EXPECT_EQ(oldText, staticRange04->endContainer()); | 230 EXPECT_EQ(oldText, staticRange04->endContainer()); |
| 240 EXPECT_EQ(4, staticRange04->endOffset()); | 231 EXPECT_EQ(4, staticRange04->endOffset()); |
| 241 | 232 |
| 242 // Invalid StaticRange. | 233 // Invalid StaticRange. |
| 243 DummyExceptionStateForTesting exceptionState; | 234 DummyExceptionStateForTesting exceptionState; |
| 244 staticRange04->toRange(exceptionState); | 235 staticRange04->toRange(exceptionState); |
| 245 EXPECT_TRUE(exceptionState.hadException()); | 236 EXPECT_TRUE(exceptionState.hadException()); |
| 246 } | 237 } |
| 247 | 238 |
| 248 } // namespace blink | 239 } // namespace blink |
| OLD | NEW |