Bug 1770877 - part 31-3: Replace insertLineBreak variable in HTMLEditor::InsertParagraphSeparatorAsSubAction with a lambda r=m_kato

This can encapsule the long if-elseif-else blocks into the lambda.

Differential Revision: https://phabricator.services.mozilla.com/D149102
This commit is contained in:
Masayuki Nakano
2022-06-22 02:54:34 +00:00
parent 341b28f4a6
commit d3f84bd498
3 changed files with 56 additions and 46 deletions

View File

@@ -1616,6 +1616,52 @@ EditActionResult HTMLEditor::InsertParagraphSeparatorAsSubAction(
return EditActionHandled(NS_ERROR_EDITOR_NO_EDITABLE_RANGE);
}
auto InsertLineBreakInstead =
[](const Element* aEditableBlockElement,
const EditorDOMPoint& aCandidatePointToSplit,
ParagraphSeparator aDefaultParagraphSeparator,
const Element& aEditingHost) {
// If there is no block parent in the editing host, i.e., the editing
// host itself is also a non-block element, we should insert a line
// break.
if (!aEditableBlockElement) {
// XXX Chromium checks if the CSS box of the editing host is a block.
return true;
}
// If the editable block element is not splittable, e.g., it's an
// editing host, and the default paragraph separator is <br> or the
// element cannot contain a <p> element, we should insert a <br>
// element.
if (!HTMLEditUtils::IsSplittableNode(*aEditableBlockElement)) {
return aDefaultParagraphSeparator == ParagraphSeparator::br ||
!HTMLEditUtils::CanElementContainParagraph(aEditingHost) ||
HTMLEditUtils::ShouldInsertLinefeedCharacter(
aCandidatePointToSplit, aEditingHost);
}
// If the nearest block parent is a single-line container declared in
// the execCommand spec and not the editing host, we should separate the
// block even if the default paragraph separator is <br> element.
if (HTMLEditUtils::IsSingleLineContainer(*aEditableBlockElement)) {
return false;
}
// Otherwise, unless there is no block ancestor which can contain <p>
// element, we shouldn't insert a line break here.
for (const Element* editableBlockAncestor = aEditableBlockElement;
editableBlockAncestor;
editableBlockAncestor = HTMLEditUtils::GetAncestorElement(
*editableBlockAncestor,
HTMLEditUtils::ClosestEditableBlockElement)) {
if (HTMLEditUtils::CanElementContainParagraph(
*editableBlockAncestor)) {
return false;
}
}
return true;
};
// Look for the nearest parent block. However, don't return error even if
// there is no block parent here because in such case, i.e., editing host
// is an inline element, we should insert <br> simply.
@@ -1626,47 +1672,11 @@ EditActionResult HTMLEditor::InsertParagraphSeparatorAsSubAction(
HTMLEditUtils::ClosestEditableBlockElement)
: nullptr;
ParagraphSeparator separator = GetDefaultParagraphSeparator();
bool insertLineBreak;
// If there is no block parent in the editing host, i.e., the editing host
// itself is also a non-block element, we should insert a line break.
if (!editableBlockElement) {
// XXX Chromium checks if the CSS box of the editing host is a block.
insertLineBreak = true;
}
// If the editable block element is not splittable, e.g., it's an editing
// host, and the default paragraph separator is <br> or the element cannot
// contain a <p> element, we should insert a <br> element.
else if (!HTMLEditUtils::IsSplittableNode(*editableBlockElement)) {
insertLineBreak =
separator == ParagraphSeparator::br ||
!HTMLEditUtils::CanElementContainParagraph(aEditingHost) ||
HTMLEditUtils::ShouldInsertLinefeedCharacter(atStartOfSelection,
aEditingHost);
}
// If the nearest block parent is a single-line container declared in
// the execCommand spec and not the editing host, we should separate the
// block even if the default paragraph separator is <br> element.
else if (HTMLEditUtils::IsSingleLineContainer(*editableBlockElement)) {
insertLineBreak = false;
}
// Otherwise, unless there is no block ancestor which can contain <p>
// element, we shouldn't insert a line break here.
else {
insertLineBreak = true;
for (const Element* editableBlockAncestor = editableBlockElement;
editableBlockAncestor && insertLineBreak;
editableBlockAncestor = HTMLEditUtils::GetAncestorElement(
*editableBlockAncestor,
HTMLEditUtils::ClosestEditableBlockElement)) {
insertLineBreak =
!HTMLEditUtils::CanElementContainParagraph(*editableBlockAncestor);
}
}
// If we cannot insert a <p>/<div> element at the selection, we should insert
// a <br> element or a linefeed instead.
if (insertLineBreak) {
const ParagraphSeparator separator = GetDefaultParagraphSeparator();
if (InsertLineBreakInstead(editableBlockElement, atStartOfSelection,
separator, aEditingHost)) {
// For backward compatibility, we should not insert a linefeed if
// paragraph separator is set to "br" which is Gecko-specific mode.
if (separator != ParagraphSeparator::br &&

View File

@@ -676,7 +676,7 @@ bool HTMLEditUtils::IsEmptyNode(nsPresContext* aPresContext,
}
bool HTMLEditUtils::ShouldInsertLinefeedCharacter(
EditorDOMPoint& aPointToInsert, const Element& aEditingHost) {
const EditorDOMPoint& aPointToInsert, const Element& aEditingHost) {
MOZ_ASSERT(aPointToInsert.IsSetAndValid());
if (!aPointToInsert.IsInContentNode()) {
@@ -1043,14 +1043,14 @@ bool HTMLEditUtils::IsContainerNode(nsHTMLTag aTagId) {
return kElements[aTagId - 1].mIsContainer;
}
bool HTMLEditUtils::IsNonListSingleLineContainer(nsINode& aNode) {
bool HTMLEditUtils::IsNonListSingleLineContainer(const nsINode& aNode) {
return aNode.IsAnyOfHTMLElements(
nsGkAtoms::address, nsGkAtoms::div, nsGkAtoms::h1, nsGkAtoms::h2,
nsGkAtoms::h3, nsGkAtoms::h4, nsGkAtoms::h5, nsGkAtoms::h6,
nsGkAtoms::listing, nsGkAtoms::p, nsGkAtoms::pre, nsGkAtoms::xmp);
}
bool HTMLEditUtils::IsSingleLineContainer(nsINode& aNode) {
bool HTMLEditUtils::IsSingleLineContainer(const nsINode& aNode) {
return IsNonListSingleLineContainer(aNode) ||
aNode.IsAnyOfHTMLElements(nsGkAtoms::li, nsGkAtoms::dt, nsGkAtoms::dd);
}

View File

@@ -311,8 +311,8 @@ class HTMLEditUtils final {
* https://w3c.github.io/editing/execCommand.html#non-list-single-line-container
* https://w3c.github.io/editing/execCommand.html#single-line-container
*/
static bool IsNonListSingleLineContainer(nsINode& aNode);
static bool IsSingleLineContainer(nsINode& aNode);
static bool IsNonListSingleLineContainer(const nsINode& aNode);
static bool IsSingleLineContainer(const nsINode& aNode);
/**
* IsVisibleTextNode() returns true if aText has visible text. If it has
@@ -421,8 +421,8 @@ class HTMLEditUtils final {
* ShouldInsertLinefeedCharacter() returns true if the caller should insert
* a linefeed character instead of <br> element.
*/
static bool ShouldInsertLinefeedCharacter(EditorDOMPoint& aPointToInsert,
const Element& aEditingHost);
static bool ShouldInsertLinefeedCharacter(
const EditorDOMPoint& aPointToInsert, const Element& aEditingHost);
/**
* IsEmptyNode() returns false if aNode has some visible content nodes,