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
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_SELECTION_TEMPLATE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_SELECTION_TEMPLATE_H_
#include <iosfwd>
#include "base/dcheck_is_on.h"
#include "base/memory/stack_allocated.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/editing/forward.h"
#include "third_party/blink/renderer/core/editing/position.h"
#include "third_party/blink/renderer/core/editing/text_affinity.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
namespace blink {
// SelectionTemplate is used for representing a selection in DOM tree or Flat
// tree with template parameter |Strategy|. Instances of |SelectionTemplate|
// are "virtually" immutable objects, we change |SelectionTemplate| by copying
// in |SelectionEdtior| and |InvalidSelectionResetter|.
//
// To construct |SelectionTemplate| object, please use |Builder| class.
template <typename Strategy>
class SelectionTemplate final {
DISALLOW_NEW();
public:
// |Builder| is a helper class for constructing |SelectionTemplate| object.
class CORE_EXPORT Builder final {
STACK_ALLOCATED();
public:
explicit Builder(const SelectionTemplate&);
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder();
SelectionTemplate Build() const;
// Move selection to |anchor|. |anchor| can't be null.
Builder& Collapse(const PositionTemplate<Strategy>& anchor);
Builder& Collapse(const PositionWithAffinityTemplate<Strategy>& anchor);
// Extend selection to |focus|. It is error if selection is none.
// |focus| can be in different tree scope of anchor, but should be in same
// document.
Builder& Extend(const PositionTemplate<Strategy>& focus);
// Select all children in |node|.
Builder& SelectAllChildren(const Node& /* node */);
// Note: Since collapsed selection is a forward selection, we can't use
// |SetAsBackwardSelection()| for collapsed range.
Builder& SetAsBackwardSelection(const EphemeralRangeTemplate<Strategy>&);
Builder& SetAsForwardSelection(const EphemeralRangeTemplate<Strategy>&);
Builder& SetBaseAndExtent(const EphemeralRangeTemplate<Strategy>&);
// |extent| can not be null if |base| isn't null.
Builder& SetBaseAndExtent(const PositionTemplate<Strategy>& base,
const PositionTemplate<Strategy>& extent);
// |extent| can be non-null while |base| is null, which is undesired.
// Note: This function should be used only in "core/editing/commands".
// TODO(yosin): Once all we get rid of all call sites, we remove this.
Builder& SetBaseAndExtentDeprecated(
const PositionTemplate<Strategy>& base,
const PositionTemplate<Strategy>& extent);
Builder& SetAffinity(TextAffinity);
private:
SelectionTemplate selection_;
};
// Resets selection at end of life time of the object when anchor and focus
// are disconnected or moved to another document.
class InvalidSelectionResetter final {
STACK_ALLOCATED();
public:
explicit InvalidSelectionResetter(const SelectionTemplate&);
InvalidSelectionResetter(const InvalidSelectionResetter&) = delete;
InvalidSelectionResetter& operator=(const InvalidSelectionResetter&) =
delete;
~InvalidSelectionResetter();
private:
const Document* const document_;
SelectionTemplate& selection_;
};
SelectionTemplate(const SelectionTemplate& other);
SelectionTemplate();
SelectionTemplate& operator=(const SelectionTemplate&) = default;
bool operator==(const SelectionTemplate&) const;
bool operator!=(const SelectionTemplate&) const;
const PositionTemplate<Strategy>& Anchor() const;
const PositionTemplate<Strategy>& Focus() const;
TextAffinity Affinity() const { return affinity_; }
bool IsAnchorFirst() const;
bool IsCaret() const;
bool IsNone() const { return anchor_.IsNull(); }
bool IsRange() const;
// Returns true if |this| selection holds valid values otherwise it causes
// assertion failure.
bool AssertValid() const;
bool AssertValidFor(const Document&) const;
const PositionTemplate<Strategy>& ComputeEndPosition() const;
const PositionTemplate<Strategy>& ComputeStartPosition() const;
EphemeralRangeTemplate<Strategy> ComputeRange() const;
void Trace(Visitor*) const;
void PrintTo(std::ostream*, const char* type) const;
#if DCHECK_IS_ON()
void ShowTreeForThis() const;
#endif
private:
friend class SelectionEditor;
friend class FrameSelection;
enum class Direction {
kNotComputed,
kForward, // anchor <= focus
kBackward, // anchor > focus
};
Document* GetDocument() const;
bool IsValidFor(const Document&) const;
void ResetDirectionCache() const;
PositionTemplate<Strategy> anchor_;
PositionTemplate<Strategy> focus_;
TextAffinity affinity_ = TextAffinity::kDownstream;
mutable Direction direction_ = Direction::kForward;
#if DCHECK_IS_ON()
uint64_t dom_tree_version_;
#endif
};
extern template class CORE_EXTERN_TEMPLATE_EXPORT
SelectionTemplate<EditingStrategy>;
extern template class CORE_EXTERN_TEMPLATE_EXPORT
SelectionTemplate<EditingInFlatTreeStrategy>;
using SelectionInDOMTree = SelectionTemplate<EditingStrategy>;
using SelectionInFlatTree = SelectionTemplate<EditingInFlatTreeStrategy>;
CORE_EXPORT SelectionInDOMTree
ConvertToSelectionInDOMTree(const SelectionInFlatTree&);
CORE_EXPORT SelectionInFlatTree
ConvertToSelectionInFlatTree(const SelectionInDOMTree&);
CORE_EXPORT std::ostream& operator<<(std::ostream&, const SelectionInDOMTree&);
CORE_EXPORT std::ostream& operator<<(std::ostream&, const SelectionInFlatTree&);
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_SELECTION_TEMPLATE_H_
|