Bug 1716863 - Make HTMLEditor::HandleInsertText() climb up the tree when Selection is in elements which cannot have text nodes r=m_kato

In most cases, it's called with selection range which is collapsed in a text
node, but otherwise, the selection may be in an element which cannot have
text nodes.  Therefore, before handling the insertion, it should look for
ancestor element which can have text nodes.

Note that this patch makes inserting text immediately before an inclusive
ancestor element whose parent can have a text node.  However, both Blink and
WebKit ignores if there are invisible/empty inline nodes.  So, even with
this patch, Gecko keeps failing in some tests of the WPT.  It should be handled
in a follow up bug because doing it requires complicated code.

Differential Revision: https://phabricator.services.mozilla.com/D119065
This commit is contained in:
Masayuki Nakano 2021-07-06 04:55:28 +00:00
parent d4cc61bf6a
commit 5dd777bdc1
3 changed files with 17 additions and 85 deletions

View File

@ -1011,12 +1011,22 @@ EditActionResult HTMLEditor::HandleInsertText(
}
MOZ_ASSERT(pointToInsert.IsSetAndValid());
// dont put text in places that can't have it
if (!pointToInsert.IsInTextNode() &&
!HTMLEditUtils::CanNodeContain(*pointToInsert.GetContainer(),
*nsGkAtoms::textTagName)) {
NS_WARNING("Selection start container couldn't have text nodes");
return EditActionHandled(NS_ERROR_FAILURE);
// If the point is not in an element which can contain text nodes, climb up
// the DOM tree.
if (!pointToInsert.IsInTextNode()) {
Element* editingHost = GetActiveEditingHost();
if (NS_WARN_IF(!editingHost)) {
return EditActionHandled(NS_ERROR_EDITOR_UNEXPECTED_DOM_TREE);
}
while (!HTMLEditUtils::CanNodeContain(*pointToInsert.GetContainer(),
*nsGkAtoms::textTagName)) {
if (NS_WARN_IF(pointToInsert.GetContainer() == editingHost) ||
NS_WARN_IF(!pointToInsert.GetContainerParentAsContent())) {
NS_WARNING("Selection start point couldn't have text nodes");
return EditActionHandled(NS_ERROR_FAILURE);
}
pointToInsert.Set(pointToInsert.ContainerAsContent());
}
}
if (aEditSubAction == EditSubAction::eInsertTextComingFromIME) {

View File

@ -116,7 +116,7 @@ pref(dom.document.exec_command.nested_calls_allowed,true) asserts(2) load 147497
asserts(1) load 1517028.html
load 1525481.html
load 1533913.html
asserts(2) load 1534394.html # assertion in WSRunScanner::GetEditableBlockParentOrTopmostEditableInlineContent()
asserts(1) load 1534394.html # assertion in WSRunScanner::GetEditableBlockParentOrTopmostEditableInlineContent()
load 1547897.html
load 1547898.html
load 1556799.html

View File

@ -1,16 +1,4 @@
[insert-text-in-void-element.tentative.html]
[Inserting text when selection is collapsed in <br> which is only child]
expected: FAIL
[Inserting text when selection is collapsed in <br> which follows a text node]
expected: FAIL
[Inserting text when selection is collapsed in <br> which is followed by a text node]
expected: FAIL
[Inserting text when selection is collapsed in <br> which follows an empty <span> element]
expected: FAIL
[Inserting text when selection is collapsed in <br> which follows a text node and an empty <span> element]
expected: FAIL
@ -20,15 +8,6 @@
[Inserting text when selection is collapsed in <br> which follows a text node, an empty <span> element and white-space only text node]
expected: FAIL
[Inserting text when selection is collapsed in <embed> which is only child]
expected: FAIL
[Inserting text when selection is collapsed in <embed> which follows a text node]
expected: FAIL
[Inserting text when selection is collapsed in <embed> which is followed by a text node]
expected: FAIL
[Inserting text when selection is collapsed in <embed> which follows an empty <span> element]
expected: FAIL
@ -41,36 +20,6 @@
[Inserting text when selection is collapsed in <embed> which follows a text node, an empty <span> element and white-space only text node]
expected: FAIL
[Inserting text when selection is collapsed in <hr> which is only child]
expected: FAIL
[Inserting text when selection is collapsed in <hr> which follows a text node]
expected: FAIL
[Inserting text when selection is collapsed in <hr> which is followed by a text node]
expected: FAIL
[Inserting text when selection is collapsed in <hr> which follows an empty <span> element]
expected: FAIL
[Inserting text when selection is collapsed in <hr> which follows a text node and an empty <span> element]
expected: FAIL
[Inserting text when selection is collapsed in <hr> which follows a non-empty <span> element]
expected: FAIL
[Inserting text when selection is collapsed in <hr> which follows a text node, an empty <span> element and white-space only text node]
expected: FAIL
[Inserting text when selection is collapsed in <img> which is only child]
expected: FAIL
[Inserting text when selection is collapsed in <img> which follows a text node]
expected: FAIL
[Inserting text when selection is collapsed in <img> which is followed by a text node]
expected: FAIL
[Inserting text when selection is collapsed in <img> which follows an empty <span> element]
expected: FAIL
@ -80,39 +29,12 @@
[Inserting text when selection is collapsed in <img> which follows a non-empty <span> element]
expected: FAIL
[Inserting text when selection is collapsed in <img> which follows a text node, an empty <span> element and white-space only text node]
expected: FAIL
[Inserting text when selection is collapsed in <input> which is only child]
expected: FAIL
[Inserting text when selection is collapsed in <input> which follows a text node]
expected: FAIL
[Inserting text when selection is collapsed in <input> which is followed by a text node]
expected: FAIL
[Inserting text when selection is collapsed in <input> which follows an empty <span> element]
expected: FAIL
[Inserting text when selection is collapsed in <input> which follows a text node and an empty <span> element]
expected: FAIL
[Inserting text when selection is collapsed in <input> which follows a non-empty <span> element]
expected: FAIL
[Inserting text when selection is collapsed in <input> which follows a text node, an empty <span> element and white-space only text node]
expected: FAIL
[Inserting text when selection is collapsed in <wbr> which is only child]
expected: FAIL
[Inserting text when selection is collapsed in <wbr> which follows a text node]
expected: FAIL
[Inserting text when selection is collapsed in <wbr> which is followed by a text node]
expected: FAIL
[Inserting text when selection is collapsed in <wbr> which follows an empty <span> element]
expected: FAIL