1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights
* reserved.
* Portions Copyright (c) 2011 Motorola Mobility, Inc. All rights reserved.
*
* 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/visible_position.h"
#include <ostream>
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/text.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/editing/local_caret_rect.h"
#include "third_party/blink/renderer/core/editing/ng_flat_tree_shorthands.h"
#include "third_party/blink/renderer/core/editing/text_affinity.h"
#include "third_party/blink/renderer/core/editing/visible_units.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/layout/inline/offset_mapping.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "ui/gfx/geometry/quad_f.h"
namespace blink {
template <typename Strategy>
VisiblePositionTemplate<Strategy>::VisiblePositionTemplate()
#if DCHECK_IS_ON()
: dom_tree_version_(0),
style_version_(0)
#endif
{
}
template <typename Strategy>
VisiblePositionTemplate<Strategy>::VisiblePositionTemplate(
const PositionWithAffinityTemplate<Strategy>& position_with_affinity)
: position_with_affinity_(position_with_affinity)
#if DCHECK_IS_ON()
,
dom_tree_version_(position_with_affinity.GetDocument()->DomTreeVersion()),
style_version_(position_with_affinity.GetDocument()->StyleVersion())
#endif
{
}
template <typename Strategy>
void VisiblePositionTemplate<Strategy>::Trace(Visitor* visitor) const {
visitor->Trace(position_with_affinity_);
}
template <typename Strategy>
static inline bool InDifferentLinesOfSameInlineFormattingContext(
const PositionWithAffinityTemplate<Strategy>& position1,
const PositionWithAffinityTemplate<Strategy>& position2) {
DCHECK(position1.IsNotNull());
DCHECK(position2.IsNotNull());
// Optimization for common cases.
if (position1 == position2)
return false;
// InSameLine may DCHECK that the anchors have a layout object.
if (!position1.AnchorNode()->GetLayoutObject() ||
!position2.AnchorNode()->GetLayoutObject())
return false;
// Return false if the positions are in the same line.
if (InSameLine(position1, position2))
return false;
// Return whether the positions are in the same inline formatting context.
const LayoutBlockFlow* block1 =
NGInlineFormattingContextOf(position1.GetPosition());
return block1 &&
block1 == NGInlineFormattingContextOf(position2.GetPosition());
}
template <typename Strategy>
VisiblePositionTemplate<Strategy> VisiblePositionTemplate<Strategy>::Create(
const PositionWithAffinityTemplate<Strategy>& position_with_affinity) {
if (position_with_affinity.IsNull())
return VisiblePositionTemplate<Strategy>();
DCHECK(position_with_affinity.IsConnected()) << position_with_affinity;
Document& document = *position_with_affinity.GetDocument();
DCHECK(position_with_affinity.IsValidFor(document)) << position_with_affinity;
DCHECK(!document.NeedsLayoutTreeUpdate());
DocumentLifecycle::DisallowTransitionScope disallow_transition(
document.Lifecycle());
const PositionTemplate<Strategy> deep_position =
CanonicalPositionOf(position_with_affinity.GetPosition());
if (deep_position.IsNull())
return VisiblePositionTemplate<Strategy>();
const PositionWithAffinityTemplate<Strategy> downstream_position(
deep_position);
if (position_with_affinity.Affinity() == TextAffinity::kDownstream) {
// Fast path for common cases.
if (position_with_affinity == downstream_position)
return VisiblePositionTemplate<Strategy>(downstream_position);
// If the canonical position went into a previous line of the same inline
// formatting context, use the start of the current line instead.
const PositionInFlatTree& flat_deep_position =
ToPositionInFlatTree(deep_position);
const PositionInFlatTree& flat_position =
ToPositionInFlatTree(position_with_affinity.GetPosition());
if (flat_deep_position.IsNotNull() && flat_position.IsNotNull() &&
flat_deep_position < flat_position &&
InDifferentLinesOfSameInlineFormattingContext(position_with_affinity,
downstream_position)) {
const PositionWithAffinityTemplate<Strategy>& start_of_line =
StartOfLine(position_with_affinity);
if (start_of_line.IsNotNull())
return VisiblePositionTemplate<Strategy>(start_of_line);
}
// Otherwise use the canonical position.
return VisiblePositionTemplate<Strategy>(downstream_position);
}
if (RuntimeEnabledFeatures::BidiCaretAffinityEnabled() &&
NGInlineFormattingContextOf(deep_position)) {
// When not at a line wrap or bidi boundary, make sure to end up with
// |TextAffinity::Downstream| affinity.
const PositionWithAffinityTemplate<Strategy> upstream_position(
deep_position, TextAffinity::kUpstream);
if (AbsoluteCaretBoundsOf(downstream_position) !=
AbsoluteCaretBoundsOf(upstream_position)) {
return VisiblePositionTemplate<Strategy>(upstream_position);
}
return VisiblePositionTemplate<Strategy>(downstream_position);
}
// When not at a line wrap, make sure to end up with
// |TextAffinity::Downstream| affinity.
const PositionWithAffinityTemplate<Strategy> upstream_position(
deep_position, TextAffinity::kUpstream);
if (InSameLine(downstream_position, upstream_position))
return VisiblePositionTemplate<Strategy>(downstream_position);
return VisiblePositionTemplate<Strategy>(upstream_position);
}
template <typename Strategy>
VisiblePositionTemplate<Strategy> VisiblePositionTemplate<Strategy>::AfterNode(
const Node& node) {
return Create(PositionWithAffinityTemplate<Strategy>(
PositionTemplate<Strategy>::AfterNode(node)));
}
template <typename Strategy>
VisiblePositionTemplate<Strategy> VisiblePositionTemplate<Strategy>::BeforeNode(
const Node& node) {
return Create(PositionWithAffinityTemplate<Strategy>(
PositionTemplate<Strategy>::BeforeNode(node)));
}
template <typename Strategy>
VisiblePositionTemplate<Strategy>
VisiblePositionTemplate<Strategy>::FirstPositionInNode(const Node& node) {
return Create(PositionWithAffinityTemplate<Strategy>(
PositionTemplate<Strategy>::FirstPositionInNode(node)));
}
template <typename Strategy>
VisiblePositionTemplate<Strategy>
VisiblePositionTemplate<Strategy>::InParentAfterNode(const Node& node) {
return Create(PositionWithAffinityTemplate<Strategy>(
PositionTemplate<Strategy>::InParentAfterNode(node)));
}
template <typename Strategy>
VisiblePositionTemplate<Strategy>
VisiblePositionTemplate<Strategy>::InParentBeforeNode(const Node& node) {
return Create(PositionWithAffinityTemplate<Strategy>(
PositionTemplate<Strategy>::InParentBeforeNode(node)));
}
template <typename Strategy>
VisiblePositionTemplate<Strategy>
VisiblePositionTemplate<Strategy>::LastPositionInNode(const Node& node) {
return Create(PositionWithAffinityTemplate<Strategy>(
PositionTemplate<Strategy>::LastPositionInNode(node)));
}
VisiblePosition CreateVisiblePosition(const Position& position,
TextAffinity affinity) {
return VisiblePosition::Create(PositionWithAffinity(position, affinity));
}
VisiblePosition CreateVisiblePosition(
const PositionWithAffinity& position_with_affinity) {
return VisiblePosition::Create(position_with_affinity);
}
VisiblePositionInFlatTree CreateVisiblePosition(
const PositionInFlatTree& position,
TextAffinity affinity) {
return VisiblePositionInFlatTree::Create(
PositionInFlatTreeWithAffinity(position, affinity));
}
VisiblePositionInFlatTree CreateVisiblePosition(
const PositionInFlatTreeWithAffinity& position_with_affinity) {
return VisiblePositionInFlatTree::Create(position_with_affinity);
}
#if DCHECK_IS_ON()
template <typename Strategy>
void VisiblePositionTemplate<Strategy>::ShowTreeForThis() const {
DeepEquivalent().ShowTreeForThis();
}
#endif
template <typename Strategy>
bool VisiblePositionTemplate<Strategy>::IsValid() const {
#if DCHECK_IS_ON()
if (IsNull())
return true;
Document& document = *position_with_affinity_.GetDocument();
return dom_tree_version_ == document.DomTreeVersion() &&
style_version_ == document.StyleVersion() &&
!document.NeedsLayoutTreeUpdate();
#else
return true;
#endif
}
template <typename Strategy>
bool VisiblePositionTemplate<Strategy>::IsValidFor(
const Document& document) const {
return position_with_affinity_.IsValidFor(document);
}
template class CORE_TEMPLATE_EXPORT VisiblePositionTemplate<EditingStrategy>;
template class CORE_TEMPLATE_EXPORT
VisiblePositionTemplate<EditingInFlatTreeStrategy>;
std::ostream& operator<<(std::ostream& ostream,
const VisiblePosition& position) {
return ostream << position.DeepEquivalent() << '/' << position.Affinity();
}
std::ostream& operator<<(std::ostream& ostream,
const VisiblePositionInFlatTree& position) {
return ostream << position.DeepEquivalent() << '/' << position.Affinity();
}
} // namespace blink
#if DCHECK_IS_ON()
void ShowTree(const blink::VisiblePosition* vpos) {
if (vpos) {
vpos->ShowTreeForThis();
return;
}
DLOG(INFO) << "Cannot showTree for (nil) VisiblePosition.";
}
void ShowTree(const blink::VisiblePosition& vpos) {
vpos.ShowTreeForThis();
}
#endif
|