Bug 1358025 - Part 4. Disable undo when input.value setter isn't user interaction. r=masayuki

See bug 1346723.  I would like to turn off undo when not user interaction.  Except to Gecko, other browsers don't create undo history by input.value setter.

MozReview-Commit-ID: 9P1eOKTXCXN

--HG--
extra : rebase_source : 44967a19300827af6187c4f906e09ed09808cd30
This commit is contained in:
Makoto Kato 2017-05-18 16:32:29 +09:00
parent 5b36447061
commit 51d94b9c3f

View File

@ -173,6 +173,35 @@ private:
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
class MOZ_RAII AutoDisableUndo final
{
public:
explicit AutoDisableUndo(nsIEditor* aEditor
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: mEditor(aEditor)
, mPreviousEnabled(true)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
MOZ_ASSERT(mEditor);
bool canUndo;
DebugOnly<nsresult> rv = mEditor->CanUndo(&mPreviousEnabled, &canUndo);
MOZ_ASSERT(NS_SUCCEEDED(rv));
mEditor->EnableUndo(false);
}
~AutoDisableUndo()
{
mEditor->EnableUndo(mPreviousEnabled);
}
private:
nsIEditor* mEditor;
bool mPreviousEnabled;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
/*static*/
bool
nsITextControlElement::GetWrapPropertyEnum(nsIContent* aContent,
@ -1560,16 +1589,11 @@ nsTextEditorState::PrepareEditor(const nsAString *aValue)
// the default value. Make sure to turn off undo before setting the default
// value, and turn it back on afterwards. This will make sure we can't undo
// past the default value.
rv = newEditor->EnableUndo(false);
NS_ENSURE_SUCCESS(rv, rv);
// So, we use eSetValue_Internal flag only that it will turn off undo.
bool success = SetValue(defaultValue, eSetValue_Internal);
NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
rv = newEditor->EnableUndo(true);
NS_ASSERTION(NS_SUCCEEDED(rv),"Transaction Manager must have failed");
// Now restore the original editor flags.
rv = newEditor->SetFlags(editorFlags);
NS_ENSURE_SUCCESS(rv, rv);
@ -2642,6 +2666,8 @@ nsTextEditorState::SetValue(const nsAString& aValue, uint32_t aFlags)
plaintextEditor->InsertText(insertValue);
}
} else {
AutoDisableUndo disableUndo(mEditor);
plaintextEditor->SetText(newValue);
}