diff --git a/index.html b/index.html index cce9947..61e2fb6 100644 --- a/index.html +++ b/index.html @@ -1,10 +1,13 @@ - - - EditContext API - - + + - -
-

{{EditContext}} is an API that allows authors to more directly participate in the text input process.

-
-
-
-

Introduction

-
-

Background and Motivation

-

Modern 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:

-
    -
  1. A user places focus into an editable region of the app.
  2. -
  3. The app produces a [=Text Edit Context=] describing its editable region according to the standards set forth by the [=Text Input Service=] and provides that [=Text Edit Context=] to the [=Text Input Service=].
  4. -
  5. The [=Text Input Service=] triggers a [=Text Input Method=] to provide some user interface for capturing text input from the user and provides the [=Text Input Method=] the app generated [=Text Edit Context=].
  6. -
  7. The [=Text Input Method=] reads the location of selection and nearby text from the [=Text Edit Context=] to help tailor its user experience.
  8. -
  9. The [=Text Input Method=] may also read screen coordinates for where the selection and editable region are located so that it can properly position its user interface next to the text being edited.
  10. -
  11. The user interacts with the [=Text Input Method=] user interface to input text in some [=Text Input Method=]-specific way.
  12. -
  13. The [=Text Input Method=] describes its desired modifications to the text and selection in the [=Text Edit Context=] in response to the user’s input.
  14. -
  15. The app handles an event describing the desired modifications to its [=Text Edit Context=] and renders the result to the user.
  16. -
-
- -
A sequence diagram illustrating a typical flow for text input.
-
-

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 Model

-

- The [=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 state

-

- Both 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 activation

-

- An {{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 host

-
-

- In 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: -

- - -

- There are also some ways that an EditContext editing host - differs from other types of [=editing hosts=]: -

-
    -
  • - When the {{Document}} being edited has an [=active EditContext=], the user agent must not update the DOM - as a direct result of a user action in the EditContext editing host - (e.g., keyboard input in an editable region, deleting or formatting text, ...). -
  • -
  • - When the {{Document}} being edited has an [=active EditContext=], the user agent must not fire the - input event - against the EditContext editing host as a direct result of user action - event as specified in [[uievents]]. -
  • -
-
- -

EditContext events

-

- 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. -

- -

Event loop changes

-

- 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|. -

-
-

Examples

-

Using 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:

-
-
-

- 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 interface

-
+
+
+
+
+

+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 interface +

+
                partial interface HTMLElement {
                     attribute EditContext? editContext;
                };
             
-

An {{HTMLElement}} has an internal slot [[\EditContext]], which is a reference to an {{EditContext}} and is intially null.

-
-
editContext
-
The {{HTMLElement/editContext}} getter steps are to return the value of [=this=]'s internal [[\EditContext]] slot.
-
The {{HTMLElement/editContext}} setter must follow these steps: -
-
-
Input
-
|editContext|
-
Output
-
None
-
-
    -
  1. If [=this=]'s [=Element/local name=] is neither a [=valid shadow host name=] nor "canvas", then [=exception/throw=] a {{"NotSupportedError"}} {{DOMException}}.
  2. -
  3. If |editContext| is not null, then: -
      -
    1. If |editContext|'s associated element is equal to [=this=], then terminate these steps.
    2. -
    3. If |editContext|'s associated element is not null, then [=exception/throw=] a {{"NotSupportedError"}} {{DOMException}}.
    4. -
    5. Set |editContext|'s associated element to [=this=].
    6. -
    -
  4. -
  5. Let |oldEditContext| be the value of [=this=]'s internal [[\EditContext]] slot.
  6. -
  7. If |oldEditContext| is not null, then: -
      -
    1. [=Assert=]: |oldEditContext|'s associated element is equal to [=this=].
    2. -
    3. Set |oldEditContext|'s associated element to null.
    4. -
    -
  8. -
  9. Set [=this=]'s internal [[\EditContext]] slot to be |editContext|.
  10. -
  11. - If |oldEditContext| is not null and |oldEditContext| is [=this=]'s - node document's - [=active EditContext=], then run the steps to [=deactivate an EditContext=] - with |oldEditContext|. -
  12. -
-
-
-
-

Handle input for EditContext

-
-
-
Input
-
|element|, the {{HTMLElement}} receiving the input
-
Output
-
None
-
-
    -
  1. - Let |editContext| be |element|'s - node document's - [=active EditContext=] -
  2. -
  3. - If |editContext| is null, return. -
  4. -
  5. -

    - 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. -

    -
  6. -
-
- -

EditContext-handled inputType

-
- An inputType is 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 beforeinput - event handler. -
-
- -

Update the EditContext

-
-
-
Input
-
|editContext|, an {{EditContext}}
-
|text|, a string
-
|textFormats|, an array of [=text format=]s from the [=Text Input Service=]
-
|selectionStart|, the new position for the start of the selection
-
|selectionEnd|, the new position for the end of the selection
-
|isComposing|, a boolean indicating whether composition should be active at the end of the update
-
|replacementRangeStart|, the start position of the current composition (0 if there is no composition)
-
|replacementRangeEnd|, the end position of the current composition (0 if there is no composition)
-
Output
-
None
-
-
    -
  1. - If |isComposing| is true, |text| is not empty, and |editContext|'s [=is composing=] is false. -
      -
    1. [=Fire an event=] named compositionstart at |editContext| using {{CompositionEvent}}. -
    2. -
    3. set |editContext|'s [=is composing=] to true.
    4. -
    -
  2. -
  3. - If |text| is empty: -
      -
    1. If |editContext|'s [=is composing=] is false, return.
    2. -
    3. - If |editContext|'s [=is composing=] is true and |isComposing| is false, then: -
        -
      1. Set |editContext|'s [=is composing=] to false.
      2. -
      3. - [=Fire an event=] named - compositionend - at |editContext| using {{CompositionEvent}}. -
      4. -
      5. Return.
      6. -
      -
    4. -
    -
  4. -
  5. If |editContext|'s [=is composing=] is true, then: -
      -
    1. Let |insertionStart| be |replacementRangeStart|.
    2. -
    3. Let |insertionEnd| be |replacementRangeEnd|.
    4. -
    -
  6. -
  7. Otherwise: -
      -
    1. Let |insertionStart| be |selectionStart|.
    2. -
    3. Let |insertionEnd| be |selectionEnd|.
    4. -
    -
  8. -
  9. - Replace the substring of |editContext|'s [=text=] in the range of |insertionStart| and |insertionEnd| with |text| -
  10. -
  11. Set |editContext|'s [=selection start=] to |selectionStart|.
  12. -
  13. Set |editContext|'s [=selection end=] to |selectionEnd|.
  14. -
  15. Set |editContext|'s [=composition start=] to |insertionStart|.
  16. -
  17. Set |editContext|'s [=composition end=] to |editContext|'s [=composition start=] plus the length of |text|.
  18. -
  19. [=Dispatch text update event=] given |editContext| and |text|.
  20. -
  21. - If |editContext|'s [=is composing=] is true, then: -
      -
    1. [=Dispatch text format update event=] given |editContext| and |textFormats|.
    2. -
    3. [=Dispatch character bounds update event=] given |editContext|.
    4. -
    5. If |isComposing| is false, then: -
        -
      1. Set |editContext|'s [=is composing=] to false.
      2. -
      3. - [=Fire an event=] named - compositionend - at |editContext| using {{CompositionEvent}}. -
      4. -
      -
    6. -
    -
  22. -
-
- -

Update the Text Edit Context

+

+An {{HTMLElement}} has an internal slot [[\EditContext]], which is a reference +to an {{EditContext}} and is intially null. +

+
+
+editContext +
+
+The {{HTMLElement/editContext}} getter steps are to return the value of +[=this=]'s internal [[\EditContext]] slot. +
+
+The {{HTMLElement/editContext}} setter must follow these steps:
-
-
Input
-
|document|, a {{Document}}
-
Output
-
None
-
-
    -
  1. Let |oldActiveEditContext| be |document|'s [=active EditContext=].
  2. -
  3. Let |newActiveEditContext| be the result of running the steps to [=determine the active EditContext=] given |document|.
  4. -
  5. If |oldActiveEditContext| is not null, then run the steps to [=deactivate an EditContext=] given |oldActiveEditContext|.
  6. -
  7. If |newActiveEditContext| is not null, then: -
      -
    1. Update the [=Text Edit Context=]'s [=text state=] to match the values in |editContext|'s [=text state=].
    2. -
    -
  8. -
  9. Set the |document|'s [=active EditContext=] to |newActiveEditContext|.
  10. -
-

- Note 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 event

-
-
-
Input
-
|editContext|, an {{EditContext}}
-
|text|, a string
-
Output
-
None
-
-
    -
  1. - [=Fire an event=] named "textupdate" at |editContext| using {{TextUpdateEvent}}, with - {{TextUpdateEvent/text}} initialized to |text|, - {{TextUpdateEvent/selectionStart}} initialized to ||editContext|'s [=selection start=], and - {{TextUpdateEvent/selectionEnd}} initialized to |editContext|'s [=selection end=]. -
  2. -
-
- -

Dispatch text format update event

-
-
-
Input
-
|editContext|, an {{EditContext}}
-
|textFormats|, an array of [=text format=]s from the [=Text Input Service=]
-
Output
-
None
-
+
+
+Input +
+
+|editContext| +
+
+Output +
+
+None +
+
+
    +
  1. If [=this=]'s [=Element/local name=] is neither a [=valid shadow host +name=] nor "canvas", then [=exception/throw=] a +{{"NotSupportedError"}} {{DOMException}}. +
  2. +
  3. If |editContext| is not null, then:
      -
    1. - Let |formats| be an array of {{TextFormat}}, initially empty. -
    2. -
    3. - For each [=text format=] |format| in |textFormats|, -
        -
      1. Let |textFormat| be a new {{TextFormat}}. with |rangeStart|, |rangeEnd|, |underlineStyle|, |underlineThickness|.
      2. -
      3. Set |textFormat|'s {{TextFormat/rangeStart}} to |format|'s [=range start=].
      4. -
      5. Set |textFormat|'s {{TextFormat/rangeEnd}} to |format|'s [=range end=].
      6. -
      7. Set |textFormat|'s {{TextFormat/underlineStyle}} to |format|'s [=underline style=].
      8. -
      9. Set |textFormat|'s {{TextFormat/underlineThickness}} to |format|'s [=underline thickness=].
      10. -
      11. Append |textFormat| to |formats|
      12. -
      -
    4. -
    5. - [=Fire an event=] named "textformatupdate" at |editContext| using {{TextFormatUpdateEvent}} with - the {{TextFormatUpdateEvent}}'s [=text format list=] initialized to |formats|. -
    6. +
    7. If |editContext|'s associated element is equal to [=this=], then +terminate these steps. +
    8. +
    9. If |editContext|'s associated element is not null, then +[=exception/throw=] a {{"NotSupportedError"}} {{DOMException}}. +
    10. +
    11. Set |editContext|'s associated element to [=this=]. +
    12. +
    +
  4. +
  5. Let |oldEditContext| be the value of [=this=]'s internal [[\EditContext]] +slot. +
  6. +
  7. If |oldEditContext| is not null, then: +
      +
    1. [=Assert=]: |oldEditContext|'s associated element is equal to +[=this=]. +
    2. +
    3. Set |oldEditContext|'s associated element to null. +
    4. +
    +
  8. +
  9. Set [=this=]'s internal [[\EditContext]] slot to be |editContext|. +
  10. +
  11. If |oldEditContext| is not null and |oldEditContext| is [=this=]'s node document's +[=active EditContext=], then run the steps to [=deactivate an EditContext=] +with |oldEditContext|. +
- -

Dispatch character bounds update event

+
+
+

+Handle input for EditContext +

-
-
Input
-
|editContext|, an {{EditContext}}
-
Output
-
None
-
+
+
+Input +
+
+|element|, the {{HTMLElement}} receiving the input +
+
+Output +
+
+None +
+
    +
  1. Let |editContext| be |element|'s node document's +[=active EditContext=] +
  2. +
  3. If |editContext| is null, return. +
  4. - [=Fire an event=] named "characterboundsupdate" at |editContext| using {{CharacterBoundsUpdateEvent}} with - {{CharacterBoundsUpdateEvent/rangeStart}} initialized to |editContext|'s [=composition start=] and - {{CharacterBoundsUpdateEvent/rangeEnd}} initialized to |editContext|'s [=composition end=]. +

    +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. +

- -

Deactivate an EditContext

+

+EditContext-handled inputType +

+
+An inputType is 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 beforeinput event +handler. +
+
+

+Update the EditContext +

-
-
Input
-
|editContext|, an {{EditContext}}
-
Output
-
None
-
-
    -
  1. Set |editContext|'s [=is composing=] to false.
  2. -
  3. - [=Fire an event=] named - compositionend - at |editContext| using {{CompositionEvent}}. -
  4. -
+
+
+Input +
+
+|editContext|, an {{EditContext}} +
+
+|text|, a string +
+
+|textFormats|, an array of [=text format=]s from the [=Text Input Service=] +
+
+|selectionStart|, the new position for the start of the selection +
+
+|selectionEnd|, the new position for the end of the selection +
+
+|isComposing|, a boolean indicating whether composition should be active at the +end of the update +
+
+|replacementRangeStart|, the start position of the current composition (0 if +there is no composition) +
+
+|replacementRangeEnd|, the end position of the current composition (0 if there +is no composition) +
+
+Output +
+
+None +
+
+
    +
  1. If |isComposing| is true, |text| is not empty, and |editContext|'s [=is +composing=] is false. +
      +
    1. [=Fire an event=] named compositionstart +at |editContext| using {{CompositionEvent}}. +
    2. +
    3. set |editContext|'s [=is composing=] to true. +
    4. +
    +
  2. +
  3. If |text| is empty: +
      +
    1. If |editContext|'s [=is composing=] is false, return. +
    2. +
    3. If |editContext|'s [=is composing=] is true and |isComposing| is false, +then: +
        +
      1. Set |editContext|'s [=is composing=] to false. +
      2. +
      3. [=Fire an event=] named compositionend +at |editContext| using {{CompositionEvent}}. +
      4. +
      5. Return. +
      6. +
      +
    4. +
    +
  4. +
  5. If |editContext|'s [=is composing=] is true, then: +
      +
    1. Let |insertionStart| be |replacementRangeStart|. +
    2. +
    3. Let |insertionEnd| be |replacementRangeEnd|. +
    4. +
    +
  6. +
  7. Otherwise: +
      +
    1. Let |insertionStart| be |selectionStart|. +
    2. +
    3. Let |insertionEnd| be |selectionEnd|. +
    4. +
    +
  8. +
  9. Replace the substring of |editContext|'s [=text=] in the range of +|insertionStart| and |insertionEnd| with |text| +
  10. +
  11. Set |editContext|'s [=selection start=] to |selectionStart|. +
  12. +
  13. Set |editContext|'s [=selection end=] to |selectionEnd|. +
  14. +
  15. Set |editContext|'s [=composition start=] to |insertionStart|. +
  16. +
  17. Set |editContext|'s [=composition end=] to |editContext|'s [=composition +start=] plus the length of |text|. +
  18. +
  19. [=Dispatch text update event=] given |editContext| and |text|. +
  20. +
  21. If |editContext|'s [=is composing=] is true, then: +
      +
    1. [=Dispatch text format update event=] given |editContext| and +|textFormats|. +
    2. +
    3. [=Dispatch character bounds update event=] given |editContext|. +
    4. +
    5. If |isComposing| is false, then: +
        +
      1. Set |editContext|'s [=is composing=] to false. +
      2. +
      3. [=Fire an event=] named compositionend +at |editContext| using {{CompositionEvent}}. +
      4. +
      +
    6. +
    +
  22. +
- -

Determine the active EditContext

+

+Update the Text Edit Context +

+
+
+
+Input +
+
+|document|, a {{Document}} +
+
+Output +
+
+None +
+
+
    +
  1. Let |oldActiveEditContext| be |document|'s [=active EditContext=]. +
  2. +
  3. Let |newActiveEditContext| be the result of running the steps to +[=determine the active EditContext=] given |document|. +
  4. +
  5. If |oldActiveEditContext| is not null, then run the steps to [=deactivate +an EditContext=] given |oldActiveEditContext|. +
  6. +
  7. If |newActiveEditContext| is not null, then: +
      +
    1. Update the [=Text Edit Context=]'s [=text state=] to match the values in +|editContext|'s [=text state=]. +
    2. +
    +
  8. +
  9. Set the |document|'s [=active EditContext=] to |newActiveEditContext|. +
  10. +
+

+Note 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 event +

-
-
Input
-
|document|, a {{Document}}
-
Output
-
An {{EditContext}}, or null.
-
-
    -
  1. Let |traversable| be |document|'s [=node navigable=]'s [=top-level traversable=].
  2. -
  3. If |traversable| is null, return null.
  4. -
  5. - Let |focused| be the DOM anchor - of the currently focused area of a top-level traversable - given |traversable|. -
  6. -
  7. -

    If |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=]. -

    -
  8. -
  9. Let |editContext| be null.
  10. -
  11. While |focused| is not null and |focused| is editable: -
      -
    1. Set |editContext| to the value of |focused|'s internal [[\EditContext]] slot.
    2. -
    3. Let |parent| be |focused|'s parent.
    4. -
    5. - If |parent| is null and |focused|'s root - is a [=shadow root=], let |parent| be |focused|'s - root's - host. -
    6. -
    7. Set |focused| to |parent|.
    8. -
    -
  12. -
  13. Return |editContext|.
  14. -
-

- 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^]. -

+
+
+Input +
+
+|editContext|, an {{EditContext}} +
+
+|text|, a string +
+
+Output +
+
+None +
+
+
    +
  1. [=Fire an event=] named "textupdate" at |editContext| using +{{TextUpdateEvent}}, with {{TextUpdateEvent/text}} initialized to |text|, +{{TextUpdateEvent/selectionStart}} initialized to ||editContext|'s [=selection +start=], and {{TextUpdateEvent/selectionEnd}} initialized to |editContext|'s +[=selection end=]. +
  2. +
- -
-

EditContext Interface

-
dictionary EditContextInit {
+

+Dispatch text format update event +

+
+
+
+Input +
+
+|editContext|, an {{EditContext}} +
+
+|textFormats|, an array of [=text format=]s from the [=Text Input Service=] +
+
+Output +
+
+None +
+
+
    +
  1. Let |formats| be an array of {{TextFormat}}, initially empty. +
  2. +
  3. For each [=text format=] |format| in |textFormats|, +
      +
    1. Let |textFormat| be a new {{TextFormat}}. with |rangeStart|, |rangeEnd|, +|underlineStyle|, |underlineThickness|. +
    2. +
    3. Set |textFormat|'s {{TextFormat/rangeStart}} to |format|'s [=range start=]. +
    4. +
    5. Set |textFormat|'s {{TextFormat/rangeEnd}} to |format|'s [=range end=]. +
    6. +
    7. Set |textFormat|'s {{TextFormat/underlineStyle}} to |format|'s [=underline +style=]. +
    8. +
    9. Set |textFormat|'s {{TextFormat/underlineThickness}} to |format|'s +[=underline thickness=]. +
    10. +
    11. Append |textFormat| to |formats| +
    12. +
    +
  4. +
  5. [=Fire an event=] named "textformatupdate" at |editContext| using +{{TextFormatUpdateEvent}} with the {{TextFormatUpdateEvent}}'s [=text format +list=] initialized to |formats|. +
  6. +
+
+

+Dispatch character bounds update event +

+
+
+
+Input +
+
+|editContext|, an {{EditContext}} +
+
+Output +
+
+None +
+
+
    +
  1. [=Fire an event=] named "characterboundsupdate" at |editContext| using +{{CharacterBoundsUpdateEvent}} with {{CharacterBoundsUpdateEvent/rangeStart}} +initialized to |editContext|'s [=composition start=] and +{{CharacterBoundsUpdateEvent/rangeEnd}} initialized to |editContext|'s +[=composition end=]. +
  2. +
+
+

+Deactivate an EditContext +

+
+
+
+Input +
+
+|editContext|, an {{EditContext}} +
+
+Output +
+
+None +
+
+
    +
  1. Set |editContext|'s [=is composing=] to false. +
  2. +
  3. [=Fire an event=] named compositionend +at |editContext| using {{CompositionEvent}}. +
  4. +
+
+

+Determine the active EditContext +

+
+
+
+Input +
+
+|document|, a {{Document}} +
+
+Output +
+
+An {{EditContext}}, or null. +
+
+
    +
  1. Let |traversable| be |document|'s [=node navigable=]'s [=top-level +traversable=]. +
  2. +
  3. If |traversable| is null, return null. +
  4. +
  5. Let |focused| be the DOM +anchor of the +currently focused area of a top-level traversable given |traversable|. +
  6. +
  7. +

    +If |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=]. +

    +
  8. +
  9. Let |editContext| be null. +
  10. +
  11. While |focused| is not null and |focused| is editable: +
      +
    1. Set |editContext| to the value of |focused|'s internal [[\EditContext]] +slot. +
    2. +
    3. Let |parent| be |focused|'s parent. +
    4. +
    5. If |parent| is null and |focused|'s root is a [=shadow root=], +let |parent| be |focused|'s root's host. +
    6. +
    7. Set |focused| to |parent|. +
    8. +
    +
  12. +
  13. Return |editContext|. +
  14. +
+

+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 Interface +

+
dictionary EditContextInit {
     DOMString text;
     unsigned long selectionStart;
     unsigned long selectionEnd;
@@ -1026,156 +1418,273 @@ 

EditContext Interface

attribute EventHandler oncompositionstart; attribute EventHandler oncompositionend; };
-
-
text
-
The {{EditContext/text}} getter steps are to return [=this=]'s [=text=].
- -
selectionStart
-
The {{EditContext/selectionStart}} getter steps are to return [=this=]'s [=selection start=].
- -
selectionEnd
-
The {{EditContext/selectionEnd}} getter steps are to return [=this=]'s [=selection end=].
- -
characterBounds
-
The {{EditContext/characterBounds}} getter steps are to return [=this=]'s [=codepoint rects=].
- -
characterBoundsRangeStart
-
The {{EditContext/characterBoundsRangeStart}} getter steps are to return [=this=]'s [=codepoint rects start index=].
- -
updateText() method
-
-

- The method must follow these steps: -

-
-
-
Input
-
|rangeStart|, an unsigned long
-
|rangeEnd|, an unsigned long
-
|newText|, a DOMString
-
Output
-
None
-
-
    -
  1. - Replace the substring of [=text=] in the range of |rangeStart| and |rangeEnd| with |newText| -
    It's permissible that |rangeStart| > |rangeEnd|. The substring between the indices should be replaced in the same way as when |rangeStart| <= |rangeEnd|.
    -
  2. -
-
-
-
updateSelection() method
-
-

- The method must follow these steps: -

-
-
-
Input
-
|start|, an unsigned long
-
|end|, an unsigned long
-
Output
-
None
-
-
    -
  1. - set [=selection start=] to |start| -
  2. -
  3. - set [=selection end=] to |end| -
  4. -
-
-
-
updateSelectionBounds() method
-
-

- The method must follow these steps: -

-
-
-
Input
-
|selectionBounds|, a {{DOMRect}}
-
Output
-
None
-
-
    -
  1. - set [=selection bounds=] to |selectionBounds| -
  2. -
-
-
-
updateControlBounds() method
-
-

- The method must follow these steps: -

-
-
-
Input
-
|controlBounds|, a {{DOMRect}}
-
Output
-
None
-
-
    -
  1. - set [=control bounds=] to |controlBounds| -
  2. -
-
-
-
updateCharacterBounds() method
-
-

- The method must follow these steps: -

-
-
-
Input
-
|rangeStart|, an unsigned long
-
|characterBounds|, an array of {{DOMRect}}
-
Output
-
None
-
-
    -
  1. - set [=codepoint rects start index=] to |rangeStart|. -
  2. -
  3. - set [=codepoint rects=] to |characterBounds|. -
  4. -
-
-
-
attachedElements() method
-

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. -

- -
ontextupdate
-

The event handler for {{TextUpdateEvent}}.

- -
oncharacterboundsupdate
-

The event handler for {{CharacterBoundsUpdateEvent}}.

- -
ontextformatupdate
-

The event handler for {{TextFormatUpdateEvent}}.

- -
oncompositionstart
-

The event handler for the compositionstart event.

- -
oncompositionend
-

The event handler for the compositionend event.

-
-
- -
-

EditContext Events

-
-

TextUpdateEvent

-
dictionary TextUpdateEventInit : EventInit {
+
+
+text +
+
+The {{EditContext/text}} getter steps are to return [=this=]'s [=text=]. +
+
+selectionStart +
+
+The {{EditContext/selectionStart}} getter steps are to return [=this=]'s +[=selection start=]. +
+
+selectionEnd +
+
+The {{EditContext/selectionEnd}} getter steps are to return [=this=]'s +[=selection end=]. +
+
+characterBounds +
+
+The {{EditContext/characterBounds}} getter steps are to return [=this=]'s +[=codepoint rects=]. +
+
+characterBoundsRangeStart +
+
+The {{EditContext/characterBoundsRangeStart}} getter steps are to return +[=this=]'s [=codepoint rects start index=]. +
+
+updateText() method +
+
+

+The method must follow these steps: +

+
+
+
+Input +
+
+|rangeStart|, an unsigned long +
+
+|rangeEnd|, an unsigned long +
+
+|newText|, a DOMString +
+
+Output +
+
+None +
+
+
    +
  1. Replace the substring of [=text=] in the range of |rangeStart| and +|rangeEnd| with |newText| +
    +It's permissible that |rangeStart| > |rangeEnd|. The substring between the +indices should be replaced in the same way as when |rangeStart| <= +|rangeEnd|. +
    +
  2. +
+
+
+
+updateSelection() method +
+
+

+The method must follow these steps: +

+
+
+
+Input +
+
+|start|, an unsigned long +
+
+|end|, an unsigned long +
+
+Output +
+
+None +
+
+
    +
  1. set [=selection start=] to |start| +
  2. +
  3. set [=selection end=] to |end| +
  4. +
+
+
+
+updateSelectionBounds() method +
+
+

+The method must follow these steps: +

+
+
+
+Input +
+
+|selectionBounds|, a {{DOMRect}} +
+
+Output +
+
+None +
+
+
    +
  1. set [=selection bounds=] to |selectionBounds| +
  2. +
+
+
+
+updateControlBounds() method +
+
+

+The method must follow these steps: +

+
+
+
+Input +
+
+|controlBounds|, a {{DOMRect}} +
+
+Output +
+
+None +
+
+
    +
  1. set [=control bounds=] to |controlBounds| +
  2. +
+
+
+
+updateCharacterBounds() method +
+
+

+The method must follow these steps: +

+
+
+
+Input +
+
+|rangeStart|, an unsigned long +
+
+|characterBounds|, an array of {{DOMRect}} +
+
+Output +
+
+None +
+
+
    +
  1. set [=codepoint rects start index=] to |rangeStart|. +
  2. +
  3. set [=codepoint rects=] to |characterBounds|. +
  4. +
+
+
+
+attachedElements() method +
+
+

+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. +

+
+
+ontextupdate +
+
+

+The event handler for {{TextUpdateEvent}}. +

+
+
+oncharacterboundsupdate +
+
+

+The event handler for {{CharacterBoundsUpdateEvent}}. +

+
+
+ontextformatupdate +
+
+

+The event handler for {{TextFormatUpdateEvent}}. +

+
+
+oncompositionstart +
+
+

+The event handler for the compositionstart +event. +

+
+
+oncompositionend +
+
+

+The event handler for the compositionend +event. +

+
+
+
+
+

+EditContext Events +

+
+

+TextUpdateEvent +

+
dictionary TextUpdateEventInit : EventInit {
     unsigned long updateRangeStart;
     unsigned long updateRangeEnd;
     DOMString text;
@@ -1194,26 +1703,45 @@ 

TextUpdateEvent

readonly attribute unsigned long selectionStart; readonly attribute unsigned long selectionEnd; };
-
-
{{TextUpdateEvent/updateRangeStart}}, of type unsigned long, readonly
-
The start position of the range that is to be replaced.
- -
{{TextUpdateEvent/updateRangeEnd}}, of type unsigned long, readonly
-
The end position of the range that is to be replaced.
- -
{{TextUpdateEvent/text}}, of type {{DOMString}}, readonly
-
The new string that is to replace the old string in the range.
- -
{{TextUpdateEvent/selectionStart}}, of type unsigned long, readonly
-
The start position of the selection after the text replacement.
- -
{{TextUpdateEvent/selectionEnd}}, of type unsigned long, readonly
-
The end position of the selection after the text replacement.
-
-
-
-

TextFormatUpdateEvent

-
enum UnderlineStyle { "none", "solid", "dotted", "dashed", "wavy" };
+
+
+{{TextUpdateEvent/updateRangeStart}}, of type unsigned long, readonly +
+
+The start position of the range that is to be replaced. +
+
+{{TextUpdateEvent/updateRangeEnd}}, of type unsigned long, readonly +
+
+The end position of the range that is to be replaced. +
+
+{{TextUpdateEvent/text}}, of type {{DOMString}}, readonly +
+
+The new string that is to replace the old string in the range. +
+
+{{TextUpdateEvent/selectionStart}}, of type unsigned long, readonly +
+
+The start position of the selection after the text replacement. +
+
+{{TextUpdateEvent/selectionEnd}}, of type unsigned long, readonly +
+
+The end position of the selection after the text replacement. +
+
+
+
+

+TextFormatUpdateEvent +

+
enum UnderlineStyle { "none", "solid", "dotted", "dashed", "wavy" };
 enum UnderlineThickness { "none", "thin", "thick" };
 
 dictionary TextFormatInit {
@@ -1241,24 +1769,50 @@ 

TextFormatUpdateEvent

constructor(DOMString type, optional TextFormatUpdateEventInit options = {}); sequence<TextFormat> getTextFormats(); };
-
-
{{TextFormat/rangeStart}}, of type unsigned long, readonly
-
An offset that respresents the position before the first codepoint that should be decorated.
-
{{TextFormat/rangeEnd}}, of type unsigned long, readonly
-
An offset that respresents the position after the last codepoint that should be decorated.
-
{{TextFormat/underlineStyle}}, of type {{UnderlineStyle}}, readonly
-
The preferred underline style of the decorated text range.
-
{{TextFormat/underlineThickness}}, of type {{UnderlineThickness}}, readonly
-
The preferred underline thickness of the decorated text range.
-
{{TextFormatUpdateEvent/getTextFormats}} method -
-
Returns [=this=]'s [=text format list=].
-
-

A {{TextFormatUpdateEvent}} has an associated text format list, a list of zero or more [=text format=]s.

-
-
-

CharacterBoundsUpdateEvent

-
dictionary CharacterBoundsUpdateEventInit : EventInit {
+
+
+{{TextFormat/rangeStart}}, of type unsigned long, readonly +
+
+An offset that respresents the position before the first codepoint that should +be decorated. +
+
+{{TextFormat/rangeEnd}}, of type unsigned long, readonly +
+
+An offset that respresents the position after the last codepoint that should be +decorated. +
+
+{{TextFormat/underlineStyle}}, of type {{UnderlineStyle}}, readonly +
+
+The preferred underline style of the decorated text range. +
+
+{{TextFormat/underlineThickness}}, of type {{UnderlineThickness}}, readonly +
+
+The preferred underline thickness of the decorated text range. +
+
+{{TextFormatUpdateEvent/getTextFormats}} method +
+
+Returns [=this=]'s [=text format list=]. +
+
+

+A {{TextFormatUpdateEvent}} has an associated text format list, a +list of zero or more [=text format=]s. +

+
+
+

+CharacterBoundsUpdateEvent +

+
dictionary CharacterBoundsUpdateEventInit : EventInit {
     unsigned long rangeStart;
     unsigned long rangeEnd;
 };
@@ -1269,28 +1823,35 @@ 

CharacterBoundsUpdateEvent

readonly attribute unsigned long rangeStart; readonly attribute unsigned long rangeEnd; };
-
-
{{CharacterBoundsUpdateEvent/rangeStart}}, of type unsigned long, readonly -
-
The start position of the range where the character bounds are needed by [=Text Input Service=] -
-
{{CharacterBoundsUpdateEvent/rangeEnd}}, of type unsigned long, readonly -
-
The end position of the range where the character bounds are needed by [=Text Input Service=] -
-
-
-
-
- -
-
-

Contributors

-

- Add contributors -

-
-
+
+
+{{CharacterBoundsUpdateEvent/rangeStart}}, of type unsigned long, readonly +
+
+The start position of the range where the character bounds are needed by [=Text +Input Service=] +
+
+{{CharacterBoundsUpdateEvent/rangeEnd}}, of type unsigned long, readonly +
+
+The end position of the range where the character bounds are needed by [=Text +Input Service=] +
+
+
+
+
+ +
+
+

+Contributors +

+

+Add contributors +

+
+
-