Skip to content

Commit

Permalink
Bug 1540037 - part 3: Move `EditorBase::InsertContainerWithTransactio…
Browse files Browse the repository at this point in the history
…n()` and related methods to `HTMLEditor` r=m_kato

They are not used by `TextEditor` so that we should move them into `HTMLEditor`.

Differential Revision: https://phabricator.services.mozilla.com/D72825
  • Loading branch information
masayuki-nakano committed Apr 30, 2020
1 parent af5c79e commit 69f21b0
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 135 deletions.
61 changes: 0 additions & 61 deletions editor/libeditor/EditorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1875,67 +1875,6 @@ nsresult EditorBase::DeleteNodeWithTransaction(nsIContent& aContent) {
return rv;
}

already_AddRefed<Element> EditorBase::InsertContainerWithTransactionInternal(
nsIContent& aContent, nsAtom& aTagName, nsAtom& aAttribute,
const nsAString& aAttributeValue) {
EditorDOMPoint pointToInsertNewContainer(&aContent);
if (NS_WARN_IF(!pointToInsertNewContainer.IsSet())) {
return nullptr;
}
// aContent will be moved to the new container before inserting the new
// container. So, when we insert the container, the insertion point
// is before the next sibling of aContent.
// XXX If pointerToInsertNewContainer stores offset here, the offset and
// referring child node become mismatched. Although, currently this
// is not a problem since InsertNodeTransaction refers only child node.
DebugOnly<bool> advanced = pointToInsertNewContainer.AdvanceOffset();
NS_WARNING_ASSERTION(advanced, "Failed to advance offset to after aContent");

// Create new container.
RefPtr<Element> newContainer = CreateHTMLContent(&aTagName);
if (NS_WARN_IF(!newContainer)) {
return nullptr;
}

// Set attribute if needed.
if (&aAttribute != nsGkAtoms::_empty) {
nsresult rv = newContainer->SetAttr(kNameSpaceID_None, &aAttribute,
aAttributeValue, true);
if (NS_FAILED(rv)) {
NS_WARNING("Element::SetAttr() failed");
return nullptr;
}
}

// Notify our internal selection state listener
AutoInsertContainerSelNotify selNotify(RangeUpdaterRef());

// Put aNode in the new container, first.
nsresult rv = DeleteNodeWithTransaction(aContent);
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::DeleteNodeWithTransaction() failed");
return nullptr;
}

{
AutoTransactionsConserveSelection conserveSelection(*this);
rv = InsertNodeWithTransaction(aContent, EditorDOMPoint(newContainer, 0));
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::InsertNodeWithTransaction() failed");
return nullptr;
}
}

// Put the new container where aNode was.
rv = InsertNodeWithTransaction(*newContainer, pointToInsertNewContainer);
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::InsertNodeWithTransaction() failed");
return nullptr;
}

return newContainer.forget();
}

nsresult EditorBase::MoveNodeWithTransaction(
nsIContent& aContent, const EditorDOMPoint& aPointToInsert) {
MOZ_ASSERT(aPointToInsert.IsSetAndValid());
Expand Down
67 changes: 0 additions & 67 deletions editor/libeditor/EditorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1470,50 +1470,6 @@ class EditorBase : public nsIEditor,
MOZ_CAN_RUN_SCRIPT void CloneAttributesWithTransaction(
Element& aDestElement, Element& aSourceElement);

/**
* InsertContainerWithTransaction() creates new element whose name is
* aTagName, moves aContent into the new element, then, inserts the new
* element into where aContent was.
* Note that this method does not check if aContent is valid child of
* the new element. So, callers need to guarantee it.
*
* @param aContent The content which will be wrapped with new
* element.
* @param aTagName Element name of new element which will wrap
* aContent and be inserted into where aContent
* was.
* @return The new element.
*/
MOZ_CAN_RUN_SCRIPT already_AddRefed<Element> InsertContainerWithTransaction(
nsIContent& aContent, nsAtom& aTagName) {
return InsertContainerWithTransactionInternal(
aContent, aTagName, *nsGkAtoms::_empty, EmptyString());
}

/**
* InsertContainerWithTransaction() creates new element whose name is
* aTagName, sets its aAttribute to aAttributeValue, moves aContent into the
* new element, then, inserts the new element into where aContent was.
* Note that this method does not check if aContent is valid child of
* the new element. So, callers need to guarantee it.
*
* @param aContent The content which will be wrapped with new
* element.
* @param aTagName Element name of new element which will wrap
* aContent and be inserted into where aContent
* was.
* @param aAttribute Attribute which should be set to the new
* element.
* @param aAttributeValue Value to be set to aAttribute.
* @return The new element.
*/
MOZ_CAN_RUN_SCRIPT already_AddRefed<Element> InsertContainerWithTransaction(
nsIContent& aContent, nsAtom& aTagName, nsAtom& aAttribute,
const nsAString& aAttributeValue) {
return InsertContainerWithTransactionInternal(aContent, aTagName,
aAttribute, aAttributeValue);
}

/**
* SplitNodeWithTransaction() creates a transaction to create a new node
* (left node) identical to an existing node (right node), and split the
Expand Down Expand Up @@ -1719,29 +1675,6 @@ class EditorBase : public nsIEditor,
uint32_t aOffset,
uint32_t aLength);

/**
* InsertContainerWithTransactionInternal() creates new element whose name is
* aTagName, moves aContent into the new element, then, inserts the new
* element into where aContent was. If aAttribute is not nsGkAtoms::_empty,
* aAttribute of the new element will be set to aAttributeValue.
*
* @param aContent The content which will be wrapped with new
* element.
* @param aTagName Element name of new element which will wrap
* aContent and be inserted into where aContent
* was.
* @param aAttribute Attribute which should be set to the new
* element. If this is nsGkAtoms::_empty,
* this does not set any attributes to the new
* element.
* @param aAttributeValue Value to be set to aAttribute.
* @return The new element.
*/
MOZ_CAN_RUN_SCRIPT already_AddRefed<Element>
InsertContainerWithTransactionInternal(nsIContent& aContent, nsAtom& aTagName,
nsAtom& aAttribute,
const nsAString& aAttributeValue);

/**
* DoSplitNode() creates a new node (left node) identical to an existing
* node (right node), and split the contents between the same point in both
Expand Down
2 changes: 1 addition & 1 deletion editor/libeditor/HTMLEditSubActionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4947,7 +4947,7 @@ EditActionResult HTMLEditor::ChangeSelectedHardLinesToList(
return EditActionResult(NS_ERROR_EDITOR_DESTROYED);
}
if (!newListItemElement) {
NS_WARNING("EditorBase::InsertContainerWithTransaction() failed");
NS_WARNING("HTMLEditor::InsertContainerWithTransaction() failed");
return EditActionResult(NS_ERROR_FAILURE);
}
// If current node is not a block element, new list item should have
Expand Down
64 changes: 63 additions & 1 deletion editor/libeditor/HTMLEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3355,6 +3355,68 @@ already_AddRefed<Element> HTMLEditor::InsertBRElementWithTransaction(
return newBRElement.forget();
}

already_AddRefed<Element> HTMLEditor::InsertContainerWithTransactionInternal(
nsIContent& aContent, nsAtom& aTagName, nsAtom& aAttribute,
const nsAString& aAttributeValue) {
EditorDOMPoint pointToInsertNewContainer(&aContent);
if (NS_WARN_IF(!pointToInsertNewContainer.IsSet())) {
return nullptr;
}
// aContent will be moved to the new container before inserting the new
// container. So, when we insert the container, the insertion point
// is before the next sibling of aContent.
// XXX If pointerToInsertNewContainer stores offset here, the offset and
// referring child node become mismatched. Although, currently this
// is not a problem since InsertNodeTransaction refers only child node.
DebugOnly<bool> advanced = pointToInsertNewContainer.AdvanceOffset();
NS_WARNING_ASSERTION(advanced, "Failed to advance offset to after aContent");

// Create new container.
RefPtr<Element> newContainer = CreateHTMLContent(&aTagName);
if (NS_WARN_IF(!newContainer)) {
return nullptr;
}

// Set attribute if needed.
if (&aAttribute != nsGkAtoms::_empty) {
nsresult rv = newContainer->SetAttr(kNameSpaceID_None, &aAttribute,
aAttributeValue, true);
if (NS_FAILED(rv)) {
NS_WARNING("Element::SetAttr() failed");
return nullptr;
}
}

// Notify our internal selection state listener
AutoInsertContainerSelNotify selNotify(RangeUpdaterRef());

// Put aNode in the new container, first.
// XXX Perhaps, we should not remove the container if it's not editable.
nsresult rv = EditorBase::DeleteNodeWithTransaction(aContent);
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::DeleteNodeWithTransaction() failed");
return nullptr;
}

{
AutoTransactionsConserveSelection conserveSelection(*this);
rv = InsertNodeWithTransaction(aContent, EditorDOMPoint(newContainer, 0));
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::InsertNodeWithTransaction() failed");
return nullptr;
}
}

// Put the new container where aNode was.
rv = InsertNodeWithTransaction(*newContainer, pointToInsertNewContainer);
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::InsertNodeWithTransaction() failed");
return nullptr;
}

return newContainer.forget();
}

already_AddRefed<Element> HTMLEditor::ReplaceContainerWithTransactionInternal(
Element& aOldContainer, nsAtom& aTagName, nsAtom& aAttribute,
const nsAString& aAttributeValue, bool aCloneAllAttributes) {
Expand Down Expand Up @@ -4697,7 +4759,7 @@ nsresult HTMLEditor::CopyLastEditableChildStylesWithTransaction(
lastClonedElement = InsertContainerWithTransaction(*lastClonedElement,
MOZ_KnownLive(*tagName));
if (!lastClonedElement) {
NS_WARNING("EditorBase::InsertContainerWithTransaction() failed");
NS_WARNING("HTMLEditor::InsertContainerWithTransaction() failed");
return NS_ERROR_FAILURE;
}
CloneAttributesWithTransaction(*lastClonedElement, *elementInPreviousBlock);
Expand Down
67 changes: 67 additions & 0 deletions editor/libeditor/HTMLEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,50 @@ class HTMLEditor final : public TextEditor,
*/
MOZ_CAN_RUN_SCRIPT nsresult RemoveContainerWithTransaction(Element& aElement);

/**
* InsertContainerWithTransaction() creates new element whose name is
* aTagName, moves aContent into the new element, then, inserts the new
* element into where aContent was.
* Note that this method does not check if aContent is valid child of
* the new element. So, callers need to guarantee it.
*
* @param aContent The content which will be wrapped with new
* element.
* @param aTagName Element name of new element which will wrap
* aContent and be inserted into where aContent
* was.
* @return The new element.
*/
MOZ_CAN_RUN_SCRIPT already_AddRefed<Element> InsertContainerWithTransaction(
nsIContent& aContent, nsAtom& aTagName) {
return InsertContainerWithTransactionInternal(
aContent, aTagName, *nsGkAtoms::_empty, EmptyString());
}

/**
* InsertContainerWithTransaction() creates new element whose name is
* aTagName, sets its aAttribute to aAttributeValue, moves aContent into the
* new element, then, inserts the new element into where aContent was.
* Note that this method does not check if aContent is valid child of
* the new element. So, callers need to guarantee it.
*
* @param aContent The content which will be wrapped with new
* element.
* @param aTagName Element name of new element which will wrap
* aContent and be inserted into where aContent
* was.
* @param aAttribute Attribute which should be set to the new
* element.
* @param aAttributeValue Value to be set to aAttribute.
* @return The new element.
*/
MOZ_CAN_RUN_SCRIPT already_AddRefed<Element> InsertContainerWithTransaction(
nsIContent& aContent, nsAtom& aTagName, nsAtom& aAttribute,
const nsAString& aAttributeValue) {
return InsertContainerWithTransactionInternal(aContent, aTagName,
aAttribute, aAttributeValue);
}

/**
* MoveNodeOrChildren() moves aContent to aPointToInsert. If cannot insert
* aContent due to invalid relation, moves only its children recursively
Expand Down Expand Up @@ -3432,6 +3476,29 @@ class HTMLEditor final : public TextEditor,
const nsAString& aAttributeValue,
bool aCloneAllAttributes);

/**
* InsertContainerWithTransactionInternal() creates new element whose name is
* aTagName, moves aContent into the new element, then, inserts the new
* element into where aContent was. If aAttribute is not nsGkAtoms::_empty,
* aAttribute of the new element will be set to aAttributeValue.
*
* @param aContent The content which will be wrapped with new
* element.
* @param aTagName Element name of new element which will wrap
* aContent and be inserted into where aContent
* was.
* @param aAttribute Attribute which should be set to the new
* element. If this is nsGkAtoms::_empty,
* this does not set any attributes to the new
* element.
* @param aAttributeValue Value to be set to aAttribute.
* @return The new element.
*/
MOZ_CAN_RUN_SCRIPT already_AddRefed<Element>
InsertContainerWithTransactionInternal(nsIContent& aContent, nsAtom& aTagName,
nsAtom& aAttribute,
const nsAString& aAttributeValue);

/**
* IndentAsSubAction() indents the content around Selection.
*/
Expand Down
10 changes: 5 additions & 5 deletions editor/libeditor/HTMLStyleEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ nsresult HTMLEditor::SetInlinePropertyOnNodeImpl(nsIContent& aContent,
spanElement = InsertContainerWithTransaction(aContent, *nsGkAtoms::span);
if (!spanElement) {
NS_WARNING(
"EditorBase::InsertContainerWithTransaction(nsGkAtoms::span) "
"HTMLEditor::InsertContainerWithTransaction(nsGkAtoms::span) "
"failed");
return NS_ERROR_FAILURE;
}
Expand Down Expand Up @@ -609,7 +609,7 @@ nsresult HTMLEditor::SetInlinePropertyOnNodeImpl(nsIContent& aContent,
aContent, aProperty, aAttribute ? *aAttribute : *nsGkAtoms::_empty,
aValue);
NS_WARNING_ASSERTION(newContainerElement,
"EditorBase::InsertContainerWithTransaction() failed");
"HTMLEditor::InsertContainerWithTransaction() failed");
return newContainerElement ? NS_OK : NS_ERROR_FAILURE;
}

Expand Down Expand Up @@ -1041,7 +1041,7 @@ nsresult HTMLEditor::RemoveStyleInside(Element& aElement, nsAtom* aProperty,
}
if (!spanElement) {
NS_WARNING(
"EditorBase::InsertContainerWithTransaction(nsGkAtoms::span) "
"HTMLEditor::InsertContainerWithTransaction(nsGkAtoms::span) "
"failed");
return NS_ERROR_FAILURE;
}
Expand Down Expand Up @@ -2301,7 +2301,7 @@ nsresult HTMLEditor::RelativeFontChangeOnTextNode(FontSize aDir,
RefPtr<Element> newElement = InsertContainerWithTransaction(
*textNodeForTheRange, MOZ_KnownLive(*nodeType));
NS_WARNING_ASSERTION(newElement,
"EditorBase::InsertContainerWithTransaction() failed");
"HTMLEditor::InsertContainerWithTransaction() failed");
return newElement ? NS_OK : NS_ERROR_FAILURE;
}

Expand Down Expand Up @@ -2429,7 +2429,7 @@ nsresult HTMLEditor::RelativeFontChangeOnNode(int32_t aSizeChange,
RefPtr<Element> newElement =
InsertContainerWithTransaction(*aNode, MOZ_KnownLive(*atom));
NS_WARNING_ASSERTION(newElement,
"EditorBase::InsertContainerWithTransaction() failed");
"HTMLEditor::InsertContainerWithTransaction() failed");
return newElement ? NS_OK : NS_ERROR_FAILURE;
}

Expand Down

0 comments on commit 69f21b0

Please sign in to comment.