Search code examples
javascriptjquerybrowserwysiwygdom-events

How do some WYSIWYG editors keep formatting of pasted text?


How do some WYSIWYG editors keep formatting of pasted text? As an example, I copied italic red text from a text-editor into a WYSIWYG and it kept the text's color and style, how is this happening? For the longest I thought JavaScript had access the clipboards text only. Is this not the case? If so then, what is it?


Solution

  • There's a content type negotiation between the source and target during the copy/paste operation. It happens sort of like this:

    1. You copy something into the copy and paste buffer. The copied data is tagged with, more or less, a MIME type and who put it there.
    2. When you paste, the paste target tells the copy-and-paste system that it understands a specific list of MIME types.
    3. The copy-and-paste system matches the available formats to the desired formats and finds text/html in both lists.
    4. Someone (probably the original source of the data) then converts the paste buffer to text/html and drops it in the editor.

    That's pretty much how things worked back when I was doing X11/Motif development (hey! get off my lawn you rotten kids!) so I'd guess that everyone does it pretty much the same way.