Bug 1156062 part 4 - Clean up ns*EditRules::WillInsert; r=ehsan

This commit is contained in:
Aryeh Gregor 2016-04-23 18:30:17 +09:00
Родитель d3b2d91e89
Коммит 258c4350c5
4 изменённых файлов: 74 добавлений и 91 удалений

Просмотреть файл

@ -660,7 +660,8 @@ nsHTMLEditRules::WillDoAction(Selection* aSelection,
return WillMakeDefListItem(aSelection, info->blockType, info->entireList,
aCancel, aHandled);
case EditAction::insertElement:
return WillInsert(aSelection, aCancel);
WillInsert(*aSelection, aCancel);
return NS_OK;
case EditAction::decreaseZIndex:
return WillRelativeChangeZIndex(aSelection, -1, aCancel, aHandled);
case EditAction::increaseZIndex:
@ -1206,54 +1207,46 @@ nsHTMLEditRules::GetFormatString(nsIDOMNode *aNode, nsAString &outFormat)
* Protected rules methods
********************************************************/
nsresult
nsHTMLEditRules::WillInsert(Selection* aSelection, bool* aCancel)
void
nsHTMLEditRules::WillInsert(Selection& aSelection, bool* aCancel)
{
nsresult res = nsTextEditRules::WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
MOZ_ASSERT(aCancel);
// Adjust selection to prevent insertion after a moz-BR.
// this next only works for collapsed selections right now,
// because selection is a pain to work with when not collapsed.
// (no good way to extend start or end of selection), so we ignore
// those types of selections.
if (!aSelection->Collapsed()) {
return NS_OK;
nsTextEditRules::WillInsert(aSelection, aCancel);
NS_ENSURE_TRUE_VOID(mHTMLEditor);
nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
// Adjust selection to prevent insertion after a moz-BR. This next only
// works for collapsed selections right now, because selection is a pain to
// work with when not collapsed. (no good way to extend start or end of
// selection), so we ignore those types of selections.
if (!aSelection.Collapsed()) {
return;
}
// if we are after a mozBR in the same block, then move selection
// to be before it
nsCOMPtr<nsIDOMNode> selNode, priorNode;
int32_t selOffset;
// get the (collapsed) selection location
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->GetStartNodeAndOffset(aSelection, getter_AddRefs(selNode),
&selOffset);
NS_ENSURE_SUCCESS(res, res);
// get prior node
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->GetPriorHTMLNode(selNode, selOffset,
address_of(priorNode));
if (NS_SUCCEEDED(res) && priorNode && nsTextEditUtils::IsMozBR(priorNode))
{
nsCOMPtr<nsIDOMNode> block1, block2;
if (IsBlockNode(selNode)) {
block1 = selNode;
}
else {
NS_ENSURE_STATE(mHTMLEditor);
block1 = mHTMLEditor->GetBlockNodeParent(selNode);
}
NS_ENSURE_STATE(mHTMLEditor);
block2 = mHTMLEditor->GetBlockNodeParent(priorNode);
// If we are after a mozBR in the same block, then move selection to be
// before it
NS_ENSURE_TRUE_VOID(aSelection.GetRangeAt(0) &&
aSelection.GetRangeAt(0)->GetStartParent());
OwningNonNull<nsINode> selNode = *aSelection.GetRangeAt(0)->GetStartParent();
int32_t selOffset = aSelection.GetRangeAt(0)->StartOffset();
// Get prior node
nsCOMPtr<nsIContent> priorNode = mHTMLEditor->GetPriorHTMLNode(selNode,
selOffset);
if (priorNode && nsTextEditUtils::IsMozBR(priorNode)) {
nsCOMPtr<Element> block1 = mHTMLEditor->GetBlock(selNode);
nsCOMPtr<Element> block2 = mHTMLEditor->GetBlockNodeParent(priorNode);
if (block1 && block1 == block2) {
// if we are here then the selection is right after a mozBR
// that is in the same block as the selection. We need to move
// the selection start to be before the mozBR.
selNode = nsEditor::GetNodeLocation(priorNode, &selOffset);
res = aSelection->Collapse(selNode,selOffset);
NS_ENSURE_SUCCESS(res, res);
// If we are here then the selection is right after a mozBR that is in
// the same block as the selection. We need to move the selection start
// to be before the mozBR.
selNode = priorNode->GetParentNode();
selOffset = selNode->IndexOf(priorNode);
nsresult res = aSelection.Collapse(selNode, selOffset);
NS_ENSURE_SUCCESS_VOID(res);
}
}
@ -1261,16 +1254,14 @@ nsHTMLEditRules::WillInsert(Selection* aSelection, bool* aCancel)
(mTheAction == EditAction::insertText ||
mTheAction == EditAction::insertIMEText ||
mTheAction == EditAction::deleteSelection)) {
res = ReapplyCachedStyles();
NS_ENSURE_SUCCESS(res, res);
nsresult res = ReapplyCachedStyles();
NS_ENSURE_SUCCESS_VOID(res);
}
// For most actions we want to clear the cached styles, but there are
// exceptions
if (!IsStyleCachePreservingAction(mTheAction)) {
ClearCachedStyles();
}
return NS_OK;
}
nsresult
@ -1307,8 +1298,7 @@ nsHTMLEditRules::WillInsertText(EditAction aAction,
NS_ENSURE_SUCCESS(res, res);
}
res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
*aCancel = false;
@ -1543,8 +1533,7 @@ nsHTMLEditRules::WillInsertBreak(Selection* aSelection,
NS_ENSURE_SUCCESS(res, res);
}
res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
@ -3063,8 +3052,7 @@ nsHTMLEditRules::WillMakeList(Selection* aSelection,
}
OwningNonNull<nsIAtom> listType = NS_Atomize(*aListType);
nsresult res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
@ -3089,7 +3077,7 @@ nsHTMLEditRules::WillMakeList(Selection* aSelection,
*aHandled = true;
res = NormalizeSelection(aSelection);
nsresult res = NormalizeSelection(aSelection);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_STATE(mHTMLEditor);
nsAutoSelectionReset selectionResetter(aSelection, mHTMLEditor);
@ -3443,12 +3431,11 @@ nsHTMLEditRules::WillMakeBasicBlock(Selection* aSelection,
*aCancel = false;
*aHandled = false;
nsresult res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
*aCancel = false;
res = NormalizeSelection(aSelection);
nsresult res = NormalizeSelection(aSelection);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_STATE(mHTMLEditor);
nsAutoSelectionReset selectionResetter(aSelection, mHTMLEditor);
@ -3622,15 +3609,14 @@ nsHTMLEditRules::WillCSSIndent(Selection* aSelection,
{
if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
nsresult res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
*aCancel = false;
*aHandled = true;
res = NormalizeSelection(aSelection);
nsresult res = NormalizeSelection(aSelection);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_STATE(mHTMLEditor);
nsAutoSelectionReset selectionResetter(aSelection, mHTMLEditor);
@ -3826,15 +3812,14 @@ nsHTMLEditRules::WillHTMLIndent(Selection* aSelection,
bool* aCancel, bool* aHandled)
{
if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
nsresult res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
*aCancel = false;
*aHandled = true;
res = NormalizeSelection(aSelection);
nsresult res = NormalizeSelection(aSelection);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_STATE(mHTMLEditor);
nsAutoSelectionReset selectionResetter(aSelection, mHTMLEditor);
@ -4663,15 +4648,14 @@ nsHTMLEditRules::WillAlign(Selection* aSelection,
{
if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
nsresult res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
*aCancel = false;
*aHandled = false;
res = NormalizeSelection(aSelection);
nsresult res = NormalizeSelection(aSelection);
NS_ENSURE_SUCCESS(res, res);
nsAutoSelectionReset selectionResetter(aSelection, mHTMLEditor);
@ -8819,8 +8803,7 @@ nsHTMLEditRules::WillAbsolutePosition(Selection* aSelection,
bool* aCancel, bool* aHandled)
{
if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
nsresult res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
@ -8829,7 +8812,8 @@ nsHTMLEditRules::WillAbsolutePosition(Selection* aSelection,
nsCOMPtr<nsIDOMElement> focusElement;
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->GetSelectionContainer(getter_AddRefs(focusElement));
nsresult res =
mHTMLEditor->GetSelectionContainer(getter_AddRefs(focusElement));
if (focusElement) {
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(focusElement);
if (nsHTMLEditUtils::IsImage(node)) {
@ -9048,8 +9032,7 @@ nsresult
nsHTMLEditRules::WillRemoveAbsolutePosition(Selection* aSelection,
bool* aCancel, bool* aHandled) {
if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
nsresult res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore aCancel from WillInsert()
@ -9058,7 +9041,8 @@ nsHTMLEditRules::WillRemoveAbsolutePosition(Selection* aSelection,
nsCOMPtr<nsIDOMElement> elt;
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(elt));
nsresult res =
mHTMLEditor->GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(elt));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_STATE(mHTMLEditor);
@ -9076,8 +9060,7 @@ nsHTMLEditRules::WillRelativeChangeZIndex(Selection* aSelection,
bool * aHandled)
{
if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
nsresult res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore aCancel from WillInsert()
@ -9086,7 +9069,8 @@ nsHTMLEditRules::WillRelativeChangeZIndex(Selection* aSelection,
nsCOMPtr<nsIDOMElement> elt;
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(elt));
nsresult res =
mHTMLEditor->GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(elt));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_STATE(mHTMLEditor);

Просмотреть файл

@ -126,7 +126,7 @@ protected:
void InitFields();
// nsHTMLEditRules implementation methods
nsresult WillInsert(mozilla::dom::Selection* aSelection, bool* aCancel);
void WillInsert(mozilla::dom::Selection& aSelection, bool* aCancel);
nsresult WillInsertText( EditAction aAction,
mozilla::dom::Selection* aSelection,
bool *aCancel,

Просмотреть файл

@ -285,7 +285,8 @@ nsTextEditRules::WillDoAction(Selection* aSelection,
case EditAction::insertElement:
// i had thought this would be html rules only. but we put pre elements
// into plaintext mail when doing quoting for reply! doh!
return WillInsert(aSelection, aCancel);
WillInsert(*aSelection, aCancel);
return NS_OK;
default:
return NS_ERROR_FAILURE;
}
@ -345,25 +346,25 @@ nsTextEditRules::DocumentIsEmpty(bool *aDocumentIsEmpty)
********************************************************/
nsresult
nsTextEditRules::WillInsert(Selection* aSelection, bool* aCancel)
void
nsTextEditRules::WillInsert(Selection& aSelection, bool* aCancel)
{
NS_ENSURE_TRUE(aSelection && aCancel, NS_ERROR_NULL_POINTER);
MOZ_ASSERT(aCancel);
CANCEL_OPERATION_IF_READONLY_OR_DISABLED
if (IsReadonly() || IsDisabled()) {
*aCancel = true;
return;
}
// initialize out param
*aCancel = false;
// check for the magic content node and delete it if it exists
if (mBogusNode)
{
NS_ENSURE_STATE(mEditor);
if (mBogusNode) {
NS_ENSURE_TRUE_VOID(mEditor);
mEditor->DeleteNode(mBogusNode);
mBogusNode = nullptr;
}
return NS_OK;
}
nsresult
@ -412,8 +413,7 @@ nsTextEditRules::WillInsertBreak(Selection* aSelection,
NS_ENSURE_SUCCESS(res, res);
}
res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
*aCancel = false;
@ -651,8 +651,7 @@ nsTextEditRules::WillInsertText(EditAction aAction,
NS_ENSURE_SUCCESS(res, res);
}
res = WillInsert(aSelection, aCancel);
NS_ENSURE_SUCCESS(res, res);
WillInsert(*aSelection, aCancel);
// initialize out param
// we want to ignore result of WillInsert()
*aCancel = false;

Просмотреть файл

@ -123,7 +123,7 @@ protected:
nsresult DidInsertBreak(mozilla::dom::Selection* aSelection,
nsresult aResult);
nsresult WillInsert(mozilla::dom::Selection* aSelection, bool* aCancel);
void WillInsert(mozilla::dom::Selection& aSelection, bool* aCancel);
nsresult DidInsert(mozilla::dom::Selection* aSelection, nsresult aResult);
nsresult WillDeleteSelection(mozilla::dom::Selection* aSelection,