views:

356

answers:

3

I was wondering how online rich text editors maintain the formatting when you paste text from a webpage or document. A standard textarea box only takes text while these WYSIWYG editors seem to use a DIV. How does it work?

+8  A: 

WYSIWYG Editors actually build on top of basic HTML Editing functionality that the browsers already have built in. In Firefox, the technology is called Midas. In IE, contentEditable.

Pekka
Curious: do Safari and Chrome use similar technology? Is something like this available in most modern browsers?
hora
Yes and yes: http://operawiki.info/TextAreaEditor I don't know what it's called for Chrome.
Pekka
+4  A: 

Online rich text editors use contentEditable or designMode to take advantage of the browser's native support for HTML editing. When you paste into a contentEditable or designMode element, the browser puts HTML directely into the element. Try it yourself by pasting into Midas Demo and then using Firebug's inspect element to look at the HTML you pasted.

JavaScript applications can use the execCommand method to format the user's selection in a rich text editor.

Annie
+1  A: 

By using existing browser capabilities (IE - ContentEditable). This allows the developer to let the user edit html directly. They usually use an iFrame to separate the editable section from the rest of the page, but this is not required.

Then the developer can simply read the html source of the iframe (or whatever) and they're done.

Gabriel McAdams