You are here: Editor project page > How the Editor Works
In this document
- The high-level picture
- Editor instantiation in a XUL window
- Editor teardown
- Editor event handling
This document describes how the editor is instantiated, and handles events, for the Composer window. This is very similar to its use in the mail compose window, and will relate to most embedding applications. For details on how the editor is created for text widgets, see the text widgets doc.
Figure 1: high-level relationships between editor components in the composer window. Solid arrows indicate common interaction paths, open arrows less important paths.
Some aspects of this picture deserve comment.
- First note that all the interaction between XUL/JS goes via the
Second, note that the editor core (
nsHTMLEditor) knows nothing of the
nsEditorShell. The editor is agnostic to who is driving it, and it's not tied to any specific front-end or environment.
- Third, note that the document being edited does not know that it is being edited. Editor acts on the document, but the document doesn't need to know anything about the editor.
The editor in XUL lives on top of a XUL
<iframe> element; it observes
document loading in this
<iframe>, and, when document loading is
complete, it instantiates an editor on the loaded document. The
contents are then editable.
The job of observing doc loading in the
<iframe> is performed
nsEditorShell, a class which implements all of the communication between
nsEditorShell implements the
The editor shell creates the editor when the time is right, holds the owning reference
to the editor object, and releases it when the XUL window is closed.
Note: We are currently transitioning to a new tag specifically for editor, called, not surprisingly,
<editor>. This is really just an
fragments here may be slightly different from what you see in the codebase.
Something, somewhere, tells Mozilla to open the composer window.
editor.xulis loaded. In
<window>tag has an onload handler:
onload="EditorOnLoad()". That causes the
The XUL contains an
<editor type="content-primary" id="content-frame" src="about:blank" flex="1"/>
type="content-primary"identifies this as the window content element, i.e. that which you get from
window._content. Having an
id="content-frame", allows us to find this element with
document.getElementById("content-frame"), and to style it from CSS.
EditorOnLoad()is called. It does some getting of window.arguments (which is a way callers can pass parameters to new windows -- we use this to get the URL to be loaded), then it calls
EditorStartup(), where the real work happens. It passes two parameters; the first indicates whether we want a plain text or HTML editor (pass
'html'here), and the second is the
<iframe>element on which we wish to create the editor. We could either pass
The important stuff in
EditorStartup()begins where we get or create an editorShell. Because of the
<editor>transition, you'll see two patterns here:
<editor>tag actually creates an nsEditorBoxObject behind the scenes. The nsEditorBoxObject creates an nsEditorShell, and holds the owning reference to it. Through the magic of XBL, the XUL bindings, and the nsIEditorBoxObject interface, you can get a JS reference to the editorShell from the editor element with
element.editorShell. We thus have an editorShell to play with.
In the absence of an
<editor>tag, we have to make the editorShell by hand in the JS:
var editorShell = Components.classes["component://netscape/editor/editorshell"].createInstance(); editorShell = editorShell.QueryInterface(Components.interfaces.nsIEditorShell);
Again, now we have an editorShell to play with.
Now we set up the editorShell by calling its Init() method, telling it what type of editor we want (text or HTML), pointing it at the webShellWindow to use, and telling it the content node that it lives on:
editorShell.Init(); editorShell.SetEditorType(editorType); editorShell.webShellWindow = window; editorShell.contentWindow = window._content;
webShellWindow(a settable attribute on nsIEditorShell) points to the top-level window element, from which the editorShell can get the XUL document in which it is living. It needs this to poke the UI (e.g. for command state maintenance, starting and stopping the throbber etc.).
contentWindow(another settable attribute on nsIEditorShell) points to the XUL element which is to become editable. [Note: since we already know this when we have an
<editor>tag, we should remove the need to call this.]
EditorStartup() does some other minor bits of setup before finally kicking off the URL load, which the most important part here.
When the XUL was parsed, the
srcattribute on the content frame was set to
about:blank(our default 'blank page' URL). We can't set that before XUL parsing, so we have to force a load of the page we now want to edit. We get the URL to load from the
argselement, then kick off the load:
var url = document.getElementById("args").getAttribute("value"); editorShell.LoadUrl(url);
Loading the document in the
<iframe>of course happens asynchronously, so we need to know when we have a document that we can start editing.
nsEditorShell is able to observe the document load on the
<iframe>, because it implements
nsIDocumentLoaderObserver, and registered itself as a doc loader when it was assigned the content window. It thus gets callbacks for the start, progress, and end of the document load.
these callbacks also fire for every subdocument that loads as a result of the parent document load, for example with frameset documents, or HTML documents with their own embedded
<iframe>s. In this case, we need to be careful to instantiate the editor on the correct document. We are currently only able to have one editor per composer window; in future, relaxing this restriction would allow us to edit all the subdocuments in a frameset at the same time.
We detect that the document we want to edit has loaded successfully in
nsEditorShell::OnEndDocumentLoad(). After checking that we can actually edit this document, we go ahead and instantiate an editor on it (in
nsEditorShell::PrepareDocumentForEditing()). As well as making the editor (which happens via
nsEditorShell::DoEditorMode()) we also hook up various listeners and observers for UI updating and user interaction, and store a file specifier for the document we opened.
The editor is now set up, and ready to go.
One thing to note about editor initialization is that we pass into the editor's
nsIContent*that corresponds to the root of the content tree that the editor is allowed to work with. When initializing the editor from the nsEditorShell, we pass
NULLhere (which tells the editor that it can edit everything under the
<body>of the document). This parameter is more important when the editor is in a text widget, where it points to the the subtree of the parent document that corresponds to widget content.
Window destruction, and hence editor teardown is initiated in two ways, listed
below. In both cases, the
is called, which causes the nsEditorShell to display a dialog asking the user
if they want to save the document, throw away their changes, or cancel. Note that
if they cancel, the close operation is aborted.
The user clicks the Close widget in their OS/window manager. In this case,
onclosemethod on the
<window>tag is called.
The user chooses 'Close' from the File menu, uses the key shortcut,
or quits the application, causing all windows to be closed. Before each
tries to call a
tryToClosemethod on each window. In
editor.js, we set this to call
If the user chooses to save the document, or throw away their changes,
then the window is closed. When the last reference to the
<iframe> case, or the nsEditorBoxObject releasing its reference in the
<editor> case) it releases the owning reference on the editor.
The following event listeners are registered:
nsHTMLEditor::InstallEventListeners(), we install the following. These get installed for all types of editor (i.e. for text widgets and composer):
nsEditorShell::PrepareDocumentForEditing(), we install a mouse listener. This only happens for situations where the nsEditorShell is used (i.e. not for text widgets):
This event listener handles key presses for typing, and other editing operations (backspace, delete, enter/return).
Cases that it does not handle explicitly it passes on to
which is where normal typing keys end up. Note that it only responds to the
KeyUp events are ignored.
The mouse listener is used to do middle-mouse paste (which is a Unix copy/paste feature). This happens
in response to
MouseClick with button 2. It also forces an IME commit.
Editor responds to
Blur events by showing and hiding the caret or selection as appropriate.
nsIDOMTextListener interface that this implements is used by the IME code. In response
HandleText event, the editor sets the inline input composition string.
nsTextEditorCompositionListener implements another IME-related interface,
nsIDOMCompositionListener. This is called by IME at the start, end, and to query the current
The drag listener handles drag and drop events in the editor. It responds to the start of a drag in
DragGesture by adding data to the drag, notifies the drag whether a drop can occur in
DragOver, and handles the drop by inserting data in
This is an odd-man-out event listener, in that it's registered from the editorShell, rather than
internally to the editor. [Note: this is ugly, and should probably be redesigned to work on callbacks out of
nsEditorShell::HandleMouseClickOnElement to show property dialogs for items that you double-click on.
So what happens to a key press once it's got to the
nsTextEditorKeyListener? How does that
end up in the document? Let's trace through.
nsTextEditorKeyListener::KeyPress()gets the key press event. For normal character keys, that falls into
nsHTMLEditor::EditorKeyPress()gets the character code from the key event, puts that into a string, and calls
nsHTMLEditor::TypedText(), which simply calls
nsHTMLEditor::InsertText()hides quite a bit of complexity in some stack-based classes.
nsAutoPlaceHolderBatchis a utility class that wraps text insertion with calls to turn off selection and layout updating (to avoid flicker), and the maintenance of a placeholder transaction. This placeholder transaction enables us to batch typing events together, so that an Undo undoes the whole series of keystrokes.
Another stack-based class,
nsAutoRules, ensures that text insertion is wrapped with calls to
EndOperation(). These functions call
AfterEdit()on the current typing rules.
Now, we initialize a
nsTextRulesInfowith the information about the string being inserted, and call
WillDoAction()on the current editing rules. Because the implementation of inserting text differs between the different rules (plain text vs. HTML, for example), it is handled entirely by the rules code, in the
In Composer, we are using
nsHTMLEditRules, so we end up in
nsHTMLEditRules::WillDoAction(). For text insertion, this drops into
nsHTMLEditRules::WillInsertText(). This code first deletes the selection if there is one (e.g. you are typing over selected text), then calls a generic pre-insertion call
WillInsert(), which sets up inline styles for the inserted text, and moves the selection to an appropriate place where the text is to be inserted.
Now we are ready to actually insert the text. Recall that we're going through a generic
InsertText()call, so this code deals with pasting long strings, as well as inserting single characters. The code thus has to do the correct thing with linebreaks, so has a special case for inserting into
<pre>sections. We call into the normal insertion code, which loops through the input string looking for linebreaks, and inserts each text run, followed by a
<br>when necessary. When handling key presses, this will just insert a single character.
We fall out of the
WillDoAction()call, and drop into
WillDoAction(), which, for text insertion, does nothing.
The last thing that happens on a keypress is that
ScrollSelectionIntoView(), which, as the name suggests, ensures that the text that was just entered is visible.