I am working on an online rich-text editor, similar to the WordPress page creator, or the Stack Overflow post creator. It has been pointed out that there are two distinct types of online rich-text editors:
- WYSIWYG editors, and
- HTML editors
I'm building the second type. Unfortunately, neither <textarea>
nor using contenteditable
are very convenient for HTML rich-text editing.
The problem with <textarea>
(as used here in Stack Overflow) is that you can't show text-level semantics in the edit field. You can't just highlight a word and make it bold, you have to insert some kind of markup (e.g. *****bold*****). Not very user friendly and not really "rich" text either.
On the other hand, using contenteditable
solves those problems but introduces a problem of control. Browsers will insert all sorts of HTML and CSS to make the edit field look good. If you hit Enter, the browser will insert <p>
, or <div>
, or <br>
, or <div><br></div>
... depending on the browser. If you paste in a few paragraphs copied from HTML, you get tons of excessive markup--beyond what was even in the source HTML. For example, the following source code:
<p>This is one paragraph!</p>
<p>This is another.</p>
Shows up on a website as:
This is one paragraph!
This is another.
...which, if you copy and paste into a contenteditable
form, can give you something like this:
<p style="line-height: 1em; color: rgb(34, 34, 34); font-size: 12px;
white-space: normal;">This is one paragraph!</p>
<p style="line-height: 1em; color: rgb(34, 34, 34); font-size: 12px;
white-space: normal;">This is another.</p>
...bringing inline styles with it, and in some cases additional HTML.
I've been trying to figure out how to limit and control the amount of HTML that the browser inserts in a contenteditable
element, but I am beginning to think that contenteditable
is only suited for WYSIWYG type editing, and will never work for HTML style rich-text editing.
Is there a third alternative, or has anyone built some sort of Javascript editor that meets my needs?
**bold**
, but there's a real-time preview that looks like bold (Re: Same as a standalone editor: stackedit.io) – Eklundbold
, ** isitalic
, what aboutblockquote
,pre
,kbd
,small
,abbr
,code
,sub
,u
,ol
,ul
,li
, etc...? And that's only the text level stuff. What if I want an image or asection
? – Scandura