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