Bug 1358025 - Part 3. Use nsIEditor.setText when input.value setter isn't user interaction. r?masayuki
When not using eSetValue_BySetUserInput, we should use SetText transaction instead for fast path. For backward compatibility, when input.value setter is by user interaction, I keep original way. Because the original way doesn't replace all text when some string matches.
MozReview-Commit-ID: IDm7Y1NBmaK
--- a/dom/html/nsTextEditorState.cpp
+++ b/dom/html/nsTextEditorState.cpp
@@ -2590,30 +2590,16 @@ nsTextEditorState::SetValue(const nsAStr
if (domSel)
{
selPriv = do_QueryInterface(domSel);
if (selPriv) {
selPriv->StartBatchChanges();
}
}
- nsCOMPtr<nsISelectionController> kungFuDeathGrip = mSelCon.get();
- uint32_t currentLength = currentValue.Length();
- uint32_t newlength = newValue.Length();
- if (!currentLength ||
- !StringBeginsWith(newValue, currentValue)) {
- // Replace the whole text.
- currentLength = 0;
- kungFuDeathGrip->SelectAll();
- } else {
- // Collapse selection to the end so that we can append data.
- mBoundFrame->SelectAllOrCollapseToEndOfText(false);
- }
- const nsAString& insertValue =
- StringTail(newValue, newlength - currentLength);
nsCOMPtr<nsIPlaintextEditor> plaintextEditor = do_QueryInterface(mEditor);
if (!plaintextEditor || !weakFrame.IsAlive()) {
NS_WARNING("Somehow not a plaintext editor?");
return true;
}
valueSetter.Init();
@@ -2621,20 +2607,39 @@ nsTextEditorState::SetValue(const nsAStr
// set the value, restore flags
{
AutoRestoreEditorState restoreState(mEditor, plaintextEditor);
mTextListener->SettingValue(true);
bool notifyValueChanged = !!(aFlags & eSetValue_Notify);
mTextListener->SetValueChanged(notifyValueChanged);
- if (insertValue.IsEmpty()) {
- mEditor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip);
+ if (aFlags & eSetValue_BySetUserInput) {
+ nsCOMPtr<nsISelectionController> kungFuDeathGrip = mSelCon.get();
+ uint32_t currentLength = currentValue.Length();
+ uint32_t newlength = newValue.Length();
+ if (!currentLength ||
+ !StringBeginsWith(newValue, currentValue)) {
+ // Replace the whole text.
+ currentLength = 0;
+ kungFuDeathGrip->SelectAll();
+ } else {
+ // Collapse selection to the end so that we can append data.
+ mBoundFrame->SelectAllOrCollapseToEndOfText(false);
+ }
+ const nsAString& insertValue =
+ StringTail(newValue, newlength - currentLength);
+
+ if (insertValue.IsEmpty()) {
+ mEditor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip);
+ } else {
+ plaintextEditor->InsertText(insertValue);
+ }
} else {
- plaintextEditor->InsertText(insertValue);
+ plaintextEditor->SetText(newValue);
}
mTextListener->SetValueChanged(true);
mTextListener->SettingValue(false);
}
if (!weakFrame.IsAlive()) {
// If the frame was destroyed because of a flush somewhere inside