1/* 2 * Copyright (C) 2012 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 are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31#include "config.h" 32#include "SurroundingText.h" 33 34#include "Document.h" 35#include "Range.h" 36#include "TextIterator.h" 37#include "VisiblePosition.h" 38#include "VisibleSelection.h" 39#include "VisibleUnits.h" 40 41namespace WebCore { 42 43SurroundingText::SurroundingText(const VisiblePosition& visiblePosition, unsigned maxLength) 44 : m_positionOffsetInContent(0) 45{ 46 if (visiblePosition.isNull()) 47 return; 48 49 const unsigned halfMaxLength = maxLength / 2; 50 CharacterIterator forwardIterator(makeRange(visiblePosition, endOfDocument(visiblePosition)).get(), TextIteratorStopsOnFormControls); 51 if (!forwardIterator.atEnd()) 52 forwardIterator.advance(maxLength - halfMaxLength); 53 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()) { 58 ASSERT(forwardRange); 59 return; 60 } 61 62 BackwardsCharacterIterator backwardsIterator(makeRange(startOfDocument(visiblePosition), visiblePosition).get(), TextIteratorStopsOnFormControls); 63 if (!backwardsIterator.atEnd()) 64 backwardsIterator.advance(halfMaxLength); 65 66 RefPtr<Range> backwardsRange = backwardsIterator.range(); 67 if (!backwardsRange) { 68 ASSERT(backwardsRange); 69 return; 70 } 71 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); 75} 76 77PassRefPtr<Range> SurroundingText::rangeFromContentOffsets(unsigned startOffsetInContent, unsigned endOffsetInContent) 78{ 79 if (startOffsetInContent >= endOffsetInContent || endOffsetInContent > content().length()) 80 return 0; 81 82 CharacterIterator iterator(m_contentRange.get()); 83 84 ASSERT(!iterator.atEnd()); 85 iterator.advance(startOffsetInContent); 86 87 ASSERT(iterator.range()); 88 Position start = iterator.range()->startPosition(); 89 90 ASSERT(!iterator.atEnd()); 91 iterator.advance(endOffsetInContent - startOffsetInContent); 92 93 ASSERT(iterator.range()); 94 Position end = iterator.range()->startPosition(); 95 96 return Range::create(start.document(), start, end); 97} 98 99String SurroundingText::content() const 100{ 101 if (m_contentRange) 102 return m_contentRange->text(); 103 return String(); 104} 105 106unsigned SurroundingText::positionOffsetInContent() const 107{ 108 return m_positionOffsetInContent; 109} 110 111} // namespace WebCore 112