1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1.  Redistributions of source code must retain the above copyright
8 *     notice, this list of conditions and the following disclaimer.
9 * 2.  Redistributions in binary form must reproduce the above copyright
10 *     notice, this list of conditions and the following disclaimer in the
11 *     documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "config.h"
26#include "DOMTextContentWalker.h"
27
28#if OS(ANDROID)
29
30#include "Range.h"
31#include "TextIterator.h"
32#include "VisiblePosition.h"
33#include "VisibleSelection.h"
34#include "visible_units.h"
35
36namespace WebCore {
37
38static PassRefPtr<Range> getRange(const Position& start, const Position& end)
39{
40    return VisibleSelection(start.parentAnchoredEquivalent(), end.parentAnchoredEquivalent(), DOWNSTREAM).firstRange();
41}
42
43DOMTextContentWalker::DOMTextContentWalker(const VisiblePosition& position, unsigned maxLength)
44    : m_hitOffsetInContent(0)
45{
46    const unsigned halfMaxLength = maxLength / 2;
47    RefPtr<Range> forwardRange = makeRange(position, endOfDocument(position));
48    if (!forwardRange)
49        return;
50    CharacterIterator forwardChar(forwardRange.get(), TextIteratorStopsOnFormControls);
51    forwardChar.advance(maxLength - halfMaxLength);
52
53    // No forward contents, started inside form control.
54    if (getRange(position.deepEquivalent(), forwardChar.range()->startPosition())->text().length() == 0)
55        return;
56
57    RefPtr<Range> backwardsRange = makeRange(startOfDocument(position), position);
58    if (!backwardsRange)
59        return;
60    BackwardsCharacterIterator backwardsChar(backwardsRange.get(), TextIteratorStopsOnFormControls);
61    backwardsChar.advance(halfMaxLength);
62
63    m_hitOffsetInContent = getRange(backwardsChar.range()->endPosition(), position.deepEquivalent())->text().length();
64    m_contentRange = getRange(backwardsChar.range()->endPosition(), forwardChar.range()->startPosition());
65}
66
67PassRefPtr<Range> DOMTextContentWalker::contentOffsetsToRange(unsigned startInContent, unsigned endInContent)
68{
69    if (startInContent >= endInContent || endInContent > content().length())
70        return 0;
71
72    CharacterIterator iterator(m_contentRange.get());
73    iterator.advance(startInContent);
74
75    Position start = iterator.range()->startPosition();
76    iterator.advance(endInContent - startInContent);
77    Position end = iterator.range()->startPosition();
78    return getRange(start, end);
79}
80
81String DOMTextContentWalker::content() const
82{
83    if (m_contentRange)
84        return m_contentRange->text();
85    return String();
86}
87
88unsigned DOMTextContentWalker::hitOffsetInContent() const
89{
90    return m_hitOffsetInContent;
91}
92
93} // namespace WebCore
94
95#endif // OS(ANDROID)
96