Skip to content

Commit

Permalink
Bug 1627175 - part 8: Move EditorBase::IsContainer() to `HTMLEditUt…
Browse files Browse the repository at this point in the history
…ils` r=m_kato

It's a virtual method which always returns true if `TextEditor`.  Therefore,
we can move it into `HTMLEditUtils` and we can make the only caller of
`EditorBase` check `IsTextEditor()` instead.

Depends on D70880

Differential Revision: https://phabricator.services.mozilla.com/D70882
  • Loading branch information
masayuki-nakano committed Apr 16, 2020
1 parent d9fe027 commit ec6cd42
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 88 deletions.
19 changes: 8 additions & 11 deletions editor/libeditor/EditorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,15 +1147,16 @@ nsresult EditorBase::CollapseSelectionToEnd() {
return NS_ERROR_NULL_POINTER;
}

nsINode* node = rootElement;
nsINode* child = node->GetLastChild();
while (child && IsContainer(child)) {
node = child;
child = node->GetLastChild();
nsIContent* lastContent = rootElement;
for (nsIContent* child = lastContent->GetLastChild();
child && (IsTextEditor() || HTMLEditUtils::IsContainerNode(*child));
child = child->GetLastChild()) {
lastContent = child;
}

uint32_t length = node->Length();
nsresult rv = SelectionRefPtr()->Collapse(node, static_cast<int32_t>(length));
uint32_t length = lastContent->Length();
nsresult rv =
SelectionRefPtr()->Collapse(lastContent, static_cast<int32_t>(length));
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Selection::Collapse() failed");
return rv;
}
Expand Down Expand Up @@ -4028,10 +4029,6 @@ bool EditorBase::IsDescendantOfEditorRoot(nsINode* aNode) const {
return aNode->IsInclusiveDescendantOf(root);
}

bool EditorBase::IsContainer(nsINode* aNode) const {
return aNode ? true : false;
}

uint32_t EditorBase::CountEditableChildren(nsINode* aNode) {
MOZ_ASSERT(aNode);
uint32_t count = 0;
Expand Down
5 changes: 0 additions & 5 deletions editor/libeditor/EditorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2066,11 +2066,6 @@ class EditorBase : public nsIEditor,
bool IsDescendantOfRoot(nsINode* inNode) const;
bool IsDescendantOfEditorRoot(nsINode* aNode) const;

/**
* Returns true if aNode is a container.
*/
virtual bool IsContainer(nsINode* aNode) const;

/**
* Counts number of editable child nodes.
*/
Expand Down
18 changes: 11 additions & 7 deletions editor/libeditor/HTMLEditSubActionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3587,7 +3587,7 @@ EditorDOMPoint HTMLEditor::GetGoodCaretPointFor(
// the non-editable content.

// If we can put caret in aContent, return start or end in it.
if (aContent.IsText() || IsContainer(&aContent) ||
if (aContent.IsText() || HTMLEditUtils::IsContainerNode(aContent) ||
NS_WARN_IF(!aContent.GetParentNode())) {
return EditorDOMPoint(&aContent, goingForward ? 0 : aContent.Length());
}
Expand Down Expand Up @@ -6616,7 +6616,9 @@ nsresult HTMLEditor::CreateStyleForInsertText(AbstractRange& aAbstractRange) {
}
pointToPutCaret = splitTextNodeResult.SplitPoint();
}
if (!IsContainer(pointToPutCaret.GetContainer())) {
if (!pointToPutCaret.IsInContentNode() ||
!HTMLEditUtils::IsContainerNode(
*pointToPutCaret.ContainerAsContent())) {
return NS_OK;
}
RefPtr<Text> newEmptyTextNode = CreateTextNode(EmptyString());
Expand Down Expand Up @@ -8350,8 +8352,8 @@ nsresult HTMLEditor::MaybeSplitElementsAtEveryBRElement(
case EditSubAction::eOutdent:
for (int32_t i = aArrayOfContents.Length() - 1; i >= 0; i--) {
OwningNonNull<nsIContent>& content = aArrayOfContents[i];
if (HTMLEditUtils::IsInlineElement(content) && IsContainer(content) &&
!content->IsText()) {
if (HTMLEditUtils::IsInlineElement(content) &&
HTMLEditUtils::IsContainerNode(content) && !content->IsText()) {
AutoTArray<OwningNonNull<nsIContent>, 24> arrayOfInlineContents;
// MOZ_KnownLive because 'aArrayOfContents' is guaranteed to keep it
// alive.
Expand Down Expand Up @@ -9015,7 +9017,7 @@ nsresult HTMLEditor::SplitParagraph(
// selection to beginning of right hand para;
// look inside any containers that are up front.
nsCOMPtr<nsIContent> child = GetLeftmostChild(&aParentDivOrP, true);
if (child && (child->IsText() || IsContainer(child))) {
if (child && (child->IsText() || HTMLEditUtils::IsContainerNode(*child))) {
nsresult rv = CollapseSelectionToStartOf(*child);
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return NS_ERROR_EDITOR_DESTROYED;
Expand Down Expand Up @@ -10134,7 +10136,8 @@ nsresult HTMLEditor::EnsureCaretInBlockElement(Element& aElement) {
lastEditableContent = &aElement;
}
EditorRawDOMPoint endPoint;
if (lastEditableContent->IsText() || IsContainer(lastEditableContent)) {
if (lastEditableContent->IsText() ||
HTMLEditUtils::IsContainerNode(*lastEditableContent)) {
endPoint.SetToEndOf(lastEditableContent);
} else {
endPoint.SetAfter(lastEditableContent);
Expand All @@ -10154,7 +10157,8 @@ nsresult HTMLEditor::EnsureCaretInBlockElement(Element& aElement) {
firstEditableContent = &aElement;
}
EditorRawDOMPoint atStartOfBlock;
if (firstEditableContent->IsText() || IsContainer(firstEditableContent)) {
if (firstEditableContent->IsText() ||
HTMLEditUtils::IsContainerNode(*firstEditableContent)) {
atStartOfBlock.Set(firstEditableContent);
} else {
atStartOfBlock.Set(firstEditableContent, 0);
Expand Down
8 changes: 4 additions & 4 deletions editor/libeditor/HTMLEditUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,11 @@ bool HTMLEditUtils::CanNodeContain(nsHTMLTag aParentTagId,
return !!(parent.mCanContainGroups & child.mGroup);
}

bool HTMLEditUtils::IsContainer(int32_t aTag) {
NS_ASSERTION(aTag > eHTMLTag_unknown && aTag <= eHTMLTag_userdefined,
"aTag out of range!");
bool HTMLEditUtils::IsContainerNode(nsHTMLTag aTagId) {
NS_ASSERTION(aTagId > eHTMLTag_unknown && aTagId <= eHTMLTag_userdefined,
"aTagId out of range!");

return kElements[aTag - 1].mIsContainer;
return kElements[aTagId - 1].mIsContainer;
}

bool HTMLEditUtils::IsNonListSingleLineContainer(nsINode& aNode) {
Expand Down
18 changes: 17 additions & 1 deletion editor/libeditor/HTMLEditUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class HTMLEditUtils final {
static bool IsMailCite(nsINode* aNode);
static bool IsFormWidget(nsINode* aNode);
static bool SupportsAlignAttr(nsINode& aNode);
static bool IsContainer(int32_t aTag);

static bool CanNodeContain(const nsINode& aParent, const nsIContent& aChild) {
switch (aParent.NodeType()) {
Expand Down Expand Up @@ -169,6 +168,22 @@ class HTMLEditUtils final {
return false;
}

/**
* IsContainerNode() returns true if aContent is a container node.
*/
static bool IsContainerNode(const nsIContent& aContent) {
nsHTMLTag tagEnum;
// XXX Should this handle #cdata-section too?
if (aContent.IsText()) {
tagEnum = eHTMLTag_text;
} else {
// XXX Why don't we use nsHTMLTags::AtomTagToId? Are there some
// difference?
tagEnum = nsHTMLTags::StringTagToId(aContent.NodeName());
}
return HTMLEditUtils::IsContainerNode(tagEnum);
}

/**
* See execCommand spec:
* https://w3c.github.io/editing/execCommand.html#non-list-single-line-container
Expand All @@ -187,6 +202,7 @@ class HTMLEditUtils final {

private:
static bool CanNodeContain(nsHTMLTag aParentTagId, nsHTMLTag aChildTagId);
static bool IsContainerNode(nsHTMLTag aTagId);
};

/**
Expand Down
29 changes: 10 additions & 19 deletions editor/libeditor/HTMLEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,9 @@ nsresult HTMLEditor::MaybeCollapseSelectionAtFirstEditableNode(
// block can not contain anything that's visible, such a block only
// makes sense if it is visible by itself, like a <hr>. We want to
// place the caret in front of that block.
if (!IsContainer(forwardScanFromPointToPutCaretResult.GetContent())) {
if (!forwardScanFromPointToPutCaretResult.GetContent() ||
!HTMLEditUtils::IsContainerNode(
*forwardScanFromPointToPutCaretResult.GetContent())) {
pointToPutCaret =
forwardScanFromPointToPutCaretResult.RawPointAtContent();
break;
Expand Down Expand Up @@ -810,7 +812,8 @@ NS_IMETHODIMP HTMLEditor::NodeIsBlock(nsINode* aNode, bool* aIsBlock) {
bool HTMLEditor::IsEmptyInlineNode(nsIContent& aContent) const {
MOZ_ASSERT(IsEditActionDataAvailable());

if (!HTMLEditUtils::IsInlineElement(aContent) || !IsContainer(&aContent)) {
if (!HTMLEditUtils::IsInlineElement(aContent) ||
!HTMLEditUtils::IsContainerNode(aContent)) {
return false;
}
return IsEmptyNode(aContent);
Expand Down Expand Up @@ -1224,10 +1227,11 @@ void HTMLEditor::CollapseSelectionToDeepestNonTableFirstChild(nsINode* aNode) {

nsCOMPtr<nsINode> node = aNode;

for (nsCOMPtr<nsIContent> child = node->GetFirstChild(); child;
for (nsIContent* child = node->GetFirstChild(); child;
child = child->GetFirstChild()) {
// Stop if we find a table, don't want to go into nested tables
if (HTMLEditUtils::IsTable(child) || !IsContainer(child)) {
if (HTMLEditUtils::IsTable(child) ||
!HTMLEditUtils::IsContainerNode(*child)) {
break;
}
node = child;
Expand Down Expand Up @@ -3763,20 +3767,6 @@ MOZ_CAN_RUN_SCRIPT_BOUNDARY void HTMLEditor::ContentRemoved(
}
}

bool HTMLEditor::IsContainer(nsINode* aNode) const {
MOZ_ASSERT(aNode);

int32_t tagEnum;
// XXX Should this handle #cdata-section too?
if (aNode->IsText()) {
tagEnum = eHTMLTag_text;
} else {
tagEnum = nsHTMLTags::StringTagToId(aNode->NodeName());
}

return HTMLEditUtils::IsContainer(tagEnum);
}

nsresult HTMLEditor::SelectEntireDocument() {
MOZ_ASSERT(IsEditActionDataAvailable());

Expand Down Expand Up @@ -4344,7 +4334,8 @@ bool HTMLEditor::IsEmptyNodeImpl(nsINode& aNode, bool aSingleBRDoesntCount,
// anchors are containers, named anchors are "empty" but we don't
// want to treat them as such. Also, don't call ListItems or table
// cells empty if caller desires. Form Widgets not empty.
if (!IsContainer(&aNode) ||
if (!aNode.IsContent() ||
!HTMLEditUtils::IsContainerNode(*aNode.AsContent()) ||
(HTMLEditUtils::IsNamedAnchor(&aNode) ||
HTMLEditUtils::IsFormWidget(&aNode) ||
(aListOrCellNotEmpty && (HTMLEditUtils::IsListItem(&aNode) ||
Expand Down
5 changes: 0 additions & 5 deletions editor/libeditor/HTMLEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -879,11 +879,6 @@ class HTMLEditor final : public TextEditor,
[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult RelativeChangeElementZIndex(
Element& aElement, int32_t aChange, int32_t* aReturn);

/**
* Returns true if aNode is a container.
*/
virtual bool IsContainer(nsINode* aNode) const override;

/**
* Join together any adjacent editable text nodes in the range.
*/
Expand Down
2 changes: 1 addition & 1 deletion editor/libeditor/HTMLEditorDataTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ nsresult HTMLEditor::DoInsertHTMLWithContext(
// If the container is a text node or a container element except `<table>`
// element, put caret a end of it.
if (containerContent->IsText() ||
(IsContainer(containerContent) &&
(HTMLEditUtils::IsContainerNode(*containerContent) &&
!HTMLEditUtils::IsTable(containerContent))) {
pointToPutCaret.SetToEndOf(containerContent);
}
Expand Down
4 changes: 3 additions & 1 deletion editor/libeditor/HTMLStyleEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,9 @@ EditResult HTMLEditor::ClearStyleAt(const EditorDOMPoint& aPoint,
RefPtr<HTMLBRElement> brElement;
// But don't try to split non-containers like `<br>`, `<hr>` and `<img>`
// element.
if (!IsContainer(atStartOfNextNode.GetContainer())) {
if (!atStartOfNextNode.IsInContentNode() ||
!HTMLEditUtils::IsContainerNode(
*atStartOfNextNode.ContainerAsContent())) {
// If it's a `<br>` element, let's move it into new node later.
brElement = HTMLBRElement::FromNode(atStartOfNextNode.GetContainer());
if (!atStartOfNextNode.GetContainerParentAsContent()) {
Expand Down
Loading

0 comments on commit ec6cd42

Please sign in to comment.