[w3c/editing] Preserving the integrity of the contentEditable's hidden state (#223)

**Context**
Empirical observation of the behavior of different browsers and platforms reveals a hidden state associated with contentEditable elements. A simple example is the following:

1. Insert a word such as "erase" by swiping on the keyboard on Android or iOS
1. Hit backspace
1. Reinsert the word "erase" by swiping again
1. Insert a space
1. Hit backspace twice

_Observed behavior:_ Hitting backspace right after a swipe insert erases the whole word whereas hitting backspace at another time only erases a single character.

**The problem**
This hidden state is not preserved when the DOM is being modified programmatically. This means that programmatically modifying the contents of a contentEditable at any time may lead to inconsistencies that cannot be prevented of remediated.
Here is an example:

1. Insert "erase" by swiping on the keyboard
1. In the inspector, apply the following steps using the console
1. `$range = new Range()`
1. `$range.selectNode(document.querySelector("[contentEditable]")`
1. `$node = document.createElement("b")`
1. `$range.surroundContents($node)`
1. Fix the selection's position by applying the following steps in the console
1. `document.getSelection().setBaseAndExtent($node, 1, $node, 1)`
1. Hit backspace on the device's keyboard

_Observed behavior:_ A single character is erased.
_Expected behavior:_ A whole word is erased.

**Why is this a problem?**
There are various use cases for which programmatic modification of the contents is required but unavoidably leads to interference with the user's ability to edit the contents in an intuitive way. Here are two important use cases:

1. Range-based metadata: applying style on ranges of text to convey metadata and enable productivity features
1. Collaborative editing: reproducing one user's changes on the other connected editors

**What is the way forward?**

I am not sure which is the best way forward at this point. I am bringing up those issues so that they can be discussed.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/editing/issues/223

Received on Monday, 2 December 2019 21:28:02 UTC