rules(mRules);
-
- AutoPlaceholderBatch treatAsOneTransaction(*this);
- AutoEditSubActionNotifier startToHandleEditSubAction(
- *this, EditSubAction::eSetOrClearAlignment, nsIEditor::eNext);
-
- bool cancel, handled;
-
- // Find out if the selection is collapsed:
- EditSubActionInfo subActionInfo(EditSubAction::eSetOrClearAlignment);
- subActionInfo.alignType = &aAlignType;
- 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 = AlignAsSubAction(aAlignType);
+ NS_WARNING_ASSERTION(result.Succeeded(), "AlignAsSubAction() failed");
+ return EditorBase::ToGenericNSResult(result.Rv());
}
Element* HTMLEditor::GetElementOrParentByTagName(const nsAtom& aTagName,
diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h
index 9fd7fdfc694e..243150cbd027 100644
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -259,8 +259,8 @@ class HTMLEditor final : public TextEditor,
MOZ_CAN_RUN_SCRIPT nsresult SetParagraphFormatAsAction(
const nsAString& aParagraphFormat, nsIPrincipal* aPrincipal = nullptr);
- nsresult AlignAsAction(const nsAString& aAlignType,
- nsIPrincipal* aPrincipal = nullptr);
+ MOZ_CAN_RUN_SCRIPT nsresult AlignAsAction(const nsAString& aAlignType,
+ nsIPrincipal* aPrincipal = nullptr);
MOZ_CAN_RUN_SCRIPT nsresult RemoveListAsAction(
const nsAString& aListType, nsIPrincipal* aPrincipal = nullptr);
@@ -2523,6 +2523,52 @@ class HTMLEditor final : public TextEditor,
SetBlockElementAlign(Element& aBlockOrHRElement, const nsAString& aAlignType,
EditTarget aEditTarget);
+ /**
+ * AlignContentsAtSelectionWithEmptyDivElement() inserts new `` element
+ * at `Selection` to align selected contents. This returns as "handled"
+ * if this modifies `Selection` so that callers shouldn't modify `Selection`
+ * in such case especially when using AutoSelectionRestorer.
+ *
+ * @param aAlignType New align attribute value where the contents
+ * should be aligned to.
+ */
+ MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE EditActionResult
+ AlignContentsAtSelectionWithEmptyDivElement(const nsAString& aAlignType);
+
+ /**
+ * AlignNodesAndDescendants() make contents of nodes in aArrayOfNodes and
+ * their descendants aligned to aAlignType.
+ *
+ * @param aAlignType New align attribute value where the contents
+ * should be aligned to.
+ */
+ MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult
+ AlignNodesAndDescendants(nsTArray>& aArrayOfNodes,
+ const nsAString& aAlignType);
+
+ /**
+ * AlignContentsAtSelection() aligns contents around Selection to aAlignType.
+ * This creates AutoSelectionRestorer. Therefore, even if this returns
+ * NS_OK, we might have been destroyed. So, every caller needs to check if
+ * Destroyed() returns false before modifying the DOM tree or changing
+ * Selection.
+ * NOTE: Call AlignAsSubAction() instead.
+ *
+ * @param aAlignType New align attribute value where the contents
+ * should be aligned to.
+ */
+ MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult
+ AlignContentsAtSelection(const nsAString& aAlignType);
+
+ /**
+ * AlignAsSubAction() handles "align" command with `Selection`.
+ *
+ * @param aAlignType New align attribute value where the contents
+ * should be aligned to.
+ */
+ MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE EditActionResult
+ AlignAsSubAction(const nsAString& aAlignType);
+
protected: // Called by helper classes.
virtual void OnStartToHandleTopLevelEditSubAction(
EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;
diff --git a/editor/nsIHTMLEditor.idl b/editor/nsIHTMLEditor.idl
index 2aba22a346d8..d0d650317b3b 100644
--- a/editor/nsIHTMLEditor.idl
+++ b/editor/nsIHTMLEditor.idl
@@ -307,6 +307,7 @@ interface nsIHTMLEditor : nsISupports
* Document me!
*
*/
+ [can_run_script]
void align(in AString aAlign);
/**