Home | History | Annotate | Download | only in dom
      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
      3 // found in the LICENSE file.
      4 
      5 #include "config.h"
      6 #include "core/dom/Range.h"
      7 
      8 #include "bindings/v8/ExceptionStatePlaceholder.h"
      9 #include "core/dom/Element.h"
     10 #include "core/dom/NodeList.h"
     11 #include "core/dom/Text.h"
     12 #include "core/html/HTMLBodyElement.h"
     13 #include "core/html/HTMLDocument.h"
     14 #include "core/html/HTMLElement.h"
     15 #include "core/html/HTMLHtmlElement.h"
     16 #include "platform/heap/Handle.h"
     17 #include "wtf/Compiler.h"
     18 #include "wtf/RefPtr.h"
     19 #include "wtf/text/AtomicString.h"
     20 #include <gtest/gtest.h>
     21 
     22 using namespace WebCore;
     23 
     24 namespace {
     25 
     26 class RangeTest : public ::testing::Test {
     27 protected:
     28     virtual void SetUp() OVERRIDE;
     29 
     30     HTMLDocument& document() const;
     31 
     32 private:
     33     RefPtrWillBePersistent<HTMLDocument> m_document;
     34 };
     35 
     36 void RangeTest::SetUp()
     37 {
     38     m_document = HTMLDocument::create();
     39     RefPtrWillBeRawPtr<HTMLHtmlElement> html = HTMLHtmlElement::create(*m_document);
     40     html->appendChild(HTMLBodyElement::create(*m_document));
     41     m_document->appendChild(html.release());
     42 }
     43 
     44 HTMLDocument& RangeTest::document() const
     45 {
     46     return *m_document;
     47 }
     48 
     49 TEST_F(RangeTest, SplitTextNodeRangeWithinText)
     50 {
     51     document().body()->setInnerHTML("1234", ASSERT_NO_EXCEPTION);
     52     Text* oldText = toText(document().body()->firstChild());
     53 
     54     RefPtrWillBeRawPtr<Range> range04 = Range::create(document(), oldText, 0, oldText, 4);
     55     RefPtrWillBeRawPtr<Range> range02 = Range::create(document(), oldText, 0, oldText, 2);
     56     RefPtrWillBeRawPtr<Range> range22 = Range::create(document(), oldText, 2, oldText, 2);
     57     RefPtrWillBeRawPtr<Range> range24 = Range::create(document(), oldText, 2, oldText, 4);
     58 
     59     oldText->splitText(2, ASSERT_NO_EXCEPTION);
     60     Text* newText = toText(oldText->nextSibling());
     61 
     62     EXPECT_TRUE(range04->boundaryPointsValid());
     63     EXPECT_EQ(oldText, range04->startContainer());
     64     EXPECT_EQ(0, range04->startOffset());
     65     EXPECT_EQ(newText, range04->endContainer());
     66     EXPECT_EQ(2, range04->endOffset());
     67 
     68     EXPECT_TRUE(range02->boundaryPointsValid());
     69     EXPECT_EQ(oldText, range02->startContainer());
     70     EXPECT_EQ(0, range02->startOffset());
     71     EXPECT_EQ(oldText, range02->endContainer());
     72     EXPECT_EQ(2, range02->endOffset());
     73 
     74     // Our implementation always moves the boundary point at the separation point to the end of the original text node.
     75     EXPECT_TRUE(range22->boundaryPointsValid());
     76     EXPECT_EQ(oldText, range22->startContainer());
     77     EXPECT_EQ(2, range22->startOffset());
     78     EXPECT_EQ(oldText, range22->endContainer());
     79     EXPECT_EQ(2, range22->endOffset());
     80 
     81     EXPECT_TRUE(range24->boundaryPointsValid());
     82     EXPECT_EQ(oldText, range24->startContainer());
     83     EXPECT_EQ(2, range24->startOffset());
     84     EXPECT_EQ(newText, range24->endContainer());
     85     EXPECT_EQ(2, range24->endOffset());
     86 }
     87 
     88 TEST_F(RangeTest, SplitTextNodeRangeOutsideText)
     89 {
     90     document().body()->setInnerHTML("<span id=\"outer\">0<span id=\"inner-left\">1</span>SPLITME<span id=\"inner-right\">2</span>3</span>", ASSERT_NO_EXCEPTION);
     91 
     92     Element* outer = document().getElementById(AtomicString::fromUTF8("outer"));
     93     Element* innerLeft = document().getElementById(AtomicString::fromUTF8("inner-left"));
     94     Element* innerRight = document().getElementById(AtomicString::fromUTF8("inner-right"));
     95     Text* oldText = toText(outer->childNodes()->item(2));
     96 
     97     RefPtrWillBeRawPtr<Range> rangeOuterOutside = Range::create(document(), outer, 0, outer, 5);
     98     RefPtrWillBeRawPtr<Range> rangeOuterInside = Range::create(document(), outer, 1, outer, 4);
     99     RefPtrWillBeRawPtr<Range> rangeOuterSurroundingText = Range::create(document(), outer, 2, outer, 3);
    100     RefPtrWillBeRawPtr<Range> rangeInnerLeft = Range::create(document(), innerLeft, 0, innerLeft, 1);
    101     RefPtrWillBeRawPtr<Range> rangeInnerRight = Range::create(document(), innerRight, 0, innerRight, 1);
    102     RefPtrWillBeRawPtr<Range> rangeFromTextToMiddleOfElement = Range::create(document(), oldText, 6, outer, 3);
    103 
    104     oldText->splitText(3, ASSERT_NO_EXCEPTION);
    105     Text* newText = toText(oldText->nextSibling());
    106 
    107     EXPECT_TRUE(rangeOuterOutside->boundaryPointsValid());
    108     EXPECT_EQ(outer, rangeOuterOutside->startContainer());
    109     EXPECT_EQ(0, rangeOuterOutside->startOffset());
    110     EXPECT_EQ(outer, rangeOuterOutside->endContainer());
    111     EXPECT_EQ(6, rangeOuterOutside->endOffset()); // Increased by 1 since a new node is inserted.
    112 
    113     EXPECT_TRUE(rangeOuterInside->boundaryPointsValid());
    114     EXPECT_EQ(outer, rangeOuterInside->startContainer());
    115     EXPECT_EQ(1, rangeOuterInside->startOffset());
    116     EXPECT_EQ(outer, rangeOuterInside->endContainer());
    117     EXPECT_EQ(5, rangeOuterInside->endOffset());
    118 
    119     EXPECT_TRUE(rangeOuterSurroundingText->boundaryPointsValid());
    120     EXPECT_EQ(outer, rangeOuterSurroundingText->startContainer());
    121     EXPECT_EQ(2, rangeOuterSurroundingText->startOffset());
    122     EXPECT_EQ(outer, rangeOuterSurroundingText->endContainer());
    123     EXPECT_EQ(4, rangeOuterSurroundingText->endOffset());
    124 
    125     EXPECT_TRUE(rangeInnerLeft->boundaryPointsValid());
    126     EXPECT_EQ(innerLeft, rangeInnerLeft->startContainer());
    127     EXPECT_EQ(0, rangeInnerLeft->startOffset());
    128     EXPECT_EQ(innerLeft, rangeInnerLeft->endContainer());
    129     EXPECT_EQ(1, rangeInnerLeft->endOffset());
    130 
    131     EXPECT_TRUE(rangeInnerRight->boundaryPointsValid());
    132     EXPECT_EQ(innerRight, rangeInnerRight->startContainer());
    133     EXPECT_EQ(0, rangeInnerRight->startOffset());
    134     EXPECT_EQ(innerRight, rangeInnerRight->endContainer());
    135     EXPECT_EQ(1, rangeInnerRight->endOffset());
    136 
    137     EXPECT_TRUE(rangeFromTextToMiddleOfElement->boundaryPointsValid());
    138     EXPECT_EQ(newText, rangeFromTextToMiddleOfElement->startContainer());
    139     EXPECT_EQ(3, rangeFromTextToMiddleOfElement->startOffset());
    140     EXPECT_EQ(outer, rangeFromTextToMiddleOfElement->endContainer());
    141     EXPECT_EQ(4, rangeFromTextToMiddleOfElement->endOffset());
    142 }
    143 
    144 }
    145