mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-10 03:45:46 +00:00
Bug 1574852 - part 111: Make methods calling HTMLEditRules::WillDoAction()
with EditSubAction::eInsertElement
stop using it r=m_kato
Unfortunately, `EditSubAction::eInsertElement` is used by 4 methods and one of them is too big method. But we should move what old `WillInsert()` does into them for stop making anybody use `HTMLEditRules`. Differential Revision: https://phabricator.services.mozilla.com/D45494 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
060b8c8c50
commit
119bd889bd
@ -771,7 +771,6 @@ nsresult HTMLEditRules::WillDoAction(EditSubActionInfo& aInfo, bool* aCancel,
|
||||
}
|
||||
|
||||
switch (aInfo.mEditSubAction) {
|
||||
case EditSubAction::eInsertElement:
|
||||
case EditSubAction::eInsertQuotedText: {
|
||||
*aCancel = IsReadonly() || IsDisabled();
|
||||
nsresult rv = MOZ_KnownLive(HTMLEditorRef())
|
||||
@ -811,6 +810,7 @@ nsresult HTMLEditRules::WillDoAction(EditSubActionInfo& aInfo, bool* aCancel,
|
||||
case EditSubAction::eDeleteSelectedContent:
|
||||
case EditSubAction::eIncreaseZIndex:
|
||||
case EditSubAction::eIndent:
|
||||
case EditSubAction::eInsertElement:
|
||||
case EditSubAction::eInsertHTMLSource:
|
||||
case EditSubAction::eInsertParagraphSeparator:
|
||||
case EditSubAction::eInsertText:
|
||||
@ -840,7 +840,6 @@ nsresult HTMLEditRules::DidDoAction(EditSubActionInfo& aInfo,
|
||||
AutoSafeEditorData setData(*this, *mHTMLEditor);
|
||||
|
||||
switch (aInfo.mEditSubAction) {
|
||||
case EditSubAction::eInsertElement:
|
||||
case EditSubAction::eInsertQuotedText:
|
||||
return NS_OK;
|
||||
case EditSubAction::eComputeTextToOutput:
|
||||
@ -851,6 +850,7 @@ nsresult HTMLEditRules::DidDoAction(EditSubActionInfo& aInfo,
|
||||
case EditSubAction::eDeleteSelectedContent:
|
||||
case EditSubAction::eIncreaseZIndex:
|
||||
case EditSubAction::eIndent:
|
||||
case EditSubAction::eInsertElement:
|
||||
case EditSubAction::eInsertHTMLSource:
|
||||
case EditSubAction::eInsertLineBreak:
|
||||
case EditSubAction::eInsertParagraphSeparator:
|
||||
|
@ -1496,95 +1496,138 @@ nsresult HTMLEditor::InsertElementAtSelectionAsAction(
|
||||
return NS_ERROR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
// Protect the edit rules object from dying
|
||||
RefPtr<TextEditRules> rules(mRules);
|
||||
|
||||
CommitComposition();
|
||||
|
||||
if (IsReadonly() || IsDisabled()) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
EditActionResult result = CanHandleHTMLEditSubAction();
|
||||
if (NS_WARN_IF(result.Failed()) || result.Canceled()) {
|
||||
return result.Rv();
|
||||
}
|
||||
|
||||
UndefineCaretBidiLevel();
|
||||
|
||||
AutoPlaceholderBatch treatAsOneTransaction(*this);
|
||||
AutoEditSubActionNotifier startToHandleEditSubAction(
|
||||
*this, EditSubAction::eInsertElement, nsIEditor::eNext);
|
||||
|
||||
// hand off to the rules system, see if it has anything to say about this
|
||||
bool cancel, handled;
|
||||
EditSubActionInfo subActionInfo(EditSubAction::eInsertElement);
|
||||
nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
|
||||
if (cancel || NS_FAILED(rv)) {
|
||||
return rv;
|
||||
nsresult rv = EnsureNoPaddingBRElementForEmptyEditor();
|
||||
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
|
||||
return EditorBase::ToGenericNSResult(NS_ERROR_EDITOR_DESTROYED);
|
||||
}
|
||||
NS_WARNING_ASSERTION(
|
||||
NS_SUCCEEDED(rv),
|
||||
"EnsureNoPaddingBRElementForEmptyEditor() failed, but ignored");
|
||||
|
||||
if (NS_SUCCEEDED(rv) && SelectionRefPtr()->IsCollapsed()) {
|
||||
nsresult rv = EnsureCaretNotAfterPaddingBRElement();
|
||||
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
|
||||
return EditorBase::ToGenericNSResult(NS_ERROR_EDITOR_DESTROYED);
|
||||
}
|
||||
NS_WARNING_ASSERTION(
|
||||
NS_SUCCEEDED(rv),
|
||||
"EnsureCaretNotAfterPaddingBRElement() failed, but ignored");
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
nsresult rv = PrepareInlineStylesForCaret();
|
||||
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
|
||||
return EditorBase::ToGenericNSResult(NS_ERROR_EDITOR_DESTROYED);
|
||||
}
|
||||
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
||||
"PrepareInlineStylesForCaret() failed, but ignored");
|
||||
}
|
||||
}
|
||||
|
||||
if (!handled) {
|
||||
if (aDeleteSelection) {
|
||||
if (!IsBlockNode(aElement)) {
|
||||
// E.g., inserting an image. In this case we don't need to delete any
|
||||
// inline wrappers before we do the insertion. Otherwise we let
|
||||
// DeleteSelectionAndPrepareToCreateNode do the deletion for us, which
|
||||
// calls DeleteSelection with aStripWrappers = eStrip.
|
||||
rv = DeleteSelectionAsSubAction(eNone, eNoStrip);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return EditorBase::ToGenericNSResult(rv);
|
||||
}
|
||||
}
|
||||
|
||||
nsresult rv = DeleteSelectionAndPrepareToCreateNode();
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
|
||||
// If deleting, selection will be collapsed.
|
||||
// so if not, we collapse it
|
||||
if (!aDeleteSelection) {
|
||||
// Named Anchor is a special case,
|
||||
// We collapse to insert element BEFORE the selection
|
||||
// For all other tags, we insert AFTER the selection
|
||||
if (HTMLEditUtils::IsNamedAnchor(aElement)) {
|
||||
SelectionRefPtr()->CollapseToStart(IgnoreErrors());
|
||||
} else {
|
||||
SelectionRefPtr()->CollapseToEnd(IgnoreErrors());
|
||||
if (aDeleteSelection) {
|
||||
if (!IsBlockNode(aElement)) {
|
||||
// E.g., inserting an image. In this case we don't need to delete any
|
||||
// inline wrappers before we do the insertion. Otherwise we let
|
||||
// DeleteSelectionAndPrepareToCreateNode do the deletion for us, which
|
||||
// calls DeleteSelection with aStripWrappers = eStrip.
|
||||
nsresult rv = DeleteSelectionAsSubAction(eNone, eNoStrip);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return EditorBase::ToGenericNSResult(rv);
|
||||
}
|
||||
}
|
||||
|
||||
if (SelectionRefPtr()->GetAnchorNode()) {
|
||||
EditorRawDOMPoint atAnchor(SelectionRefPtr()->AnchorRef());
|
||||
// Adjust position based on the node we are going to insert.
|
||||
EditorDOMPoint pointToInsert =
|
||||
GetBetterInsertionPointFor(*aElement, atAnchor);
|
||||
if (NS_WARN_IF(!pointToInsert.IsSet())) {
|
||||
return NS_ERROR_FAILURE;
|
||||
nsresult rv = DeleteSelectionAndPrepareToCreateNode();
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
// If deleting, selection will be collapsed.
|
||||
// so if not, we collapse it
|
||||
else {
|
||||
// Named Anchor is a special case,
|
||||
// We collapse to insert element BEFORE the selection
|
||||
// For all other tags, we insert AFTER the selection
|
||||
if (HTMLEditUtils::IsNamedAnchor(aElement)) {
|
||||
SelectionRefPtr()->CollapseToStart(IgnoreErrors());
|
||||
if (NS_WARN_IF(Destroyed())) {
|
||||
return EditorBase::ToGenericNSResult(NS_ERROR_EDITOR_DESTROYED);
|
||||
}
|
||||
|
||||
EditorDOMPoint insertedPoint =
|
||||
InsertNodeIntoProperAncestorWithTransaction(
|
||||
*aElement, pointToInsert,
|
||||
SplitAtEdges::eAllowToCreateEmptyContainer);
|
||||
if (NS_WARN_IF(!insertedPoint.IsSet())) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
// Set caret after element, but check for special case
|
||||
// of inserting table-related elements: set in first cell instead
|
||||
if (!SetCaretInTableCell(aElement)) {
|
||||
rv = CollapseSelectionAfter(*aElement);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
// check for inserting a whole table at the end of a block. If so insert
|
||||
// a br after it.
|
||||
if (HTMLEditUtils::IsTable(aElement) && IsLastEditableChild(aElement)) {
|
||||
DebugOnly<bool> advanced = insertedPoint.AdvanceOffset();
|
||||
NS_WARNING_ASSERTION(advanced,
|
||||
"Failed to advance offset from inserted point");
|
||||
// Collapse selection to the new <br> element node after creating it.
|
||||
RefPtr<Element> newBrElement =
|
||||
InsertBRElementWithTransaction(insertedPoint, ePrevious);
|
||||
if (NS_WARN_IF(!newBrElement)) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
} else {
|
||||
SelectionRefPtr()->CollapseToEnd(IgnoreErrors());
|
||||
if (NS_WARN_IF(Destroyed())) {
|
||||
return EditorBase::ToGenericNSResult(NS_ERROR_EDITOR_DESTROYED);
|
||||
}
|
||||
}
|
||||
}
|
||||
rv = rules->DidDoAction(subActionInfo, rv);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
|
||||
if (!SelectionRefPtr()->GetAnchorNode()) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
EditorRawDOMPoint atAnchor(SelectionRefPtr()->AnchorRef());
|
||||
// Adjust position based on the node we are going to insert.
|
||||
EditorDOMPoint pointToInsert =
|
||||
GetBetterInsertionPointFor(*aElement, atAnchor);
|
||||
if (NS_WARN_IF(!pointToInsert.IsSet())) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
EditorDOMPoint insertedPoint = InsertNodeIntoProperAncestorWithTransaction(
|
||||
*aElement, pointToInsert, SplitAtEdges::eAllowToCreateEmptyContainer);
|
||||
if (NS_WARN_IF(!insertedPoint.IsSet())) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
// Set caret after element, but check for special case
|
||||
// of inserting table-related elements: set in first cell instead
|
||||
if (!SetCaretInTableCell(aElement)) {
|
||||
if (NS_WARN_IF(Destroyed())) {
|
||||
return EditorBase::ToGenericNSResult(NS_ERROR_EDITOR_DESTROYED);
|
||||
}
|
||||
rv = CollapseSelectionAfter(*aElement);
|
||||
if (NS_WARN_IF(Destroyed())) {
|
||||
return EditorBase::ToGenericNSResult(NS_ERROR_EDITOR_DESTROYED);
|
||||
}
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
if (NS_WARN_IF(Destroyed())) {
|
||||
return EditorBase::ToGenericNSResult(NS_ERROR_EDITOR_DESTROYED);
|
||||
}
|
||||
|
||||
// check for inserting a whole table at the end of a block. If so insert
|
||||
// a br after it.
|
||||
if (HTMLEditUtils::IsTable(aElement) && IsLastEditableChild(aElement)) {
|
||||
DebugOnly<bool> advanced = insertedPoint.AdvanceOffset();
|
||||
NS_WARNING_ASSERTION(advanced,
|
||||
"Failed to advance offset from inserted point");
|
||||
// Collapse selection to the new `<br>` element node after creating it.
|
||||
RefPtr<Element> newBRElement =
|
||||
InsertBRElementWithTransaction(insertedPoint, ePrevious);
|
||||
if (NS_WARN_IF(Destroyed())) {
|
||||
return EditorBase::ToGenericNSResult(NS_ERROR_EDITOR_DESTROYED);
|
||||
}
|
||||
if (NS_WARN_IF(!newBRElement)) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
@ -3209,11 +3209,9 @@ class HTMLEditor final : public TextEditor,
|
||||
* text.
|
||||
* @param aNodeInserted [OUT] The new <blockquote> element.
|
||||
*/
|
||||
MOZ_CAN_RUN_SCRIPT
|
||||
nsresult InsertAsCitedQuotationInternal(const nsAString& aQuotedText,
|
||||
const nsAString& aCitation,
|
||||
bool aInsertHTML,
|
||||
nsINode** aNodeInserted);
|
||||
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult InsertAsCitedQuotationInternal(
|
||||
const nsAString& aQuotedText, const nsAString& aCitation,
|
||||
bool aInsertHTML, nsINode** aNodeInserted);
|
||||
|
||||
/**
|
||||
* InsertNodeIntoProperAncestorWithTransaction() attempts to insert aNode
|
||||
@ -3766,9 +3764,8 @@ class HTMLEditor final : public TextEditor,
|
||||
* @return aNodeInserted The node spanning the insertion, if applicable.
|
||||
* If aAddCites is false, this will be null.
|
||||
*/
|
||||
MOZ_CAN_RUN_SCRIPT
|
||||
nsresult InsertAsPlaintextQuotation(const nsAString& aQuotedText,
|
||||
bool aAddCites, nsINode** aNodeInserted);
|
||||
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult InsertAsPlaintextQuotation(
|
||||
const nsAString& aQuotedText, bool aAddCites, nsINode** aNodeInserted);
|
||||
|
||||
/**
|
||||
* InsertObject() inserts given object at aPointToInsert.
|
||||
@ -3922,8 +3919,7 @@ class HTMLEditor final : public TextEditor,
|
||||
* aClearStyle should be set to false if you want the paste to be affected by
|
||||
* local style (e.g., for the insertHTML command).
|
||||
*/
|
||||
MOZ_CAN_RUN_SCRIPT
|
||||
nsresult DoInsertHTMLWithContext(
|
||||
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult DoInsertHTMLWithContext(
|
||||
const nsAString& aInputString, const nsAString& aContextStr,
|
||||
const nsAString& aInfoStr, const nsAString& aFlavor, Document* aSourceDoc,
|
||||
const EditorDOMPoint& aPointToInsert, bool aDeleteSelection,
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user