I intended to use Document.execCommand()
method along with contenteditable
attribute to build my custom WYSIWYG editor. But when I checked the documentation for Document.execCommand()
, I found that it's now obsolete. What's the modern (or extant) alternative for it?
I created the Rich Editor for my platform's XML (HTML5 + XHTML) editing purposes. I would not say that document.execCommand()
is completely dead because some parts of it still work fine. Unfortunately the primary issue for me was that browsers use a lot of different code to generate those styles which are not recognized by screen readers used by those who are blind or nearly so.
Additionally the most expensive time bug I ever had to conquer was a Gecko/Presto bug where the visual and technical selections (why they aren't the same thing, don't ask me) would result in part of the DOM being changed that the user did not intend and this would come down to the fact that the pixel count per character is low so if the Rich Editor did not honor visual selections a user would very quickly walk away. That required four months to conquer and there are other bugs too.
Ultimately it's a harsh though achievable endeavor though if you intend to build an HTML/XML editor like I did you should plan for at least six months if you plan to not only do it properly though test it to the point of hating cake to then only have someone come along and point out yet another bug.
Your primary focus JavaScript wise should be on the following:
document.createRange()
window.getSelection()
appendChild
insertBefore
insertBefore
+ nextSibling
replaceChild
In place of inconsistent code generated by using execCommand()
from different browsers (often setting inline styling which would complicate your site's CSS if not outright negating it) you should stick to using the following elements which you not only can have control over though are compatible with screen readers:
em
for emphasis (or "italics", <i>
is deprecated).strong
for strongly read text (or "bold", <b>
is deprecated).u
for underline (be sure your anchors are styled to differentiate from u elements; u
might be considered "deprecated" though I will reverse this when I fix the standards in the next ten years or so, use it appropriately).sub
for sub-line text that appears vertically lower than normal text.sup
for supper-line text that appears vertically higher than normal text.<span>
element to specifically add these styles as screen readers will not understand or reveal buggy behavior; it is still a valid generic inline element when used appropriately.I actually have been intending to revise my Rich Editor (it's been getting patched though not yet properly rewritten) though you're welcome to look at the source code when it loads on a blog page in the site linked in my profile. The original project took me 11 months though with my experience now I think it would take me about three to four. If you're serious I highly recommend staying the hell away from frameworks and libraries. "But ... but, they make life easier!" ... until you want to use a new version and have to rewrite the entire project. Use pure JavaScript the first time and negate pointless maintenance. Good luck!