| 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 "core/editing/EditingUtilities.h" |
| 34 #include "core/editing/iterators/BackwardsCharacterIterator.h" |
| 35 #include "core/editing/iterators/BackwardsTextBuffer.h" |
| 36 #include "core/editing/iterators/CharacterIterator.h" |
| 37 #include "core/editing/iterators/ForwardsTextBuffer.h" |
| 38 #include "core/editing/iterators/SimplifiedBackwardsTextIterator.h" |
| 39 #include "platform/instrumentation/tracing/TraceEvent.h" |
| 40 #include "platform/text/TextBoundaries.h" |
| 41 #include "platform/text/TextBreakIterator.h" |
| 42 |
| 43 namespace blink { |
| 44 |
| 45 namespace { |
| 46 |
| 47 unsigned EndWordBoundary( |
| 48 const UChar* characters, |
| 49 unsigned length, |
| 50 unsigned offset, |
| 51 BoundarySearchContextAvailability may_have_more_context, |
| 52 bool& need_more_context) { |
| 53 DCHECK_LE(offset, length); |
| 54 if (may_have_more_context && |
| 55 EndOfFirstWordBoundaryContext(characters + offset, length - offset) == |
| 56 static_cast<int>(length - offset)) { |
| 57 need_more_context = true; |
| 58 return length; |
| 59 } |
| 60 need_more_context = false; |
| 61 return FindWordEndBoundary(characters, length, offset); |
| 62 } |
| 63 |
| 64 template <typename Strategy> |
| 65 PositionTemplate<Strategy> EndOfWordAlgorithm( |
| 66 const VisiblePositionTemplate<Strategy>& c, |
| 67 EWordSide side) { |
| 68 DCHECK(c.IsValid()) << c; |
| 69 VisiblePositionTemplate<Strategy> p = c; |
| 70 if (side == kLeftWordIfOnBoundary) { |
| 71 if (IsStartOfParagraph(c)) |
| 72 return c.DeepEquivalent(); |
| 73 |
| 74 p = PreviousPositionOf(c); |
| 75 if (p.IsNull()) |
| 76 return c.DeepEquivalent(); |
| 77 } else if (IsEndOfParagraph(c)) { |
| 78 return c.DeepEquivalent(); |
| 79 } |
| 80 |
| 81 return NextBoundary(p, EndWordBoundary); |
| 82 } |
| 83 |
| 84 unsigned NextWordPositionBoundary( |
| 85 const UChar* characters, |
| 86 unsigned length, |
| 87 unsigned offset, |
| 88 BoundarySearchContextAvailability may_have_more_context, |
| 89 bool& need_more_context) { |
| 90 if (may_have_more_context && |
| 91 EndOfFirstWordBoundaryContext(characters + offset, length - offset) == |
| 92 static_cast<int>(length - offset)) { |
| 93 need_more_context = true; |
| 94 return length; |
| 95 } |
| 96 need_more_context = false; |
| 97 return FindNextWordFromIndex(characters, length, offset, true); |
| 98 } |
| 99 |
| 100 unsigned PreviousWordPositionBoundary( |
| 101 const UChar* characters, |
| 102 unsigned length, |
| 103 unsigned offset, |
| 104 BoundarySearchContextAvailability may_have_more_context, |
| 105 bool& need_more_context) { |
| 106 if (may_have_more_context && |
| 107 !StartOfLastWordBoundaryContext(characters, offset)) { |
| 108 need_more_context = true; |
| 109 return 0; |
| 110 } |
| 111 need_more_context = false; |
| 112 return FindNextWordFromIndex(characters, length, offset, false); |
| 113 } |
| 114 |
| 115 unsigned StartWordBoundary( |
| 116 const UChar* characters, |
| 117 unsigned length, |
| 118 unsigned offset, |
| 119 BoundarySearchContextAvailability may_have_more_context, |
| 120 bool& need_more_context) { |
| 121 TRACE_EVENT0("blink", "startWordBoundary"); |
| 122 DCHECK(offset); |
| 123 if (may_have_more_context && |
| 124 !StartOfLastWordBoundaryContext(characters, offset)) { |
| 125 need_more_context = true; |
| 126 return 0; |
| 127 } |
| 128 need_more_context = false; |
| 129 int start, end; |
| 130 U16_BACK_1(characters, 0, offset); |
| 131 FindWordBoundary(characters, length, offset, &start, &end); |
| 132 return start; |
| 133 } |
| 134 |
| 135 template <typename Strategy> |
| 136 PositionTemplate<Strategy> StartOfWordAlgorithm( |
| 137 const VisiblePositionTemplate<Strategy>& c, |
| 138 EWordSide side) { |
| 139 DCHECK(c.IsValid()) << c; |
| 140 // TODO(yosin) This returns a null VP for c at the start of the document |
| 141 // and |side| == |LeftWordIfOnBoundary| |
| 142 VisiblePositionTemplate<Strategy> p = c; |
| 143 if (side == kRightWordIfOnBoundary) { |
| 144 // at paragraph end, the startofWord is the current position |
| 145 if (IsEndOfParagraph(c)) |
| 146 return c.DeepEquivalent(); |
| 147 |
| 148 p = NextPositionOf(c); |
| 149 if (p.IsNull()) |
| 150 return c.DeepEquivalent(); |
| 151 } |
| 152 return PreviousBoundary(p, StartWordBoundary); |
| 153 } |
| 154 |
| 155 } // namespace |
| 156 |
| 157 Position EndOfWordPosition(const VisiblePosition& position, EWordSide side) { |
| 158 return EndOfWordAlgorithm<EditingStrategy>(position, side); |
| 159 } |
| 160 |
| 161 VisiblePosition EndOfWord(const VisiblePosition& position, EWordSide side) { |
| 162 return CreateVisiblePosition(EndOfWordPosition(position, side), |
| 163 VP_UPSTREAM_IF_POSSIBLE); |
| 164 } |
| 165 |
| 166 PositionInFlatTree EndOfWordPosition(const VisiblePositionInFlatTree& position, |
| 167 EWordSide side) { |
| 168 return EndOfWordAlgorithm<EditingInFlatTreeStrategy>(position, side); |
| 169 } |
| 170 |
| 171 VisiblePositionInFlatTree EndOfWord(const VisiblePositionInFlatTree& position, |
| 172 EWordSide side) { |
| 173 return CreateVisiblePosition(EndOfWordPosition(position, side), |
| 174 VP_UPSTREAM_IF_POSSIBLE); |
| 175 } |
| 176 |
| 177 VisiblePosition NextWordPosition(const VisiblePosition& c) { |
| 178 DCHECK(c.IsValid()) << c; |
| 179 VisiblePosition next = CreateVisiblePosition( |
| 180 NextBoundary(c, NextWordPositionBoundary), VP_UPSTREAM_IF_POSSIBLE); |
| 181 return HonorEditingBoundaryAtOrAfter(next, c.DeepEquivalent()); |
| 182 } |
| 183 |
| 184 VisiblePosition PreviousWordPosition(const VisiblePosition& c) { |
| 185 DCHECK(c.IsValid()) << c; |
| 186 VisiblePosition prev = |
| 187 CreateVisiblePosition(PreviousBoundary(c, PreviousWordPositionBoundary)); |
| 188 return HonorEditingBoundaryAtOrBefore(prev, c.DeepEquivalent()); |
| 189 } |
| 190 |
| 191 Position StartOfWordPosition(const VisiblePosition& position, EWordSide side) { |
| 192 return StartOfWordAlgorithm<EditingStrategy>(position, side); |
| 193 } |
| 194 |
| 195 VisiblePosition StartOfWord(const VisiblePosition& position, EWordSide side) { |
| 196 return CreateVisiblePosition(StartOfWordPosition(position, side)); |
| 197 } |
| 198 |
| 199 PositionInFlatTree StartOfWordPosition( |
| 200 const VisiblePositionInFlatTree& position, |
| 201 EWordSide side) { |
| 202 return StartOfWordAlgorithm<EditingInFlatTreeStrategy>(position, side); |
| 203 } |
| 204 |
| 205 VisiblePositionInFlatTree StartOfWord(const VisiblePositionInFlatTree& position, |
| 206 EWordSide side) { |
| 207 return CreateVisiblePosition(StartOfWordPosition(position, side)); |
| 208 } |
| 209 |
| 210 } // namespace blink |
| OLD | NEW |