Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / editing / SurroundingText.cpp
index cefe400..cf3f8c2 100644 (file)
 #include "core/editing/SurroundingText.h"
 
 #include "core/dom/Document.h"
+#include "core/dom/Element.h"
+#include "core/dom/Position.h"
 #include "core/dom/Range.h"
 #include "core/editing/TextIterator.h"
-#include "core/editing/VisiblePosition.h"
-#include "core/editing/VisibleUnits.h"
 
-namespace WebCore {
+namespace blink {
 
-SurroundingText::SurroundingText(const VisiblePosition& visiblePosition, unsigned maxLength)
-    : m_positionOffsetInContent(0)
+SurroundingText::SurroundingText(const Range& range, unsigned maxLength)
+    : m_startOffsetInContent(0)
+    , m_endOffsetInContent(0)
 {
-    if (visiblePosition.isNull())
-        return;
+    initialize(range.startPosition(), range.endPosition(), maxLength);
+}
+
+SurroundingText::SurroundingText(const Position& position, unsigned maxLength)
+    : m_startOffsetInContent(0)
+    , m_endOffsetInContent(0)
+{
+    initialize(position, position, maxLength);
+}
+
+void SurroundingText::initialize(const Position& startPosition, const Position& endPosition, unsigned maxLength)
+{
+    ASSERT(startPosition.document() == endPosition.document());
 
     const unsigned halfMaxLength = maxLength / 2;
-    CharacterIterator forwardIterator(makeRange(visiblePosition, endOfDocument(visiblePosition)).get(), TextIteratorStopsOnFormControls);
+
+    Document* document = startPosition.document();
+    // The position will have no document if it is null (as in no position).
+    if (!document)
+        return;
+
+    // The forward range starts at the selection end and ends at the document's
+    // end. It will then be updated to only contain the text in the text in the
+    // right range around the selection.
+    RefPtrWillBeRawPtr<Range> forwardRange = Range::create(*document, endPosition, lastPositionInNode(document->documentElement()).parentAnchoredEquivalent());
+    CharacterIterator forwardIterator(forwardRange.get(), TextIteratorStopsOnFormControls);
+    // FIXME: why do we stop going trough the text if we were not able to select something on the right?
     if (!forwardIterator.atEnd())
         forwardIterator.advance(maxLength - halfMaxLength);
 
-    Position position = visiblePosition.deepEquivalent().parentAnchoredEquivalent();
-    Document* document = position.document();
-    ASSERT(document);
-    RefPtrWillBeRawPtr<Range> forwardRange = forwardIterator.range();
-    if (!forwardRange || !Range::create(*document, position, forwardRange->startPosition())->text().length()) {
+    forwardRange = forwardIterator.createRange();
+    if (!forwardRange || !Range::create(*document, endPosition, forwardRange->startPosition())->text().length()) {
         ASSERT(forwardRange);
         return;
     }
 
-    BackwardsCharacterIterator backwardsIterator(makeRange(startOfDocument(visiblePosition), visiblePosition).get(), TextIteratorStopsOnFormControls);
+    // Same as with the forward range but with the backward range. The range
+    // starts at the document's start and ends at the selection start and will
+    // be updated.
+    RefPtrWillBeRawPtr<Range> backwardsRange = Range::create(*document, firstPositionInNode(document->documentElement()).parentAnchoredEquivalent(), startPosition);
+    BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextIteratorStopsOnFormControls);
     if (!backwardsIterator.atEnd())
         backwardsIterator.advance(halfMaxLength);
 
-    RefPtrWillBeRawPtr<Range> backwardsRange = backwardsIterator.range();
-    if (!backwardsRange) {
-        ASSERT(backwardsRange);
-        return;
-    }
-
-    m_positionOffsetInContent = Range::create(*document, backwardsRange->endPosition(), position)->text().length();
-    m_contentRange = Range::create(*document, backwardsRange->endPosition(), forwardRange->startPosition());
+    m_startOffsetInContent = Range::create(*document, backwardsIterator.endPosition(), startPosition)->text().length();
+    m_endOffsetInContent = Range::create(*document, backwardsIterator.endPosition(), endPosition)->text().length();
+    m_contentRange = Range::create(*document, backwardsIterator.endPosition(), forwardRange->startPosition());
     ASSERT(m_contentRange);
 }
 
@@ -84,14 +103,12 @@ PassRefPtrWillBeRawPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned
     ASSERT(!iterator.atEnd());
     iterator.advance(startOffsetInContent);
 
-    ASSERT(iterator.range());
-    Position start = iterator.range()->startPosition();
+    Position start = iterator.startPosition();
 
     ASSERT(!iterator.atEnd());
     iterator.advance(endOffsetInContent - startOffsetInContent);
 
-    ASSERT(iterator.range());
-    Position end = iterator.range()->startPosition();
+    Position end = iterator.startPosition();
 
     ASSERT(start.document());
     return Range::create(*start.document(), start, end);
@@ -104,9 +121,14 @@ String SurroundingText::content() const
     return String();
 }
 
-unsigned SurroundingText::positionOffsetInContent() const
+unsigned SurroundingText::startOffsetInContent() const
+{
+    return m_startOffsetInContent;
+}
+
+unsigned SurroundingText::endOffsetInContent() const
 {
-    return m_positionOffsetInContent;
+    return m_endOffsetInContent;
 }
 
-} // namespace WebCore
+} // namespace blink