File: InsertIncrementalTextCommand.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (183 lines) | stat: -rw-r--r-- 7,360 bytes parent folder | download
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
// Copyright (c) 2016 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.

#include "core/editing/commands/InsertIncrementalTextCommand.h"

#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/dom/Text.h"
#include "core/editing/EditingUtilities.h"
#include "core/editing/Editor.h"
#include "core/editing/PlainTextRange.h"
#include "core/editing/VisibleUnits.h"
#include "core/editing/iterators/CharacterIterator.h"
#include "core/html/HTMLSpanElement.h"

namespace blink {

namespace {

size_t computeCommonPrefixLength(const String& str1, const String& str2) {
  const size_t maxCommonPrefixLength = std::min(str1.length(), str2.length());
  for (size_t index = 0; index < maxCommonPrefixLength; ++index) {
    if (str1[index] != str2[index])
      return index;
  }
  return maxCommonPrefixLength;
}

size_t computeCommonSuffixLength(const String& str1, const String& str2) {
  const size_t length1 = str1.length();
  const size_t length2 = str2.length();
  const size_t maxCommonSuffixLength = std::min(length1, length2);
  for (size_t index = 0; index < maxCommonSuffixLength; ++index) {
    if (str1[length1 - index - 1] != str2[length2 - index - 1])
      return index;
  }
  return maxCommonSuffixLength;
}

// If current position is at grapheme boundary, return 0; otherwise, return the
// distance to its nearest left grapheme boundary.
size_t computeDistanceToLeftGraphemeBoundary(const Position& position) {
  const Position& adjustedPosition = previousPositionOf(
      nextPositionOf(position, PositionMoveType::GraphemeCluster),
      PositionMoveType::GraphemeCluster);
  DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode());
  DCHECK_GE(position.computeOffsetInContainerNode(),
            adjustedPosition.computeOffsetInContainerNode());
  return static_cast<size_t>(position.computeOffsetInContainerNode() -
                             adjustedPosition.computeOffsetInContainerNode());
}

size_t computeCommonGraphemeClusterPrefixLength(
    const int selectionStart,
    const String& oldText,
    const String& newText,
    const Element* rootEditableElement) {
  const size_t commonPrefixLength = computeCommonPrefixLength(oldText, newText);

  // For grapheme cluster, we should adjust it for grapheme boundary.
  const EphemeralRange& range =
      PlainTextRange(0, selectionStart + commonPrefixLength)
          .createRange(*rootEditableElement);
  if (range.isNull())
    return 0;
  const Position& position = range.endPosition();
  const size_t diff = computeDistanceToLeftGraphemeBoundary(position);
  DCHECK_GE(commonPrefixLength, diff);
  return commonPrefixLength - diff;
}

// If current position is at grapheme boundary, return 0; otherwise, return the
// distance to its nearest right grapheme boundary.
size_t computeDistanceToRightGraphemeBoundary(const Position& position) {
  const Position& adjustedPosition = nextPositionOf(
      previousPositionOf(position, PositionMoveType::GraphemeCluster),
      PositionMoveType::GraphemeCluster);
  DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode());
  DCHECK_GE(adjustedPosition.computeOffsetInContainerNode(),
            position.computeOffsetInContainerNode());
  return static_cast<size_t>(adjustedPosition.computeOffsetInContainerNode() -
                             position.computeOffsetInContainerNode());
}

size_t computeCommonGraphemeClusterSuffixLength(
    const int selectionStart,
    const String& oldText,
    const String& newText,
    const Element* rootEditableElement) {
  const size_t commonSuffixLength = computeCommonSuffixLength(oldText, newText);

  // For grapheme cluster, we should adjust it for grapheme boundary.
  const EphemeralRange& range =
      PlainTextRange(0, selectionStart + oldText.length() - commonSuffixLength)
          .createRange(*rootEditableElement);
  if (range.isNull())
    return 0;
  const Position& position = range.endPosition();
  const size_t diff = computeDistanceToRightGraphemeBoundary(position);
  DCHECK_GE(commonSuffixLength, diff);
  return commonSuffixLength - diff;
}

const String computeTextForInsertion(const String& newText,
                                     const size_t commonPrefixLength,
                                     const size_t commonSuffixLength) {
  return newText.substring(
      commonPrefixLength,
      newText.length() - commonPrefixLength - commonSuffixLength);
}

VisibleSelection computeSelectionForInsertion(
    const EphemeralRange& selectionRange,
    const int offset,
    const int length,
    const bool isDirectional) {
  CharacterIterator charIt(selectionRange);
  const EphemeralRange& rangeForInsertion =
      charIt.calculateCharacterSubrange(offset, length);
  const VisibleSelection& selection =
      createVisibleSelection(SelectionInDOMTree::Builder()
                                 .setBaseAndExtent(rangeForInsertion)
                                 .setIsDirectional(isDirectional)
                                 .build());
  return selection;
}

}  // anonymous namespace

InsertIncrementalTextCommand* InsertIncrementalTextCommand::create(
    Document& document,
    const String& text,
    bool selectInsertedText,
    RebalanceType rebalanceType) {
  return new InsertIncrementalTextCommand(document, text, selectInsertedText,
                                          rebalanceType);
}

InsertIncrementalTextCommand::InsertIncrementalTextCommand(
    Document& document,
    const String& text,
    bool selectInsertedText,
    RebalanceType rebalanceType)
    : InsertTextCommand(document, text, selectInsertedText, rebalanceType) {}

void InsertIncrementalTextCommand::doApply(EditingState* editingState) {
  const Element* element = endingSelection().rootEditableElement();
  DCHECK(element);

  const EphemeralRange selectionRange(endingSelection().start(),
                                      endingSelection().end());
  const String oldText = plainText(selectionRange);
  const String& newText = m_text;

  const int selectionStart =
      endingSelection().start().computeOffsetInContainerNode();
  const size_t newTextLength = newText.length();
  const size_t oldTextLength = oldText.length();
  const size_t commonPrefixLength = computeCommonGraphemeClusterPrefixLength(
      selectionStart, oldText, newText, element);
  // We should ignore common prefix when finding common suffix.
  const size_t commonSuffixLength = computeCommonGraphemeClusterSuffixLength(
      selectionStart, oldText.right(oldTextLength - commonPrefixLength),
      newText.right(newTextLength - commonPrefixLength), element);
  DCHECK_GE(oldTextLength, commonPrefixLength + commonSuffixLength);

  m_text =
      computeTextForInsertion(m_text, commonPrefixLength, commonSuffixLength);

  const int offset = static_cast<int>(commonPrefixLength);
  const int length =
      static_cast<int>(oldTextLength - commonPrefixLength - commonSuffixLength);
  const VisibleSelection& selectionForInsertion = computeSelectionForInsertion(
      selectionRange, offset, length, endingSelection().isDirectional());

  setEndingSelectionWithoutValidation(selectionForInsertion.start(),
                                      selectionForInsertion.end());

  InsertTextCommand::doApply(editingState);
}

}  // namespace blink