summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/dom/range_boundary_point.h
blob: 66fd77584ddb6853ea3c4682d13d717d1607c10d (plain)
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
/*
 * Copyright (C) 2008 Apple 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 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 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.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_DOM_RANGE_BOUNDARY_POINT_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_DOM_RANGE_BOUNDARY_POINT_H_

#include "third_party/blink/renderer/core/dom/character_data.h"
#include "third_party/blink/renderer/core/dom/node.h"
#include "third_party/blink/renderer/core/dom/node_traversal.h"
#include "third_party/blink/renderer/core/editing/position.h"

namespace blink {

class RangeBoundaryPoint {
  DISALLOW_NEW();

 public:
  explicit RangeBoundaryPoint(Node& container);

  RangeBoundaryPoint(const RangeBoundaryPoint&);
  RangeBoundaryPoint& operator=(const RangeBoundaryPoint&);

  bool IsConnected() const;
  const Position ToPosition() const;

  Node& Container() const;
  unsigned Offset() const;
  Node* ChildBefore() const;

  void Set(Node& container, unsigned offset, Node* child_before);
  void SetOffset(unsigned);

  void SetToBeforeChild(Node&);
  void SetToStartOfNode(Node&);
  void SetToEndOfNode(Node&);

  void ChildBeforeWillBeRemoved();
  void InvalidateOffset();
  void MarkValid() const;

  void Trace(Visitor* visitor) const {
    visitor->Trace(container_node_);
    visitor->Trace(child_before_boundary_);
  }

 private:
  uint64_t DomTreeVersion() const;
  void EnsureOffsetIsValid() const;
  bool IsOffsetValid() const;

  static const unsigned kInvalidOffset = static_cast<unsigned>(-1);

  Member<Node> container_node_;
  Member<Node> child_before_boundary_;
  mutable uint64_t dom_tree_version_;
  mutable unsigned offset_in_container_;
};

inline RangeBoundaryPoint::RangeBoundaryPoint(Node& container)
    : container_node_(container),
      child_before_boundary_(nullptr),
      dom_tree_version_(DomTreeVersion()),
      offset_in_container_(0) {}

inline RangeBoundaryPoint::RangeBoundaryPoint(const RangeBoundaryPoint&) =
    default;

inline RangeBoundaryPoint& RangeBoundaryPoint::operator=(
    const RangeBoundaryPoint& other) = default;

inline Node& RangeBoundaryPoint::Container() const {
  return *container_node_;
}

inline Node* RangeBoundaryPoint::ChildBefore() const {
  return child_before_boundary_.Get();
}

inline uint64_t RangeBoundaryPoint::DomTreeVersion() const {
  return container_node_->GetDocument().DomTreeVersion();
}

inline void RangeBoundaryPoint::EnsureOffsetIsValid() const {
  if (IsOffsetValid())
    return;
  DCHECK(!container_node_->IsCharacterDataNode());
  MarkValid();
  if (!child_before_boundary_) {
    offset_in_container_ = 0;
    return;
  }
  offset_in_container_ = child_before_boundary_->NodeIndex() + 1;
}

inline bool RangeBoundaryPoint::IsConnected() const {
  return container_node_ && container_node_->isConnected();
}

inline bool RangeBoundaryPoint::IsOffsetValid() const {
  if (offset_in_container_ == kInvalidOffset) {
    DCHECK(!container_node_->IsTextNode());
    return false;
  }
  return DomTreeVersion() == dom_tree_version_ ||
         container_node_->IsCharacterDataNode();
}

inline const Position RangeBoundaryPoint::ToPosition() const {
  EnsureOffsetIsValid();
  // TODO(yosin): We should return |Position::BeforeAnchor| when
  // |container_node_| isn't a |Text| node.
  return Position(container_node_.Get(), offset_in_container_);
}

inline unsigned RangeBoundaryPoint::Offset() const {
  EnsureOffsetIsValid();
  return offset_in_container_;
}

inline void RangeBoundaryPoint::Set(Node& container,
                                    unsigned offset,
                                    Node* child_before) {
  DCHECK_GE(offset, 0u);
  DCHECK_EQ(child_before,
            offset ? NodeTraversal::ChildAt(container, offset - 1) : nullptr);
  container_node_ = container;
  offset_in_container_ = offset;
  child_before_boundary_ = child_before;
  MarkValid();
}

inline void RangeBoundaryPoint::SetOffset(unsigned offset) {
  DCHECK(container_node_);
  DCHECK(container_node_->IsCharacterDataNode());
  DCHECK_GE(offset_in_container_, 0u);
  DCHECK(!child_before_boundary_);
  offset_in_container_ = offset;
  MarkValid();
}

inline void RangeBoundaryPoint::SetToBeforeChild(Node& child) {
  DCHECK(child.parentNode());
  child_before_boundary_ = child.previousSibling();
  container_node_ = child.parentNode();
  offset_in_container_ = child_before_boundary_ ? kInvalidOffset : 0;
  MarkValid();
}

inline void RangeBoundaryPoint::SetToStartOfNode(Node& container) {
  container_node_ = &container;
  offset_in_container_ = 0;
  child_before_boundary_ = nullptr;
  MarkValid();
}

inline void RangeBoundaryPoint::SetToEndOfNode(Node& container) {
  container_node_ = &container;
  if (auto* character_data = DynamicTo<CharacterData>(container_node_.Get())) {
    offset_in_container_ = character_data->length();
    child_before_boundary_ = nullptr;
  } else {
    child_before_boundary_ = container_node_->lastChild();
    offset_in_container_ = child_before_boundary_ ? kInvalidOffset : 0;
  }
  MarkValid();
}

inline void RangeBoundaryPoint::ChildBeforeWillBeRemoved() {
  child_before_boundary_ = child_before_boundary_->previousSibling();
  if (!IsOffsetValid())
    return;
  DCHECK_GT(offset_in_container_, 0u);
  if (!child_before_boundary_)
    offset_in_container_ = 0;
  else if (offset_in_container_ > 0)
    --offset_in_container_;
  MarkValid();
}

inline void RangeBoundaryPoint::InvalidateOffset() {
  offset_in_container_ = kInvalidOffset;
}

inline void RangeBoundaryPoint::MarkValid() const {
  dom_tree_version_ = DomTreeVersion();
}

inline bool operator==(const RangeBoundaryPoint& a,
                       const RangeBoundaryPoint& b) {
  if (a.Container() != b.Container())
    return false;
  if (a.ChildBefore() || b.ChildBefore()) {
    if (a.ChildBefore() != b.ChildBefore())
      return false;
  } else {
    if (a.Offset() != b.Offset())
      return false;
  }
  return true;
}

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_DOM_RANGE_BOUNDARY_POINT_H_