Bug 1574852 - part 99: Move `HTMLEditRules::WillRelativeChangeZIndex()` to `HTMLEditor` r=m_kato

Differential Revision: https://phabricator.services.mozilla.com/D45293

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2019-09-12 06:20:49 +00:00
Родитель fee93a3a4d
Коммит d87ff4830d
5 изменённых файлов: 47 добавлений и 71 удалений

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

@ -138,30 +138,9 @@ nsresult HTMLEditor::AddZIndexAsAction(int32_t aChange,
return NS_ERROR_NOT_INITIALIZED;
}
AutoPlaceholderBatch treatAsOneTransaction(*this);
AutoEditSubActionNotifier startToHandleEditSubAction(
*this,
aChange < 0 ? EditSubAction::eDecreaseZIndex
: EditSubAction::eIncreaseZIndex,
nsIEditor::eNext);
// brade: can we get rid of this comment?
// Find out if the selection is collapsed:
EditSubActionInfo subActionInfo(aChange < 0 ? EditSubAction::eDecreaseZIndex
: EditSubAction::eIncreaseZIndex);
bool cancel, handled;
// Protect the edit rules object from dying
RefPtr<TextEditRules> rules(mRules);
nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
if (cancel || NS_FAILED(rv)) {
return EditorBase::ToGenericNSResult(rv);
}
rv = rules->DidDoAction(subActionInfo, rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return EditorBase::ToGenericNSResult(rv);
}
return NS_OK;
EditActionResult result = AddZIndexAsSubAction(aChange);
NS_WARNING_ASSERTION(result.Succeeded(), "AddZIndexAsSubAction() failed");
return EditorBase::ToGenericNSResult(result.Rv());
}
int32_t HTMLEditor::GetZIndex(Element& aElement) {

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

@ -798,13 +798,11 @@ nsresult HTMLEditRules::WillDoAction(EditSubActionInfo& aInfo, bool* aCancel,
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "WillInsert() failed");
return NS_OK;
}
case EditSubAction::eDecreaseZIndex:
return WillRelativeChangeZIndex(-1, aCancel, aHandled);
case EditSubAction::eIncreaseZIndex:
return WillRelativeChangeZIndex(1, aCancel, aHandled);
case EditSubAction::eCreateOrChangeDefinitionListItem:
case EditSubAction::eCreateOrChangeList:
case EditSubAction::eCreateOrRemoveBlock:
case EditSubAction::eDecreaseZIndex:
case EditSubAction::eIncreaseZIndex:
case EditSubAction::eIndent:
case EditSubAction::eInsertHTMLSource:
case EditSubAction::eInsertParagraphSeparator:
@ -843,6 +841,8 @@ nsresult HTMLEditRules::DidDoAction(EditSubActionInfo& aInfo,
case EditSubAction::eCreateOrChangeDefinitionListItem:
case EditSubAction::eCreateOrChangeList:
case EditSubAction::eCreateOrRemoveBlock:
case EditSubAction::eDecreaseZIndex:
case EditSubAction::eIncreaseZIndex:
case EditSubAction::eIndent:
case EditSubAction::eInsertHTMLSource:
case EditSubAction::eInsertParagraphSeparator:
@ -11242,49 +11242,51 @@ EditActionResult HTMLEditor::SetSelectionToStaticAsSubAction() {
: EditActionHandled(NS_OK);
}
nsresult HTMLEditRules::WillRelativeChangeZIndex(int32_t aChange, bool* aCancel,
bool* aHandled) {
MOZ_ASSERT(IsEditorDataAvailable());
EditActionResult HTMLEditor::AddZIndexAsSubAction(int32_t aChange) {
MOZ_ASSERT(IsEditActionDataAvailable());
if (NS_WARN_IF(!aCancel) || NS_WARN_IF(!aHandled)) {
return NS_ERROR_INVALID_ARG;
AutoPlaceholderBatch treatAsOneTransaction(*this);
AutoEditSubActionNotifier startToHandleEditSubAction(
*this,
aChange < 0 ? EditSubAction::eDecreaseZIndex
: EditSubAction::eIncreaseZIndex,
nsIEditor::eNext);
EditActionResult result = CanHandleHTMLEditSubAction();
if (NS_WARN_IF(result.Failed()) || result.Canceled()) {
return result;
}
// FYI: Ignore cancel result of WillInsert().
nsresult rv = MOZ_KnownLive(HTMLEditorRef()).WillInsert();
nsresult rv = WillInsert();
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return NS_ERROR_EDITOR_DESTROYED;
return EditActionHandled(NS_ERROR_EDITOR_DESTROYED);
}
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "WillInsert() failed");
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "WillInsert() failed, but ignored");
*aCancel = false;
*aHandled = true;
RefPtr<Element> element =
HTMLEditorRef().GetAbsolutelyPositionedSelectionContainer();
if (NS_WARN_IF(!element)) {
return NS_ERROR_FAILURE;
RefPtr<Element> absolutelyPositionedElement =
GetAbsolutelyPositionedSelectionContainer();
if (NS_WARN_IF(!absolutelyPositionedElement)) {
return EditActionHandled(NS_ERROR_FAILURE);
}
{
AutoSelectionRestorer restoreSelectionLater(HTMLEditorRef());
AutoSelectionRestorer restoreSelectionLater(*this);
int32_t zIndex;
nsresult rv = MOZ_KnownLive(HTMLEditorRef())
.RelativeChangeElementZIndex(*element, aChange, &zIndex);
if (NS_WARN_IF(!CanHandleEditAction())) {
return NS_ERROR_EDITOR_DESTROYED;
nsresult rv = RelativeChangeElementZIndex(*absolutelyPositionedElement,
aChange, &zIndex);
if (NS_WARN_IF(Destroyed())) {
return EditActionHandled(NS_ERROR_EDITOR_DESTROYED);
}
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
return EditActionHandled(rv);
}
}
// Restoring Selection might cause destroying the HTML editor.
if (NS_WARN_IF(!CanHandleEditAction())) {
return NS_ERROR_EDITOR_DESTROYED;
}
return NS_OK;
return NS_WARN_IF(Destroyed()) ? EditActionHandled(NS_ERROR_EDITOR_DESTROYED)
: EditActionHandled(NS_OK);
}
nsresult HTMLEditRules::DocumentModified() {

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

@ -113,20 +113,6 @@ class HTMLEditRules : public TextEditRules {
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult DidDeleteSelection();
/**
* Called before changing z-index.
* This method actually changes z-index of nearest absolute positioned
* element relatively. Therefore, this might cause destroying the HTML
* editor.
*
* @param aChange Amount to change z-index.
* @param aCancel Returns true if the operation is canceled.
* @param aHandled Returns true if the edit action is handled.
*/
MOZ_CAN_RUN_SCRIPT
MOZ_MUST_USE nsresult WillRelativeChangeZIndex(int32_t aChange, bool* aCancel,
bool* aHandled);
nsresult AppendInnerFormatNodes(nsTArray<OwningNonNull<nsINode>>& aArray,
nsINode* aNode);
nsresult GetFormatString(nsINode* aNode, nsAString& outFormat);

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

@ -426,8 +426,8 @@ class HTMLEditor final : public TextEditor,
* JS. If set to nullptr, will be treated as
* called by system.
*/
nsresult AddZIndexAsAction(int32_t aChange,
nsIPrincipal* aPrincipal = nullptr);
MOZ_CAN_RUN_SCRIPT nsresult
AddZIndexAsAction(int32_t aChange, nsIPrincipal* aPrincipal = nullptr);
MOZ_CAN_RUN_SCRIPT nsresult SetBackgroundColorAsAction(
const nsAString& aColor, nsIPrincipal* aPrincipal = nullptr);
@ -2687,6 +2687,15 @@ class HTMLEditor final : public TextEditor,
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE EditActionResult
SetSelectionToStaticAsSubAction();
/**
* AddZIndexAsSubAction() adds aChange to `z-index` of nearest parent
* absolute-positioned element from current selection.
*
* @param aChange Amount to change `z-index`.
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE EditActionResult
AddZIndexAsSubAction(int32_t aChange);
protected: // Called by helper classes.
virtual void OnStartToHandleTopLevelEditSubAction(
EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;

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

@ -972,7 +972,7 @@ nsresult DecreaseZIndexCommand::DoCommand(Command aCommand,
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
nsresult rv = htmlEditor->AddZIndexAsAction(-1, aPrincipal);
nsresult rv = MOZ_KnownLive(htmlEditor)->AddZIndexAsAction(-1, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "AddZIndexAsAction() failed");
return rv;
}
@ -1012,7 +1012,7 @@ nsresult IncreaseZIndexCommand::DoCommand(Command aCommand,
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE;
}
nsresult rv = htmlEditor->AddZIndexAsAction(1, aPrincipal);
nsresult rv = MOZ_KnownLive(htmlEditor)->AddZIndexAsAction(1, aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "AddZIndexAsAction() failed");
return rv;
}