Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beforeinput: Should we fire replaceContent during IME composition? #122

Closed
chong-z opened this issue Apr 12, 2016 · 5 comments
Closed

beforeinput: Should we fire replaceContent during IME composition? #122

chong-z opened this issue Apr 12, 2016 · 5 comments

Comments

@chong-z
Copy link

chong-z commented Apr 12, 2016

From my understanding replaceContent should be fired when the existing text will be changed, for example:

  1. Type "abc" in an input field (insertText should be fired for each letter)
  2. Select "bc" (no beforeinput event)
  3. Press "d" (replaceContent should be fired instead of insertText)

But for an IME composition text will usually keep changing while typing, and after composition the marked text will be replaced by the final text once again.
So should the browser use the logic like:

  1. If the old composition text is a prefix of the new one, send insertText
  2. Otherwise send replaceContent?

Also I'm not sure why do we need replaceContent instead of using insertText with a targetRanges? (I could't find replace* in https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Commands, they only got insertText I guess?)

@johanneswilm
Copy link
Contributor

@choniong: it seems insertText (formerly insertCharacter) is separate from replaceContent because the insertText event is not cancelable during composition and because cE=typing will have a default action for insertText, but not for replaceContent.

  1. Type "abc" in an input field (insertText should be fired for each letter)
  2. Select "bc" (no beforeinput event)
  3. Press "d" (replaceContent should be fired instead of insertText)

That may sound a bit strange, but instead of just selecting one character, the selection could also span over a more complex piece of piece of the DOM. This means it would be difficult to handle the two correctly in JS if the two were merged in cE=typing. One would have to check:

  • Whether the inserted content is plain text.
  • Whether the selection is collapsed.

Only if both are true would there be a default action, otherwise no.

Also I'm not sure why do we need replaceContent instead of using insertText with a targetRanges? (I could't find replace* in https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Commands, they only got insertText I guess?)

Indeed, it seems not all the actions we need can necessarily be defined in terms of execCommand commands, and vice versa. This was just an idea that came out of the browser meeting. It hasn't been discussed or investigated much beyond that.

@chong-z
Copy link
Author

chong-z commented Apr 14, 2016

@johanneswilm Thanks for the explanation!

So if I'm understanding it correctly we only have insertText and deleteComposedCharacter during composition (but not replaceContent), my concern is how do we reflect changes to existing marked text? Or do we only care about user's actions?

For example on Mac with Japanese IME pressing the following key:

  1. s, marked text: 's' (with an underline)
  2. i, 'し'
  3. m, 'しm'
  4. i, 'シミ'
  5. ArrowDown, marked text will keep changing

Do we just fire insertText for step 1-4 even if the existing marked text was changed?

Here is a demo for the above example:
japime

Thanks!

@johanneswilm
Copy link
Contributor

johanneswilm commented Apr 14, 2016

It is my understanding that we first send deleteComposedCharacter and then insertText to add the new characters.

Similarly, if one selects some content, and then starts an IME input session: This will be divided into two steps: first deleteContent (which can be canceled), and if the caret then is collapsed and left in a place where it can insertText, the composition is started there with an insertText event.

One difference is of course that during the composition, the beforeInput events are not cancelable and if the selection is moved, the composition ends.

Btw, your demo above crashed both Chrome on Android and Linux after about a minute or so.

@chong-z
Copy link
Author

chong-z commented Apr 14, 2016

OK I see, delete then insert could work. Thanks for the help!

And sorry for the crash, I guess I should reduce gif size before uploading... (8MB for this one)

@johanneswilm
Copy link
Contributor

This seems to have been resolved. Please reopen if incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants