This specification defines additions to events for text and related input to allow for the monitoring and manipulation of default browser behavior in the context of text editor applications and other applications that deal with text input and text formatting. This specification builds on the UI events spec [[!UI-EVENTS]].

Input Events Level 2 replaces the first version of Input Events [[INPUT-EVENTS]] and includes:

The test suite and implementation reports for the Input Events specification are still work in progress.

Introduction

This document describes editing related additions to 2 events - Input and beforeinput which are described in the UI events spec [[!UI-EVENTS]]. The goal of these events is to allow authors to understand and/or override default edit behavior both before and after editing occurs.

Definitions

DataTransfer
An DataTransfer object contains richtext and plaintext data for some inputtypes. [[!DATA-TRANSFER]]
express intention
A user can express an intention to execute a specific editing operation by means of keyboard, IME, speech, or similar method. Specific input actions are mapped to intentions following platform-specific conventions.
kill buffer

This section is not normative.

A kill buffer is a in-memory store of richtext content that is separate from the clipboard which allows for the temporal storage of content that was deleted using specific deletion commands. The user can replace the current selection with the content held in the kill buffer by indicating an intention to yank the kill buffer.

Problems solved

This section is not normative.

Creating a webbased texteditor requires a considerable amount of JavaScript on top of the browser code, among other things because:

  1. Browsers do not handle all editing operations the same way.
  2. Browsers are often buggy in the way they handle certain editing operations.
  3. Individual sites may have custom preferences for how they want to handle certain editing operations.
  4. The development of high-level text editing features in browsers has not followed the principles laid out in the Extensible Web Manifesto in that development of these features has not always been in coordination with the needs of the web developer community.

This spec seeks to alleviate the problem by providing a simple way for web developers to both override browser handling of all user input related to text editing through the beforeinput event, and to monitor what browsers have changed in the DOM due to user input trough the input event.

Use cases

This section is not normative.

  1. Creating a JavaScript text editor in which the <strong>-tag is used instead of the <b>-tag to mark text that the user marks as bold, using any browser-builtin way to mark a text as bold, without having access to all existing browsers.
  2. Creating a JavaScript text editor which works with a data model in the background where JavaScript takes care of rendering changes to the edited text to the DOM.
  3. Creating a JavaScript editor which only allows a subset of richtext editing (for example: bold is allowed, but italic not).
  4. Creating a collaborative editor in which JavaScript is used to render changes to the DOM, based on user intentions with users using different browsers with different ways of expressing specific intentions.
  5. Creating a JavaScript editor with different user access options, where some users only can add or delete text and other users only can add or remove certain types of formatting.

Input Event Types

Input events are sent before (beforeinput event) and after (input event) a user attempts to edit the markup. This includes characters, deletions, and other related edits.

Interface InputEvent

                partial interface InputEvent {
                    readonly attribute DOMString     inputType;
                    readonly attribute DataTransfer? dataTransfer;
                    sequence<StaticRange> getTargetRanges();
                };

                partial dictionary InputEventInit {
                    DOMString inputType = "";
                    DataTransfer? dataTransfer = null;
                    sequence<StaticRange> targetRanges = [];
                };
            

The InputEvent and InputEventInit objects are defined in [[!UI-EVENTS]].

The attributes inputType, dataTransfer and targetRanges of InputEventInit initialize the corresponding attributes of the InputEvent object.

Overview

This section is not normative.

The following table provides a summary of when the data and dataTransfer attributes contain contents and when they are null as well as when the getTargetRanges() method returns an empty or non-empty Array, based on the inputType.

Editing hostinputTypedatadataTransfer getTargetRanges()
Contenteditable "insertText", "insertCompositionText", "insertFromComposition", "formatSetBlockTextDirection", "formatSetInlineTextDirection", "formatBackColor", "formatFontColor", "formatFontName", "insertLink" YesnullNon-empty Array
Contenteditable "insertFromPaste", "insertFromDrop", "insertReplacementText", "insertFromYank" nullYesNon-empty Array
<textarea>, <input type="text"> "insertText", "insertCompositionText", "insertFromComposition", "insertFromPaste", "insertFromDrop", "insertReplacementText", "insertFromYank", "formatSetBlockTextDirection", "formatSetInlineTextDirection", "formatBackColor", "formatFontColor", "formatFontName", "insertLink" YesnullEmpty Array
All "historyUndo", "historyRedo" nullnullEmpty Array
ContenteditableAll RemainingnullnullNon-empty Array
<textarea>, <input type="text"> All RemainingnullnullEmpty Array
Attributes

inputType holds the type of input. Allowed values are the following: "insertText", "insertReplacementText", "insertLineBreak", "insertParagraph", "insertOrderedList", "insertUnorderedList", "insertHorizontalRule", "insertFromYank", "insertFromDrop", "insertFromPaste", "insertCompositionText", "insertFromComposition", "deleteWordForward", "deleteWordBackward", "deleteSoftLineForward", "deleteSoftLineBackward", "deleteEntireSoftLine", "deleteHardLineForward", "deleteHardLineBackward", "deleteCompositionText", "deleteByComposition", "deleteByDrag", "deleteByCut", "deleteContent", "deleteContentBackward", "deleteContentForward", "historyUndo", "historyRedo", "insertTranspose", "formatBold", "formatItalic", "formatUnderline", "formatStrikeThrough", "formatSuperscript", "formatSubscript", "formatJustifyCenter", "formatJustifyRight", "formatJustifyLeft", "formatIndent", "formatOutdent", "formatRemove", "formatSetBlockTextDirection", "formatSetInlineTextDirection", "formatBackColor", "formatFontColor", "formatFontName", "insertLink" . During initialization, the user agent [[!UI-EVENTS]] is responsible for populating this field as follows:

The existence of the above mentioned inputTypes does not mean that any given implementation will support all of these. But if a given browser supports an editing operation which potentially leads to a change of the DOM, it MUST dispatch [[!UI-EVENTS]] the corresponding beforeinput and input events.
  1. If the user expresses an intention to insert typed plain text, the inputType MUST be "insertText".
  2. If the user expresses an intention to replace existing text by means of a spell checker, auto-correct or similar, the inputType MUST be "insertReplacementText".
  3. If the user expresses an intention to insert a line break, the inputType MUST be "insertLineBreak".
  4. If the user expresses an intention to insert a paragraph break, the inputType MUST be "insertParagraph".
  5. If the user expresses an intention to insert a numbered list, the inputType MUST be "insertOrderedList".
  6. If the user expresses an intention to insert a bulleted list, the inputType MUST be "insertUnorderedList".
  7. If the user expresses an intention to insert a horizontal rule, the inputType MUST be "insertHorizontalRule".
  8. If the user expresses an intention to replace the current selection with content stored in a kill buffer, the inputType MUST be "insertFromYank".
  9. If the user expresses an intention to insert content into the DOM by means of drop, the inputType MUST be "insertFromDrop".
  10. If the user expresses an intention to paste, the inputType MUST be "insertFromPaste".
  11. If the user expresses an intention to transpose the last two characters that were entered, the inputType MUST be "insertTranspose".
  12. If the user expresses an intention during a composition using IME to replace the current composition string, the inputType MUST be "insertCompositionText".
    1. Set the value of cancelable to false.
  13. If the user expresses an intention during a composition using IME to insert into the DOM a finalized composed string that will not form part of the next composition string, the inputType MUST be "insertFromComposition".
  14. If the user expresses an intention during a composition using IME to delete the current composition string before commiting a finalized string to the DOM, the inputType MUST be "deleteCompositionText".
    1. Set the value of cancelable to false.
  15. If the user expresses an intention to remove a part of the DOM in order to recompose this part using IME, the inputType MUST be "deleteByComposition".
  16. If the user expresses an intention to delete a word directly before the caret position, the inputType MUST be "deleteWordBackward".
  17. If the user expresses an intention to delete a word directly after the caret position, and the selection is empty, the inputType MUST be "deleteWordForward".
  18. If the user expresses an intention to delete from the caret to the nearest softline break before the caret position and the selection is empty, the inputType MUST be "deleteSoftLineBackward".
  19. If the user expresses an intention to delete from the caret to the nearest softline break after the caret position and the selection is empty, the inputType MUST be "deleteSoftLineForward".
  20. If the user expresses an intention to delete from to the nearest softline break before the caret position to the nearest softline break after the caret position and the selection is empty, the inputType MUST be "deleteEntireSoftLine".
  21. If the user expresses an intention to delete from the caret to the nearest hardline break before the caret position and the selection is empty, the inputType MUST be "deleteHardlineBackward".
  22. If the user expresses an intention to delete from the caret to the nearest hardline break after the caret position and the selection is empty, the inputType MUST be "deleteHardlineForward".
  23. If the user expresses an intention to remove content from the DOM by means of drag, the inputType MUST be "deleteByDrag".
  24. If the user expresses an intention to remove the current selection as part of a cut, the inputType MUST be "deleteByCut".
  25. If the user expresses an intention to delete the current, non-collapsed selection without specifying the direction of the deletion and this intention is not covered by another inputType, the inputType MUST be "deleteContent".
  26. If the user expresses an intention to delete the content directly before the caret position and this intention is not covered by another inputType or the user intends to delete a non-collapsed selection with the selection collapsing to its start after the deletion, the inputType MUST be "deleteContentBackward".
    If the selection is collapsed, "deleteContentBackward" will be used both when the user asks for text deletion within a text node, and when the user shows the intention to deletion of more complex elements or merge paragraphs if the caret is at the start of a text node. In some scripts, backward deletion within a text node with a collapsed selection will delete a code point rather than a grapheme. A grapheme can consist of several characters, where each character corresponds to a code point. The getTargetRanges() method can be used to find out how many characters the browser will remove by default if deleting within a text node.
  27. If the user expresses an intention to delete the content directly after the caret position and this intention is not covered by another inputType or the user expresses an intention to delete a non-collapsed selection with the selection collapsing to its end after the deletion, the inputType MUST be "deleteContentForward".
    If the selection is collapsed, "deleteContentForward" will be used both when the user asks for text deletion within a text node, and when the user shows the intention to deletion of more complex elements or merge paragraphs if the caret is at the end of a text node. In some scripts, forward deletion within a text node with a collapsed selection will delete a grapheme rather than a code point. A grapheme can consist of several characters, where each character corresponds to a code point. The getTargetRanges() method can be used to find out how many characters the browser will remove by default if deleting within a text node.
  28. If the user expresses an intention to undo the last editing action, the inputType MUST be "historyUndo".
  29. If the user expresses an intention to redo the last editing action, the inputType MUST be "historyRedo".
  30. If the user expresses an intention to initiate bold text, the inputType MUST be "formatBold".
  31. If the user expresses an intention to initiate italic text, the inputType MUST be "formatItalic".
  32. If the user expresses an intention to initiate underline text, the inputType MUST be "formatUnderline".
  33. If the user expresses an intention to initiate stricken through text, the inputType MUST be "formatStrikeThrough".
  34. If the user expresses an intention to initiate superscript through text, the inputType MUST be "formatSuperscript".
  35. If the user expresses an intention to initiate subscript through text, the inputType MUST be "formatSubscript".
  36. If the user expresses an intention to justify center the current selection, the inputType MUST be "formatJustifyCenter".
  37. If the user expresses an intention to justify right the current selection, the inputType MUST be "formatJustifyRight".
  38. If the user expresses an intention to justify left the current selection, the inputType MUST be "formatJustifyLeft".
  39. If the user expresses an intention to indent the current selection, the inputType MUST be "formatIndent".
  40. If the user expresses an intention to outdent the current selection, the inputType MUST be "formatOutdent".
  41. If the user expresses an intention to remove all formatting from the current selection, the inputType MUST be "formatRemove".
  42. If the user expresses an intention to set the text block direction, the inputType MUST be "formatSetBlockTextDirection".
  43. If the user expresses an intention to set the text inline direction, the inputType MUST be "formatSetInlineTextDirection".
  44. If the user expresses an intention to change the background color, the inputType MUST be "formatBackColor".
  45. If the user expresses an intention to change the font color, the inputType MUST be "formatFontColor".
  46. If the user expresses an intention to insert a link, the inputType MUST be "insertLink".
  47. If the user expresses an intention to change the font-family, the inputType MUST be ."formatFontName".

This section is not normative.

An IME composition causes several beforeinput events, not all of which can be canceled.

  1. If the composition is starting with an empty initial composition string (i.e. the composition is not recomposing an existing range of the DOM), this step is skipped.

    The UA extracts the initial composition string from the DOM.

    A beforeinput event with the inputType "deleteByComposition" is triggered. The targetRanges of this beforeinput event is an array of static ranges covering the part of the DOM from which the initial composition string was extracted.

    The default action for this beforeinput event is the removal of the targetRanges from the DOM (i.e. the initial composition string will be removed from the DOM).

    The beforeinput event can be canceled. Canceling the event will prevent the browser from removing the initial composition string from the DOM. Canceling or handling the event will not prevent the composition from taking place or influence the contents of the initial composition string.

  2. The composition starts.

  3. If the composition has an empty initial composition string, this step is skipped.

    A non-cancelable beforeinput event with the inputType "insertCompositionText" is triggered with the data attribute set to the initial composition string. The default action for this event is the insertion of the initial composition string into the range of the DOM controlled by the composition process.

  4. With each DOM update of the composition string, a non-cancelable beforeinput event with the inputType "insertCompositionText" is triggered with the data attribute set to the updated contents of the composition text. The default action for this event is the modification of range of the DOM controlled by the composition as well as the composition string so that both match the value of the data attribute of the beforeinput event.

    This step is repeated as long as no text is to be committed to the DOM (i.e. is made to be part of the DOM outside the range controlled by the composition).

  5. When the part or the entire composition string is to be committed to the DOM, a non-cancelable beforeinput event with the inputType "deleteCompositionText" is triggered. The default action for this event is that the range controlled by the composition process is remvoed from the DOM is removed from the DOM.

  6. A cancelable beforeinput event with the inputType "insertFromComposition" is triggered with the data attribute set to the part of the final composition string that is to be committed to the DOM. The default action for this event is inserting the value of the data attribute into the DOM at the place of the selection.

  7. If only a part of the composition string was committed to the DOM, while another part is not yet to be committed, the process starts over from step 3 with the non-committed part of the final composition string as the new initial composition string.

  8. The composition ends.

data holds information plaintext data related to what is to be added to the document.

  1. If the inputType is "insertText", "insertCompositionText" or "insertFromComposition", the value of data is to be the plain text string to be inserted.
  2. If the editing host is not a contenteditable element and the inputType is "insertFromPaste", "insertFromDrop", "insertTranspose", "insertReplacementText", or "insertFromYank" the value of data is to be the plain text string to be inserted.
  3. If the inputType is "formatSetBlockTextDirection" the value of data is to be one of "ltr", "rtl", "auto" or "null".
  4. If the inputType is "formatSetInlineTextDirection" or "formatFontColor" the value of data is to be one of "ltr", "rtl", "auto" or "null".
  5. If the inputType is "formatBackColor" or "formatFontColor" the value of data is to be the hex digit value of the proposed color.
  6. If the inputType is "formatFontName" the value of data is to be the proposed value of the font-family CSS property.
  7. If the inputType is "insertLink" the value of data is to be the url of the proposed link.
  8. Else, the value of data is to be null.

dataTransfer holds information about richtext and plaintext data that is to be taken from or added to the document in a DataTransfer object if there is relevant data.

  1. If the editing host is a contenteditable element and the inputType is "insertFromPaste", "insertFromDrop", "insertTranspose", "insertReplacementText", or "insertFromYank" the dataTransfer attribute is a prepopulated DataTransfer object so that:
    1. The DataTransfer object's drag data store item list contains one entry with the draf data item type string "text/html", whose kind is Plain Unicode string, and whose data is a HTML representation of the content that is in the clipboard, or in the kill buffer, to be dropped or otherwise the content that is to be added. [[!HTML-LIVING]]
    2. The DataTransfer object's drag data store item list contains one entry with the draf data item type string "text/plain", whose kind is Plain Unicode string, and whose data is a plain text representation of the content that is to be pasted, dropped or otherwise added. [[!HTML-LIVING]]
  2. Else, the dataTransfer attribute is null.
Methods

getTargetRanges() returns an Array of StaticRanges [[!STATIC-RANGE]] associated with this event, unless the inputType is "historyUndo" or "historyRedo" or the editing host is not a contenteditable element, in which case it returns an empty Array.

Event definitions

beforeinput
Type beforeinput
Interface InputEvent
Sync / Async Sync
Bubbles Yes
Trusted Targets Any Element with contenteditable attribute enabled.
Default action [[!UI-EVENTS]] Varies: 'Update the DOM' for contentEditable=typing editing hosts for inputTypes "insertCompositionText" and "deleteCompositionText". 'Update the DOM element' for contentEditable="true" editing hosts for all inputTypes. None otherwise.
Context
(trusted events)
  • InputEvent.data: the string containing the data that was added to the element, which MAY be null if it doesn't apply.
  • InputEvent.dataTransfer: richtext data added or removed from element, which MAY be null if it doesn't apply.
  • InputEvent.getTargetRanges(): returns an arrays of Static Cranges which will be affected by the change to the DOM if it is not canceled.

A user agent MUST dispatch [[!UI-EVENTS]] this event when the user has attempted to input in a contenteditable element. It does not necessarily mean the user agent [[!UI-EVENTS]] will then update the DOM.

A user agent MUST NOT dispatch [[!UI-EVENTS]] this event due to events that are not caused by attempted user input, such as system events.

input
Type input
Interface InputEvent
Sync / Async Sync
Bubbles Yes
Trusted Targets Any Element with contenteditable attribute enabled.
Default action [[!UI-EVENTS]] None
Context
(trusted events)
  • InputEvent.data: the string containing the data that was added to the element, which MAY be null if it doesn't apply.
  • InputEvent.dataTransfer: richtext data added or removed from element, which MAY be null if it doesn't apply.

A user agent MUST dispatch [[!UI-EVENTS]] this event immediately after the DOM has been updated due to a user expressed intention to change the document contents which the browser has handled.

Privacy and security considerations

There are no known security or privacy impacts of this feature.

Acknowledgements

Thanks to: Michael Aufreiter, Adrian Bateman, Oliver Buchtala, Robin Berjon, Enrica Casucci, Bo Cupp, Domenic Denicola, Emil Eklund, Olivier Forget, Aryeh Gregor, Marijn Haverbeke, Yoshifumi Inoue, Koji Ishii, Gary Kacmarcik, Ian Kilpatrick, Frederico Caldeira Knabben, Takayoshi Kochi, Piotrek Koszuliński, Travis Leithead, Grisha Lyukshin, Miles Maxfield, Chaals McCathie Nevile, Masayuki Nakano, Ryosuke Niwa, Julie Parent, Ben Peters, Florian Rivoal, Morgan Smith, Hallvord R. M. Steen, Johan Sörlin, Cristian Talau, Dave Tapuska, Ojan Vafai, Léonie Watson, Xiaoqian Wu, Chong Zhang, Joanmarie, and everyone in the Editing Taskforce for their input and feedback.