[whatwg] Ongoing work on an editing commands (execCommand()) specification

Tim Down timdown at gmail.com
Thu Apr 7 14:57:31 PDT 2011


On 7 April 2011 18:36, Aryeh Gregor <Simetrical+w3c at gmail.com> wrote:
> On Wed, Apr 6, 2011 at 7:40 PM, Tim Down <timdown at gmail.com> wrote:
>> Is there an overwhelming reason why execCommand() should be restricted
>> to contentEditable/designMode elements, as the spec seems to suggest?
>
> IIRC from testing, that's how all browsers but IE9 behave.  I guess
> the reason is that if you have a typical WYSIWYG editor, which has
> non-editable stuff surrounding it, and the user has selected some
> non-editable stuff on the page and clicks the "bold" button, you don't
> want the non-editable stuff to be bolded -- the button should only
> affect the editable area.
>
> Also, in a typical case, you're letting the user edit so that the
> markup will be saved or submitted to a server, and the markup that
> you'll save or submit is probably only the editable stuff.  So if you
> do something like push down styles from outside the editable area,
> that change will not only unexpectedly affect the appearance of the
> non-editable part of the page, the change might not get saved.
>
> Of course, this makes it less convenient to use the commands for
> non-editing purposes, but they're mostly not very useful for that
> anyway.  Do you have particular use-cases for using execCommand()
> outside of contentEditable/designMode?  I'll probably put off tackling
> details like this until a much later date, once I've specified the
> algorithms themselves to a satisfactory extent.

I don't recall ever wanting to use execCommand() in non-editable
content myself (although I wouldn't rule it out), but I've answered a
few questions on Stack Overflow where the asker has wanted to
highlight (i.e. change the background colour of) the current
selection. I've suggested temporarily putting the document into
designMode, calling execCommand() with "HiliteColor" / "BackColor" and
turning designMode off again.

Here's an example of someone wanting to do this for a bookmarklet:

http://stackoverflow.com/questions/3223682/change-css-of-selected-text-using-javascript

I found about five others that were not so specific about why they
wanted it, but all wanted to highlight the selected text using a
background colour. One more example:

http://stackoverflow.com/questions/1622629/javascript-highlight-selected-range-button

Tim



More information about the whatwg mailing list