2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "SurroundingText.h"
36 #include "TextIterator.h"
37 #include "VisiblePosition.h"
38 #include "VisibleSelection.h"
39 #include "visible_units.h"
43 SurroundingText::SurroundingText(const VisiblePosition& visiblePosition, unsigned maxLength)
44 : m_positionOffsetInContent(0)
46 if (visiblePosition.isNull())
49 const unsigned halfMaxLength = maxLength / 2;
50 CharacterIterator forwardIterator(makeRange(visiblePosition, endOfDocument(visiblePosition)).get(), TextIteratorStopsOnFormControls);
51 if (!forwardIterator.atEnd())
52 forwardIterator.advance(maxLength - halfMaxLength);
54 Position position = visiblePosition.deepEquivalent().parentAnchoredEquivalent();
55 Document* document = position.document();
56 RefPtr<Range> forwardRange = forwardIterator.range();
57 if (!forwardRange || !Range::create(document, position, forwardRange->startPosition())->text().length()) {
62 BackwardsCharacterIterator backwardsIterator(makeRange(startOfDocument(visiblePosition), visiblePosition).get(), TextIteratorStopsOnFormControls);
63 if (!backwardsIterator.atEnd())
64 backwardsIterator.advance(halfMaxLength);
66 RefPtr<Range> backwardsRange = backwardsIterator.range();
67 if (!backwardsRange) {
68 ASSERT(backwardsRange);
72 m_positionOffsetInContent = Range::create(document, backwardsRange->endPosition(), position)->text().length();
73 m_contentRange = Range::create(document, backwardsRange->endPosition(), forwardRange->startPosition());
74 ASSERT(m_contentRange);
77 PassRefPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned startOffsetInContent, unsigned endOffsetInContent)
79 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > content().length())
82 CharacterIterator iterator(m_contentRange.get());
84 ASSERT(!iterator.atEnd());
85 iterator.advance(startOffsetInContent);
87 ASSERT(iterator.range());
88 Position start = iterator.range()->startPosition();
90 ASSERT(!iterator.atEnd());
91 iterator.advance(endOffsetInContent - startOffsetInContent);
93 ASSERT(iterator.range());
94 Position end = iterator.range()->startPosition();
96 return Range::create(start.document(), start, end);
99 String SurroundingText::content() const
102 return m_contentRange->text();
106 unsigned SurroundingText::positionOffsetInContent() const
108 return m_positionOffsetInContent;
111 } // namespace WebCore