w3c / w3c/editing

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

Open
#522 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
202
Forks
43
PR merge metrics
No merged PRs in 30d

Description

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>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the insertText command algorithm linked in the issue, focusing on steps 6 and 7 and the handling of the selection's start container. Use the supplied contenteditable test case to verify insertion inside a br moves to its parent, and confirm the resulting editor HTML contains the inserted text in the expected location.

Written by the indexing model from the issue text.

Assessment

Tech stack
html
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.