If one moves the caret/selection to a different element when compoisitionstart is triggered, browsers different on compositionend:
- Chrome/Safari trigger the
compositionend event on the element where composition takes place.
- Firefox triggers the
compositionend event on the element where the caret was at the time when the compositionstart event was triggered.
Additionally, browsers behave differently if the caret is moved to a different element when compositionend is triggered:
- Chrome/Safari paste the composed characters in the place where the caret is moved to.
- Firefox moves the caret to a different element and inserts no characters there by default. If the JS inserts the characters by means of execCommand/insertHTML, the endComposition event is triggered a second time.
The Chrome/Safari behavior is desired for the use case where one wants to have IME character insertion into the document stream be atomic -- either by doing composition somewhere else [1] or by using a Shadow DOM during composition [2].
[1] http://jsbin.com/wimuqowimu/15/edit?html,js,console,output (works in Chrome/FF)
[2] http://jsbin.com/pacisugiwu/1/edit?html,js,output (only works in Chrome)
Edge behavior is close to FF behavior and Safari close to Chrome.
See also debate about this here: https://lists.w3.org/Archives/Public/www-dom/2015JulSep/0044.html
If one moves the caret/selection to a different element when
compoisitionstartis triggered, browsers different oncompositionend:compositionendevent on the element where composition takes place.compositionendevent on the element where the caret was at the time when thecompositionstartevent was triggered.Additionally, browsers behave differently if the caret is moved to a different element when
compositionendis triggered:The Chrome/Safari behavior is desired for the use case where one wants to have IME character insertion into the document stream be atomic -- either by doing composition somewhere else [1] or by using a Shadow DOM during composition [2].
[1] http://jsbin.com/wimuqowimu/15/edit?html,js,console,output (works in Chrome/FF)
[2] http://jsbin.com/pacisugiwu/1/edit?html,js,output (only works in Chrome)
Edge behavior is close to FF behavior and Safari close to Chrome.
See also debate about this here: https://lists.w3.org/Archives/Public/www-dom/2015JulSep/0044.html