|
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/. */ |
|
5 |
|
6 #include "TextUpdater.h" |
|
7 |
|
8 #include "Accessible-inl.h" |
|
9 #include "DocAccessible-inl.h" |
|
10 #include "TextLeafAccessible.h" |
|
11 #include <algorithm> |
|
12 |
|
13 using namespace mozilla::a11y; |
|
14 |
|
15 void |
|
16 TextUpdater::Run(DocAccessible* aDocument, TextLeafAccessible* aTextLeaf, |
|
17 const nsAString& aNewText) |
|
18 { |
|
19 NS_ASSERTION(aTextLeaf, "No text leaf accessible?"); |
|
20 |
|
21 const nsString& oldText = aTextLeaf->Text(); |
|
22 uint32_t oldLen = oldText.Length(), newLen = aNewText.Length(); |
|
23 uint32_t minLen = std::min(oldLen, newLen); |
|
24 |
|
25 // Skip coinciding begin substrings. |
|
26 uint32_t skipStart = 0; |
|
27 for (; skipStart < minLen; skipStart++) { |
|
28 if (aNewText[skipStart] != oldText[skipStart]) |
|
29 break; |
|
30 } |
|
31 |
|
32 // The text was changed. Do update. |
|
33 if (skipStart != minLen || oldLen != newLen) { |
|
34 TextUpdater updater(aDocument, aTextLeaf); |
|
35 updater.DoUpdate(aNewText, oldText, skipStart); |
|
36 } |
|
37 } |
|
38 |
|
39 void |
|
40 TextUpdater::DoUpdate(const nsAString& aNewText, const nsAString& aOldText, |
|
41 uint32_t aSkipStart) |
|
42 { |
|
43 Accessible* parent = mTextLeaf->Parent(); |
|
44 if (!parent) |
|
45 return; |
|
46 |
|
47 mHyperText = parent->AsHyperText(); |
|
48 if (!mHyperText) { |
|
49 NS_ERROR("Text leaf parent is not hypertext!"); |
|
50 return; |
|
51 } |
|
52 |
|
53 // Get the text leaf accessible offset and invalidate cached offsets after it. |
|
54 mTextOffset = mHyperText->GetChildOffset(mTextLeaf, true); |
|
55 NS_ASSERTION(mTextOffset != -1, |
|
56 "Text leaf hasn't offset within hyper text!"); |
|
57 |
|
58 uint32_t oldLen = aOldText.Length(), newLen = aNewText.Length(); |
|
59 uint32_t minLen = std::min(oldLen, newLen); |
|
60 |
|
61 // Trim coinciding substrings from the end. |
|
62 uint32_t skipEnd = 0; |
|
63 while (minLen - skipEnd > aSkipStart && |
|
64 aNewText[newLen - skipEnd - 1] == aOldText[oldLen - skipEnd - 1]) { |
|
65 skipEnd++; |
|
66 } |
|
67 |
|
68 uint32_t strLen1 = oldLen - aSkipStart - skipEnd; |
|
69 uint32_t strLen2 = newLen - aSkipStart - skipEnd; |
|
70 |
|
71 const nsAString& str1 = Substring(aOldText, aSkipStart, strLen1); |
|
72 const nsAString& str2 = Substring(aNewText, aSkipStart, strLen2); |
|
73 |
|
74 // Increase offset of the text leaf on skipped characters amount. |
|
75 mTextOffset += aSkipStart; |
|
76 |
|
77 // It could be single insertion or removal or the case of long strings. Do not |
|
78 // calculate the difference between long strings and prefer to fire pair of |
|
79 // insert/remove events as the old string was replaced on the new one. |
|
80 if (strLen1 == 0 || strLen2 == 0 || |
|
81 strLen1 > kMaxStrLen || strLen2 > kMaxStrLen) { |
|
82 if (strLen1 > 0) { |
|
83 // Fire text change event for removal. |
|
84 nsRefPtr<AccEvent> textRemoveEvent = |
|
85 new AccTextChangeEvent(mHyperText, mTextOffset, str1, false); |
|
86 mDocument->FireDelayedEvent(textRemoveEvent); |
|
87 } |
|
88 |
|
89 if (strLen2 > 0) { |
|
90 // Fire text change event for insertion. |
|
91 nsRefPtr<AccEvent> textInsertEvent = |
|
92 new AccTextChangeEvent(mHyperText, mTextOffset, str2, true); |
|
93 mDocument->FireDelayedEvent(textInsertEvent); |
|
94 } |
|
95 |
|
96 mDocument->MaybeNotifyOfValueChange(mHyperText); |
|
97 |
|
98 // Update the text. |
|
99 mTextLeaf->SetText(aNewText); |
|
100 return; |
|
101 } |
|
102 |
|
103 // Otherwise find the difference between strings and fire events. |
|
104 // Note: we can skip initial and final coinciding characters since they don't |
|
105 // affect the Levenshtein distance. |
|
106 |
|
107 // Compute the flat structured matrix need to compute the difference. |
|
108 uint32_t len1 = strLen1 + 1, len2 = strLen2 + 1; |
|
109 uint32_t* entries = new uint32_t[len1 * len2]; |
|
110 |
|
111 for (uint32_t colIdx = 0; colIdx < len1; colIdx++) |
|
112 entries[colIdx] = colIdx; |
|
113 |
|
114 uint32_t* row = entries; |
|
115 for (uint32_t rowIdx = 1; rowIdx < len2; rowIdx++) { |
|
116 uint32_t* prevRow = row; |
|
117 row += len1; |
|
118 row[0] = rowIdx; |
|
119 for (uint32_t colIdx = 1; colIdx < len1; colIdx++) { |
|
120 if (str1[colIdx - 1] != str2[rowIdx - 1]) { |
|
121 uint32_t left = row[colIdx - 1]; |
|
122 uint32_t up = prevRow[colIdx]; |
|
123 uint32_t upleft = prevRow[colIdx - 1]; |
|
124 row[colIdx] = std::min(upleft, std::min(left, up)) + 1; |
|
125 } else { |
|
126 row[colIdx] = prevRow[colIdx - 1]; |
|
127 } |
|
128 } |
|
129 } |
|
130 |
|
131 // Compute events based on the difference. |
|
132 nsTArray<nsRefPtr<AccEvent> > events; |
|
133 ComputeTextChangeEvents(str1, str2, entries, events); |
|
134 |
|
135 delete [] entries; |
|
136 |
|
137 // Fire events. |
|
138 for (int32_t idx = events.Length() - 1; idx >= 0; idx--) |
|
139 mDocument->FireDelayedEvent(events[idx]); |
|
140 |
|
141 mDocument->MaybeNotifyOfValueChange(mHyperText); |
|
142 |
|
143 // Update the text. |
|
144 mTextLeaf->SetText(aNewText); |
|
145 } |
|
146 |
|
147 void |
|
148 TextUpdater::ComputeTextChangeEvents(const nsAString& aStr1, |
|
149 const nsAString& aStr2, |
|
150 uint32_t* aEntries, |
|
151 nsTArray<nsRefPtr<AccEvent> >& aEvents) |
|
152 { |
|
153 int32_t colIdx = aStr1.Length(), rowIdx = aStr2.Length(); |
|
154 |
|
155 // Point at which strings last matched. |
|
156 int32_t colEnd = colIdx; |
|
157 int32_t rowEnd = rowIdx; |
|
158 |
|
159 int32_t colLen = colEnd + 1; |
|
160 uint32_t* row = aEntries + rowIdx * colLen; |
|
161 uint32_t dist = row[colIdx]; // current Levenshtein distance |
|
162 while (rowIdx && colIdx) { // stop when we can't move diagonally |
|
163 if (aStr1[colIdx - 1] == aStr2[rowIdx - 1]) { // match |
|
164 if (rowIdx < rowEnd) { // deal with any pending insertion |
|
165 FireInsertEvent(Substring(aStr2, rowIdx, rowEnd - rowIdx), |
|
166 rowIdx, aEvents); |
|
167 } |
|
168 if (colIdx < colEnd) { // deal with any pending deletion |
|
169 FireDeleteEvent(Substring(aStr1, colIdx, colEnd - colIdx), |
|
170 rowIdx, aEvents); |
|
171 } |
|
172 |
|
173 colEnd = --colIdx; // reset the match point |
|
174 rowEnd = --rowIdx; |
|
175 row -= colLen; |
|
176 continue; |
|
177 } |
|
178 --dist; |
|
179 if (dist == row[colIdx - 1 - colLen]) { // substitution |
|
180 --colIdx; |
|
181 --rowIdx; |
|
182 row -= colLen; |
|
183 continue; |
|
184 } |
|
185 if (dist == row[colIdx - colLen]) { // insertion |
|
186 --rowIdx; |
|
187 row -= colLen; |
|
188 continue; |
|
189 } |
|
190 if (dist == row[colIdx - 1]) { // deletion |
|
191 --colIdx; |
|
192 continue; |
|
193 } |
|
194 NS_NOTREACHED("huh?"); |
|
195 return; |
|
196 } |
|
197 |
|
198 if (rowEnd) |
|
199 FireInsertEvent(Substring(aStr2, 0, rowEnd), 0, aEvents); |
|
200 if (colEnd) |
|
201 FireDeleteEvent(Substring(aStr1, 0, colEnd), 0, aEvents); |
|
202 } |