2011-02-08 04:48:41 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-02-08 04:48:41 +00:00
|
|
|
|
|
|
|
#include "TextUpdater.h"
|
|
|
|
|
2012-04-13 14:17:03 +00:00
|
|
|
#include "Accessible-inl.h"
|
2012-07-28 04:21:40 +00:00
|
|
|
#include "DocAccessible-inl.h"
|
2012-05-23 18:05:57 +00:00
|
|
|
#include "TextLeafAccessible.h"
|
2013-01-15 12:22:03 +00:00
|
|
|
#include <algorithm>
|
2012-05-23 18:05:57 +00:00
|
|
|
|
|
|
|
using namespace mozilla::a11y;
|
2011-02-08 04:48:41 +00:00
|
|
|
|
2012-05-27 09:01:40 +00:00
|
|
|
void TextUpdater::Run(DocAccessible* aDocument, TextLeafAccessible* aTextLeaf,
|
2011-02-08 04:48:41 +00:00
|
|
|
const nsAString& aNewText) {
|
|
|
|
NS_ASSERTION(aTextLeaf, "No text leaf accessible?");
|
|
|
|
|
|
|
|
const nsString& oldText = aTextLeaf->Text();
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t oldLen = oldText.Length(), newLen = aNewText.Length();
|
2013-01-15 12:22:03 +00:00
|
|
|
uint32_t minLen = std::min(oldLen, newLen);
|
2011-02-08 04:48:41 +00:00
|
|
|
|
|
|
|
// Skip coinciding begin substrings.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t skipStart = 0;
|
2011-02-08 04:48:41 +00:00
|
|
|
for (; skipStart < minLen; skipStart++) {
|
|
|
|
if (aNewText[skipStart] != oldText[skipStart]) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The text was changed. Do update.
|
|
|
|
if (skipStart != minLen || oldLen != newLen) {
|
|
|
|
TextUpdater updater(aDocument, aTextLeaf);
|
|
|
|
updater.DoUpdate(aNewText, oldText, skipStart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextUpdater::DoUpdate(const nsAString& aNewText, const nsAString& aOldText,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aSkipStart) {
|
2021-02-16 20:05:10 +00:00
|
|
|
Accessible* parent = mTextLeaf->LocalParent();
|
2011-07-23 08:38:33 +00:00
|
|
|
if (!parent) return;
|
2011-02-08 04:48:41 +00:00
|
|
|
|
|
|
|
mHyperText = parent->AsHyperText();
|
|
|
|
if (!mHyperText) {
|
2018-06-18 22:19:09 +00:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Text leaf parent is not hypertext!");
|
2011-02-08 04:48:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the text leaf accessible offset and invalidate cached offsets after it.
|
2011-10-17 14:59:28 +00:00
|
|
|
mTextOffset = mHyperText->GetChildOffset(mTextLeaf, true);
|
2011-02-08 04:48:41 +00:00
|
|
|
NS_ASSERTION(mTextOffset != -1, "Text leaf hasn't offset within hyper text!");
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t oldLen = aOldText.Length(), newLen = aNewText.Length();
|
2013-01-15 12:22:03 +00:00
|
|
|
uint32_t minLen = std::min(oldLen, newLen);
|
2011-02-08 04:48:41 +00:00
|
|
|
|
|
|
|
// Trim coinciding substrings from the end.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t skipEnd = 0;
|
2011-02-08 04:48:41 +00:00
|
|
|
while (minLen - skipEnd > aSkipStart &&
|
|
|
|
aNewText[newLen - skipEnd - 1] == aOldText[oldLen - skipEnd - 1]) {
|
|
|
|
skipEnd++;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t strLen1 = oldLen - aSkipStart - skipEnd;
|
|
|
|
uint32_t strLen2 = newLen - aSkipStart - skipEnd;
|
2011-02-08 04:48:41 +00:00
|
|
|
|
|
|
|
const nsAString& str1 = Substring(aOldText, aSkipStart, strLen1);
|
|
|
|
const nsAString& str2 = Substring(aNewText, aSkipStart, strLen2);
|
|
|
|
|
|
|
|
// Increase offset of the text leaf on skipped characters amount.
|
|
|
|
mTextOffset += aSkipStart;
|
|
|
|
|
2011-08-04 03:57:08 +00:00
|
|
|
// It could be single insertion or removal or the case of long strings. Do not
|
|
|
|
// calculate the difference between long strings and prefer to fire pair of
|
|
|
|
// insert/remove events as the old string was replaced on the new one.
|
|
|
|
if (strLen1 == 0 || strLen2 == 0 || strLen1 > kMaxStrLen ||
|
|
|
|
strLen2 > kMaxStrLen) {
|
|
|
|
if (strLen1 > 0) {
|
|
|
|
// Fire text change event for removal.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<AccEvent> textRemoveEvent =
|
2011-10-17 14:59:28 +00:00
|
|
|
new AccTextChangeEvent(mHyperText, mTextOffset, str1, false);
|
2012-11-20 04:53:38 +00:00
|
|
|
mDocument->FireDelayedEvent(textRemoveEvent);
|
2011-08-04 03:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strLen2 > 0) {
|
|
|
|
// Fire text change event for insertion.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<AccEvent> textInsertEvent =
|
2011-10-17 14:59:28 +00:00
|
|
|
new AccTextChangeEvent(mHyperText, mTextOffset, str2, true);
|
2012-11-20 04:53:38 +00:00
|
|
|
mDocument->FireDelayedEvent(textInsertEvent);
|
2011-08-04 03:57:08 +00:00
|
|
|
}
|
|
|
|
|
2011-12-01 16:12:02 +00:00
|
|
|
mDocument->MaybeNotifyOfValueChange(mHyperText);
|
2011-08-04 03:57:08 +00:00
|
|
|
|
|
|
|
// Update the text.
|
|
|
|
mTextLeaf->SetText(aNewText);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise find the difference between strings and fire events.
|
|
|
|
// Note: we can skip initial and final coinciding characters since they don't
|
|
|
|
// affect the Levenshtein distance.
|
|
|
|
|
2011-02-08 04:48:41 +00:00
|
|
|
// Compute the flat structured matrix need to compute the difference.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len1 = strLen1 + 1, len2 = strLen2 + 1;
|
|
|
|
uint32_t* entries = new uint32_t[len1 * len2];
|
2011-02-08 04:48:41 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t colIdx = 0; colIdx < len1; colIdx++) entries[colIdx] = colIdx;
|
2011-02-08 04:48:41 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t* row = entries;
|
|
|
|
for (uint32_t rowIdx = 1; rowIdx < len2; rowIdx++) {
|
|
|
|
uint32_t* prevRow = row;
|
2011-02-08 04:48:41 +00:00
|
|
|
row += len1;
|
|
|
|
row[0] = rowIdx;
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t colIdx = 1; colIdx < len1; colIdx++) {
|
2011-02-08 04:48:41 +00:00
|
|
|
if (str1[colIdx - 1] != str2[rowIdx - 1]) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t left = row[colIdx - 1];
|
|
|
|
uint32_t up = prevRow[colIdx];
|
|
|
|
uint32_t upleft = prevRow[colIdx - 1];
|
2013-01-15 12:22:03 +00:00
|
|
|
row[colIdx] = std::min(upleft, std::min(left, up)) + 1;
|
2011-02-08 04:48:41 +00:00
|
|
|
} else {
|
|
|
|
row[colIdx] = prevRow[colIdx - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute events based on the difference.
|
2015-10-18 05:24:48 +00:00
|
|
|
nsTArray<RefPtr<AccEvent> > events;
|
2011-02-08 04:48:41 +00:00
|
|
|
ComputeTextChangeEvents(str1, str2, entries, events);
|
|
|
|
|
|
|
|
delete[] entries;
|
|
|
|
|
|
|
|
// Fire events.
|
2012-08-22 15:56:38 +00:00
|
|
|
for (int32_t idx = events.Length() - 1; idx >= 0; idx--)
|
2012-11-20 04:53:38 +00:00
|
|
|
mDocument->FireDelayedEvent(events[idx]);
|
2011-02-08 04:48:41 +00:00
|
|
|
|
2011-12-01 16:12:02 +00:00
|
|
|
mDocument->MaybeNotifyOfValueChange(mHyperText);
|
2011-02-08 04:48:41 +00:00
|
|
|
|
|
|
|
// Update the text.
|
|
|
|
mTextLeaf->SetText(aNewText);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextUpdater::ComputeTextChangeEvents(
|
|
|
|
const nsAString& aStr1, const nsAString& aStr2, uint32_t* aEntries,
|
2015-10-18 05:24:48 +00:00
|
|
|
nsTArray<RefPtr<AccEvent> >& aEvents) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t colIdx = aStr1.Length(), rowIdx = aStr2.Length();
|
2011-02-08 04:48:41 +00:00
|
|
|
|
|
|
|
// Point at which strings last matched.
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t colEnd = colIdx;
|
|
|
|
int32_t rowEnd = rowIdx;
|
2011-02-08 04:48:41 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t colLen = colEnd + 1;
|
|
|
|
uint32_t* row = aEntries + rowIdx * colLen;
|
|
|
|
uint32_t dist = row[colIdx]; // current Levenshtein distance
|
2011-02-08 04:48:41 +00:00
|
|
|
while (rowIdx && colIdx) { // stop when we can't move diagonally
|
|
|
|
if (aStr1[colIdx - 1] == aStr2[rowIdx - 1]) { // match
|
|
|
|
if (rowIdx < rowEnd) { // deal with any pending insertion
|
|
|
|
FireInsertEvent(Substring(aStr2, rowIdx, rowEnd - rowIdx), rowIdx,
|
|
|
|
aEvents);
|
|
|
|
}
|
|
|
|
if (colIdx < colEnd) { // deal with any pending deletion
|
|
|
|
FireDeleteEvent(Substring(aStr1, colIdx, colEnd - colIdx), rowIdx,
|
|
|
|
aEvents);
|
|
|
|
}
|
|
|
|
|
|
|
|
colEnd = --colIdx; // reset the match point
|
|
|
|
rowEnd = --rowIdx;
|
|
|
|
row -= colLen;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
--dist;
|
|
|
|
if (dist == row[colIdx - 1 - colLen]) { // substitution
|
|
|
|
--colIdx;
|
|
|
|
--rowIdx;
|
|
|
|
row -= colLen;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (dist == row[colIdx - colLen]) { // insertion
|
|
|
|
--rowIdx;
|
|
|
|
row -= colLen;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (dist == row[colIdx - 1]) { // deletion
|
|
|
|
--colIdx;
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-18 05:43:11 +00:00
|
|
|
MOZ_ASSERT_UNREACHABLE("huh?");
|
2011-02-08 04:48:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rowEnd) FireInsertEvent(Substring(aStr2, 0, rowEnd), 0, aEvents);
|
|
|
|
if (colEnd) FireDeleteEvent(Substring(aStr1, 0, colEnd), 0, aEvents);
|
|
|
|
}
|