diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp
index e7f0b2dcc250..4b57ea4e8100 100644
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -2605,9 +2605,20 @@ already_AddRefed HTMLEditor::GetSelectedElement(const nsAtom* aTagName,
// - [def}
// Note that we don't need special handling for because double
// clicking it selects the element and we use the first path to handle it.
- if (lastElementInRange->GetNextSibling() &&
- lastElementInRange->GetNextSibling()->IsHTMLElement(nsGkAtoms::br)) {
- return nullptr;
+ // Additionally, we have this case too:
+ // - [def}
+ // In these cases, the
element is not listed up by PostContentIterator.
+ // So, we should return nullptr if next sibling is a `
` element or
+ // next sibling starts with `
` element.
+ if (nsIContent* nextSibling = lastElementInRange->GetNextSibling()) {
+ if (nextSibling->IsHTMLElement(nsGkAtoms::br)) {
+ return nullptr;
+ }
+ nsIContent* firstEditableLeaf = GetLeftmostChild(nextSibling);
+ if (firstEditableLeaf &&
+ firstEditableLeaf->IsHTMLElement(nsGkAtoms::br)) {
+ return nullptr;
+ }
}
if (!aTagName) {
diff --git a/editor/libeditor/tests/test_nsIHTMLEditor_getSelectedElement.html b/editor/libeditor/tests/test_nsIHTMLEditor_getSelectedElement.html
index 99dd9ca9f25f..255f5b7a7c5e 100644
--- a/editor/libeditor/tests/test_nsIHTMLEditor_getSelectedElement.html
+++ b/editor/libeditor/tests/test_nsIHTMLEditor_getSelectedElement.html
@@ -779,6 +779,27 @@ SimpleTest.waitForFocus(async function() {
null,
"#9-1 nsIHTMLEditor::getSelectedElement(\"anchor\") should return null when Selection starts from a text node in element and ends before the following
element");
+ editor.innerHTML = "b1
b2
";
+ editor.focus();
+
+ // [b1}
b2
+ // This is usual case of double-clicking in the first element.
+ range = document.createRange();
+ range.setStart(editor.firstChild.firstChild.firstChild, 0);
+ range.setEnd(editor.firstChild.firstChild.nextSibling, 0);
+ selection.removeAllRanges();
+ selection.addRange(range);
+
+ is(SpecialPowers.unwrap(getHTMLEditor().getSelectedElement("")),
+ null,
+ "#10-1 nsIHTMLEditor::getSelectedElement(\"\") should return null when Selection starts from a text node in element and ends before the following
element in another element");
+ is(SpecialPowers.unwrap(getHTMLEditor().getSelectedElement("href")),
+ null,
+ "#10-1 nsIHTMLEditor::getSelectedElement(\"href\") should return null when Selection starts from a text node in element and ends before the following
element in another element");
+ is(SpecialPowers.unwrap(getHTMLEditor().getSelectedElement("anchor")),
+ null,
+ "#10-1 nsIHTMLEditor::getSelectedElement(\"anchor\") should return null when Selection starts from a text node in element and ends before the following
element in another element");
+
SimpleTest.finish();
});