Bug 1606232 - Make `nsIHTMLEditor::GetSelectedElement()` check whether the found element is followed by `<br>` element deeper r=m_kato

Current `nsIHTMLEditor::GetSelectedElement()` just checks whether the
found element is followed by `<br>` element or not.  However, following
element may start with `<br>` element and if there is end of the range,
`PostContentIterator` does not list up the `<br>` element.

Therefore, it should check whether the found element is followed by
`<br>` element which is starts the next sibling element too.

Differential Revision: https://phabricator.services.mozilla.com/D60385

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2020-01-21 08:16:03 +00:00
Родитель 6975d37d43
Коммит 6ea35a12b9
2 изменённых файлов: 35 добавлений и 3 удалений

Просмотреть файл

@ -2605,9 +2605,20 @@ already_AddRefed<Element> HTMLEditor::GetSelectedElement(const nsAtom* aTagName,
// - <p><b>[def</b>}<br></p>
// Note that we don't need special handling for <a href> 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:
// - <p><b>[def</b><b>}<br></b></p>
// In these cases, the <br> element is not listed up by PostContentIterator.
// So, we should return nullptr if next sibling is a `<br>` element or
// next sibling starts with `<br>` 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) {

Просмотреть файл

@ -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 <b> element and ends before the following <br> element");
editor.innerHTML = "<p><b>b1</b><b><br></b><b>b2</b></p>";
editor.focus();
// <p><b>[b1</b><b>}<br></b><b>b2</b></p>
// This is usual case of double-clicking in the first <b> 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 <b> element and ends before the following <br> element in another <b> element");
is(SpecialPowers.unwrap(getHTMLEditor().getSelectedElement("href")),
null,
"#10-1 nsIHTMLEditor::getSelectedElement(\"href\") should return null when Selection starts from a text node in <b> element and ends before the following <br> element in another <b> element");
is(SpecialPowers.unwrap(getHTMLEditor().getSelectedElement("anchor")),
null,
"#10-1 nsIHTMLEditor::getSelectedElement(\"anchor\") should return null when Selection starts from a text node in <b> element and ends before the following <br> element in another <b> element");
SimpleTest.finish();
});