You are currently viewing a snapshot of www.mozilla.org taken on April 21, 2008. Most of this content is highly out of date (some pages haven't been updated since the project began in 1998) and exists for historical purposes only. If there are any pages on this archive site that you think should be added back to www.mozilla.org, please file a bug.



Last revised:  August 23, 2005

This document describes the rich text editing controls for web pages available in recent mozilla browsers.

Enabling Rich Text Editing in your web page:

Given a document, you can add the attribute "designMode" and set it to "on" to get an editable document.  For example, in JavaScript, if you have an iframe with an id of 'edit', you can get its contentDocument and set designMode to "on" like this:
document.getElementById("edit").contentDocument.designMode="on";

Right now, you can't completely turn off editing by setting designMode to "off."  Setting designMode to "off" will prevent certain operations from being handled but typing and other actions are still possible.

Invoking Commands:

execCommand

Given a document that has rich text editing enabled (see above), you can invoke specific commands on the document by calling execCommand with 3 parameters.  For example, in JavaScript, if you have an editable document, you can invoke the bold command by calling this:
editableDocument.execCommand("Bold", false, null);

Notes:  If you haven't set designMode to "On", you will get an error.  This could also happen if you call execCommand with the wrong document.

There are 3 required parameters for execCommand:
  1. command string
  2. boolean flag for showing UI
  3. value string
The first parameter is a string which contains the command.  The second parameter is a boolean flag.  If it is set to true, you will get an error (NS_ERROR_NOT_IMPLEMENTED).  The third parameter is a string which is the value.  Some commands will require details such as the particular size you want to set when setting a font size.

The section on Supported Commands will document each command and any corresponding values needed.

queryCommandEnabled

This command operates on the editable document.  There is one required parameter (the command string).  The result is a boolean which is true if the command is can be done given the current selection and/or caret position.  The result is false if the command should not be invoked (execCommand) given the current selection and/or caret position.

queryCommandState

This section needs to be written.  It can be called similarly to IE implementation.

queryCommandValue

This section needs to be written.  It can be called similarly to IE implementation.

Supported Commands:

The following list of commands is presented in alphabetical order.  The commands may be mixed case or whatever makes your code more readable.

command value explanation / behavior
backcolor ???? This command will set the background color of the document.
bold none If there is no selection, the insertion point will set bold for subsequently typed characters.

If there is a selection and all of the characters are already bold, the bold will be removed.  Otherwise, all selected characters will become bold.
contentReadOnly true
false
This command will make the editor readonly (true) or editable (false).  Anticipated usage is for temporarily disabling input while something else is occurring elsewhere in the web page.
copy none If there is a selection, this command will copy the selection to the clipboard.  If there isn't a selection, nothing will happen.

note: this command won't work without setting a pref or using signed JS.  See: http://www.mozilla.org/editor/midasdemo/securityprefs.html

note:  the shortcut key will automatically trigger this command (typically accel-C) with or without the signed JS or any code on the page to handle it.
createlink url (href) This command will not do anything if no selection is made.  If there is a selection, a link will be inserted around the selection with the url parameter as the href of the link.
cut none If there is a selection, this command will copy the selection to the clipboard and remove the selection from the edit control.  If there isn't a selection, nothing will happen.

note: this command won't work without setting a pref or using signed JS.  See: http://www.mozilla.org/editor/midasdemo/securityprefs.html

note:  the shortcut key will automatically trigger this command (typically accel-X) with or without the signed JS or any code on the page to handle it.
decreasefontsize none This command will add a <small> tag around selection or at insertion point.
delete none This command will delete all text and objects that are selected.
fontname ???? This command will set the fontface for a selection or at the insertion point if there is no selection.
fontsize ???? This command will set the fontsize for a selection or at the insertion point if there is no selection.
forecolor ???? This command will set the text color of the selection or at the insertion point.
formatblock <h1>
<h2>
<h3>
<h4>
<h5>
<h6>
<pre>
<address>
<p>
p
[this list may not be complete]

heading <h1>
<h2>
<h3>
<h4>
<h5>
<h6>

hilitecolor ???? This command will set the hilite color of the selection or at the insertion point.  It only works with usecss enabled.
increasefontsize none This command will add a <big> tag around selection or at insertion point.
indent none Indent the block where the caret is located.
inserthorizontalrule none This command will insert a horizontal rule (line) at the insertion point.

Does it delete the selection?
inserthtml valid html string This command will insert the given html into the <body> in place of the current selection or at the caret location.
insertimage url (src) This command will insert an image (referenced by url) at the insertion point.

Does it delete the selection?
insertorderedlist none
insertunorderedlist none
insertparagraph none
italic none If there is no selection, the insertion point will set italic for subsequently typed characters.  

If there is a selection and all of the characters are already italic, the italic will be removed.  Otherwise, all selected characters will become italic.
justifycenter none
justifyfull none
justifyleft none
justifyright none
outdent none Outdent the block where the caret is located.  If the block is not indented prior to calling outdent, nothing will happen.

note:  is an error thrown if no outdenting is done?
paste none This command will paste the contents of the clipboard at the location of the caret.  If there is a selection, it will be deleted prior to the insertion of the clipboard's contents.

note: this command won't work without setting a pref or using signed JS.
user_pref("capability.policy.policynames", "allowclipboard"); user_pref("capability.policy.allowclipboard.Clipboard.paste", "allAccess");
See: http://www.mozilla.org/editor/midasdemo/securityprefs.html

note:  the shortcut key will automatically trigger this command (typically accel-V) with or without the signed JS or any code on the page to handle it.
redo none This command will redo the previous undo action.  If undo was not the most recent action, this command will have no effect.

note:  the shortcut key will automatically trigger this command (typically accel-shift-Z)
removeformat none
selectall none This command will select all of the contents within the editable area.

note:  the shortcut key will automatically trigger this command (typically accel-A)
strikethrough none If there is no selection, the insertion point will set strikethrough for subsequently typed characters.

If there is a selection and all of the characters are already striked, the strikethrough will be removed. Otherwise, all selected characters will have a line drawn through them.
styleWithCSS true
false
This command is used for toggling the format of generated content.  By default (at least today), this is true.  An example of the differences is that the "bold" command will generate <b> if the styleWithCSS command is false and generate css style attribute if the styleWithCSS command is true.
subscript none If there is no selection, the insertion point will set subscript for subsequently typed characters.  

If there is a selection and all of the characters are already subscripted, the subscript will be removed.  Otherwise, all selected characters will be drawn slightly lower than normal text.
superscript none If there is no selection, the insertion point will set superscript for subsequently typed characters.

If there is a selection and all of the characters are already superscripted, the superscript will be removed.  Otherwise, all selected characters will be drawn slightly higher than normal text.
underline none If there is no selection, the insertion point will set underline for subsequently typed characters.

If there is a selection and all of the characters are already underlined, the underline will be removed.  Otherwise, all selected characters will become underlined.
undo none This command will undo the previous action.  If no action has occurred in the document, then this command will have no effect.

note:  the shortcut key will automatically trigger this command (typically accel-Z)
unlink none

DEPRECATED COMMANDS
readonly true
false
This command has been replaced with contentReadOnly.  It takes the same values as contentReadOnly, but the meaning of true and false are inversed.
useCSS truefalse This command has been replaced with styleWithCSS.  It takes the same values as styleWithCSS, but the meaning of true and false are inversed.