summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/editing/ime/cached_text_input_info.h
blob: 255b3f32e7c04ba655be751f3111074d73cb3da7 (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
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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_IME_CACHED_TEXT_INPUT_INFO_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_IME_CACHED_TEXT_INPUT_INFO_H_

#include "third_party/blink/renderer/core/editing/iterators/text_iterator_behavior.h"
#include "third_party/blink/renderer/core/editing/plain_text_range.h"
#include "third_party/blink/renderer/core/editing/position.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

class LayoutObject;

// |CachedTextInputInfo| holds plain text, and plain text range for
// composition and selection in |element_| until layout of |element_|
// changed. This class also provides faster version of |PlaintTextRange|.
class CORE_EXPORT CachedTextInputInfo final {
  DISALLOW_NEW();

 public:
  CachedTextInputInfo(const CachedTextInputInfo&) = delete;
  CachedTextInputInfo() = default;

  CachedTextInputInfo& operator=(const CachedTextInputInfo&) = delete;

  void EnsureCached(const ContainerNode& element) const;
  PlainTextRange GetComposition(const EphemeralRange& range) const;
  PlainTextRange GetPlainTextRange(const EphemeralRange& range) const;
  PlainTextRange GetSelection(const EphemeralRange& range) const;
  String GetText() const;
  bool IsValidFor(const ContainerNode& element) const;

  // For cache invalidation
  void DidChangeVisibility(const LayoutObject& layout_object);
  void DidLayoutSubtree(const LayoutObject& layout_object);
  void DidUpdateLayout(const LayoutObject& layout_object);
  void LayoutObjectWillBeDestroyed(const LayoutObject& layout_object);

  void Trace(Visitor*) const;

 private:
  class CachedPlainTextRange final {
    DISALLOW_NEW();

   public:
    CachedPlainTextRange(const CachedPlainTextRange&) = delete;
    CachedPlainTextRange() = default;

    CachedPlainTextRange& operator=(const CachedPlainTextRange&) = delete;

    void Clear();
    PlainTextRange Get() const;
    bool IsValidFor(const EphemeralRange& range) const;
    void Set(const EphemeralRange& range, const PlainTextRange& text_range);

    void Trace(Visitor*) const;

   private:
    // |start_| and |end_| can be disconnected from document.
    mutable Position start_;
    mutable Position end_;
    mutable unsigned start_offset_ = kNotFound;
    mutable unsigned end_offset_ = kNotFound;
  };

  static TextIteratorBehavior Behavior();
  void ClearIfNeeded(const LayoutObject& layout_object);
  PlainTextRange GetPlainTextRangeWithCache(
      const EphemeralRange& range,
      CachedPlainTextRange* text_range) const;
  unsigned RangeLength(const EphemeralRange& range) const;

  mutable Member<const ContainerNode> container_;
  mutable const LayoutObject* layout_object_ = nullptr;
  // Records offset of text node from start of |container_|.
  mutable HeapHashMap<Member<const Text>, unsigned> offset_map_;
  mutable String text_;
  mutable CachedPlainTextRange composition_;
  mutable CachedPlainTextRange selection_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_IME_CACHED_TEXT_INPUT_INFO_H_