Bug 1626988 [wpt PR 22658] - wake lock: Remove WakeLockPermissionDescriptor, use...
[gecko.git] / accessible / base / TextUpdater.cpp
blob0ebe53d01b98f43000f5a2401c852d0f4a73037a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "TextUpdater.h"
8 #include "Accessible-inl.h"
9 #include "DocAccessible-inl.h"
10 #include "TextLeafAccessible.h"
11 #include <algorithm>
13 using namespace mozilla::a11y;
15 void TextUpdater::Run(DocAccessible* aDocument, TextLeafAccessible* aTextLeaf,
16 const nsAString& aNewText) {
17 NS_ASSERTION(aTextLeaf, "No text leaf accessible?");
19 const nsString& oldText = aTextLeaf->Text();
20 uint32_t oldLen = oldText.Length(), newLen = aNewText.Length();
21 uint32_t minLen = std::min(oldLen, newLen);
23 // Skip coinciding begin substrings.
24 uint32_t skipStart = 0;
25 for (; skipStart < minLen; skipStart++) {
26 if (aNewText[skipStart] != oldText[skipStart]) break;
29 // The text was changed. Do update.
30 if (skipStart != minLen || oldLen != newLen) {
31 TextUpdater updater(aDocument, aTextLeaf);
32 updater.DoUpdate(aNewText, oldText, skipStart);
36 void TextUpdater::DoUpdate(const nsAString& aNewText, const nsAString& aOldText,
37 uint32_t aSkipStart) {
38 Accessible* parent = mTextLeaf->Parent();
39 if (!parent) return;
41 mHyperText = parent->AsHyperText();
42 if (!mHyperText) {
43 MOZ_ASSERT_UNREACHABLE("Text leaf parent is not hypertext!");
44 return;
47 // Get the text leaf accessible offset and invalidate cached offsets after it.
48 mTextOffset = mHyperText->GetChildOffset(mTextLeaf, true);
49 NS_ASSERTION(mTextOffset != -1, "Text leaf hasn't offset within hyper text!");
51 uint32_t oldLen = aOldText.Length(), newLen = aNewText.Length();
52 uint32_t minLen = std::min(oldLen, newLen);
54 // Trim coinciding substrings from the end.
55 uint32_t skipEnd = 0;
56 while (minLen - skipEnd > aSkipStart &&
57 aNewText[newLen - skipEnd - 1] == aOldText[oldLen - skipEnd - 1]) {
58 skipEnd++;
61 uint32_t strLen1 = oldLen - aSkipStart - skipEnd;
62 uint32_t strLen2 = newLen - aSkipStart - skipEnd;
64 const nsAString& str1 = Substring(aOldText, aSkipStart, strLen1);
65 const nsAString& str2 = Substring(aNewText, aSkipStart, strLen2);
67 // Increase offset of the text leaf on skipped characters amount.
68 mTextOffset += aSkipStart;
70 // It could be single insertion or removal or the case of long strings. Do not
71 // calculate the difference between long strings and prefer to fire pair of
72 // insert/remove events as the old string was replaced on the new one.
73 if (strLen1 == 0 || strLen2 == 0 || strLen1 > kMaxStrLen ||
74 strLen2 > kMaxStrLen) {
75 if (strLen1 > 0) {
76 // Fire text change event for removal.
77 RefPtr<AccEvent> textRemoveEvent =
78 new AccTextChangeEvent(mHyperText, mTextOffset, str1, false);
79 mDocument->FireDelayedEvent(textRemoveEvent);
82 if (strLen2 > 0) {
83 // Fire text change event for insertion.
84 RefPtr<AccEvent> textInsertEvent =
85 new AccTextChangeEvent(mHyperText, mTextOffset, str2, true);
86 mDocument->FireDelayedEvent(textInsertEvent);
89 mDocument->MaybeNotifyOfValueChange(mHyperText);
91 // Update the text.
92 mTextLeaf->SetText(aNewText);
93 return;
96 // Otherwise find the difference between strings and fire events.
97 // Note: we can skip initial and final coinciding characters since they don't
98 // affect the Levenshtein distance.
100 // Compute the flat structured matrix need to compute the difference.
101 uint32_t len1 = strLen1 + 1, len2 = strLen2 + 1;
102 uint32_t* entries = new uint32_t[len1 * len2];
104 for (uint32_t colIdx = 0; colIdx < len1; colIdx++) entries[colIdx] = colIdx;
106 uint32_t* row = entries;
107 for (uint32_t rowIdx = 1; rowIdx < len2; rowIdx++) {
108 uint32_t* prevRow = row;
109 row += len1;
110 row[0] = rowIdx;
111 for (uint32_t colIdx = 1; colIdx < len1; colIdx++) {
112 if (str1[colIdx - 1] != str2[rowIdx - 1]) {
113 uint32_t left = row[colIdx - 1];
114 uint32_t up = prevRow[colIdx];
115 uint32_t upleft = prevRow[colIdx - 1];
116 row[colIdx] = std::min(upleft, std::min(left, up)) + 1;
117 } else {
118 row[colIdx] = prevRow[colIdx - 1];
123 // Compute events based on the difference.
124 nsTArray<RefPtr<AccEvent> > events;
125 ComputeTextChangeEvents(str1, str2, entries, events);
127 delete[] entries;
129 // Fire events.
130 for (int32_t idx = events.Length() - 1; idx >= 0; idx--)
131 mDocument->FireDelayedEvent(events[idx]);
133 mDocument->MaybeNotifyOfValueChange(mHyperText);
135 // Update the text.
136 mTextLeaf->SetText(aNewText);
139 void TextUpdater::ComputeTextChangeEvents(
140 const nsAString& aStr1, const nsAString& aStr2, uint32_t* aEntries,
141 nsTArray<RefPtr<AccEvent> >& aEvents) {
142 int32_t colIdx = aStr1.Length(), rowIdx = aStr2.Length();
144 // Point at which strings last matched.
145 int32_t colEnd = colIdx;
146 int32_t rowEnd = rowIdx;
148 int32_t colLen = colEnd + 1;
149 uint32_t* row = aEntries + rowIdx * colLen;
150 uint32_t dist = row[colIdx]; // current Levenshtein distance
151 while (rowIdx && colIdx) { // stop when we can't move diagonally
152 if (aStr1[colIdx - 1] == aStr2[rowIdx - 1]) { // match
153 if (rowIdx < rowEnd) { // deal with any pending insertion
154 FireInsertEvent(Substring(aStr2, rowIdx, rowEnd - rowIdx), rowIdx,
155 aEvents);
157 if (colIdx < colEnd) { // deal with any pending deletion
158 FireDeleteEvent(Substring(aStr1, colIdx, colEnd - colIdx), rowIdx,
159 aEvents);
162 colEnd = --colIdx; // reset the match point
163 rowEnd = --rowIdx;
164 row -= colLen;
165 continue;
167 --dist;
168 if (dist == row[colIdx - 1 - colLen]) { // substitution
169 --colIdx;
170 --rowIdx;
171 row -= colLen;
172 continue;
174 if (dist == row[colIdx - colLen]) { // insertion
175 --rowIdx;
176 row -= colLen;
177 continue;
179 if (dist == row[colIdx - 1]) { // deletion
180 --colIdx;
181 continue;
183 MOZ_ASSERT_UNREACHABLE("huh?");
184 return;
187 if (rowEnd) FireInsertEvent(Substring(aStr2, 0, rowEnd), 0, aEvents);
188 if (colEnd) FireDeleteEvent(Substring(aStr1, 0, colEnd), 0, aEvents);