| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights |
| 3 * reserved. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions |
| 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright |
| 11 * notice, this list of conditions and the following disclaimer in the |
| 12 * documentation and/or other materials provided with the distribution. |
| 13 * |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ |
| 26 |
| 27 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 28 // Use of this source code is governed by a BSD-style license that can be |
| 29 // found in the LICENSE file. |
| 30 |
| 31 #include "core/editing/VisibleUnits.h" |
| 32 |
| 33 #include "platform/text/TextBreakIterator.h" |
| 34 |
| 35 namespace blink { |
| 36 |
| 37 namespace { |
| 38 |
| 39 unsigned EndSentenceBoundary(const UChar* characters, |
| 40 unsigned length, |
| 41 unsigned, |
| 42 BoundarySearchContextAvailability, |
| 43 bool&) { |
| 44 TextBreakIterator* iterator = SentenceBreakIterator(characters, length); |
| 45 return iterator->next(); |
| 46 } |
| 47 |
| 48 unsigned NextSentencePositionBoundary(const UChar* characters, |
| 49 unsigned length, |
| 50 unsigned, |
| 51 BoundarySearchContextAvailability, |
| 52 bool&) { |
| 53 // FIXME: This is identical to endSentenceBoundary. This isn't right, it needs |
| 54 // to move to the equivlant position in the following sentence. |
| 55 TextBreakIterator* iterator = SentenceBreakIterator(characters, length); |
| 56 return iterator->following(0); |
| 57 } |
| 58 |
| 59 unsigned PreviousSentencePositionBoundary(const UChar* characters, |
| 60 unsigned length, |
| 61 unsigned, |
| 62 BoundarySearchContextAvailability, |
| 63 bool&) { |
| 64 // FIXME: This is identical to startSentenceBoundary. I'm pretty sure that's |
| 65 // not right. |
| 66 TextBreakIterator* iterator = SentenceBreakIterator(characters, length); |
| 67 // FIXME: The following function can return -1; we don't handle that. |
| 68 return iterator->preceding(length); |
| 69 } |
| 70 |
| 71 unsigned StartSentenceBoundary(const UChar* characters, |
| 72 unsigned length, |
| 73 unsigned, |
| 74 BoundarySearchContextAvailability, |
| 75 bool&) { |
| 76 TextBreakIterator* iterator = SentenceBreakIterator(characters, length); |
| 77 // FIXME: The following function can return -1; we don't handle that. |
| 78 return iterator->preceding(length); |
| 79 } |
| 80 |
| 81 // TODO(yosin) This includes the space after the punctuation that marks the end |
| 82 // of the sentence. |
| 83 template <typename Strategy> |
| 84 static VisiblePositionTemplate<Strategy> EndOfSentenceAlgorithm( |
| 85 const VisiblePositionTemplate<Strategy>& c) { |
| 86 DCHECK(c.IsValid()) << c; |
| 87 return CreateVisiblePosition(NextBoundary(c, EndSentenceBoundary), |
| 88 VP_UPSTREAM_IF_POSSIBLE); |
| 89 } |
| 90 |
| 91 template <typename Strategy> |
| 92 VisiblePositionTemplate<Strategy> StartOfSentenceAlgorithm( |
| 93 const VisiblePositionTemplate<Strategy>& c) { |
| 94 DCHECK(c.IsValid()) << c; |
| 95 return CreateVisiblePosition(PreviousBoundary(c, StartSentenceBoundary)); |
| 96 } |
| 97 |
| 98 } // namespace |
| 99 |
| 100 VisiblePosition EndOfSentence(const VisiblePosition& c) { |
| 101 return EndOfSentenceAlgorithm<EditingStrategy>(c); |
| 102 } |
| 103 |
| 104 VisiblePositionInFlatTree EndOfSentence(const VisiblePositionInFlatTree& c) { |
| 105 return EndOfSentenceAlgorithm<EditingInFlatTreeStrategy>(c); |
| 106 } |
| 107 |
| 108 EphemeralRange ExpandEndToSentenceBoundary(const EphemeralRange& range) { |
| 109 DCHECK(range.IsNotNull()); |
| 110 const VisiblePosition& visible_end = |
| 111 CreateVisiblePosition(range.EndPosition()); |
| 112 DCHECK(visible_end.IsNotNull()); |
| 113 const Position& sentence_end = EndOfSentence(visible_end).DeepEquivalent(); |
| 114 // TODO(xiaochengh): |sentenceEnd < range.endPosition()| is possible, |
| 115 // which would trigger a DCHECK in EphemeralRange's constructor if we return |
| 116 // it directly. However, this shouldn't happen and needs to be fixed. |
| 117 return EphemeralRange( |
| 118 range.StartPosition(), |
| 119 sentence_end.IsNotNull() && sentence_end > range.EndPosition() |
| 120 ? sentence_end |
| 121 : range.EndPosition()); |
| 122 } |
| 123 |
| 124 EphemeralRange ExpandRangeToSentenceBoundary(const EphemeralRange& range) { |
| 125 DCHECK(range.IsNotNull()); |
| 126 const VisiblePosition& visible_start = |
| 127 CreateVisiblePosition(range.StartPosition()); |
| 128 DCHECK(visible_start.IsNotNull()); |
| 129 const Position& sentence_start = |
| 130 StartOfSentence(visible_start).DeepEquivalent(); |
| 131 // TODO(xiaochengh): |sentenceStart > range.startPosition()| is possible, |
| 132 // which would trigger a DCHECK in EphemeralRange's constructor if we return |
| 133 // it directly. However, this shouldn't happen and needs to be fixed. |
| 134 return ExpandEndToSentenceBoundary(EphemeralRange( |
| 135 sentence_start.IsNotNull() && sentence_start < range.StartPosition() |
| 136 ? sentence_start |
| 137 : range.StartPosition(), |
| 138 range.EndPosition())); |
| 139 } |
| 140 |
| 141 VisiblePosition NextSentencePosition(const VisiblePosition& c) { |
| 142 DCHECK(c.IsValid()) << c; |
| 143 VisiblePosition next = CreateVisiblePosition( |
| 144 NextBoundary(c, NextSentencePositionBoundary), VP_UPSTREAM_IF_POSSIBLE); |
| 145 return HonorEditingBoundaryAtOrAfter(next, c.DeepEquivalent()); |
| 146 } |
| 147 |
| 148 VisiblePosition PreviousSentencePosition(const VisiblePosition& c) { |
| 149 DCHECK(c.IsValid()) << c; |
| 150 VisiblePosition prev = CreateVisiblePosition( |
| 151 PreviousBoundary(c, PreviousSentencePositionBoundary)); |
| 152 return HonorEditingBoundaryAtOrBefore(prev, c.DeepEquivalent()); |
| 153 } |
| 154 |
| 155 VisiblePosition StartOfSentence(const VisiblePosition& c) { |
| 156 return StartOfSentenceAlgorithm<EditingStrategy>(c); |
| 157 } |
| 158 |
| 159 VisiblePositionInFlatTree StartOfSentence(const VisiblePositionInFlatTree& c) { |
| 160 return StartOfSentenceAlgorithm<EditingInFlatTreeStrategy>(c); |
| 161 } |
| 162 |
| 163 } // namespace blink |
| OLD | NEW |