Bug 1364814 - Use RAII class to set and restore editor flags. r=masayuki

input.value setter removes editor flags and max-length to set value.  To clean up code, I would like to use RAII class to set and restore it.

Actually, when the frame is being destroyed by InsertText, it isn't restored.  But it might be safe since the flags is set again on nsTextEditorState::PrepareEditor by focus.


MozReview-Commit-ID: J0OYYluWD8z

--HG--
extra : rebase_source : b0489a381cdea76fe98f328cbd88cd18d2576a93
This commit is contained in:
Makoto Kato 2017-05-15 13:22:25 +09:00
parent b32a939c73
commit ae158ad730

View File

@ -133,6 +133,45 @@ private:
nsTextEditorState* mTextEditorState;
};
class MOZ_RAII AutoRestoreEditorState final
{
public:
explicit AutoRestoreEditorState(nsIEditor* aEditor,
nsIPlaintextEditor* aPlaintextEditor
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: mEditor(aEditor)
, mPlaintextEditor(aPlaintextEditor)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
MOZ_ASSERT(mEditor);
MOZ_ASSERT(mPlaintextEditor);
uint32_t flags;
mEditor->GetFlags(&mSavedFlags);
flags = mSavedFlags;
flags &= ~(nsIPlaintextEditor::eEditorDisabledMask);
flags &= ~(nsIPlaintextEditor::eEditorReadonlyMask);
flags |= nsIPlaintextEditor::eEditorDontEchoPassword;
mEditor->SetFlags(flags);
mPlaintextEditor->GetMaxTextLength(&mSavedMaxLength);
mPlaintextEditor->SetMaxTextLength(-1);
}
~AutoRestoreEditorState()
{
mPlaintextEditor->SetMaxTextLength(mSavedMaxLength);
mEditor->SetFlags(mSavedFlags);
}
private:
nsIEditor* mEditor;
nsIPlaintextEditor* mPlaintextEditor;
uint32_t mSavedFlags;
int32_t mSavedMaxLength;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
/*static*/
bool
nsITextControlElement::GetWrapPropertyEnum(nsIContent* aContent,
@ -2551,8 +2590,9 @@ nsTextEditorState::SetValue(const nsAString& aValue, uint32_t aFlags)
if (domSel)
{
selPriv = do_QueryInterface(domSel);
if (selPriv)
if (selPriv) {
selPriv->StartBatchChanges();
}
}
nsCOMPtr<nsISelectionController> kungFuDeathGrip = mSelCon.get();
@ -2577,34 +2617,25 @@ nsTextEditorState::SetValue(const nsAString& aValue, uint32_t aFlags)
valueSetter.Init();
// get the flags, remove readonly and disabled, set the value,
// restore flags
uint32_t flags, savedFlags;
mEditor->GetFlags(&savedFlags);
flags = savedFlags;
flags &= ~(nsIPlaintextEditor::eEditorDisabledMask);
flags &= ~(nsIPlaintextEditor::eEditorReadonlyMask);
flags |= nsIPlaintextEditor::eEditorDontEchoPassword;
mEditor->SetFlags(flags);
// get the flags, remove readonly, disabled and max-length,
// set the value, restore flags
{
AutoRestoreEditorState restoreState(mEditor, plaintextEditor);
mTextListener->SettingValue(true);
bool notifyValueChanged = !!(aFlags & eSetValue_Notify);
mTextListener->SetValueChanged(notifyValueChanged);
mTextListener->SettingValue(true);
bool notifyValueChanged = !!(aFlags & eSetValue_Notify);
mTextListener->SetValueChanged(notifyValueChanged);
// Also don't enforce max-length here
int32_t savedMaxLength;
plaintextEditor->GetMaxTextLength(&savedMaxLength);
plaintextEditor->SetMaxTextLength(-1);
if (insertValue.IsEmpty()) {
mEditor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip);
} else {
plaintextEditor->InsertText(insertValue);
}
if (insertValue.IsEmpty()) {
mEditor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip);
} else {
plaintextEditor->InsertText(insertValue);
mTextListener->SetValueChanged(true);
mTextListener->SettingValue(false);
}
mTextListener->SetValueChanged(true);
mTextListener->SettingValue(false);
if (!weakFrame.IsAlive()) {
// If the frame was destroyed because of a flush somewhere inside
// InsertText, mBoundFrame here will be false. But it's also possible
@ -2623,10 +2654,9 @@ nsTextEditorState::SetValue(const nsAString& aValue, uint32_t aFlags)
}
}
plaintextEditor->SetMaxTextLength(savedMaxLength);
mEditor->SetFlags(savedFlags);
if (selPriv)
if (selPriv) {
selPriv->EndBatchChanges();
}
}
}
} else {