Warning: The content of this article may be out of date.
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
<iframe> contents are then editable.
Note that the
<editor> element is really just an
editor element in editor.xml.
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
htmlhere), 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. The
<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
element.editorShell. We thus 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></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.
Note: 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
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
- The user clicks the Close widget in their OS/window manager. In this case, the
onclosemethod on the
<window>element is called.
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
nsEditorShell goes away (as a result of
nsEditorBoxObject releasing its reference) it releases the owning reference on the editor.
Editor event handling
nsEditorShell. This editor command dispatching is described separately.
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
nsEditorShellis used (i.e. not for text widgets):
Note: Starting in Gecko 12, the editor refuses any events sent by unprivileged content.
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
nsHTMLEditor::EditorKeyPress(), 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 to the
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 composition.
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
nsEditorShellMouseListener essentially calls
nsEditorShell::HandleMouseClickOnElement to show property dialogs for items that you double-click on.
The path of a key press
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.
Original Document Information