Bug 1425412 - part 9: Create factory methods of ChangeAttributeTransaction and remove EditorBase::CreateTxnForSetAttribute() and EditorBase::CreateTxnForRemoveAttribute() r?m_kato
This patch creates two factory methods of ChangeAttributeTransaction. One is
for setting an attribute to specific value. The other is for removing an
attribute. So, EditorBase::CreateTxnForSetAttribute() and
EditorBase::CreateTxnForRemoveAttribute() are unnecessary anymore.
MozReview-Commit-ID: 2fEVd3pDXsf
--- a/editor/libeditor/ChangeAttributeTransaction.cpp
+++ b/editor/libeditor/ChangeAttributeTransaction.cpp
@@ -9,26 +9,46 @@
#include "nsAString.h"
#include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc.
namespace mozilla {
using namespace dom;
+// static
+already_AddRefed<ChangeAttributeTransaction>
+ChangeAttributeTransaction::Create(Element& aElement,
+ nsAtom& aAttribute,
+ const nsAString& aValue)
+{
+ RefPtr<ChangeAttributeTransaction> transaction =
+ new ChangeAttributeTransaction(aElement, aAttribute, &aValue);
+ return transaction.forget();
+}
+
+// static
+already_AddRefed<ChangeAttributeTransaction>
+ChangeAttributeTransaction::CreateToRemove(Element& aElement,
+ nsAtom& aAttribute)
+{
+ RefPtr<ChangeAttributeTransaction> transaction =
+ new ChangeAttributeTransaction(aElement, aAttribute, nullptr);
+ return transaction.forget();
+}
+
ChangeAttributeTransaction::ChangeAttributeTransaction(Element& aElement,
nsAtom& aAttribute,
const nsAString* aValue)
: EditTransactionBase()
, mElement(&aElement)
, mAttribute(&aAttribute)
, mValue(aValue ? *aValue : EmptyString())
, mRemoveAttribute(!aValue)
, mAttributeWasSet(false)
- , mUndoValue()
{
}
ChangeAttributeTransaction::~ChangeAttributeTransaction()
{
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTransaction,
--- a/editor/libeditor/ChangeAttributeTransaction.h
+++ b/editor/libeditor/ChangeAttributeTransaction.h
@@ -22,26 +22,46 @@ class Element;
} // namespace dom
/**
* A transaction that changes an attribute of a content node. This transaction
* covers add, remove, and change attribute.
*/
class ChangeAttributeTransaction final : public EditTransactionBase
{
-public:
- /**
- * @param aElement the element whose attribute will be changed
- * @param aAttribute the name of the attribute to change
- * @param aValue the new value for aAttribute, or null to remove
- */
+protected:
ChangeAttributeTransaction(dom::Element& aElement,
nsAtom& aAttribute,
const nsAString* aValue);
+public:
+ /**
+ * Creates a change attribute transaction to set an attribute to something.
+ * This method never returns nullptr.
+ *
+ * @param aElement The element whose attribute will be changed.
+ * @param aAttribute The name of the attribute to change.
+ * @param aValue The new value for aAttribute.
+ */
+ static already_AddRefed<ChangeAttributeTransaction>
+ Create(dom::Element& aElement,
+ nsAtom& aAttribute,
+ const nsAString& aValue);
+
+ /**
+ * Creates a change attribute transaction to remove an attribute. This
+ * method never returns nullptr.
+ *
+ * @param aElement The element whose attribute will be changed.
+ * @param aAttribute The name of the attribute to remove.
+ */
+ static already_AddRefed<ChangeAttributeTransaction>
+ CreateToRemove(dom::Element& aElement,
+ nsAtom& aAttribute);
+
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ChangeAttributeTransaction,
EditTransactionBase)
NS_DECL_EDITTRANSACTIONBASE
NS_IMETHOD RedoTransaction() override;
@@ -52,21 +72,21 @@ private:
nsCOMPtr<dom::Element> mElement;
// The attribute to change
RefPtr<nsAtom> mAttribute;
// The value to set the attribute to (ignored if mRemoveAttribute==true)
nsString mValue;
+ // The value to set the attribute to for undo
+ nsString mUndoValue;
+
// True if the operation is to remove mAttribute from mElement
bool mRemoveAttribute;
// True if the mAttribute was set on mElement at the time of execution
bool mAttributeWasSet;
-
- // The value to set the attribute to for undo
- nsString mUndoValue;
};
} // namespace mozilla
#endif // #ifndef ChangeAttributeTransaction_h
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -1267,17 +1267,17 @@ EditorBase::SetAttribute(nsIDOMElement*
}
nsresult
EditorBase::SetAttribute(Element* aElement,
nsAtom* aAttribute,
const nsAString& aValue)
{
RefPtr<ChangeAttributeTransaction> transaction =
- CreateTxnForSetAttribute(*aElement, *aAttribute, aValue);
+ ChangeAttributeTransaction::Create(*aElement, *aAttribute, aValue);
return DoTransaction(transaction);
}
NS_IMETHODIMP
EditorBase::GetAttributeValue(nsIDOMElement* aElement,
const nsAString& aAttribute,
nsAString& aResultValue,
bool* aResultIsSet)
@@ -1311,17 +1311,17 @@ EditorBase::RemoveAttribute(nsIDOMElemen
return RemoveAttribute(element, attribute);
}
nsresult
EditorBase::RemoveAttribute(Element* aElement,
nsAtom* aAttribute)
{
RefPtr<ChangeAttributeTransaction> transaction =
- CreateTxnForRemoveAttribute(*aElement, *aAttribute);
+ ChangeAttributeTransaction::CreateToRemove(*aElement, *aAttribute);
return DoTransaction(transaction);
}
bool
EditorBase::OutputsMozDirty()
{
// Return true for Composer (!IsInteractionAllowed()) or mail
// (IsMailEditor()), but false for webpages.
@@ -4584,37 +4584,16 @@ EditorBase::DoAfterUndoTransaction()
void
EditorBase::DoAfterRedoTransaction()
{
// all redoable transactions are non-transient
MOZ_ALWAYS_SUCCEEDS(IncrementModificationCount(1));
}
-already_AddRefed<ChangeAttributeTransaction>
-EditorBase::CreateTxnForSetAttribute(Element& aElement,
- nsAtom& aAttribute,
- const nsAString& aValue)
-{
- RefPtr<ChangeAttributeTransaction> transaction =
- new ChangeAttributeTransaction(aElement, aAttribute, &aValue);
-
- return transaction.forget();
-}
-
-already_AddRefed<ChangeAttributeTransaction>
-EditorBase::CreateTxnForRemoveAttribute(Element& aElement,
- nsAtom& aAttribute)
-{
- RefPtr<ChangeAttributeTransaction> transaction =
- new ChangeAttributeTransaction(aElement, aAttribute, nullptr);
-
- return transaction.forget();
-}
-
already_AddRefed<AddStyleSheetTransaction>
EditorBase::CreateTxnForAddStyleSheet(StyleSheet* aSheet)
{
RefPtr<AddStyleSheetTransaction> transaction =
new AddStyleSheetTransaction(*this, aSheet);
return transaction.forget();
}
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -499,31 +499,16 @@ public:
RangeUpdater& RangeUpdaterRef() { return mRangeUpdater; }
protected:
nsresult DetermineCurrentDirection();
void FireInputEvent();
/**
- * Create a transaction for setting aAttribute to aValue on aElement. Never
- * returns null.
- */
- already_AddRefed<ChangeAttributeTransaction>
- CreateTxnForSetAttribute(Element& aElement, nsAtom& aAttribute,
- const nsAString& aValue);
-
- /**
- * Create a transaction for removing aAttribute on aElement. Never returns
- * null.
- */
- already_AddRefed<ChangeAttributeTransaction>
- CreateTxnForRemoveAttribute(Element& aElement, nsAtom& aAttribute);
-
- /**
* Create an element node whose name is aTag at before aPointToInsert. When
* this succeed to create an element node, this sets aPointToInsert to the
* new element because the relation of child and offset may be broken.
* If the caller needs to collapse the selection to next to the new element
* node, it should call |aPointToInsert.AdvanceOffset()| after calling this.
*
* @param aTag The element name to create.
* @param aPointToInsert The insertion point of new element. If this refers