bmcmahen / react-wysiwyg

retain some control over contenteditable input
169 stars 37 forks source link

Make paste behavior mirror the behavior browsers typically exhibit. #29

Closed cgc closed 9 years ago

cgc commented 9 years ago

This PR implements the typical behavior for pasting into a textarea or input text box. It's a considerable departure from the current implementation, but it is well-tested and (I'd claim) more intuitive than the current behavior. paste-demo

This implementation follows the following rules:

  1. When a paste occurs with no selection (more explicitly represented as a selection of range 0), the paste content is inserted at the cursor position and the cursor position is set to the end of the pasted content.
  2. When a paste occurs with a selection, we delete the selected content and then do case 1.

    Open questions:

    • should the input accept plain text like it does now, or should it insert HTML that is pasted to preserve the formatting of the content?
    • are there security concerns in inserting pasted HTML?
    • Should parsing/rendering pasted HTML be an option?
    • [ ] Might not work when textarea has no content.
bmcmahen commented 9 years ago

Woah, nice. Thanks! I'll take a look over the next few days (on holiday right now), and will merge as soon as possible.

bmcmahen commented 9 years ago

This looks good, thanks.

There are security concerns pasting html. It should be escaped, as you've done, but indeed, we should allow the user to potentially override the escaping behaviour. I think it's possible with a onPaste event, and the user can control paste behaviour in their parent component.