Bug 1451672 - part 20: Rename HTMLEditor::MakeDefinitionItem() and HTMLEditor::InsertBasicBlock() with "WithTransaction" postfix r?m_kato
MozReview-Commit-ID: 2D50suUnFcw
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -1745,22 +1745,24 @@ HTMLEditor::SetCaretAfterElement(nsIDOME
return NS_ERROR_FAILURE;
}
return selection->Collapse(afterElement);
}
NS_IMETHODIMP
HTMLEditor::SetParagraphFormat(const nsAString& aParagraphFormat)
{
- nsAutoString tag; tag.Assign(aParagraphFormat);
- ToLowerCase(tag);
- if (tag.EqualsLiteral("dd") || tag.EqualsLiteral("dt")) {
- return MakeDefinitionItem(tag);
- }
- return InsertBasicBlock(tag);
+ nsAutoString lowerCaseTagName(aParagraphFormat);
+ ToLowerCase(lowerCaseTagName);
+ RefPtr<nsAtom> tagName = NS_Atomize(lowerCaseTagName);
+ MOZ_ASSERT(tagName);
+ if (tagName == nsGkAtoms::dd || tagName == nsGkAtoms::dt) {
+ return MakeDefinitionListItemWithTransaction(*tagName);
+ }
+ return InsertBasicBlockWithTransaction(*tagName);
}
NS_IMETHODIMP
HTMLEditor::GetParagraphState(bool* aMixed,
nsAString& outFormat)
{
if (!mRules) {
return NS_ERROR_NOT_INITIALIZED;
@@ -2112,69 +2114,79 @@ HTMLEditor::RemoveList(const nsAString&
}
// no default behavior for this yet. what would it mean?
return rules->DidDoAction(selection, &ruleInfo, rv);
}
nsresult
-HTMLEditor::MakeDefinitionItem(const nsAString& aItemType)
+HTMLEditor::MakeDefinitionListItemWithTransaction(nsAtom& aTagName)
{
if (!mRules) {
return NS_ERROR_NOT_INITIALIZED;
}
+ MOZ_ASSERT(&aTagName == nsGkAtoms::dt ||
+ &aTagName == nsGkAtoms::dd);
+
// Protect the edit rules object from dying
RefPtr<TextEditRules> rules(mRules);
bool cancel, handled;
AutoPlaceholderBatch beginBatching(this);
AutoRules beginRulesSniffing(this, EditAction::makeDefListItem,
nsIEditor::eNext);
// pre-process
RefPtr<Selection> selection = GetSelection();
- NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
+ if (NS_WARN_IF(!selection)) {
+ return NS_ERROR_FAILURE;
+ }
+ nsDependentAtomString tagName(&aTagName);
RulesInfo ruleInfo(EditAction::makeDefListItem);
- ruleInfo.blockType = &aItemType;
+ ruleInfo.blockType = &tagName;
nsresult rv = rules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (cancel || NS_FAILED(rv)) {
return rv;
}
if (!handled) {
// todo: no default for now. we count on rules to handle it.
}
return rules->DidDoAction(selection, &ruleInfo, rv);
}
nsresult
-HTMLEditor::InsertBasicBlock(const nsAString& aBlockType)
+HTMLEditor::InsertBasicBlockWithTransaction(nsAtom& aTagName)
{
if (!mRules) {
return NS_ERROR_NOT_INITIALIZED;
}
+ MOZ_ASSERT(&aTagName != nsGkAtoms::dd &&
+ &aTagName != nsGkAtoms::dt);
+
// Protect the edit rules object from dying
RefPtr<TextEditRules> rules(mRules);
bool cancel, handled;
AutoPlaceholderBatch beginBatching(this);
AutoRules beginRulesSniffing(this, EditAction::makeBasicBlock,
nsIEditor::eNext);
// pre-process
RefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
+ nsDependentAtomString tagName(&aTagName);
RulesInfo ruleInfo(EditAction::makeBasicBlock);
- ruleInfo.blockType = &aBlockType;
+ ruleInfo.blockType = &tagName;
nsresult rv = rules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (cancel || NS_FAILED(rv)) {
return rv;
}
if (!handled && selection->Collapsed()) {
nsRange* firstRange = selection->GetRangeAt(0);
if (NS_WARN_IF(!firstRange)) {
@@ -2185,18 +2197,17 @@ HTMLEditor::InsertBasicBlock(const nsASt
if (NS_WARN_IF(!atStartOfSelection.IsSet()) ||
NS_WARN_IF(!atStartOfSelection.GetContainerAsContent())) {
return NS_ERROR_FAILURE;
}
// Have to find a place to put the block.
EditorDOMPoint pointToInsertBlock(atStartOfSelection);
- RefPtr<nsAtom> blockAtom = NS_Atomize(aBlockType);
- while (!CanContainTag(*pointToInsertBlock.GetContainer(), *blockAtom)) {
+ while (!CanContainTag(*pointToInsertBlock.GetContainer(), aTagName)) {
pointToInsertBlock.Set(pointToInsertBlock.GetContainer());
if (NS_WARN_IF(!pointToInsertBlock.IsSet()) ||
NS_WARN_IF(!pointToInsertBlock.GetContainerAsContent())) {
return NS_ERROR_FAILURE;
}
}
if (pointToInsertBlock.GetContainer() !=
@@ -2214,17 +2225,17 @@ HTMLEditor::InsertBasicBlock(const nsASt
return NS_ERROR_FAILURE;
}
}
// Create a block and insert it before the right node if we split some
// parents of start of selection above, or just start of selection
// otherwise.
RefPtr<Element> newBlock =
- CreateNodeWithTransaction(*blockAtom, pointToInsertBlock);
+ CreateNodeWithTransaction(aTagName, pointToInsertBlock);
if (NS_WARN_IF(!newBlock)) {
return NS_ERROR_FAILURE;
}
// reposition selection to inside the block
ErrorResult error;
selection->Collapse(RawRangeBoundary(newBlock, 0), error);
if (NS_WARN_IF(error.Failed())) {
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -886,20 +886,32 @@ protected:
* is neither none, another <br> element nor
* different block level element.
*/
EditorRawDOMPoint
GetBetterInsertionPointFor(nsINode& aNodeToInsert,
const EditorRawDOMPoint& aPointToInsert);
/**
- * Helpers for block transformations.
+ * MakeDefinitionListItemWithTransaction() replaces parent list of current
+ * selection with <dl> or create new <dl> element and creates a definition
+ * list item whose name is aTagName.
+ *
+ * @param aTagName Must be nsGkAtoms::dt or nsGkAtoms::dd.
*/
- nsresult MakeDefinitionItem(const nsAString& aItemType);
- nsresult InsertBasicBlock(const nsAString& aBlockType);
+ nsresult MakeDefinitionListItemWithTransaction(nsAtom& aTagName);
+
+ /**
+ * InsertBasicBlockWithTransaction() inserts a block element whose name
+ * is aTagName at selection.
+ *
+ * @param aTagName A block level element name. Must NOT be
+ * nsGkAtoms::dt nor nsGkAtoms::dd.
+ */
+ nsresult InsertBasicBlockWithTransaction(nsAtom& aTagName);
/**
* Increase/decrease the font size of selection.
*/
enum class FontSize { incr, decr };
nsresult RelativeFontChange(FontSize aDir);
/**