| 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 "bindings/core/v8/V8BindingForTesting.h" |
| 8 #include "core/dom/Element.h" | 9 #include "core/dom/Element.h" |
| 9 #include "core/dom/NodeList.h" | 10 #include "core/dom/NodeList.h" |
| 10 #include "core/dom/Range.h" | 11 #include "core/dom/Range.h" |
| 11 #include "core/dom/Text.h" | 12 #include "core/dom/Text.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" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 36 HTMLHtmlElement* html = HTMLHtmlElement::create(*m_document); | 37 HTMLHtmlElement* html = HTMLHtmlElement::create(*m_document); |
| 37 html->appendChild(HTMLBodyElement::create(*m_document)); | 38 html->appendChild(HTMLBodyElement::create(*m_document)); |
| 38 m_document->appendChild(html); | 39 m_document->appendChild(html); |
| 39 } | 40 } |
| 40 | 41 |
| 41 HTMLDocument& StaticRangeTest::document() const { | 42 HTMLDocument& StaticRangeTest::document() const { |
| 42 return *m_document; | 43 return *m_document; |
| 43 } | 44 } |
| 44 | 45 |
| 45 TEST_F(StaticRangeTest, SplitTextNodeRangeWithinText) { | 46 TEST_F(StaticRangeTest, SplitTextNodeRangeWithinText) { |
| 47 V8TestingScope scope; |
| 46 document().body()->setInnerHTML("1234"); | 48 document().body()->setInnerHTML("1234"); |
| 47 Text* oldText = toText(document().body()->firstChild()); | 49 Text* oldText = toText(document().body()->firstChild()); |
| 48 | 50 |
| 49 StaticRange* staticRange04 = | 51 StaticRange* staticRange04 = |
| 50 StaticRange::create(document(), oldText, 0, oldText, 4); | 52 StaticRange::create(document(), oldText, 0, oldText, 4); |
| 51 StaticRange* staticRange02 = | 53 StaticRange* staticRange02 = |
| 52 StaticRange::create(document(), oldText, 0, oldText, 2); | 54 StaticRange::create(document(), oldText, 0, oldText, 2); |
| 53 StaticRange* staticRange22 = | 55 StaticRange* staticRange22 = |
| 54 StaticRange::create(document(), oldText, 2, oldText, 2); | 56 StaticRange::create(document(), oldText, 2, oldText, 2); |
| 55 StaticRange* staticRange24 = | 57 StaticRange* staticRange24 = |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 EXPECT_EQ(oldText, staticRange22->endContainer()); | 108 EXPECT_EQ(oldText, staticRange22->endContainer()); |
| 107 EXPECT_EQ(2, staticRange22->endOffset()); | 109 EXPECT_EQ(2, staticRange22->endOffset()); |
| 108 | 110 |
| 109 EXPECT_EQ(oldText, staticRange24->startContainer()); | 111 EXPECT_EQ(oldText, staticRange24->startContainer()); |
| 110 EXPECT_EQ(2, staticRange24->startOffset()); | 112 EXPECT_EQ(2, staticRange24->startOffset()); |
| 111 EXPECT_EQ(oldText, staticRange24->endContainer()); | 113 EXPECT_EQ(oldText, staticRange24->endContainer()); |
| 112 EXPECT_EQ(4, staticRange24->endOffset()); | 114 EXPECT_EQ(4, staticRange24->endOffset()); |
| 113 } | 115 } |
| 114 | 116 |
| 115 TEST_F(StaticRangeTest, SplitTextNodeRangeOutsideText) { | 117 TEST_F(StaticRangeTest, SplitTextNodeRangeOutsideText) { |
| 118 V8TestingScope scope; |
| 116 document().body()->setInnerHTML( | 119 document().body()->setInnerHTML( |
| 117 "<span id=\"outer\">0<span id=\"inner-left\">1</span>SPLITME<span " | 120 "<span id=\"outer\">0<span id=\"inner-left\">1</span>SPLITME<span " |
| 118 "id=\"inner-right\">2</span>3</span>"); | 121 "id=\"inner-right\">2</span>3</span>"); |
| 119 | 122 |
| 120 Element* outer = document().getElementById(AtomicString::fromUTF8("outer")); | 123 Element* outer = document().getElementById(AtomicString::fromUTF8("outer")); |
| 121 Element* innerLeft = | 124 Element* innerLeft = |
| 122 document().getElementById(AtomicString::fromUTF8("inner-left")); | 125 document().getElementById(AtomicString::fromUTF8("inner-left")); |
| 123 Element* innerRight = | 126 Element* innerRight = |
| 124 document().getElementById(AtomicString::fromUTF8("inner-right")); | 127 document().getElementById(AtomicString::fromUTF8("inner-right")); |
| 125 Text* oldText = toText(outer->childNodes()->item(2)); | 128 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()); | 219 EXPECT_EQ(innerRight, staticRangeInnerRight->endContainer()); |
| 217 EXPECT_EQ(1, staticRangeInnerRight->endOffset()); | 220 EXPECT_EQ(1, staticRangeInnerRight->endOffset()); |
| 218 | 221 |
| 219 EXPECT_EQ(oldText, staticRangeFromTextToMiddleOfElement->startContainer()); | 222 EXPECT_EQ(oldText, staticRangeFromTextToMiddleOfElement->startContainer()); |
| 220 EXPECT_EQ(6, staticRangeFromTextToMiddleOfElement->startOffset()); | 223 EXPECT_EQ(6, staticRangeFromTextToMiddleOfElement->startOffset()); |
| 221 EXPECT_EQ(outer, staticRangeFromTextToMiddleOfElement->endContainer()); | 224 EXPECT_EQ(outer, staticRangeFromTextToMiddleOfElement->endContainer()); |
| 222 EXPECT_EQ(3, staticRangeFromTextToMiddleOfElement->endOffset()); | 225 EXPECT_EQ(3, staticRangeFromTextToMiddleOfElement->endOffset()); |
| 223 } | 226 } |
| 224 | 227 |
| 225 TEST_F(StaticRangeTest, InvalidToRange) { | 228 TEST_F(StaticRangeTest, InvalidToRange) { |
| 229 V8TestingScope scope; |
| 226 document().body()->setInnerHTML("1234"); | 230 document().body()->setInnerHTML("1234"); |
| 227 Text* oldText = toText(document().body()->firstChild()); | 231 Text* oldText = toText(document().body()->firstChild()); |
| 228 | 232 |
| 229 StaticRange* staticRange04 = | 233 StaticRange* staticRange04 = |
| 230 StaticRange::create(document(), oldText, 0, oldText, 4); | 234 StaticRange::create(document(), oldText, 0, oldText, 4); |
| 231 | 235 |
| 232 // Valid StaticRange. | 236 // Valid StaticRange. |
| 233 staticRange04->toRange(ASSERT_NO_EXCEPTION); | 237 staticRange04->toRange(ASSERT_NO_EXCEPTION); |
| 234 | 238 |
| 235 oldText->splitText(2, ASSERT_NO_EXCEPTION); | 239 oldText->splitText(2, ASSERT_NO_EXCEPTION); |
| 236 // StaticRange shouldn't mutate, endOffset() become invalid after splitText(). | 240 // StaticRange shouldn't mutate, endOffset() become invalid after splitText(). |
| 237 EXPECT_EQ(oldText, staticRange04->startContainer()); | 241 EXPECT_EQ(oldText, staticRange04->startContainer()); |
| 238 EXPECT_EQ(0, staticRange04->startOffset()); | 242 EXPECT_EQ(0, staticRange04->startOffset()); |
| 239 EXPECT_EQ(oldText, staticRange04->endContainer()); | 243 EXPECT_EQ(oldText, staticRange04->endContainer()); |
| 240 EXPECT_EQ(4, staticRange04->endOffset()); | 244 EXPECT_EQ(4, staticRange04->endOffset()); |
| 241 | 245 |
| 242 // Invalid StaticRange. | 246 // Invalid StaticRange. |
| 243 DummyExceptionStateForTesting exceptionState; | 247 DummyExceptionStateForTesting exceptionState; |
| 244 staticRange04->toRange(exceptionState); | 248 staticRange04->toRange(exceptionState); |
| 245 EXPECT_TRUE(exceptionState.hadException()); | 249 EXPECT_TRUE(exceptionState.hadException()); |
| 246 } | 250 } |
| 247 | 251 |
| 248 } // namespace blink | 252 } // namespace blink |
| OLD | NEW |