/* * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All * rights reserved. * Copyright (C) 2005 Alexey Proskuryakov. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "third_party/blink/renderer/core/editing/iterators/character_iterator.h" #include "third_party/blink/renderer/core/editing/ephemeral_range.h" namespace blink { template CharacterIteratorAlgorithm::CharacterIteratorAlgorithm( const PositionTemplate& start, const PositionTemplate& end, const TextIteratorBehavior& behavior) : offset_(0), run_offset_(0), at_break_(true), text_iterator_(start, end, behavior) { Initialize(); } template CharacterIteratorAlgorithm::CharacterIteratorAlgorithm( const EphemeralRangeTemplate& range, const TextIteratorBehavior& behavior) : CharacterIteratorAlgorithm(range.StartPosition(), range.EndPosition(), behavior) {} template void CharacterIteratorAlgorithm::Initialize() { while (!AtEnd() && !text_iterator_.length()) text_iterator_.Advance(); } template const Document& CharacterIteratorAlgorithm::OwnerDocument() const { return text_iterator_.OwnerDocument(); } template const Node& CharacterIteratorAlgorithm::CurrentContainer() const { return text_iterator_.CurrentContainer(); } template int CharacterIteratorAlgorithm::StartOffset() const { if (!text_iterator_.AtEnd()) { if (text_iterator_.length() > 1) return text_iterator_.StartOffsetInCurrentContainer() + run_offset_; DCHECK(!run_offset_); } return text_iterator_.StartOffsetInCurrentContainer(); } template int CharacterIteratorAlgorithm::EndOffset() const { if (!text_iterator_.AtEnd()) { if (text_iterator_.length() > 1) return text_iterator_.StartOffsetInCurrentContainer() + run_offset_ + 1; DCHECK(!run_offset_); } return text_iterator_.EndOffsetInCurrentContainer(); } template PositionTemplate CharacterIteratorAlgorithm::GetPositionBefore() const { return text_iterator_.GetPositionBefore(run_offset_); } template PositionTemplate CharacterIteratorAlgorithm::GetPositionAfter() const { return text_iterator_.GetPositionAfter(run_offset_); } template PositionTemplate CharacterIteratorAlgorithm::StartPosition() const { if (!text_iterator_.AtEnd()) { if (text_iterator_.length() > 1) { const Node& node = text_iterator_.CurrentContainer(); int offset = text_iterator_.StartOffsetInCurrentContainer() + run_offset_; return PositionTemplate::EditingPositionOf(&node, offset); } DCHECK(!run_offset_); } return text_iterator_.StartPositionInCurrentContainer(); } template PositionTemplate CharacterIteratorAlgorithm::EndPosition() const { if (!text_iterator_.AtEnd()) { if (text_iterator_.length() > 1) { const Node& node = text_iterator_.CurrentContainer(); int offset = text_iterator_.StartOffsetInCurrentContainer() + run_offset_; return PositionTemplate::EditingPositionOf(&node, offset + 1); } DCHECK(!run_offset_); } return text_iterator_.EndPositionInCurrentContainer(); } template void CharacterIteratorAlgorithm::Advance(int count) { if (count <= 0) { DCHECK(!count); return; } DCHECK(!AtEnd()); at_break_ = false; // easy if there is enough left in the current text_iterator_ run int remaining = text_iterator_.length() - run_offset_; if (count < remaining) { run_offset_ += count; offset_ += count; return; } // exhaust the current text_iterator_ run count -= remaining; offset_ += remaining; // move to a subsequent text_iterator_ run for (text_iterator_.Advance(); !AtEnd(); text_iterator_.Advance()) { int run_length = text_iterator_.length(); if (!run_length) { at_break_ = text_iterator_.BreaksAtReplacedElement(); } else { // see whether this is text_iterator_ to use if (count < run_length) { run_offset_ = count; offset_ += count; return; } // exhaust this text_iterator_ run count -= run_length; offset_ += run_length; } } // ran to the end of the text_iterator_... no more runs left at_break_ = true; run_offset_ = 0; } template EphemeralRangeTemplate CharacterIteratorAlgorithm::CalculateCharacterSubrange(int offset, int length) { Advance(offset); const PositionTemplate start_pos = StartPosition(); if (!length) return EphemeralRangeTemplate(start_pos, start_pos); if (length > 1) Advance(length - 1); return EphemeralRangeTemplate(start_pos, EndPosition()); } EphemeralRange CalculateCharacterSubrange(const EphemeralRange& range, int character_offset, int character_count) { CharacterIterator entire_range_iterator( range, TextIteratorBehavior::Builder() .SetEmitsObjectReplacementCharacter(true) .Build()); return entire_range_iterator.CalculateCharacterSubrange(character_offset, character_count); } template class CORE_TEMPLATE_EXPORT CharacterIteratorAlgorithm; template class CORE_TEMPLATE_EXPORT CharacterIteratorAlgorithm; } // namespace blink