{{EditContext}} is an API that allows authors to more directly participate in the text input process.
Introduction Background and MotivationModern operating systems provide mechanisms to produce text in a variety of ways: speech-to-text, virtual keyboards, handwriting recognition and many more. When an app wants to consume text input from these various sources, it must first provide a view of its currently editable text to the operating system. The view of editable text provides a common language that apps (having a variety of different document models) and sources of text (having a variety of different input methods) can both understand. Both the apps and input sources communicate with one another by expressing their desired changes to the state of the common view as an event that the other can handle to facilitate the text input process.
For the purposes of this document, a producer of text is known as a Text Input Method. The view provided by an app which wants to consume text is called a Text Edit Context. The service provided by the OS to facilitate the editing of text in the [=Text Edit Context=] by the [=Text Input Methods=] is called a Text Input Service.
Many [=Text Input Methods=] using a [=Text Input Service=] to communicate with many apps through their [=Text Edit Contexts=].Here’s a typical flow for the text input process in more detail:
Existing user agents handle the details of this text input process so that the author’s responsibility ends at declaring what elements of the document represent an editable region. Authors express which regions are editable using input elements, textarea elements, contenteditable elements, or by setting the {{Document/designMode}} attribute to "on
" to mark an entire document as editable.
As an editable region of the document is focused, the user agent automatically produces the [=Text Edit Context=] from the contents of the editable region and the position of the selection within it. When a [=Text Input Method=] produces text, the user agent translates the events against its [=Text Edit Context=] into a set of DOM and style modifications – only some of which are described using existing events that an author can handle.
Authors that want to produce sophisticated editing experiences may be challenged by the current approach. If, for example, the text and selection are rendered to a canvas, user agents are unable to produce a [=Text Edit Context=] to drive the text input process. Authors compensate by resorting to offscreen editable elements, but this approach comes with negative implications for accessibility, it deteriorates the input experience, and requires complex code to synchronize the position of the text in the offscreen editable element with the corresponding text in the canvas.
With the introduction of this EditContext API, authors can more directly participate in the protocol for text input and avoid the pitfalls described above.
The EditContext ModelThe [=Text Input Service=] and [=Text Edit Context=] are abstractions representing the common aspects of text input across many operating systems. An {{EditContext}} is a JavaScript reflection of the [=Text Edit Context=]. When changes are made to the [=Text Edit Context=] by the [=Text Input Service=], those changes are reflected to the author asynchronously in the form of events which are dispatched against the [=active EditContext=]. When the author makes changes to the [=active EditContext=], those changes will be reflected in the [=Text Edit Context=] during the next lifecycle update.
EditContext stateBoth the [=Text Edit Context=] and {{EditContext}} have a [=text state=] which holds the information exchanged in the aforementioned updates. The text state consists of:
text format is a struct that indicates decorative properties that should be applied to the ranges of [=text=]. The struct contains:
[=Codepoint rects=] provides the means for the user agent to query a range of [=text=] for positioning information. The [=Text Input Service=] will use this information, in tandem with the [=control bounds=] and [=selection bounds=], to support the Text Input Method in properly displaying its user interface. For example, the info can be used to position an IME window adjacent to text being composed. Different platforms may require different positions to be cached to fulfill queries from the [=Text Input Service=]. The user agent will indicate which positions are required by firing {{CharacterBoundsUpdateEvent}}.
[=Control bounds=], [=selection bounds=], and [=codepoint rects=] are given in the client coordinate system, which is defined as a two-dimensional Cartesian coordinate system (x, y) where the origin is the top-left corner of the layout viewport, the x-axis points towards the top-right of the layout viewport, and the y-axis points towards the bottom-left of the layout viewport. The units of the client coordinate system are CSS pixels.
Since EditContext bounds are defined in client coordinates, the coordinates indicating a given piece of content on a page will change as the user scrolls the document even if the content itself does not change position in the document. A scenario where authors may want to take this into account is the case where the user scrolls the page where the user has an active composition. If the author does not update the EditContext's bounds information (e.g. during a scroll event listener), the IME window may no longer line up with the text being composed for the duration of the composition.
However, some platforms do not adjust IME windows during an active composition, so updating bounds information mid-composition does not guarantee that the IME window will be repositioned until it's closed and reopened.
Association and activationAn {{EditContext}} has an associated element, an {{HTMLElement}}. An element becomes an {{EditContext}}'s associated element by assigning the {{EditContext}} to the element's {{HTMLElement/editContext}} property. An {{HTMLElement}} can be associated with at most one {{EditContext}}.
An {{EditContext}} keeps its [=associated element=] alive, so developers should be aware that assigning an {{EditContext}} to an element's {{HTMLElement/editContext}} property will prevent the element from being garbage collected until the property is cleared or the {{EditContext}} is garbage collected.
If an {{EditContext}}'s associated element's parent is not editable and is not a [=Document=] whose {{Document/designMode}} attribute is "on
", then the associated element becomes an EditContext editing host. An EditContext editing host is a type of [=editing host=] whose behaviors are described in [[[#edit-context-differences]]].
There are a couple implications of this. Firstly, if an element that is already an [=editing host=] due to [^html-global/contenteditable^] becomes an {{EditContext}}'s associated element, then that element becomes an EditContext editing host. In other words, if both {{EditContext}} and [^html-global/contenteditable^] are set on an element, the EditContext behavior "wins".
Secondly, if an element is editable but not an [=editing host=] (i.e. it is a child in the subtree of an [=editing host=]), then becoming an {{EditContext}}'s associated element has no effect on that element. This is analogous to the behavior of [^html-global/contenteditable^], where setting [^html-global/contenteditable^] to "true
" on an editable element that is not an [=editing host=] has no effect. Taken together, these rules imply that an editable tree of nodes will follow either the {{EditContext}} behavior or non-{{EditContext}} behavior, but the behaviors cannot be mixed.
A {{Document}} has an active EditContext, which may be null.
The following paragraph can be removed once the behavior change lands in [[input-events]].
When an [=EditContext editing host=] receives text input from the [=Text Input Service=], as the default action for the beforeinput event fired as a result of that input the user agent must run [=Handle Input for EditContext=] given the [=EditContext editing host=] .
Differences for an EditContext editing hostIn many ways, an EditContext editing host behaves in the same way as other types of [=editing host=], e.g. for a [^html-global/contenteditable^] element. Notable similarities include:
false
".There are also some ways that an EditContext editing host differs from other types of [=editing hosts=]:
The user agent fires several types of events against the {{EditContext}} in order to inform the author when they must update the state of the DOM in response to changes from the [=Text Input Service=], or respond to a query from the [=Text Input Service=]. Since the timings of [=Text Input Service=]s are platform-specific, authors should avoid taking dependencies on the timing of these events.
The user agent must fire {{CharacterBoundsUpdateEvent}} when the [=Text Input Service=] indicates that it requires character bounds information to support the [=Text Input Method=] in properly displaying its user interface. After receiving {{CharacterBoundsUpdateEvent}}, the author must compute the requested character bounds and call {{EditContext/updateCharacterBounds}} to update the character bounds in the EditContext's [=text state=]. The author should perform the {{EditContext/updateCharacterBounds}} call synchronously within the {{CharacterBoundsUpdateEvent}} event handler if possible; if not, it is permissible to call it asynchronously. Upon receiving {{EditContext/updateCharacterBounds}}, the user agent must pass the character bounds information on to the [=Text Input Service=].
The longer the author delays the {{EditContext/updateCharacterBounds}} call, the higher the likelihood that the user will observe a visual stutter as the IME window repositions itself in the middle of a composition.
A new step will be introduced as a substep within the [=Update the rendering=] step in the HTML Event Loops Processing Model, immediately following step 15 (which runs the focusing steps for {{Document}}s whose focused areas become non-focusable). The step is: For each fully active {{Document}} |doc|, [=queue a global task=] on the [=DOM manipulation task source=] given |doc|'s [=relevant global object=] to run the [=Update the Text Edit Context=] steps given |doc|.
ExamplesUsing an {{EditContext}}, an author can mark a region of the document editable by associating an {{EditContext}} object with an element as shown in the example below:
<script type="module"> let canvas = document.querySelector("canvas") canvas.editContext = new EditContext() // When the associated element is focused, the EditContext is automatically activated. canvas.focus(); </script> <canvas></canvas>
In the example below, the author is using a canvas to draw an editable region that allows the user to input a single line of text rendered with a monospace font. The text for the editable region is maintained by the author as a String. The text offsets for the selection in the editable region are maintained by the author as a pair of Numbers: selectionStart and selectionEnd. The Numbers refer to the count of the number of UTF-16 codepoints to the left of the start and end of the selection respectively. For the sake of communicating the bounding boxes for the current selection and the editable region of the document to Text Input Services, the author also computes the bounding rectangle in CSS pixels for the selection and the editable region of the document. The offset of the rectangle is expressed relative to the origin of the canvas element since that is the element to which the author has associated an EditContext. Since the model for the author’s representation of text and selection location matches the form expected by the EditContext API, the author can simply assign those properties to the EditContext associated with the canvas whenever those values change.
<script type="module"> // This example is built on top of example 1. // Only the added logic is shown here for brevity. class EditingModel { constructor(text, selectionStart, selectionEnd) { this.text = text this.selectionStart = selectionStart this.selectionEnd = selectionEnd } } class EditingView { constructor(canvas, model) { this.canvas = canvas this.model = model } render() { // render the text let canvasContext2D = this.canvas.getContext("2d"); canvasContext2D.strokeText(this.model.text); // render the selection (implementation omitted for brevity) } computeSelectionBound() { // implementation omitted for brevity } computeControlBound() { // implementation omitted for brevity } } class EditingController { constructor(model, view, editContext) { this.view = view this.model = model this.editContext = editContext } render() { view.render() // keep EditContext in sync, which essentially means // rendering the plain text view of the model. let editContext = this.canvas.editContext this.editContext.updateText(0, this.model.text.length, this.model.text) this.editContext.updateSelection(this.model.selectionStart, this.model.selectionEnd) let selectionBounds = view.computeSelectionBound() let controlBounds = view.computeControlBound() // These bounds have x, y, width and height members // so they can be assigned directly to EditContext. editContext.updateSelectionBounds(selectionBounds) editContext.updateControlBounds(controlBounds) } } let canvas = document.querySelector("canvas") canvas.editContext = new EditContext() let editingModel = new EditingModel("", 0, 0, 0, 0) let editingView = new EditingView(canvas, editingModel) let editingController = new EditingController(editingModel, editingView, canvas.editContext); editingController.render() </script> <canvas></canvas>
Building on the previous example, in response to user input, authors should handle the events of both the editable element (in this case a canvas) and the EditContext.
Input events against the DOM continue to describe the user’s intent
The below example shows how to handle {{TextUpdateEvent}}, {{TextFormatUpdateEvent}}, and {{CharacterBoundsUpdateEvent}} to update the model and render the result to the canvas.
<script> // This example is built on top of example 1 and example 2. // Only the added logic is shown here for brevity. class EditingModel { updateText(updateRangeStart, updateRangeEnd, updateText) { this.text = this.text.substring(0, updateRangeStart) + updateText + this.text.substring(updateRangeEnd) } updateSelection(selectionStart, selectionEnd) { this.selectionStart = selectionStart this.selectionEnd = selectionEnd } updateTextFormats(textFormats) { this.textFormats = textFormats } } class EditingView { render() { // render the text (implementation omitted for brevity) // render the selection (implementation omitted for brevity) // render the IME decoration this.model.textFormats.forEach( textFormat => { // For brevity, these variables' initialization are omitted: // anchorX: the x coordinate of the beginning of the string // lineY: the y coordinate of the underline // charWidth: the width of the character (here we are using a mono-spaced font) // thickWidth/thinWidth: pre-defined width for thick/thin lines. let lineStartX = anchorX + textFormat.rangeStart * charWidth; let lineEndX = anchorX + textFormat.rangeEnd * charWidth; canvasContext2D.lineWidth = (textFormat.underlineThickness == 'Thick')? thickWidth : thinWidth; canvasContext2D.beginPath(); canvasContext2D.moveTo(lineStartX, lineY); canvasContext2D.lineTo(lineEndX, lineY); canvasContext2D.stroke(); }) } computeCharacterBounds(rangeStart, rangeEnd) { // implementation omitted for brevity } } class EditingController { handleTextUpdate(updateRangeStart, updateRangeEnd, text, selectionStart, selectionEnd) { this.model.updateText(updateRangeStart, updateRangeEnd, text) this.model.updateSelection(selectionStart, selectionEnd) } handleTextFormatUpdate(textFormats) { this.model.updateTextFormats(textFormats); } handleCharacterBoundsUpdate(rangeStart, rangeEnd) { characterBounds = this.view.computeCharacterBounds(rangeStart, rangeEnd); this.editContext.updateCharacterBounds(rangeStart, characterBounds); } } // When the user is typing, EditContext will receive textupdate events, // which can be used to update the editor's model. editContext.addEventListener("textupdate", e => { editingController.handleTextUpdate(e.updateRangeStart, e.updateRangeEnd, e.text, e.selectionStart, e.selectionEnd) }); // EditContext will also receive textformatupdate event for IME decoration. // Ex. thin/thick underline for the "phrase mode" in Japanese IME. editContext.addEventListener("textformatupdate", e => { editingcontroller.handleTextFormatUpdate(e.getTextFormats()); }); // When receiving CharacterBoundsUpdate event, the author is responsible to compute // the bounds of characters from rangeStart to rangeEnd, and call // EditContext.updateCharacterBounds to update the character bounds in EditContext. editContext.addEventListener("characterboundsupdate", e => { editingcontroller.handleCharacterBoundsUpdate(e.rangeStart, e.rangeEnd); }); </script>Interactions with Other Editing Primitives
An author doesn’t have to use a canvas element with an EditContext. In the example below the author uses a div to establish an editable region of the document and renders the contents into that editable region using various other styled elements, images and text. This allows the author to leverage other built-in editing primitives from the user agent such as selection and spellcheck.
<div></div> <script> // This example reuses EditModel and EditController in Example 2 and 3. let div = document.querySelector("div") div.editContext = new EditContext() div.focus(); class EditingView { constructor(div, model) { this.div = div this.model = model } render() { // render the text this.div.innerText = this.model.text // render the selection (implementation omitted for brevity) } computeSelectionBound() { // implementation omitted for brevity } computeControlBound() { // implementation omitted for brevity } computeCharacterBounds(rangeStart, rangeEnd) { // implementation omitted for brevity } } class EditingController { handleSelectionChange() { // Authors are responsible to map the selection from the DOM space to the // plain text space. One approach is to use range.toString() to get the plain text // before the selection start/end, and use the text length as the selection index. let s = document.getSelection() let range = new Range() range.setEnd(s.anchorNode, s.anchorOffset) range.setStartBefore(parentContainer) let selectionStart = range.toString().length range.setEnd(s.focusNode, s.focusOffset) range.setStartBefore(parentContainer) let selectionEnd = range.toString().length // Update model this.model.updateSelection(selectionStart, selectionEnd) this.render() } } document.addEventListener("selectionchange", e => { editingController.handleSelectionChange() }); </script>
<script> // This example is built on top of example 4. // Only the added logic is shown here for brevity. class EditingController { handleSpellCheck(newText, range) { let updateRangeStart = range.startOffset let updateRangeEnd = range.endOffset // Update model this.model.updateText(updateRangeStart, updateRangeEnd, newText) let newCaretPosition = updateRangeStart + newText.length this.model.updateSelection(newCaretPosition, newCaretPosition) this.render() } } div.spellcheck = true; div.addEventListener("beforeinput", e => { if (e.inputType === "insertReplacementText") { // for spellcheck let newText = e.dataTransfer.getData("text"); let range = e.getTargetRanges()[0]; editingController.handleSpellCheck(newText, range) } }) </script>
<script> // This example is built on top of example 4. // Only the added logic is shown here for brevity. class EditingController { handleDeleteByDrag() { // Update model this.model.updateText(this.model.selectionStart, this.model.selectionEnd, "") this.render() } handleInsertFromDrop(newText, caretPosition) { // Update model editingModel.updateText(caretPosition, caretPosition, newText) editingModel.updateSelection(caretPosition, caretPosition + newText.length) this.render() } } div.addEventListener("beforeinput", e => { if (e.inputType === "deleteByDrag") { editingController.handleDeleteByDrag() } if (e.inputType === "insertFromDrop") { newText = e.dataTransfer.getData('text/plain'); let selection = document.getSelection(); let caretPosition = selection.anchorOffset; editingController.handleInsertFromDrop(newText, caretPosition) } }) </script>
This specification defines conformance criteria that apply to a single product: the user agent that implements the interfaces that it contains.
Conformance requirements phrased as algorithms or specific steps may be implemented in any manner, so long as the end result is equivalent. (In particular, the algorithms defined in this specification are intended to be easy to follow, and not intended to be performant.)
EditContext API Extensions to the HTMLElement interfacepartial interface HTMLElement { attribute EditContext? editContext; };
An {{HTMLElement}} has an internal slot [[\EditContext]], which is a reference to an {{EditContext}} and is intially null.
canvas
", then [=exception/throw=] a {{"NotSupportedError"}} {{DOMException}}.Run the steps to [=Update the EditContext=] given |editContext| and the [=Text Edit Context=]'s [=text state=]'s [=text=], [=text formats=], [=selection start=], [=selection end=], [=is composing=], [=composition start=], and [=composition end=].
Since [=Text Edit Context=] is an abstraction over the common aspects of text input across different operating systems, the determination of the values in the [=Text Edit Context=] is explicitly not given in this specification. They will vary across different operating systems and input devices.
An
inputTypeis an [=EditContext-handled inputType=] if it is one of the following:
insertText
insertTranspose
deleteWordBackward
deleteWordForward
deleteContent
deleteContentBackward
deleteContentForward
The
inputTypes handled by {{EditContext}} are those which operate only on raw text. Other
inputTypes that depend on formats, clipboard/dragdrop, undo, or browser UI like spellcheck cannot be handled by {{EditContext}} since {{EditContext}}'s state does not include these concepts. If an author wants their application to handle those
inputTypes, they need to process them manually in a
beforeinputevent handler.
Update the EditContextNote that the steps to update the [=Text Edit Context=]'s [=text state=] are dependent on the nature of the abstraction created over a platform-specific [=Text Input Service=]. Those details are not part of this specification.
Dispatch text update eventIf |focused| is null or if the [=shadow-including root=] of |focused| is not |document|, return null.
The purpose of getting |focusable| through the [=top-level traversable=] is that we want there to be only one [=active EditContext=] at a time per [=top-level traversable=]. So if system focus is in some other document, this document can't have an [=active EditContext=].
If an {{EditContext}}'s [=associated element=]'s parent is editable, that {{EditContext}} can't become the [=active EditContext=]. This is the case regardless of whether that parent is editable due to another {{EditContext}} or due to [^html-global/contenteditable^].
EditContext Interfacedictionary EditContextInit { DOMString text; unsigned long selectionStart; unsigned long selectionEnd; }; [Exposed=Window] interface EditContext : EventTarget { constructor(optional EditContextInit options = {}); undefined updateText(unsigned long rangeStart, unsigned long rangeEnd, DOMString text); undefined updateSelection(unsigned long start, unsigned long end); undefined updateControlBounds(DOMRect controlBounds); undefined updateSelectionBounds(DOMRect selectionBounds); undefined updateCharacterBounds(unsigned long rangeStart, sequence<DOMRect> characterBounds); sequence<HTMLElement> attachedElements(); readonly attribute DOMString text; readonly attribute unsigned long selectionStart; readonly attribute unsigned long selectionEnd; readonly attribute unsigned long characterBoundsRangeStart; sequence<DOMRect> characterBounds(); attribute EventHandler ontextupdate; attribute EventHandler ontextformatupdate; attribute EventHandler oncharacterboundsupdate; attribute EventHandler oncompositionstart; attribute EventHandler oncompositionend; };
The method must follow these steps:
It's permissible that |rangeStart| > |rangeEnd|. The substring between the indices should be replaced in the same way as when |rangeStart| <= |rangeEnd|.
The method must follow these steps:
The method must follow these steps:
The method must follow these steps:
The method must follow these steps:
The method returns a list with one item which is the the {{EditContext}}'s associated element, or an empty list if the {{EditContext}}'s associated element is null.
This method returns a list instead of a single element for forward compatibility if {{EditContext}} is ever granted the ability to have multiple associated elements.
The event handler for {{TextUpdateEvent}}.
The event handler for {{CharacterBoundsUpdateEvent}}.
The event handler for {{TextFormatUpdateEvent}}.
The event handler for the compositionstart event.
The event handler for the compositionend event.
dictionary TextUpdateEventInit : EventInit { unsigned long updateRangeStart; unsigned long updateRangeEnd; DOMString text; unsigned long selectionStart; unsigned long selectionEnd; unsigned long compositionStart; unsigned long compositionEnd; }; [Exposed=Window] interface TextUpdateEvent : Event { constructor(DOMString type, optional TextUpdateEventInit options = {}); readonly attribute unsigned long updateRangeStart; readonly attribute unsigned long updateRangeEnd; readonly attribute DOMString text; readonly attribute unsigned long selectionStart; readonly attribute unsigned long selectionEnd; };
enum UnderlineStyle { "none", "solid", "dotted", "dashed", "wavy" }; enum UnderlineThickness { "none", "thin", "thick" }; dictionary TextFormatInit { unsigned long rangeStart; unsigned long rangeEnd; UnderlineStyle underlineStyle; UnderlineThickness underlineThickness; }; [Exposed=Window] interface TextFormat { constructor(optional TextFormatInit options = {}); readonly attribute unsigned long rangeStart; readonly attribute unsigned long rangeEnd; readonly attribute UnderlineStyle underlineStyle; readonly attribute UnderlineThickness underlineThickness; }; dictionary TextFormatUpdateEventInit : EventInit { sequence<TextFormat> textFormats; }; [Exposed=Window] interface TextFormatUpdateEvent : Event { constructor(DOMString type, optional TextFormatUpdateEventInit options = {}); sequence<TextFormat> getTextFormats(); };
A {{TextFormatUpdateEvent}} has an associated text format list, a list of zero or more [=text format=]s.
CharacterBoundsUpdateEventdictionary CharacterBoundsUpdateEventInit : EventInit { unsigned long rangeStart; unsigned long rangeEnd; }; [Exposed=Window] interface CharacterBoundsUpdateEvent : Event { constructor(DOMString type, optional CharacterBoundsUpdateEventInit options = {}); readonly attribute unsigned long rangeStart; readonly attribute unsigned long rangeEnd; };
Add contributors
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3