Skip to content

insertText should move to parent node if range's start container is a void element #522

@gmta

Description

@gmta

When inserting text, we should not use void elements (such as <br>) as the target for insertion but rather move up the tree to its parent. I suspect we need to add a step between step 6 and 7 of the inserttext command stating that if node is a void element, set offset to node's index and node to node's parent.

Test case:

<!DOCTYPE html>
<div id="editor" contenteditable>
  <div><br></div>
</div>
<button onclick="test()">Insert "hello" at selection inside &lt;br&gt;</button>
<pre id="result"></pre>
<script>
function test() {
    const br = editor.querySelector('br');
    getSelection().collapse(br, 0);  // collapse selection inside the <br>
    document.execCommand('insertText', false, 'hello');
    result.textContent = 'Result: ' + editor.innerHTML;
}
</script>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions