Commit e7cb864f authored by mlamouri@chromium.org's avatar mlamouri@chromium.org

Use Position instead of VisiblePosition for SurroundingText.

Using VisiblePosition has some not required performance implications.

BUG=None

Review URL: https://codereview.chromium.org/307353002

git-svn-id: svn://svn.chromium.org/blink/trunk@175788 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent ddc83fc1
......@@ -3387,6 +3387,7 @@
'dom/MainThreadTaskRunnerTest.cpp',
'dom/RangeTest.cpp',
'dom/TreeScopeTest.cpp',
'editing/SurroundingTextTest.cpp',
'editing/TextIteratorTest.cpp',
'editing/VisibleSelectionTest.cpp',
'fetch/CachingCorrectnessTest.cpp',
......
......@@ -32,38 +32,46 @@
#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 {
SurroundingText::SurroundingText(const VisiblePosition& visiblePosition, unsigned maxLength)
SurroundingText::SurroundingText(const Position& position, unsigned maxLength)
: m_positionOffsetInContent(0)
{
if (visiblePosition.isNull())
const unsigned halfMaxLength = maxLength / 2;
Document* document = position.document();
// The |position| will have no document if it is null (as in no position).
if (!document)
return;
const unsigned halfMaxLength = maxLength / 2;
CharacterIterator forwardIterator(makeRange(visiblePosition, endOfDocument(visiblePosition)).get(), TextIteratorStopsOnFormControls);
// 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, position, lastPositionInNode(document->documentElement()).parentAnchoredEquivalent());
CharacterIterator forwardIterator(forwardRange.get(), TextIteratorStopsOnFormControls);
if (!forwardIterator.atEnd())
forwardIterator.advance(maxLength - halfMaxLength);
Position position = visiblePosition.deepEquivalent().parentAnchoredEquivalent();
Document* document = position.document();
ASSERT(document);
RefPtrWillBeRawPtr<Range> forwardRange = forwardIterator.range();
forwardRange = forwardIterator.range();
if (!forwardRange || !Range::create(*document, position, 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(), position);
BackwardsCharacterIterator backwardsIterator(backwardsRange.get(), TextIteratorStopsOnFormControls);
if (!backwardsIterator.atEnd())
backwardsIterator.advance(halfMaxLength);
RefPtrWillBeRawPtr<Range> backwardsRange = backwardsIterator.range();
backwardsRange = backwardsIterator.range();
if (!backwardsRange) {
ASSERT(backwardsRange);
return;
......
......@@ -36,13 +36,13 @@
namespace WebCore {
class Position;
class Range;
class VisiblePosition;
class SurroundingText {
WTF_MAKE_NONCOPYABLE(SurroundingText);
public:
SurroundingText(const VisiblePosition&, unsigned maxLength);
SurroundingText(const Position&, unsigned maxLength);
String content() const;
unsigned positionOffsetInContent() const;
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "core/editing/SurroundingText.h"
#include "core/dom/Position.h"
#include "core/dom/Range.h"
#include "core/dom/Text.h"
#include "core/editing/VisibleSelection.h"
#include "core/html/HTMLElement.h"
#include "core/testing/DummyPageHolder.h"
#include <gtest/gtest.h>
using namespace WebCore;
namespace {
class SurroundingTextTest : public ::testing::Test {
protected:
Document& document() const { return m_dummyPageHolder->document(); }
void setHTML(const String&);
VisibleSelection select(int offset) { return select(offset, offset); }
VisibleSelection select(int start, int end);
private:
virtual void SetUp() OVERRIDE;
OwnPtr<DummyPageHolder> m_dummyPageHolder;
};
void SurroundingTextTest::SetUp()
{
m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600));
}
void SurroundingTextTest::setHTML(const String& content)
{
document().body()->setInnerHTML(content, ASSERT_NO_EXCEPTION);
}
VisibleSelection SurroundingTextTest::select(int start, int end)
{
Element* element = document().getElementById("selection");
VisibleSelection selection;
selection.setBase(Position(toText(element->firstChild()), start));
selection.setExtent(Position(toText(element->firstChild()), end));
return selection;
}
TEST_F(SurroundingTextTest, BasicCaretSelection)
{
setHTML(String("<p id='selection'>foo bar</p>"));
{
VisibleSelection selection = select(0);
SurroundingText surroundingText(selection.start(), 1);
EXPECT_EQ("f", surroundingText.content());
EXPECT_EQ(0u, surroundingText.positionOffsetInContent());
}
{
VisibleSelection selection = select(0);
SurroundingText surroundingText(selection.start(), 5);
// maxlength/2 is used on the left and right.
EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace());
EXPECT_EQ(1u, surroundingText.positionOffsetInContent());
}
{
VisibleSelection selection = select(0);
SurroundingText surroundingText(selection.start(), 42);
EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace());
EXPECT_EQ(1u, surroundingText.positionOffsetInContent());
}
{
// FIXME: if the selection is at the end of the text, SurroundingText
// will return nothing.
VisibleSelection selection = select(7);
SurroundingText surroundingText(selection.start(), 42);
EXPECT_EQ(0u, surroundingText.content().length());
EXPECT_EQ(0u, surroundingText.positionOffsetInContent());
}
{
VisibleSelection selection = select(6);
SurroundingText surroundingText(selection.start(), 2);
EXPECT_EQ("ar", surroundingText.content());
EXPECT_EQ(1u, surroundingText.positionOffsetInContent());
}
{
VisibleSelection selection = select(6);
SurroundingText surroundingText(selection.start(), 42);
EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace());
EXPECT_EQ(7u, surroundingText.positionOffsetInContent());
}
}
TEST_F(SurroundingTextTest, TreeCaretSelection)
{
setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the selected node</div>"));
{
VisibleSelection selection = select(0);
SurroundingText surroundingText(selection.start(), 1);
EXPECT_EQ("f", surroundingText.content());
EXPECT_EQ(0u, surroundingText.positionOffsetInContent());
}
{
VisibleSelection selection = select(0);
SurroundingText surroundingText(selection.start(), 5);
EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace());
EXPECT_EQ(1u, surroundingText.positionOffsetInContent());
}
{
VisibleSelection selection = select(0);
SurroundingText surroundingText(selection.start(), 1337);
EXPECT_EQ("This is outside of foo bar the selected node", surroundingText.content().simplifyWhiteSpace());
EXPECT_EQ(20u, surroundingText.positionOffsetInContent());
}
{
VisibleSelection selection = select(6);
SurroundingText surroundingText(selection.start(), 2);
EXPECT_EQ("ar", surroundingText.content());
EXPECT_EQ(1u, surroundingText.positionOffsetInContent());
}
{
VisibleSelection selection = select(6);
SurroundingText surroundingText(selection.start(), 1337);
EXPECT_EQ("This is outside of foo bar the selected node", surroundingText.content().simplifyWhiteSpace());
EXPECT_EQ(26u, surroundingText.positionOffsetInContent());
}
}
} // anonymous namespace
......@@ -2338,7 +2338,7 @@ String Internals::textSurroundingNode(Node* node, int x, int y, unsigned long ma
if (!node)
return String();
blink::WebPoint point(x, y);
SurroundingText surroundingText(VisiblePosition(node->renderer()->positionForPoint(static_cast<IntPoint>(point))), maxLength);
SurroundingText surroundingText(VisiblePosition(node->renderer()->positionForPoint(static_cast<IntPoint>(point))).deepEquivalent().parentAnchoredEquivalent(), maxLength);
return surroundingText.content();
}
......
......@@ -45,7 +45,7 @@ void WebSurroundingText::initialize(const WebNode& webNode, const WebPoint& node
if (!node || !node->renderer())
return;
m_private.reset(new SurroundingText(VisiblePosition(node->renderer()->positionForPoint(static_cast<IntPoint>(nodePoint))), maxLength));
m_private.reset(new SurroundingText(VisiblePosition(node->renderer()->positionForPoint(static_cast<IntPoint>(nodePoint))).deepEquivalent().parentAnchoredEquivalent(), maxLength));
}
WebString WebSurroundingText::textContent() const
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment