1773979f92560dd1aead375c82fd75b584a141e5dJohn Reck/* 2773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * Copyright (C) 2011 Google Inc. All rights reserved. 3773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * 4773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * Redistribution and use in source and binary forms, with or without 5773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * modification, are permitted provided that the following conditions 6773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * are met: 7773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * 1. Redistributions of source code must retain the above copyright 8773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * notice, this list of conditions and the following disclaimer. 9773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * 2. Redistributions in binary form must reproduce the above copyright 10773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * notice, this list of conditions and the following disclaimer in the 11773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * documentation and/or other materials provided with the distribution. 12773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * 13773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22773979f92560dd1aead375c82fd75b584a141e5dJohn Reck * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23773979f92560dd1aead375c82fd75b584a141e5dJohn Reck */ 24773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 25773979f92560dd1aead375c82fd75b584a141e5dJohn Reck#include "config.h" 26773979f92560dd1aead375c82fd75b584a141e5dJohn Reck#include "DOMTextContentWalker.h" 27773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 28773979f92560dd1aead375c82fd75b584a141e5dJohn Reck#if OS(ANDROID) 29773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 30773979f92560dd1aead375c82fd75b584a141e5dJohn Reck#include "Range.h" 31773979f92560dd1aead375c82fd75b584a141e5dJohn Reck#include "TextIterator.h" 32773979f92560dd1aead375c82fd75b584a141e5dJohn Reck#include "VisiblePosition.h" 33773979f92560dd1aead375c82fd75b584a141e5dJohn Reck#include "VisibleSelection.h" 34773979f92560dd1aead375c82fd75b584a141e5dJohn Reck#include "visible_units.h" 35773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 36773979f92560dd1aead375c82fd75b584a141e5dJohn Recknamespace WebCore { 37773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 38773979f92560dd1aead375c82fd75b584a141e5dJohn Reckstatic PassRefPtr<Range> getRange(const Position& start, const Position& end) 39773979f92560dd1aead375c82fd75b584a141e5dJohn Reck{ 40773979f92560dd1aead375c82fd75b584a141e5dJohn Reck return VisibleSelection(start.parentAnchoredEquivalent(), end.parentAnchoredEquivalent(), DOWNSTREAM).firstRange(); 41773979f92560dd1aead375c82fd75b584a141e5dJohn Reck} 42773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 43773979f92560dd1aead375c82fd75b584a141e5dJohn ReckDOMTextContentWalker::DOMTextContentWalker(const VisiblePosition& position, unsigned maxLength) 44773979f92560dd1aead375c82fd75b584a141e5dJohn Reck : m_hitOffsetInContent(0) 45773979f92560dd1aead375c82fd75b584a141e5dJohn Reck{ 46773979f92560dd1aead375c82fd75b584a141e5dJohn Reck const unsigned halfMaxLength = maxLength / 2; 479b759976fec55ad944eba534a560c9c83400ed91John Reck RefPtr<Range> forwardRange = makeRange(position, endOfDocument(position)); 489b759976fec55ad944eba534a560c9c83400ed91John Reck if (!forwardRange) 499b759976fec55ad944eba534a560c9c83400ed91John Reck return; 509b759976fec55ad944eba534a560c9c83400ed91John Reck CharacterIterator forwardChar(forwardRange.get(), TextIteratorStopsOnFormControls); 51773979f92560dd1aead375c82fd75b584a141e5dJohn Reck forwardChar.advance(maxLength - halfMaxLength); 52773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 53773979f92560dd1aead375c82fd75b584a141e5dJohn Reck // No forward contents, started inside form control. 549b759976fec55ad944eba534a560c9c83400ed91John Reck if (getRange(position.deepEquivalent(), forwardChar.range()->startPosition())->text().length() == 0) 55773979f92560dd1aead375c82fd75b584a141e5dJohn Reck return; 56773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 579b759976fec55ad944eba534a560c9c83400ed91John Reck RefPtr<Range> backwardsRange = makeRange(startOfDocument(position), position); 589b759976fec55ad944eba534a560c9c83400ed91John Reck if (!backwardsRange) 599b759976fec55ad944eba534a560c9c83400ed91John Reck return; 609b759976fec55ad944eba534a560c9c83400ed91John Reck BackwardsCharacterIterator backwardsChar(backwardsRange.get(), TextIteratorStopsOnFormControls); 61773979f92560dd1aead375c82fd75b584a141e5dJohn Reck backwardsChar.advance(halfMaxLength); 62773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 63773979f92560dd1aead375c82fd75b584a141e5dJohn Reck m_hitOffsetInContent = getRange(backwardsChar.range()->endPosition(), position.deepEquivalent())->text().length(); 64773979f92560dd1aead375c82fd75b584a141e5dJohn Reck m_contentRange = getRange(backwardsChar.range()->endPosition(), forwardChar.range()->startPosition()); 65773979f92560dd1aead375c82fd75b584a141e5dJohn Reck} 66773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 67773979f92560dd1aead375c82fd75b584a141e5dJohn ReckPassRefPtr<Range> DOMTextContentWalker::contentOffsetsToRange(unsigned startInContent, unsigned endInContent) 68773979f92560dd1aead375c82fd75b584a141e5dJohn Reck{ 69773979f92560dd1aead375c82fd75b584a141e5dJohn Reck if (startInContent >= endInContent || endInContent > content().length()) 70773979f92560dd1aead375c82fd75b584a141e5dJohn Reck return 0; 71773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 72773979f92560dd1aead375c82fd75b584a141e5dJohn Reck CharacterIterator iterator(m_contentRange.get()); 73773979f92560dd1aead375c82fd75b584a141e5dJohn Reck iterator.advance(startInContent); 74773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 75773979f92560dd1aead375c82fd75b584a141e5dJohn Reck Position start = iterator.range()->startPosition(); 76773979f92560dd1aead375c82fd75b584a141e5dJohn Reck iterator.advance(endInContent - startInContent); 77773979f92560dd1aead375c82fd75b584a141e5dJohn Reck Position end = iterator.range()->startPosition(); 78773979f92560dd1aead375c82fd75b584a141e5dJohn Reck return getRange(start, end); 79773979f92560dd1aead375c82fd75b584a141e5dJohn Reck} 80773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 81773979f92560dd1aead375c82fd75b584a141e5dJohn ReckString DOMTextContentWalker::content() const 82773979f92560dd1aead375c82fd75b584a141e5dJohn Reck{ 83773979f92560dd1aead375c82fd75b584a141e5dJohn Reck if (m_contentRange) 84773979f92560dd1aead375c82fd75b584a141e5dJohn Reck return m_contentRange->text(); 85773979f92560dd1aead375c82fd75b584a141e5dJohn Reck return String(); 86773979f92560dd1aead375c82fd75b584a141e5dJohn Reck} 87773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 88773979f92560dd1aead375c82fd75b584a141e5dJohn Reckunsigned DOMTextContentWalker::hitOffsetInContent() const 89773979f92560dd1aead375c82fd75b584a141e5dJohn Reck{ 90773979f92560dd1aead375c82fd75b584a141e5dJohn Reck return m_hitOffsetInContent; 91773979f92560dd1aead375c82fd75b584a141e5dJohn Reck} 92773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 93773979f92560dd1aead375c82fd75b584a141e5dJohn Reck} // namespace WebCore 94773979f92560dd1aead375c82fd75b584a141e5dJohn Reck 95773979f92560dd1aead375c82fd75b584a141e5dJohn Reck#endif // OS(ANDROID) 96