Bug 848895 part 2. Null-check the pointer to the editor that the edit rules hold. r=ehsan

This commit is contained in:
Boris Zbarsky 2013-03-22 23:47:58 -04:00
parent d1287439d0
commit 2b8ebb0a5d
2 changed files with 626 additions and 52 deletions

File diff suppressed because it is too large Load Diff

View File

@ -161,6 +161,7 @@ nsTextEditRules::BeforeEdit(EditAction action,
// get the selection and cache the position before editing
nsCOMPtr<nsISelection> selection;
NS_ENSURE_STATE(mEditor);
nsresult res = mEditor->GetSelection(getter_AddRefs(selection));
NS_ENSURE_SUCCESS(res, res);
@ -184,9 +185,11 @@ nsTextEditRules::AfterEdit(EditAction action,
if (!--mActionNesting)
{
nsCOMPtr<nsISelection>selection;
NS_ENSURE_STATE(mEditor);
res = mEditor->GetSelection(getter_AddRefs(selection));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_STATE(mEditor);
res = mEditor->HandleInlineSpellCheck(action, selection,
mCachedSelectionNode, mCachedSelectionOffset,
nullptr, 0, nullptr, 0);
@ -261,6 +264,7 @@ NS_IMETHODIMP
nsTextEditRules::DidDoAction(nsISelection *aSelection,
nsRulesInfo *aInfo, nsresult aResult)
{
NS_ENSURE_STATE(mEditor);
// don't let any txns in here move the selection around behind our back.
// Note that this won't prevent explicit selection setting from working.
nsAutoTxnsConserveSelection dontSpazMySelection(mEditor);
@ -323,6 +327,7 @@ nsTextEditRules::WillInsert(nsISelection *aSelection, bool *aCancel)
// check for the magic content node and delete it if it exists
if (mBogusNode)
{
NS_ENSURE_STATE(mEditor);
mEditor->DeleteNode(mBogusNode);
mBogusNode = nullptr;
}
@ -371,6 +376,7 @@ nsTextEditRules::WillInsertBreak(Selection* aSelection,
NS_ENSURE_SUCCESS(res, res);
if (!bCollapsed)
{
NS_ENSURE_STATE(mEditor);
res = mEditor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip);
NS_ENSURE_SUCCESS(res, res);
}
@ -406,6 +412,7 @@ nsTextEditRules::CollapseSelectionToTrailingBRIfNeeded(nsISelection* aSelection)
int32_t selOffset;
nsCOMPtr<nsIDOMNode> selNode;
nsresult res;
NS_ENSURE_STATE(mEditor);
res = mEditor->GetStartNodeAndOffset(aSelection, getter_AddRefs(selNode), &selOffset);
NS_ENSURE_SUCCESS(res, res);
@ -423,6 +430,7 @@ nsTextEditRules::CollapseSelectionToTrailingBRIfNeeded(nsISelection* aSelection)
int32_t parentOffset;
nsCOMPtr<nsIDOMNode> parentNode = nsEditor::GetNodeLocation(selNode, &parentOffset);
NS_ENSURE_STATE(mEditor);
nsCOMPtr<nsIDOMNode> root = do_QueryInterface(mEditor->GetRoot());
NS_ENSURE_TRUE(root, NS_ERROR_NULL_POINTER);
if (parentNode != root) return NS_OK;
@ -588,6 +596,7 @@ nsTextEditRules::WillInsertText(EditAction aAction,
// handle password field docs
if (IsPasswordEditor()) {
NS_ENSURE_STATE(mEditor);
nsContentUtils::GetSelectionInTextControl(aSelection, mEditor->GetRoot(),
start, end);
}
@ -598,6 +607,7 @@ nsTextEditRules::WillInsertText(EditAction aAction,
NS_ENSURE_SUCCESS(res, res);
if (!bCollapsed)
{
NS_ENSURE_STATE(mEditor);
res = mEditor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip);
NS_ENSURE_SUCCESS(res, res);
}
@ -632,6 +642,7 @@ nsTextEditRules::WillInsertText(EditAction aAction,
{
nsAutoString tString(*outString);
NS_ENSURE_STATE(mEditor);
HandleNewLines(tString, mEditor->mNewlineHandling);
outString->Assign(tString);
@ -667,20 +678,24 @@ nsTextEditRules::WillInsertText(EditAction aAction,
// get the (collapsed) selection location
nsCOMPtr<nsIDOMNode> selNode;
int32_t selOffset;
NS_ENSURE_STATE(mEditor);
res = mEditor->GetStartNodeAndOffset(aSelection, getter_AddRefs(selNode), &selOffset);
NS_ENSURE_SUCCESS(res, res);
// don't put text in places that can't have it
NS_ENSURE_STATE(mEditor);
if (!mEditor->IsTextNode(selNode) &&
!mEditor->CanContainTag(selNode, nsGkAtoms::textTagName)) {
return NS_ERROR_FAILURE;
}
// we need to get the doc
NS_ENSURE_STATE(mEditor);
nsCOMPtr<nsIDOMDocument> doc = mEditor->GetDOMDocument();
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
if (aAction == EditAction::insertIMEText) {
NS_ENSURE_STATE(mEditor);
res = mEditor->InsertTextImpl(*outString, address_of(selNode), &selOffset, doc);
NS_ENSURE_SUCCESS(res, res);
} else {
@ -689,6 +704,7 @@ nsTextEditRules::WillInsertText(EditAction aAction,
int32_t curOffset = selOffset;
// don't spaz my selection in subtransactions
NS_ENSURE_STATE(mEditor);
nsAutoTxnsConserveSelection dontSpazMySelection(mEditor);
res = mEditor->InsertTextImpl(*outString, address_of(curNode),
@ -781,6 +797,7 @@ nsTextEditRules::WillDeleteSelection(Selection* aSelection,
if (IsPasswordEditor())
{
NS_ENSURE_STATE(mEditor);
res = mEditor->ExtendSelectionForDelete(aSelection, &aCollapsedAction);
NS_ENSURE_SUCCESS(res, res);
@ -817,6 +834,7 @@ nsTextEditRules::WillDeleteSelection(Selection* aSelection,
{
nsCOMPtr<nsIDOMNode> startNode;
int32_t startOffset;
NS_ENSURE_STATE(mEditor);
res = mEditor->GetStartNodeAndOffset(aSelection, getter_AddRefs(startNode), &startOffset);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(startNode, NS_ERROR_FAILURE);
@ -833,10 +851,12 @@ nsTextEditRules::WillDeleteSelection(Selection* aSelection,
NS_ENSURE_SUCCESS(res, res);
if (*aCancel) return NS_OK;
NS_ENSURE_STATE(mEditor);
res = mEditor->ExtendSelectionForDelete(aSelection, &aCollapsedAction);
NS_ENSURE_SUCCESS(res, res);
}
NS_ENSURE_STATE(mEditor);
res = mEditor->DeleteSelectionImpl(aCollapsedAction, nsIEditor::eStrip);
NS_ENSURE_SUCCESS(res, res);
@ -852,6 +872,7 @@ nsTextEditRules::DidDeleteSelection(nsISelection *aSelection,
{
nsCOMPtr<nsIDOMNode> startNode;
int32_t startOffset;
NS_ENSURE_STATE(mEditor);
nsresult res = mEditor->GetStartNodeAndOffset(aSelection, getter_AddRefs(startNode), &startOffset);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(startNode, NS_ERROR_FAILURE);
@ -904,6 +925,7 @@ nsTextEditRules::DidUndo(nsISelection *aSelection, nsresult aResult)
// If aResult is an error, we return it.
NS_ENSURE_SUCCESS(aResult, aResult);
NS_ENSURE_STATE(mEditor);
dom::Element* theRoot = mEditor->GetRoot();
NS_ENSURE_TRUE(theRoot, NS_ERROR_FAILURE);
nsIContent* node = mEditor->GetLeftmostChild(theRoot);
@ -933,6 +955,7 @@ nsTextEditRules::DidRedo(nsISelection *aSelection, nsresult aResult)
if (!aSelection) { return NS_ERROR_NULL_POINTER; }
if (NS_SUCCEEDED(res))
{
NS_ENSURE_STATE(mEditor);
nsCOMPtr<nsIDOMElement> theRoot = do_QueryInterface(mEditor->GetRoot());
NS_ENSURE_TRUE(theRoot, NS_ERROR_FAILURE);
@ -1015,6 +1038,7 @@ nsTextEditRules::RemoveRedundantTrailingBR()
if (IsSingleLineEditor())
return NS_OK;
NS_ENSURE_STATE(mEditor);
nsRefPtr<dom::Element> body = mEditor->GetRoot();
if (!body)
return NS_ERROR_NULL_POINTER;
@ -1056,6 +1080,7 @@ nsTextEditRules::CreateTrailingBRIfNeeded()
return NS_OK;
}
NS_ENSURE_STATE(mEditor);
dom::Element* body = mEditor->GetRoot();
NS_ENSURE_TRUE(body, NS_ERROR_NULL_POINTER);
@ -1160,6 +1185,7 @@ nsTextEditRules::TruncateInsertionIfNeeded(Selection* aSelection,
*aTruncated = false;
}
NS_ENSURE_STATE(mEditor);
if ((-1 != aMaxLength) && IsPlaintextEditor() && !mEditor->IsIMEComposing() )
{
// Get the current text length.
@ -1253,6 +1279,7 @@ nsresult nsTextEditRules::HideLastPWInput() {
nsAutoString hiddenText;
FillBufWithPWChars(&hiddenText, mLastLength);
NS_ENSURE_STATE(mEditor);
nsRefPtr<Selection> selection = mEditor->GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
int32_t start, end;
@ -1298,6 +1325,7 @@ nsTextEditRules::CreateMozBR(nsIDOMNode* inParent, int32_t inOffset,
NS_ENSURE_TRUE(inParent, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMNode> brNode;
NS_ENSURE_STATE(mEditor);
nsresult res = mEditor->CreateBR(inParent, inOffset, address_of(brNode));
NS_ENSURE_SUCCESS(res, res);