I need to create a simple rich-text editor that saves its contents to an XML file using arbitrary markup to indicate special text styles (e.g: [b]...[/b]
for bold and [i]...[/i]
for italic). All the backend PHP stuff seems fairly straightforward, but the front-end WYSIWYG portion of the feature seems a bit more convoluted. I've been reticent to use one of the currently-available JavaScript-based WYSIWYG editors because the rich-text options I want to allow are so limited, and these applications are so fully-featured that it almost seems like more work to stip them down to the functions I need.
So, in setting out to create a bare-bones rich-text editor, I've encountered three approaches:
- The first two approaches use the
contentEditable
ordesignMode
properties to create an editable element, and theexecCommand()
method to apply new text styles to a selected range.- The first option uses a standard
div
element, executes all styling commands on that elements contents.
- The first option uses a standard
- The second option uses the editible
body
of a window enclosed in aniframe
, then passes any styling commands initiated from buttons in the parent document into itscontentWindow
to alter selected ranges in the contained body. This seems like several extra steps to accomplish the same effect as option one, but I suppose the isolation of the editable content in its own document has its advantages. - The third option uses a
textarea
overlaying adiv
, and uses theoninput
JS event to update the background div'sinnerHTML
to match the input textarea'svalue
whenever it changes. Obviously, this requires some string finagling to to convert elements likenewline
characters in the textarea to<br/>
in the div, but this would allow me to preserve the integrity of my[/]
markup, while relegating the potentially-messy DOM manipulation to front-end display only.
I can see benefits and drawbacks for each method. the contentEditable
solutions seem initially the simplest, but support for this features tends to vary across browsers, and each browser that DOES support it seems to manipulate the DOM differently when implementing execCommand()
. As mentioned before, the textarea/div solution seems like the best way to preserve my arbitrary styling conventions, but the custom string-manipulation procedure to display rich text in the output div could get pretty hairy.
So, I submit to you my question: Given the development goals I've outlined, which method would you choose, and why? And of course, if there's another method I'm overlooking that might better serve my purpose, please enlighten me!
Thanks in advance!