<?xml version='1.0' encoding="utf-8"?>
<!--
/* ***** BEGIN LICENSE BLOCK *****
 * Licensed under Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * Full Terms at http://mozile.mozdev.org/0.8/LICENSE
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is James A. Overton's code (james@overton.ca).
 *
 * The Initial Developer of the Original Code is James A. Overton.
 * Portions created by the Initial Developer are Copyright (C) 2005-2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *	James A. Overton <james@overton.ca>
 *
 * ***** END LICENSE BLOCK ***** */

/**
 * @fileoverview High level documentation for version 0.8 of the Mozile project.
 * <p>Project Homepage: http://mozile.mozdev.org 
 * @author James A. Overton <james@overton.ca>
 * @version 0.8
 * $Id: mozile.xml,v 1.11 2006/11/07 20:30:55 jameso Exp $
 */
-->

<?xml-stylesheet href="../docbook-css-0.4/driver.css" type="text/css"?>
<book xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink/">
	<title>Mozile 0.8 Documentation</title>
	<author>James A. Overton</author>

	<chapter id="Introduction">
		<title>Introduction</title>
		
		<section id="WhatIsMozile">
			<title>What is Mozile?</title>
			<para>Mozile adds what-you-see-is-what-you-get (<acronym>WYSIWYG</acronym>) editing to your web pages. The name "Mozile" comes from <emphasis role="bold">Moz</emphasis>illa <emphasis role="bold">I</emphasis>n<emphasis role="bold">l</emphasis>ine <emphasis role="bold">E</emphasis>ditor. As of version 0.8, Mozile is no longer limited to Mozilla browsers like Firefox. But it's still an inline editor, which means that it allows you to edit the content of a web page without switching to another program, embedding an <code>IFRAME</code>, or using a special mode. With Mozile you can just click in the page and start editing, like you're using a word processor.</para>
			<para>Mozile is an open source project, and is distributed free of charge under the terms of our <link href="http://mozile.mozdev.org/0.8/LICENSE">license</link>. Please see our homepage at <link href="http://mozile.mozdev.org">http://mozile.mozdev.org</link> for more information. Mozile is written using JavaScript, and version 0.8 is designed to work in most modern browsers, including <link href="http://www.mozilla.com/firefox/">Mozilla Firefox</link> and <link href="http://www.microsoft.com/windows/ie/">Internet Explorer</link>. However Mozile is still in development, and only Firefox 1.5 and Internet Explorer 6 are currently supported.</para>
		</section>
		
		<section id="WhyUseMozile">
			<title>Why Use Mozile?</title>
			<para>For a long time, people have been trying to edit rich content in their web browsers using HTML text boxes and awkward markup syntax. As wikis and content management systems become more popular, more and more content is being generated in our browsers. Why can't we have WYSIWYG editing, like in a word processor?</para>
			<para>Without Mozile, there are two incompatible ways to do rich text editing in a browser. Internet Explorer allows you to set a <code>contentEditable</code> attribute on an element, and then edit the contents. Firefox and other Mozilla browsers have a setting called <code>designMode</code>, which lets you edit the whole page, like the old Netscape Composer. The Internet Explorer method is missing some important features, like <emphasis>redo</emphasis>. The Firefox <code>designMode</code> won't let you pick which parts of a page are editable. But the worst part is that both of these methods are trapped in the last decade: they use out of date HTML code like <code>font</code> tags, and they have no idea what CSS is. You're limited to the small number of built-in editing commands.</para>
			<para>Mozile overcomes these problems in three ways:</para>
			<orderedlist numeration="arabic">
				<listitem>Mozile works the same way in both in Firefox and Internet Explorer, and we're working on support for other browsers as well.</listitem>
				<listitem>Mozile uses pure DOM methods to manipulate the document, so the code you get is nice, clean X/HTML, XML, and CSS.</listitem>
				<listitem>You can customize commands and add new ones, so you can edit the document in any way that JavaScript, DOM, and CSS allow.</listitem>
			</orderedlist>
		</section>
		
		<section id="GettingStarted">
			<title>Getting Started</title>
			<para>Here's how you can get a copy of Mozile, modify your pages to use Mozile, and configure Mozile's features.</para>
			
			<section id="GettingMozile">
				<title>Getting Mozile</title>
				<para>You can download the latest version of Mozile from our web site at <link href="http://downloads.mozdev.org/mozile/">http://downloads.mozdev.org/mozile/</link>. Simply unzip the archive and copy the files to an accessible location on your web server.</para>
				<para>The Mozile distribution includes demonstrations, documentation, a testing system, and tools for development. If you don't want to use these features, you can safely delete those folders: <filename>demos</filename>, <filename>doc</filename>, <filename>jsunit</filename>, <filename>tests</filename>, and <filename>tools</filename>. It is important to leave the <filename>images</filename>, <filename>lib</filename>, and <filename>src</filename> directories intact.</para>
			</section>
		
			<section id="UsingMozile">
				<title>Using Mozile</title>
				<para>You include Mozile in your web pages by adding an HTML <code>script</code> tag to the <code>head</code> of your HTML page. The <code>script</code> tag should look like this:</para>
				<example id="IncludingTheMozileScript">
					<title>Including the <filename>mozile.js</filename> script</title>
				<programlisting><![CDATA[<script type="text/javascript" src="path/to/mozile.js"></script>]]></programlisting>
				</example>
				<para>The <code>src</code> attribute must be a valid path from the web page to Mozile's <filename>mozile.js</filename> file.</para>
				<para>Next you have to specify which parts of the document are editable. You can do this in two ways:</para>
				<itemizedlist>
					<listitem>Set the <code>contentEditable</code> attribute to <code>true</code> to make that element editable.
						<example id="UsingContentEditable">
							<title>Making an element editable with <code>contentEditable</code></title>
						<programlisting><![CDATA[<div contentEditable="true">
	<p>This paragraph is editable.</p>
</div>]]></programlisting>
						</example>
					</listitem>

					<listitem>Use the <code>mozile.editElements("editor")</code> command  to edit all elements with the <code>class="editor"</code> attribute. See the <link href="#ConfiguringMozile">configuration section</link> below for more configuration options.
						<example id="UsingEditElements">
							<title>Making an element editable with <code>mozile.editElement("editor")</code></title>
						<programlisting><![CDATA[<div class="editor">
	<p>This paragraph is editable.</p>
</div>]]></programlisting>
						</example>
					</listitem>
				</itemizedlist>
				
			</section>
			
			<para>At this point you should be able to edit your web page with Mozile in any supported web browser.</para>
		
			<section id="ConfiguringMozile">
				<title>Configuring Mozile</title>
				<para>The default settings enable basic editing. But with another <code>script</code> element you can configure Mozile to suit your needs.</para>
				<example id="ConfiguringMozileInline">
					<title>Configuring Mozile</title>
				<programlisting><![CDATA[<script type="text/javascript" src="path/to/mozile.js"></script>
<script type="text/javascript">
	// Configuration Options
</script>]]></programlisting>
				</example>
				<para>If you are using the same configuration options in many files, you can include them in a separate <filename>config.js</filename> file which all your pages can share.</para>
				<example id="ConfiguringMozileWithAFile">
					<title>Configuring Mozile with a file</title>
				<programlisting><![CDATA[<script type="text/javascript" src="path/to/mozile.js"></script>
<script type="text/javascript" src="config.js"></script>]]></programlisting>
				</example>
				<para>Here are some of the most important settings and configuration commands you can use with Mozile. Below are some example and a longer description of how they work. Configuration for the save system is described in the <link href="#SavingChanges">next section</link>.</para>
				<itemizedlist>
					<title>Important Configuration Settings</title>
					<listitem><code>mozile.root</code> - Indicates where the <filename>mozile</filename> directory is relative to the document. Mozile tries to set this automatically, but sometimes it is necessary to set the root manually.</listitem>
					<listitem><code>mozile.useDesignMode</code> - When <code>true</code> the <code>document.designMode</code> will be used in browsers that support it. When false <code>designMode</code> is not used, and Mozilla/Firefox users will have to press <keycap>F7</keycap> to enable an editing cursor. Because of bugs with <code>designMode</code> the default is now <code>false</code>.</listitem>
					<listitem><code>mozile.alternateSpace</code> - Set this to <code>"\u00A0"</code> (a non-breaking space) if you want Mozile to insert spaces every time the user presses the space bar. This makes Mozile behave more like a word-processor. If no <code>mozile.alternateSpace</code> is specified, Mozile will keep the HTML code clean and ignore extra spaces. See the <link href="#TextAndWhiteSpace">notes on white space</link> below for more details.</listitem>
					<listitem><code>mozile.emptyToken</code> - By default this is set  to <code>"\u00A0"</code> (a non-breaking space). In order to avoid display bugs, the empty token is automatically inserted into any empty element which has CSS <code>display: block</code>. See the <link href="#TextAndWhiteSpace">notes on white space</link> below for more details.</listitem>
					<listitem><code>mozile.help</code> - The URL of a help page which will be opened by a help command. Defaults to <filename>mozile.root/doc/html/index.html</filename>.</listitem>
					<listitem><code>mozile.debug.alertLevel</code> and <code>mozile.debug.logLevel</code> - These can be set to <code>"suppress"</code>, <code>"warn"</code>, <code>"inform"</code>, or <code>"debug"</code>, in order from quietest to most verbose. They indicate what kinds of messages the user should be alerted to, and which should be logged. To turn off all messages use <code>"suppress"</code>.</listitem>
					<listitem><code>mozile.defaultNS</code> - Sets the default namespace URI. Should be <code>null</code> for HTML documents. For XHTML documents use <code>"http://www.w3.org/1999/xhtml"</code>. For XML documents use the XHTML namespace or the default XML namespace.</listitem>
				</itemizedlist>
				<itemizedlist>
					<title>Important Configuration Functions</title>
					<listitem><code>mozile.require(module name)</code> - Makes sure that a Mozile module is loaded.</listitem>
					<listitem><code>mozile.editElement(element or id)</code> - Makes a single element editable.</listitem>
					<listitem><code>mozile.editElements(list or class)</code> - Makes a set of elements editable.</listitem>
					<listitem><code>mozile.editDocument()</code> - Makes the whole document editable.</listitem>
					<listitem><code>mozile.protectElement(element or id)</code> - Protects a single element from editing.</listitem>
					<listitem><code>mozile.protectElements(list or class)</code> - Protects a set of elements from editing.</listitem>
					<listitem><code>mozile.useSchema(schema file)</code> - Tells Mozile to use an RNG schema to <link href="#SemanticStructure">structure editing</link>. The <code>schema file</code> argument is either an absolute path or a relative path from mozile.root to the schema file.</listitem>
					<listitem><code>mozile.enableEditing(rich)</code> - Enables editing without using an RNG schema. The <code>rich</code> argument can be <code>false</code> for basic text editing, and <code>true</code> for rich editing of elements. You probably want to use <emphasis>either</emphasis> <code>mozile.useSchema()</code> <emphasis>or</emphasis> <code>mozile.enableEditing()</code> but not both.</listitem>
				</itemizedlist>
				<para>Here are some examples of configuration settings.</para>
				<example id="ConfigureBasic">
					<title>Configure some basic Mozile options</title>
					<programlisting><![CDATA[<script type="text/javascript" src="path/to/mozile.js"></script>
<script type="text/javascript">
  // Specify the "mozile" directory.
  mozile.root = "path/to/";
  // Insert spaces like a word processor.
  mozile.alternateSpace = "\u00A0";
  // Wait until the document is loaded to run these commands.
  window.onload = function() {
    // Make all elements with class "editor" editable.
    mozile.editElements("editor");
    // Use the XHTML RNG schema.
    mozile.useSchema("lib/xhtml.rng");
  }
</script>]]></programlisting>
				</example>
				<example id="ConfigureNoSchema">
					<title>Configure Mozile to edit without an RNG schema</title>
					<programlisting><![CDATA[<script type="text/javascript" src="path/to/mozile.js"></script>
<script type="text/javascript">
  // Enable rich text editing without an RNG schema.
  mozile.enableEditing(true);
  // Wait until the document is loaded to run these commands.
  window.onload = function() {
    // Make all elements with class "editor" editable.
    mozile.editElements("editor");
  }
</script>]]></programlisting>
				</example>
				<example id="ConfigureXML">
					<title>Configure Mozile for XML editing</title>
					<programlisting><![CDATA[<script type="text/javascript" src="path/to/mozile.js"></script>
<script type="text/javascript">
  // Set a default XML namespace.
  mozile.defaultNS = "http://somewhere.com/xml-namespace";
  // Make the whole document editable.
  mozile.editDocument();
  // Wait until the document is loaded to run these commands.
  window.onload = function() {
    // Specify an RNG schema file to use.
    mozile.useSchema("schemas/custom-schema.rng");
  }
</script>]]></programlisting>
				</example>
				<para>The most important setting is <code>mozile.root</code>, which tells Mozile where to find the <filename>mozile</filename> directory containing all of the Mozile files. Mozile tries to automatically detect the location of the <filename>mozile</filename> directory by looking at the <code>src</code> attribute of the <code>script</code> tag which loaded <filename>mozile.js</filename> into the page. This should work, as long as the <filename>mozile.js</filename> is in the <filename>mozile</filename> directory. If you have moved the <filename>mozile.js</filename> file, then you <emphasis>must</emphasis> change the <code>mozile.root</code> setting so that it points from the web page to the <filename>mozile</filename> directory.</para>
				<para>Another important function is <code>mozile.require()</code>. It makes sure that the named module has been loaded into Mozile. If one module depends on others, the dependencies will be loaded automatically. If there is a module that you do not want to load, you can either remove the <code>mozile.require()</code> line, or you can "comment out" that module by adding <code>//</code> to the beginning of the line. The <code>//</code> tells JavaScript to ignore the rest of the line after the double-slash, treating it as a comment and not as code.</para>
				<para>After the modules are loaded, you can change other settings which apply to the loaded modules. In most cases you won't need to change any, but you can look at Mozile's <link href="../jsdoc/index.html">API documentation</link> for settings which you might like to change. You probably want to change the setting <emphasis>after</emphasis> the module has been loaded, so that you change doesn't get overridden.</para>
				<caution>If you have commented out a module, be sure to also comment out any settings that apply to that module. Otherwise there may be errors that will stop Mozile from running.</caution>
				<para>You can use the <code>mozile.editElement()</code>, <code>mozile.editElements()</code> and <code>mozile.editDocument()</code> methods to determine which parts of the document are editable. You can call <code>mozile.editElement()</code> and <code>mozile.editElements()</code> as many times as you like, but it only makes sense to call <code>mozile.editDocument()</code> once.</para>
				<para>You can use the <code>mozile.useSchema()</code> and <code>mozile.enableEditing()</code> commands to determine how the document will be edited. It's better to use <code>mozile.useSchema()</code> when possible (<link href="#EditingRelaxNG">see below</link>) but sometimes the unstructured editing of <code>mozile.enableEditing()</code> is the right choice. There aren't many cases where you would want to use both of them.</para>
				<para>A <code>window.onload = function() { ... }</code> block is used to specify code which is supposed to be executed only after the document has finished loading. It is usually best to put <code>mozile.editElement()</code>, <code>mozile.editElements()</code>, and <code>mozile.useSchema()</code> commands inside this block.</para>
			</section>

			<section id="SavingChanges">
				<title>Saving Changes</title>
				<para>The changes that Mozile makes to the document are reflected immediately in the DOM. Mozile provides tools to help you save the content of the document. Currently we have a simple "display" save method and one using HTTP POST. Save functionality is provided in <link href="../jsdoc/mozile.save.html"><code>mozile.save</code></link> module. These save options are for web site designers to configure how Mozile works in their sites. In the future we plan to include more options for Mozile users to save their content.</para>
				<para>The default save method is <code>mozile.save.display</code>, which  takes the content of the document and displays it as source code using the <code>mozile.gui.display()</code> method. For the default HTML Toolbar GUI this means displaying the source in a new window. You can then copy and paste the source code as you like.</para>
				<para>The <code>mozile.save.post</code> method uses <code>XMLHttpRequest</code> to send the content of the document to a web server using the HTTP POST operation.</para>

				<itemizedlist>
					<title>General Save Configuration</title>
					<listitem><code>mozile.save.target</code> - Indicates the node which should be saved. The default is <code>document</code>.</listitem>
					<listitem><code>mozile.save.format</code> - Used to format the saved data. Can be <code>"uppercase"</code> or <code>"lowercase"</code>, which will change the case of all tag names. Defaults to <code>null</code> which means no reformatting is done.</listitem>
					<listitem><code>mozile.save.warn</code> - When <code>true</code> (the default) the user will be warned before she leaves a document with unsaved changes.</listitem>
					<listitem><code>mozile.save.method</code> - Sets the method to be used by save operations.</listitem>
				</itemizedlist>
				<itemizedlist>
					<title>Save Configuration for <code>mozile.save.post</code></title>
					<listitem><code>mozile.save.post.async</code> - When <code>true</code> the POST is done asynchronously.</listitem>
					<listitem><code>mozile.save.post.uri</code> - The URI to be POSTed to. Security restrictions on <code>XMLHttpRequest</code> require that the URI must be in the same domain as the web page it is called from.</listitem>
					<listitem><code>mozile.save.post.user</code> - Optional. The name of the user to POST as.</listitem>
					<listitem><code>mozile.save.post.password</code> - Optional. The password for the user.</listitem>
					<listitem><code>mozile.save.post.showResponse</code> - When <code>true</code> the result of the POST operation will be displayed. This is meant to be used for debugging, and it defaults to <code>false</code>.</listitem>
				</itemizedlist>
				<para>Here is an example using all of these save configuration options.</para>
				<example id="IncludingTheMozileScript">
					<title>Configuring Mozile</title>
				<programlisting><![CDATA[<script type="text/javascript" src="path/to/mozile.js"></script>
<script type="text/javascript">
  mozile.require("mozile.save.post");
  mozile.save.target = document;
  mozile.save.format = null;
  mozile.save.warn = true;
  mozile.save.method = mozile.save.post;
  mozile.save.post.async = true;
  mozile.save.post.uri = "http://somesite.com/save_data.php?id=Mozile";
  mozile.save.post.user = null;
  mozile.save.post.password = null;
  mozile.save.post.showResponse = false;
</script>]]></programlisting>
				</example>
				<para>When you call the <code>mozile.save.save()</code> or <code>mozile.save.saveAs()</code> methods, Mozile will execute the <code>save()</code> or <code>saveAs()</code> methods of the current <code>mozile.save.module</code>.</para>
				<para>Of course, you can write your own code for saving changes. The first step is to create a new instance of the <link href="../jsdoc/mozile.save.Method.html"><code>mozile.save.Method</code></link> class. You can also make use of the <code>mozile.save.getContent()</code> method, which takes a <code>Node</code> or a <code>Document</code> as its argument and returns a string representation.</para>

				<warning>You <emphasis>must</emphasis> take steps to ensure that the data submitted by your users is safe. Failure to do so could result in a <link href="http://en.wikipedia.org/wiki/Cross_site_scripting">cross site scripting</link> (XSS) security vulnerability.</warning>
				<para>An XSS vulnerability can occur when your site accepts content generated by one user and displays it to another user without ensuring that the content does not include malicious code. For example, an attacker posts a message to a message board with malicious JavaScript embedded. The user loads the message in his browser and the attacker's JavaScript is executed: it reads the user's cookie or login information. Then the malicious code sends that information back to the attacker by setting the <code>document.location</code> to a URL which encodes the data, or by other means. Now the attacker has access to the user's cookie or login information, which may be used to hijack the user's account on the web site or to violate the user's privacy in other ways.</para>
				<para><emphasis>Mozile does not cause XSS vulnerabilities.</emphasis> Any vulnerability that exists in a web site can be exploited without Mozile. However, Mozile is usually used to edit rich HTML and submit that HTML back to a web site. Any web site that accepts HTML content from its users must be very careful to sanitize submitted data before displaying it.</para>
				<para>JavaScript code can be embedded in <code>script</code> tags, <code>src</code> or <code>onload</code> attributes, or even in CSS with IE's <code>expression</code> statement. Not just JavaScript, but objects like Flash applets can also be used for XSS attacks. There's a wide variety of XSS attack vectors. Be very careful.</para>
				<para>Every site has different needs and there is no one best way to sanitize HTML. Different tools exist for different programming languages which may be used to run a dynamic web site. Most CMS systems provide tools for sanitizing submitted data. Although we cannot offer specific advice, here are some resources that should be generally useful.</para>
				<itemizedlist>
					<title>XSS Resources</title>
					<listitem><link href="http://tidy.sourceforge.net/">HTML Tidy</link> - Does not sanitize data, but formats HTML in a standard way. This will make it easier for sanitizers to do their work.</listitem>
					<listitem><link href="http://ha.ckers.org/xss.html">XSS Cheat Sheet</link> - A long list of XSS vectors.</listitem>
					<listitem><link href="http://blog.bitflux.ch/wiki/XSS_Prevention">Bitflux XSS Prevention</link> - An example of using regular expressions to sanitize HTML.</listitem>
				</itemizedlist>
				<para>Programmers should make it a habit to check all data entered by users. The web is no exception.</para>
			</section>

		</section>

		<section id="IntegratingMozile">
			<title>Integrating Mozile</title>
			<para>Mozile can work side-by-side with other JavaScript in your pages. Of course, integration will take some extra work, and you'll need to know more about how Mozile functions than the average user. Here's some information to get you started.</para>
			<para>The first step will be to remove the parts of Mozile that you don't need. If you have your own GUI, then disable the <code>mozile.gui</code> module. If you have your own system for saving, disable the <code>mozile.save</code> module. The section on <link href="#PackagingMozile">packaging Mozile</link> below has more details.</para>
			<para>The second step will be to tell Mozile what to do. Our <link href="../jsdoc/index.html">development documentation</link> includes the full Mozile API, generated from the source code. Here are some of the methods you might be interested in.</para>
			<itemizedlist>
				<listitem><code>mozile.execCommand(name, arguments...)</code> - Execute a Mozile command. For the list of available commands see <code>mozile.edit.allCommands</code>.</listitem>
				<listitem><code>mozile.edit.enable()</code> and <code>disable()</code> - These turn Mozile editing on and off throughout the whole document.</listitem>
				<listitem><code>mozile.save.save()</code> and <code>saveAs()</code> - Use Mozile's save system.</listitem>
				<listitem><code>mozile.dom.selection.get()</code> - Gets a cross-browser selection object which behaves like the Mozilla <code>Selection</code> object.</listitem>
			</itemizedlist>
		</section>
		
		<section id="LearningMore">
			<title>Learning More</title>
			<para>The rest of this document includes more information on the <link href="#MozileDesign">design</link> and <link href="#MozileDevelopment">development</link> of Mozile. There are several other places where you can find more information and ask any questions you have:</para>
			<itemizedlist>
				<listitem>The Mozile homepage at <link href="http://mozile.mozdev.org">http://mozile.mozdev.org</link></listitem>
				<listitem>The Mozile 0.8 <link href="http://mozile.mozdev.org/0.8/doc/jsdoc/">API documentation</link>, generated from comments in the code by <link href="#DocumentationWithJSDoc">JSDoc</link>.</listitem>
				<listitem>The Mozile <link href="http://mozile.mozdev.org/list.html">mailing list</link>.</listitem>
				<listitem>The Mozile IRC channel at <link href="irc://irc.freenode.net/#mozile">irc://irc.freenode.net/#mozile</link></listitem>
			</itemizedlist>
		</section>
		
	</chapter>


	<chapter id="MozileDesign">
		<title>Mozile Design</title>
		
		<section id="HowDoesMozileWork">
			<title>How Does Mozile Work?</title>
			<para>The Mozile code is written entirely in JavaScript, and editing operations are done entirely through the Document Object Mode (<acronym>DOM</acronym>). One of the benefits of this is that Mozile is not limited to editing HTML and XHTML documents, and we expect to support all sorts of XML documents in the future.</para>
			<para>This section includes information on Mozile's design, which will help you understand how Mozile works "under the hood". If you're interested in customizing Mozile or in helping to develop it, read on! You don't need to read this section to use Mozile for basic editing. If there information here isn't enough, continue on to the <link href="#MozileDevelopment">development</link> section.</para>
		</section>
		
		<section id="MozileOrganization">
			<title>Mozile's Organization</title>
			<section id="MozileDirectories">
				<title>Mozile Directories</title>
				<para>The root <link href="../../"><filename>mozile</filename></link> directory contains the following sub-directories:</para>
				<itemizedlist>
					<listitem><link href="../../demos"><filename>demos</filename></link> contains demonstrations of Mozile in action.</listitem>
					<listitem><link href="../../doc"><filename>doc</filename></link> contains documentation, including this documentation and API documentation generated by <link href="http://jsdoc.sourceforge.net/">JSDoc</link>.</listitem>
					<listitem><link href="../../images"><filename>images</filename></link> contains image files used by Mozile.</listitem>
					<listitem><link href="../../jsunit"><filename>jsunit</filename></link> contains the <link href="http://www.jsunit.net/">JsUnit</link> testing framework, which drives Mozile's unit tests.</listitem>
					<listitem><link href="../../lib"><filename>lib</filename></link> contains RelaxNG schemas which are used for structured editing and validation.</listitem>
					<listitem><link href="../../src"><filename>src</filename></link> contains the JavaScript code which makes Mozile work.</listitem>
					<listitem><link href="../../tests"><filename>tests</filename></link> contains the unit testing code which verifies that Mozile is working correctly.</listitem>
				</itemizedlist>
				<para>Most of these directories don't require further description, but a few do.</para>
			</section>
			
			<section id="MozileModules">
				<title>Mozile Modules</title>
				<para>The layout of the <link href="../../src"><filename>src</filename></link> directory corresponds to Mozile's concept of code "modules". JavaScript has many virtues as a programming language, but it also lacks many important features that we take for granted in other languages. One of these features is support for code libraries, and that means namespace support is also lacking. Because there is no built-in way to package code into libraries and import the libraries into your code, JavaScript developers have come up with their own methods and conventions.</para>
				<para>Mozile follows the lead of JavaScript toolkit projects such as <link href="http://dojotoolkit.org/">Dojo</link>. One of the basic data types in JavaScript is the <code>Object</code>, and objects can have properties which are also objects. Properties are accessed using the common "dot" notation, so we can have a hierarchy of objects such as <code>parentObject.childObject.grandchildObject.foo</code>, where <code>foo</code> is some property. This looks a lot like the way packages are accessed in Java, for example, and for most of our purposes it behaves the same way.</para>
				<para>Because there are no namespaces in JavaScript, we keep Mozile's code separate from other code running in the same page by adopting a convention. We declare a global <code>mozile</code> object in the page, and all of the rest of our variables will be assigned as properties of the root <code>mozile</code> object or its descendants. We try to organize our code in a sensible way, like any code library would.</para>
				<para>This is how we get Mozile's code modules. The file <link href="../../src/dom.js"><filename>src/dom.js</filename></link> defines the <link href="../jsdoc/mozile.dom.html"><code>mozile.dom</code></link> object, which has as its children a bunch of properties like <code>mozile.dom.TEXT_NODE</code>, and a bunch of functions like <code>mozile.dom.insertAfter()</code> and <code>mozile.dom.removeChildNodes()</code>. (We often use "function" and "method" as synonyms, because the usual differences between them don't make as much sense in JavaScript as in other languages.)</para>
				<para>Also, the file <link href="../../src/dom/TreeWalker.js"><filename>src/dom/TreeWalker.js</filename></link> defines an object called <link href="../jsdoc/mozile.dom.TreeWalker.html"><code>mozile.dom.TreeWalker</code></link>. In this way modules can have sub-modules.</para>
				<para>The <code>mozile</code> object itself is defined in <link href="../../src/core.js"><filename>src/core.js</filename></link>. This file has all the code required to bootstrap Mozile. After it is loaded, the <code>mozile.loadModule()</code> function is used to load other modules.</para>
				<para>Every module file should begin with a license block, a JSDoc summary, and <code>mozile.require()</code> and <code>mozile.provide()</code> statements. The <code>mozile.require()</code> call will make sure that an dependencies are loaded before the rest of the module is loaded. The <code>mozile.provide()</code> call indicates what properties and functions the module will provide, but currently it has no real effect.</para>
			</section>
				
			<section id="MozileTests">
				<title>Mozile Tests</title>
				<para>All of Mozile's testing code is kept in the <link href="../../tests"><filename>tests</filename></link> directory, and the JsUnit code which runs the tests is kept in the <link href="../../jsunit"><filename>jsunit</filename></link> directory.</para>
				<para>Within the <filename>tests</filename> directory there are sub-directories for most of the modules. The <link href="../../tests/core"><filename>tests/core</filename></link> directory is the default location for tests when they don't merit their own directory. The <link href="../../tests/shared"><filename>tests/shared</filename></link> directory contains the code which is common to all the tests.</para>
				<para>Within each sub-directory, the test files are named either after the module which they test, or specific classes, objects, or functionality which belongs to that module. The point is simply to make it clear what is being tested by the particular test file.</para>
			</section>
			
			<para>The overriding goal is avoid any surprises by keeping the code separated by its function and storing it where you would expect to find it.</para>
		</section>



		<section id="LoadingMozile">
			<title>Loading Mozile</title>
			<para>In this section we describe the steps that Mozile goes through as it loads itself and its modules into a web page.</para>
			<para>Mozile loading starts when the user accesses a web page which includes an HTML <code>script</code> tag with a <code>src</code> attribute which points to the <link href="../../mozile.js"><filename>mozile.js</filename></link> file. Most browsers will immediately access the JavaScript file and evaluate its contents, although some may wait until the full HTML page is loaded.</para>
			<para>The <filename>mozile.js</filename> file includes the code from all the Mozile modules, and has been compressed by removing all comments and unnecessary white space. The compression saves bandwidth for users downloading Mozile from your web site, but makes it very difficult for a person to read the code. The <link href="../../mozile-src.js"><filename>mozile-src.js</filename></link> file contains the same code as <filename>mozile.js</filename>, but has not been compressed. Both of these files are generated from the original modules in the <filename>src</filename> directory, which are divided into small parts and include lots of comments. When developing you should make changes to the original module files and not to the <filename>mozile.js</filename> and <filename>mozile-src.js</filename> files.</para>
			<para>The most important module of all is <link href="../../src/core.js"><filename>mozile/src/core.js</filename></link>. It defines the root <code>mozile</code> object and the basic functions required to load other modules. For example, <code>mozile.load()</code> uses <link href="http://en.wikipedia.org/wiki/XMLHttpRequest"><code>XMLHttpRequest</code></link> to get the text contents of files. <code>mozile.findRoot()</code> is used to set the <code>mozile.root</code> variable if it hasn't already been set. <code>mozile.loadModule()</code> translates a module name like "mozile.edit.InsertionPoint" into the file path "mozile.root/src/edit/InsertionPoint.js". Then it fetches the file using <code>mozile.load()</code> and uses the JavaScript <code>eval()</code> function to evaluate the contents of the loaded file as code.</para>
			<caution>The <code>eval()</code> function call is made inside the <code>mozile.loadModule()</code> function. Because of JavaScript's scope rules, a global variable <code>foo</code> defined in a JavaScript file <filename>bar.js</filename> will become a local variable of <code>mozile.loadModule</code>, and not a global variable as might be expected. This is another good reason to always use the <code>mozile</code> object as a "namespace", since <code>mozile.foo</code> defined in <filename>bar.js</filename> will evaluate to <code>mozile.foo</code>, as expected.</caution>
			<para>JavaScript's <code>eval()</code> function evaluates from the beginning of the string to the end. When a <code>mozile.require()</code> call is encountered, it is immediately executed. <code>mozile.require()</code> will call <code>mozile.loadModule()</code>, which uses <code>mozile.findModule()</code> to check whether the named module has been defined already. If "mozile.edit.InsertionPoint" is given, then <code>mozile.findModule()</code> will test whether <code>mozile</code>, <code>mozile.edit</code>, and <code>mozile.edit.InsertionPoint</code> are all defined. If any one of these is not defined, then the <code>mozile.edit.InsertionPoint</code> module will be loaded. Once the required module (and all of its requirements, and so on) have been loaded, the evaluation of the original string will continue.</para>
			<caution>Mozile does not check for circular requirements, and so it is possible to create an infinite loop if two modules require each other.</caution>
			<para>When <filename>mozile.js</filename> has been completely loaded Mozile is ready to go. It's possible to call <code>mozile.loadModule()</code> or <code>mozile.require()</code> after this point to load modules dynamically, for example as part of a conditional statement. The <code>mozile.load()</code> function can be used to load other text files, although there are security limitations on the <code>XMLHttpRequest</code> system in most browsers which prevent loading files from web sites other than the one the page belongs to. For loading XML files you can also use the <code>mozile.xml.load()</code> function.</para>
			<para>Since <filename>mozile.js</filename> is a single small file, it's ideal for Mozile deployment. But when developing Mozile it's usually more convenient to load the <filename>src/core.js</filename> file, set the <code>mozile.root</code>, and load the required modules dynamically. For example,</para>
			<example id="ConfigureCore">
				<title>Configure Mozile using <filename>core.js</filename></title>
				<programlisting><![CDATA[<script type="text/javascript" src="path/to/src/core.js"></script>
<script type="text/javascript">
  // Specify the "mozile" directory.
  mozile.root = "path/to/";
  // Add modules as required.
  mozile.require("mozile.dom");
  mozile.require("mozile.xml");
  mozile.require("mozile.xpath");
  mozile.require("mozile.util");
  mozile.require("mozile.edit");
  mozile.require("mozile.edit.rich");
  mozile.require("mozile.event");
  mozile.require("mozile.save");
  mozile.require("mozile.gui");
  mozile.require("mozile.gui.htmlToolbar");
</script>]]></programlisting>
			</example>
		</section>



		<section id="Editing">
			<title>Editing</title>
			<para>Once Mozile has been loaded, it acts in response to events triggered by the user. These can be mouse events, like clicking on a part of the document or a button, or keyboard events like typing or entering a command's keyboard shortcut. Mozile listens for these events and then uses them to trigger commands which make changes to the document.</para>
			
			<section id="Events">
				<title>Events</title>
				<para>Events are part of the Document Object Model (<acronym>DOM</acronym>) defined by the <acronym>W3C</acronym>. Although there are differences between browsers in the way they handle events, the essentials are the same. The DOM is a representation of an HTML or XML document as a tree made up of nodes. There are many kinds of nodes, but we're usually only concerned with <code>Text</code> nodes and <code>Element</code> nodes, and sometimes <code>Attr</code> or "attribute" nodes. Any text displayed in the document will belong to a text node, and text nodes are the children of elements. Elements can contain text nodes as well as other elements. (We usually use the words "element" and "tag" as synonyms.) Every node has a parent node, until we get to the "root" of the tree, which is the <code>documentElement</code>. In HTML this is the <code>html</code> element. The parent of the <code>documentElement</code> is the <code>document</code> object, and it has no parent.</para>
				<para>An event like a mouse click will start at the node that was clicked and travel up the tree from child to parent until it reaches the root. Keyboard events start at the <code>documentElement</code> or the <code>body</code> or the <code>contentEditable</code> container, depending on the browser, but they also work their way toward the root.</para>
				<para>In order to catch the events as they pass through the DOM tree and do something about them, we need to assign "event listeners". The <code>mozile.event.listen()</code> method, called at the end of <link href="../../src/event.js"><filename>src/event.js</filename></link>, creates the event listeners and attaches them to the document.</para>
				<note>There are many different event types, such as "mouseclick" and "keypress", and <code>mozile.event.listen()</code> only assigns listeners for some of these. If you have a command that you want triggered by a certain event type, make sure that that type is included by <code>mozile.event.listen()</code>.</note>
				<para>The event listeners listen for the events, and when they "hear" one they pass the event object to <code>mozile.event.handle()</code>. <code>mozile.event.handle()</code> then does several things. It checks to see if the event took place in an editable area, which means that the original target node of the event has an ancestor with <code>contentEditable="true"</code> or it was marked as editable by <code>mozile.editElement()</code>. It can also pass the event to other functions and let them try to handle it. An event is "handled" when it triggers an appropriate action, like a command. Once the event is handled it will be cancelled using <code>mozile.event.cancel()</code>, and that will be the end of it. The event will stop moving through the DOM tree, and no other code will see it. If Mozile doesn't handle an event it will let the event keep travelling on its way.</para>
				<para>The most important thing that events do is trigger commands, but there are a few more things to explain before we get to Mozile's command system.</para>
			</section>
			
			<section id="Selection">
				<title>Selection</title>
				<para>A mouse click event will have an <code>event.target</code> property which indicates which node was clicked. Keyboard events don't have this property, so instead we determine the target node using the document's <code>Selection</code> object. The selection is displayed as a cursor or as highlighted text in the document.</para>
				<para>Most of the time, browsers don't display a cursor in a web page. The arrows keys will move the whole page up and down. But when Internet Explorer detects the <code>contentEditable</code> attribute, or Firefox is in <code>designMode</code>, a cursor will appear. In Firefox you can also enable a cursor by pressing <keycap>F7</keycap> to activate "Caret Browsing", which is an accessibility feature. In order to have a cursor displayed automatically, Mozile uses <code>contentEditable</code> and sometimes <code>designMode</code>. But that's all they are used for; the acutal editing operations are done using the DOM.</para>
				<para>A selection can include a range of text and elements, or it can be "collapsed" to a single point. Related to the selection object is the <code>Range</code> object. Every selection contains at least one range, but a range does not have to be selected. The beginning and end points of a range or a selection are indicated by the pair of a node and an offset, which is an integer indicating where the point is within that node. In the case of a text node, the offset will be a number of characters, so you can select the third character of the text node. In the case of an element node, the offset will be the number of child nodes, so you can select the third child of the element node.</para>
				<para>This description of nodes and offsets applies to Firefox and other browsers that comply with the W3C standard for ranges and selections. Internet Explorer has its own way of handling ranges and selections, which is incompatible. After a lot of work we've managed to make Internet Explorer behave according to the standards, for the most part. For the code that does this, see <link href="../../src/dom/InternetExplorerRange.js"><filename>src/dom/InternetExplorerRange.js</filename></link> and <link href="../../src/dom/InternetExplorerSelection.js"><filename>src/dom/InternetExplorerSelection.js</filename></link>.</para>
				<para>When <code>mozile.event.handle()</code> receives a keyboard event it looks for the current selection and uses the first node which contains both the start and the end of the selection as the target of the event. Once this is done, keyboard and mouse events are treated in the same way.</para>
				<para>Mozile's <link href="../jsdoc/mozile.edit.InsertionPoint.html"><code>mozile.edit.InsertionPoint</code></link> (<acronym>IP</acronym>) object is related to ranges and selections. It also uses a node and an offset to indicate a location in the document. However, the insertion point code knows a lot more than the normal range and selection objects do about how to handle white space and which elements are allowed to contain text. So some of Mozile's code just uses the range and selection objects, while other code uses insertion points for finer control.</para>
			</section>
			
			<section id="EditingRelaxNG">
				<title>RelaxNG</title>
				<para>Once <code>mozile.event.handle()</code> knows which node is the target of the current event, it needs to decide how to handle the event. Mozile is a context sensitive editor, which means that it's smart enough to tell the difference between editing a paragraph and editing a list, for example. We call this "structured editing"; Mozile knows about the way documents are structured, and helps maintain that structure as changes are made.</para>
				<para>Mozile is sensitive to the structure of documents, but you have to tell it what the structure should be. We do this using a "schema language" called RelaxNG (<acronym>RNG</acronym>). All you have to do is tell Mozile where to find an RNG file for the document type you want to edit, and Mozile will do the rest.</para>
				<para>Here is an example of how RNG describes the structure of an <code>a</code> element:</para>
				<example id="ARelaxNGDefinitpion">
					<title>A RelaxNG definition</title>
				<programlisting><![CDATA[<define name="a">
  <element name="a">
    <attribute name="href"/>
    <text/>
    <ref name="Inline.model"/>
  </element>
</define>
]]></programlisting>
				</example>
				<para>This bit of RNG says that an <code>a</code> element must have an <code>href</code> attribute, and that it's allowed to contain non-white-space text. It also refers to another definition named "Inline.model", which will have more details about what the <code>a</code> element is allowed to contain. RNG files can also contain Mozile Editing Schema information, which adds details about the commands associated with various elements. More about that below.</para>
				<para>After <code>mozile.event.handle()</code> finds the target of an event, it looks for a RNG representation of that node. This will be a <link href="../jsdoc/mozile.rng.Element.html"><code>mozile.rng.Element</code></link> object. If the node is a text node, it uses the parent element instead. Mozile stores all sorts of information from the RNG schema about the document's elements, and this includes what commands are associated with what kinds of elements.</para>
				<para>So <code>mozile.event.handle()</code> takes an event, figures out the target node, and then figures out what <code>mozile.rng.Element</code> object matches up with the target node. Once it has this information, it calls the <code>mozile.rng.Element.commands.trigger()</code> method, and that method calls the commands associated with the <code>mozile.rng.Element</code>.</para>
				<note>Mozile 0.8 will also support the validation nodes and documents against RelaxNG schemas, however the validation system is not yet complete.</note>
			</section>
			
			<section id="GlobalAndDefaultCommands">
				<title>Global and Default Commands</title>
				<para>Not all commands are attached to RNG system. Some commands are "global" and apply to the whole document, like Save, Undo, and Redo. These commands are grouped together in the <code>mozile.edit.commands</code> object. You can add a global command using <code>mozile.edit.commands.addCommand()</code>.</para>
				<para>You don't always need a RelaxNG schema in order to edit documents with Mozile. <code>mozile.event.handle()</code> first tries to use the RNG system, and if the event has not been handled it will test a set of default editing commands.</para>
				<para>Default editing commands are grouped together in the <code>mozile.edit.defaults</code> object. You can add new ones using the <code>mozile.edit.defaults.addCommand()</code> method, and they are handled using the <code>mozile.edit.defaults.trigger()</code> method. However the easiest way to add a good selection of default editing commands is to use the <code>mozile.enableEditing(rich)</code> command in your configuration. If <code>rich</code> is <code>false</code> then only commands for inserting and removing text will be added as default editing commands. This means that elements cannot be created or destroyed, and only the text that they contain can be changed. If <code>rich</code> is <code>true</code> then text editing commands and commands for spitting, merging, and removing elements will be added.</para>
				<para>In most cases using an RNG schema is recommended, because without an RNG schema to guide its actions Mozile will not be able to maintain the validity of the document. For example, Mozile will not be able to tell which elements are allowed to contain non-white-space text and which are not allowed. However there are cases where a schema is unavailable or undesirable, and the default commands serve that purpose.</para>
			</section>
			
			<section id="Commands">
				<title>Commands</title>
				<para>All of Mozile's editing operations are performed by commands. There is a <link href="../jsdoc/mozile.edit.Command.html"><code>mozile.edit.Command</code></link> "class", and several sub-classes with specialized functions. ("Class" is put in quotation marks because JavaScript doesn't have classes in the same sense that Java does. However, for most of our purposes they behave the same.) Commands can be executed and "unexecuted", so every editing operation can be done, undone, and redone. After undoing an editing command the document will be in exactly the same state as it was before you called the command in the first place.</para>
				<para>Commands can be associated with <code>mozile.rng.Element</code> objects, such as text or element insertion commands, or with the document as a whole, like the Undo and Redo commands. They can also be associated with the GUI. Every command has a unique name and all of the commands are stored in an associative array named <code>mozile.edit.allCommands</code>.</para>
				<para>Every command has the following methods:</para>
				<itemizedlist>
					<listitem><code>respond()</code> - given a change code, determines whether the command should be updated. <link href="#MozileEditingSchema">See below</link> for notes on change codes and the <code>makesChanges</code> and <code>watchesChanges</code> properties.</listitem>
					<listitem><code>isAvailable()</code> - given an event, determines whether the command is available for use in the current editing context.</listitem>
					<listitem><code>isActive()</code> - given an event, determines whether the command is active or disabled in the current editing context.</listitem>
					<listitem><code>test()</code> - given an event or other arguments, determines whether the command can be executed.</listitem>
					<listitem><code>trigger()</code> - takes an event and if the <code>test</code> is successful it executes the command. Use this method to activate a command as the result of a user event.</listitem>
					<listitem><code>request()</code> - takes a state and some arguments and if the <code>test</code> is successful it executes the command. Use this method when calling one command from another command.</listitem>
					<listitem><code>prepare()</code> - used to extract information from the event object and other arguments and store it in a <code>mozile.edit.State</code> object. The state object contains enough information for the command to be executed and unexecuted, and the state is stored by the undo system.</listitem>
					<listitem><code>execute()</code> - takes the state object and does the actual work of manuipulating the document.</listitem>
					<listitem><code>unexecute()</code> - takes the state object and undoes everything which <code>execute()</code> did, leaving the document exactly as it was.</listitem>
				</itemizedlist>
				<para>There are other methods and properties which we will ignore for the time being. <link href="#MozileEditingSchema">See below</link> for more details.</para>
				<para>The <link href="../jsdoc/mozile.edit.html"><code>mozile.edit</code></link> module defines a few basic text editing commands. The <code>mozile.edit.rich</code> module defines more primitive commands for editing text and elements. Very powerful commands can be created simply by combining these primitive commands. Here is a list of some commands built in to Mozile (they all belong to <code>mozile.edit.*</code>):</para>
				<itemizedlist>
					<listitem><code>insertText</code></listitem>
					<listitem><code>removeText</code></listitem>
					<listitem><code>insertNode</code></listitem>
					<listitem><code>removeNode</code></listitem>
					<listitem><code>moveNode</code></listitem>
					<listitem><code>remove</code> removes combinations of text and elements</listitem>
					<listitem><code>mergeNodes</code></listitem>
					<listitem><code>splitNode</code></listitem>
					<listitem><code>splitNodes</code></listitem>
				</itemizedlist>
				<para>Here are the classes of commands that are available (they all belong to <code>mozile.edit.*</code>):</para>
				<itemizedlist>
					<listitem><link href="../jsdoc/mozile.edit.Command.html"><code>Command</code></link> is a generic command</listitem>
					<listitem><link href="../jsdoc/mozile.edit.CommandGroup.html"><code>CommandGroup</code></link> groups commands together, but does not perform editing operations.</listitem>
					<listitem><link href="../jsdoc/mozile.edit.Navigate.html"><code>Navigate</code></link> moves the selection through the document.</listitem>
					<listitem><link href="../jsdoc/mozile.edit.Split.html"><code>Split</code></link> splits an element and its contents in two.</listitem>
					<listitem><link href="../jsdoc/mozile.edit.Insert.html"><code>Insert</code></link> inserts either text or an element at the current selection.</listitem>
					<listitem><link href="../jsdoc/mozile.edit.Wrap.html"><code>Wrap</code></link> wraps the current selection inside one or more new elements.</listitem>
					<listitem><link href="../jsdoc/mozile.edit.Unwrap.html"><code>Unwrap</code></link> removes a target element but keeps all the child nodes.</listitem>
					<listitem><link href="../jsdoc/mozile.edit.Replace.html"><code>Replace</code></link> replaces a target element with a new element, without changing the child nodes.</listitem>
					<listitem><link href="../jsdoc/mozile.edit.Style.html"><code>Style</code></link> changes the CSS style of a target element.</listitem>
				</itemizedlist>
				<para>These are the building blocks from which you can create powerful editing behaviours.</para>
				<para>Commands are always grouped together by a <code>CommandGroup</code>. When the groups has its <code>trigger(event)</code> method called, it passes the event to the <code>trigger()</code> method for each command in thr group. If one of the commands is triggered, then the command is executed and the resulting state object is returned. That state object is stored in the undo system by <code>mozile.edit.done()</code>. Then the event is cancelled so it won't trigger any more commands.</para>
				<para>If none of the commands are triggered, then the whole process is repeated with the target node's parent node. <code>mozile.edit.handle()</code> keeps trying all of the ancestor nodes, until the event triggers a command or the ancestor is no longer inside an editable element. Then it tries the default commands in the <code>mozile.edit.defaults</code> <code>CommandGroup</code>. If the event isn't handled by any command then Mozile lets the it continue on its way.</para>
			</section>
			
			<section id="States">
				<title>States</title>
				<para><link href="../jsdoc/mozile.edit.State.html"><code>mozile.edit.State</code></link> objects are very important for the command system. A command's <code>prepare()</code> method will prepare a new <code>mozile.edit.State</code> instance containing all of the information needed to execute and unexecute the command. When the command is executed the prepared state is sent to the command's <code>execute()</code> method and then stored by the undo system. When the command is undone, the state is sent to the <code>unexecute()</code> method. When the command is redone, the state is sent to the <code>execute()</code> method again.</para>
				<para>Commands may add, remove, and alter the nodes in the document. When a command is undone, the structure of the document will be exactly the same as it was before the command as executed. However, the particular node objects that make up that structure may not be exactly the same as before. In other words, while the XPath addresses of all the parts of the document structure will be the same as before, the identity of particular DOM objects may have changed. For this reason the <code>prepare()</code> should almost always store references to nodes using their XPath addresses, which will stay the same, and not references to the node objects, which may change. Failure to do this can lead to strange bugs which only show up after a Redo operation.</para>
			</section>
			
			<section id="TextAndWhiteSpace">
				<title>Text and White Space</title>
				<para>JavaScript supports <link href="http://unicode.org">Unicode</link> characters, and so does Mozile. The key problem using Unicode is font support. Mozile will insert any Unicode character, but the browser might not be able to render it properly if the available fonts do not include the character. Keep this in mind.</para>
				<para>White space is a tricky matter for Mozile. Like most programming languages, the rules for HTML and XML say that white space characters (like spaces and tabs) are treated differently than normal text. Multiple white space characters are considered equivalent to a single character. This allows you to add tabs and spaces to your HTML source, making it more readable without changing the way in which the document is displayed.</para>
				<para>Word processors don't follow the same rules. You usually expect every press of the space bar to insert a space character. Since Mozile edits HTML and XML documents in a browser, but it tries to act like a word processor, this creates some problems. Even worse, different browsers handle white space in different ways.</para>
				<para>There are two special white space cases that Mozile handles: inline text and blocks. The inline case includes HTML tags like <code>strong</code> and <code>em</code>, while the block case includes <code>p</code> and <code>div</code>. Both of these are values of the CSS <code>display</code> property.</para>
				<para>The problem with blocks is that Mozilla's Gecko rendering engine will collapse a block that  contain only normal white-space characters. So a <code>p</code> tag that contains a space character will look different than one that includes a non-breaking space character. Mozile handles this case using the <code>mozile.emptyToken</code>. By default this is set to the Unicode non-breaking space character <code>\u00A0</code>. When Mozile makes a change to a block element which removes all the non-white-space text, then an empty token will be inserted. If text is later added, the empty token is removed.</para>
				<para>The inline case uses the <code>mozile.alternateSpace</code> setting. By default the value is <code>null</code>, and Mozile will ignore events which try to insert multiple consecutive spaces. A maximum of two consecutive spaces will be created by Mozile. This keeps the source code clean, but can cause some strange looking behaviour with the cursor around white space characers. If the <code>alternateSpace</code> is set to <code>\u00A0</code> (or some other character), then Mozile will apply a somewhat complicated set of rules to insert space characters and alternate space characters when the user presses the space bar. Normally the two different characters will alternate, but sometimes two alternate space characters will be placed consecutively.</para>
			</section>


		</section>


		
		<section id="RelaxNG">
			<title>RelaxNG</title>
			<para>In the previous section we saw what RNG does for Mozile. This section will go into more detail, particularly about Mozile Editing Schemas (<acronym>MES</acronym>).</para>
			
			<section id="SemanticStructure">
				<title>Semantic Structure</title>
				<para>Mozile 0.8 is designed to be an XHTML and XML+CSS editor. At the current time XML editing is not complete, however many of the features required to support XML editing are already in place.</para>
				<para>A key difference between a rich-text document and an XML document is that the XML document has more structure. This is often called "semantic" structure, because XML tags carry information about the meaning of their contents. A rich-text file might have a selection of text which is bold and in large font. When a person reads the document she will recognize the selection as a headline, because of its format and position, and she will expect the headline to be a very brief summary of the rest of the document. However, that information about the meaning of the bold text isn't readily available to a computer program. In an equivalent XML document the selection will be marked with a <code>headline</code> tag, and a stylesheet would be used to display it as bold and in large font. This way both the person and the computer can easily pick out the headline.</para>
				<para>A computer might not understand the <emphasis>meaning</emphasis> of the headline, not in the way a person does, but it can understand the structure of an XML document if it's properly described. There are several ways to describe XML structure, including Document Type Definitions (<acronym>DTD</acronym>) and the XML Schema language. However many people prefer RelaxNG, and this is the method that Mozile supports.</para>
				<para>XML stands for eXtensible Markup Language; it's really a set of rules for creating particular markup languages. We usually call a particular XML markup language an "XML dialect". Examples include XHTML, DocBook, MathML, SVG, OpenDocument, etc. A RelaxNG schema describes the structure of the XML dialect. It tells us the elements, attributes, and text nodes which are allowed, and the patterns in which they can occur. One RNG schema describes all the documents that share the same XML dialect.</para> 
				<para>For an XML dialect with a small number of different tags and very strict rules, the RNG schema will be small. On the other hand, XHTML and DocBook have a large number of different tags, and the tags can be mixed quite freely, so their RNG schemas are large and complex.</para>
				<para>Once you have an XML document and an RNG schema, you can check the document against the schema to make sure that it's valid. This is called validation, and Mozile has some support for validating XML documents with RNG schemas. We're working on improving it.</para>
				<para>But when you're editing a document with all this rich structure, it's less important to know that the document <emphasis>was</emphasis> valid, and more important to know that document <emphasis>stays</emphasis> valid as you make your changes. This is the main reason that Mozile uses RNG.</para>
				<note>The following paragraph explains planned functionality which is not yet complete.</note>
				<para>This is what we mean by "structured editing". Using the information from an RNG schema, Mozile is able to present the user with editing options that are appropriate to the structure of the document, and to verify that the changes do not make the structure invalid. For example, inside an XHTML <code>p</code> paragraph you are allowed to insert <code>strong</code> and <code>span</code> tags, but not another <code>p</code> tag, or a <code>script</code> tag or an <code>li</code> tag. Doing so would violate the RNG schema for XHTML, and so Mozile doesn't allow it. Mozile doesn't even present the option.</para>
			</section>

			<section id="RelaxNGObjects">
				<title>RelaxNG Objects</title>
				<para>Before an RNG schema can be used, Mozile has to parse it. The <code>mozile.useSchema()</code> function starts the parsing operation.</para>
				<para>The <link href="../jsdoc/mozile.rng.html"><code>mozile.rng</code></link> module contains a number of classes. Every time a new schema is parsed, a new <link href="../jsdoc/mozile.rng.Schema.html"><code>mozile.rng.Schema</code></link> object is created. It loads the RNG file and then creates a new object for each RNG element it encounters. <link href="../jsdoc/mozile.rng.Node.html"><code>mozile.rng.Node</code></link> is the parent class for these objects, and there is a sub-class for each element type available in RNG: <link href="../jsdoc/mozile.rng.Element.html"><code>mozile.rng.Element</code></link>, <link href="../jsdoc/mozile.rng.Text.html"><code>mozile.rng.Text</code></link>, <link href="../jsdoc/mozile.rng.Choice.html"><code>mozile.rng.Choice</code></link>, etc. So the parsing operation creates a new hierarchy of JavaScript objects which matches the hierarchy of RNG elements.</para>
				<para>Each of the <code>mozile.rng.Node</code> objects has a <code>validate()</code> method, which takes a node from the document and validates it against part of the RNG schema. The validation applies to the target node and all of its descendants. In this way we can validate the whole document or just pieces. Information about the validation operation is passed between objects by a <link href="../jsdoc/mozile.rng.Validation.html"><code>mozile.rng.Validation</code></link> object. The validation object is returned when the validation operation is complete. It has an <code>isValid</code> Boolean property which indicates whether the validation succeeded or failed. It also has  <code>report()</code> and <code>getFirstError()</code> methods which can be used to get information about the validation operation once it is complete.</para>
				<para>The <code>mozile.rng.Node</code> objects also provide methods, such as <code>mustContain()</code> and <code>mayContain()</code>, which we can use to determine what kinds of nodes are allowed by the RNG schema.</para>
				<para>Of the RNG objects, the <code>mozile.rng.Element</code> object is the most important. Validation, events, and editing commands all focus on the elements of the document.</para>
			</section>

			<section id="MozileEditingSchema">
				<title>Mozile Editing Schema</title>
				<para>A RelaxNG schema provides information about the structure of an XML document. We can use this information to figure out which commands should be allowed. But there are many cases when we need more specific information about which editing commands are allowed, and how those commands function.</para>
				<para>For this we use another XML dialect called Mozile Editing Schema (<acronym>MES</acronym>). RNG files are XML, and they allow "annotations" in the form of XML element from other namespaces. We mix RNG and MES elements in the same file, using different namespaces, so MES can take advantage of the RNG structure.</para>
				<note>The MES system is currently still in flux, and some aspects may change.</note>
				<para>There are five MES elements:</para>
				<itemizedlist>
					<listitem><code>command</code> - describes an editing command. This is the most important MES element.</listitem>
					<listitem><code>group</code> -groups commands together. Associated with the <code>mozile.edit.CommandGroup</code> class.</listitem>
					<listitem><code>separator</code> - divides commands.</listitem>
					<listitem><code>define</code> - used like the RNG <code>define</code> element to group elements for reuse. Must have a <code>name</code> attribute.</listitem>
					<listitem><code>ref</code> - used like the RNG <code>ref</code> element to refer to definitions. Must have a <code>name</code> attribute.</listitem>
				</itemizedlist>
				<para>Mozile's MES system is aware of the rules for RNG <code>define</code> and <code>ref</code> elements, and it will follow RNG references as it searches for <code>command</code> elements. However the RNG parser will ignore all of the MES elements.</para>
				<para>When the <code>mozile.useSchema()</code> function is called, the RNG schema will be loaded and parsed. Mozile will create the RNG objects. Then it will run through the list of all the <code>mozile.rng.Element</code>s and for each one of these it will generate the appropriate commands and attach them. The command names must be unique, and Mozile will not generate a new command if the name is already taken.</para>
				<para>The <code>command</code> and <code>group</code> tags can have the following attributes. All of them will become properties of the JavaScript <code>Command</code> object which is generated from the MES.</para>
				<itemizedlist>
					<listitem><code>name</code> - a unique name which identifies the command or group among all the others. Should only contain alphanumeric characters.</listitem>
					<listitem><code>label</code> - a short description which will appear on buttons and menu items.</listitem>
					<listitem><code>priority</code> - a number which allows you to have some commands run before others. The default is 0, with higher numbers meaning higher priority. For example, <code>insertText</code> has priority 10. Negative values can be assigned. </listitem>
					<listitem><code>image</code> - a path to an icon for the command in the <link href="../../images"><filename>images</filename></link> directory. Should be a 16x16 pixel PNG image.</listitem>
					<listitem><code>tooltip</code> - a longer description of the command or group.</listitem>
				</itemizedlist>
				
				<para>These attributes apply only to <code>command</code> tags:</para>
				<itemizedlist>
					<listitem><code>class</code> - the name of a command class, as <link href="#Commands">described above</link>. When the MES is parsed by Mozile, a new instance of the class will be created. The default is <code>Command</code>.</listitem>
					<listitem><code>text</code> - text content to be inserted by the <code>Insert</code> command. To specify Unicode characters, use this syntax: <code><![CDATA[&#x00A0;]]></code>. See the <link href="http://www.unicode.org/charts/">Unicode Charts</link> for the character codes. If an <code>element</code> is given it will be used instead.</listitem>
					<listitem><code>element</code> - the tag name of an element to be created by <code>Wrap</code>, <code>Insert</code>, or <code>Replace</code> commands. Inside a <code>script</code> tag you can set <code>this.element</code> to an <code>Element</code> object.</listitem>
					<listitem><code>accel</code> is an "accelerator" or keyboard shortcut combination. In order to work across platforms we usually use the "Command" keyword, which translates to <keycap>Ctrl</keycap> on Windows and Linux and <keycap>&#x2318;</keycap> on Mac OS X. The sequence should be "Command-Meta-Control-Alt-Shift-Character", where "Character" is a single upper case letter (like "A") or the name of a key (like "Enter"). Key names include: "Backspace", "Tab", "Clear", "Return", "Enter", "Pause", "Escape", "Space", "Page-Up", "Page-Down", "End", "Home", "Left", "Up", "Right", "Down", "Insert", "Delete", and "F1" through "F12". Examples of accelerators include: "Command-U", "Command-Shift-U", "Command-Alt-Shift-U", "Shift-U", "Enter", "Command-Enter", "Shift-Left", etc. You can also specify a space separated list of accelerators, any of which will trigger the command: "Enter Command-Enter Command-Alt-Enter".</listitem>
					<listitem><code>makesChanges</code> - a "change code" which indicates what kinds of change the command makes. Can be <code>"none"</code> if no changes are made; <code>"state"</code> if only the undo state is changed; <code>"text"</code> if only text node data is changed; or <code>"node"</code> which includes changes to elements and attributes. A <code>"text"</code> change includes a <code>"state"</code> change, and a <code>"node"</code> change includes both of these. The default value is <code>"node"</code>.</listitem>
					<listitem><code>watchesChanges</code> - indicates which change codes the command is sensitive to. The possible values are the same as those for <code>makesChanges</code>. The default is <code>"node"</code>, which means that the command will update only when the node changes, and not on a <code>"text"</code> or <code>"state"</code> change. A <code>"text"</code> value means updates on <code>"node"</code> and <code>"text"</code> changes, but not <code>"state"</code> changes. A value of <code>"none"</code> indicates that this command never needs to update. This value is used by the <code>respond()</code> method to determine if a command's GUI representation should be checked for updates.</listitem>
					<listitem><code>remove</code> - when <code>true</code> the <code>Insert</code> command will perform a remove operation before inserting, removing any contents of the selection. When <code>false</code> it will move the contents of the selection into the inserted element, which is useful for elements like links (<code>a</code> tags). The default is <code>true</code>.</listitem>
					<listitem><code>nested</code> - when <code>true</code> the <code>Wrap</code> command will create wrappers inside other wrappers if told to do so. When <code>false</code> it will remove the wrapper if called inside another wrapper. The default is <code>false</code>.</listitem>
					<listitem><code>target</code> - used by the <code>Navigate</code>, <code>Split</code>, <code>Unwrap</code>, <code>Replace</code>, and <code>Style</code> commands to determine which node will be manipulated. Values can be:
						<itemizedlist>
							<listitem><code>any</code> - the first node found.</listitem>
							<listitem><code>text</code> - the first text node found.</listitem>
							<listitem><code>element</code> - the first element found.</listitem>
							<listitem><code>block</code> - the first element which counts as a block-level element (usually because it has CSS <code>display: block</code>).</listitem>
							<listitem><code>localName [tagName]</code> - the first element which has a local name matching the given <code>tagName</code> (matching is done lower case).</listitem>
						</itemizedlist> 
						Inside a <code>script</code> tag you can set <code>this.target</code> to a function which returns a node. Any time a target is used a <code>direction</code> can be specified.</listitem>
					<listitem><code>direction</code> - used wherever <code>target</code> is used, this indicates the direction to use when searching for the target. Can be <code>ancestor</code> (the default), <code>next</code>, <code>previous</code>, or <code>descendant</code>.</listitem>
					<listitem><code>collapse</code> - used by <code>Navigate</code> to determine whether the selection should be collapsed after it is moved. Can be <code>null</code> (the default), <code>start</code>, or <code>end</code>.</listitem>
					<listitem><code>copyAttributes</code> - when <code>true</code> the <code>Replace</code> command will copy all of the attributes of the target element into the replacement element. When <code>false</code> it will not. The default is <code>true</code>.</listitem>
					<listitem><code>className</code> - used t o set the <code>class</code> attribute of a created element.</listitem>
					<listitem><code>styleName</code> - determines the CSS style to be set for a created element or for the <code>Style</code> command class.</listitem>
					<listitem><code>styleValue</code> - determines the new CSS value to be set for a created element or for the <code>Style</code> command class.</listitem>
				</itemizedlist>
				
				<para><code>command</code> elements can also have child elements. Both of these are optional, and no more than one of each should be included.</para>
				<itemizedlist>
					<listitem><code>element</code> - the first child element inside the <code>element</code> tag will be used as a template. Commands like <code>Wrap</code> and <code>Insert</code> will clone this template and insert the clone into the document. <code>Wrap</code> will then append other elements as children of the clone. If you set the <code>import</code> attribute to <code>true</code> Mozile will copy the element into the current namespace -- this is important when dealing with HTML elements.</listitem>
					<listitem><code>script</code> - can contain JavaScript code which will be evaluated inside the command object after it has been created and initialized. This allows you to customize the behaviour of the built-in Mozile command classes. Be careful of JavaScript scope rules, however. The <code>this</code> keyword will refer to the command object. We recommend including your JavaScript code as a CDATA section, as in the example below; this will avoid problems with the <code>&lt;</code>, <code>&gt;</code>, and <code>&amp;</code> characters.</listitem>
				</itemizedlist>
				
					<para>Creating your own commands is easy. Here are some examples.</para>
				
				<example id="MESExample1">
					<title>Creating a <code>command</code></title>
					<para>Here is a simple command that will wrap a selection in a <code>span</code> tag and use CSS to underline the text.</para>
<programlisting><![CDATA[<m:command name="underline" class="Wrap" accel="Command-U"
  element="span" styleName="text-decoration" styleValue="underline"
  label="Underline" image="silk/text_underline" tooltip="Underline text"/>]]></programlisting>
					<para>No JavaScript is needed, however we are limited to using a single CSS style.</para>
				</example>

				<example id="MESExample2">
					<title>Creating a <code>command</code> using <code>script</code></title>
					<para>This command will underline selected text and make it red.</para>
<programlisting><![CDATA[<m:command name="underline" class="Wrap" accel="Command-U"
  label="Underline" image="silk/text_underline" tooltip="Underline text">
  <m:script>]]>
    &lt;![CDATA[
    this.element = mozile.dom.createElement("span");
    mozile.dom.setStyle(this.element, "text-decoration", "underline");
    mozile.dom.setStyle(this.element, "color", "red");
    ]]&gt;<![CDATA[
  </m:script>
</m:command>]]></programlisting>
					<para>The command has the unique name "underline". It uses the <code>mozile.edit.Wrap</code> class to do most of the work. You can use the keyboard shortcut "Command-U" to trigger the command; on Windows and Linux this means <keycombo action="simul"><keycap>Ctrl</keycap><keycap>U</keycap></keycombo>, and on Mac OS X it means <keycombo action="simul"><keycap>&#x2318;</keycap><keycap>U</keycap></keycombo>. The icon for the button will be <filename>images/silk/text_underline.png</filename>, the label will be "Underline", and the tooltip will be "Underline text", if these are displayed by the GUI. The script tag says to create a new <code>span</code> element and set the "text-decoration" style to "underline".</para>
				</example>
				
				<example id="MESExample3">
					<title>Creating a <code>command</code> using <code>element</code></title>
					<para>The same thing can be done using the <code>element</code> tag, with a few reservations.</para>
<programlisting><![CDATA[<m:command name="underline" class="Wrap" accel="Command-U"
  label="Underline" image="silk/text_underline" tooltip="Underline text">
  <m:element import="true">
    <span xmlns="http://www.w3.org/1999/xhtml" style="text-decoration: underline; color: red"/>
  </m:element>
</m:command>]]></programlisting>
					<para>No JavaScript is needed. However, note that the <code>span</code> element has the attribute <code>xmlns="http://www.w3.org/1999/xhtml"</code>, which indicates that it is an XHTML element. However, since the <code>import</code> attribute is set Mozile will import a copy of the element into the current document and namespace, whatever that might be. When dealing with HTML document <code>import</code> is important! For XHTML and XML documents you may want to mix namespaces.</para>
				</example>

			</section>
		</section>
		
		<section id="GraphicalUserInterface">
			<title>Graphical User Interface</title>
			<note>The GUI code is new and still in flux. There may be changes to it.</note>
			<para>Like any editor, Mozile can present the available editing commands as icons for the user to click and as keyboard shortcuts. The Mozile GUI isn't always necessary, but in most cases a GUI will be useful. Mozile 0.8 is designed to easily allow different GUI modules to be used, presenting different ways for the user to interact with the editor. At this point, however, only one GUI has been implemented: the HTML Toolbar interface <link href="../jsdoc/mozile.gui.htmlToolbar.html"><code>mozile.gui.htmlToolbar</code></link>.</para>
			<para>Because Mozile is restricted to the web page that it is loaded in, any graphical interface that it presents must be part of the web page. The HTML Toolbar interface uses CSS and XHTML to display a small toolbar, and keep it positioned above the page contents at the top of the browser's document window. It provides buttons and menus, like most toolbars.</para>
			<para>The HTML Toolbar is an instance of the abstract <link href="../jsdoc/mozile.gui.Factory.html"><code>mozile.gui.Factory</code></link> class. The <code>mozile.event.handle()</code> function passes events to the <code>mozile.gui.update()</code> function, which calls the <code>update()</code> method of whichever GUI Factory is assigned to <code>mozile.gui.factory</code>. You can change the GUI factory by calling <code>destroy()</code> on the current factory, and then setting <code>mozile.gui.factory</code> to the new factory.</para>
			<note>As of the writing of this document, the <code>destroy()</code> method hasn't been implemented.</note>
			<para>The <code>update()</code> method takes the event and does whatever is needed to update the interface. It has access to the event object, which includes information about the target node. It can use the target node to lookup the appropriate RNG element, and discover its commands.</para>
			<para>In the case of the HTML Toolbar interface, a new button is created for each command or command group. Command groups are rendered as menus, with menu items which may themselves be menus. The buttons are built using HTML elements, and are stored with the commands. The buttons are added and removed from the toolbar element as they are needed. CSS is used to display the toolbar at the top of the window.</para>
			<para>The HTML Toolbar is designed to show only those commands which belong to the target node's RNG element, and some global commands like Undo and Redo. Another interface, with more space to display information, could display commands belonging to the ancestors of the target node as well.</para>
		</section>

	</chapter>


	<chapter id="MozileDevelopment">
		<title>Mozile Development</title>
		
		<section id="HelpingToDevelopMozile">
			<title>Helping to Develop Mozile</title>
			<para>Mozile is an open source project with a small but friendly community behind it. We welcome anyone willing to lend us a hand. Whether it's a bug report or spelling correction, a patch or a new module, every improvement is appreciated.</para>
			<para>This chapter is meant to help you climb the learning curve and make it as easy as possible for you to contribute.</para>
		
			<section id="WhatYouNeed">
				<title>What You Need</title>
				<para>Mozile doesn't require any fancy tools. Everything you need is probably on your computer already. If not, you can get it for free on the Internet.</para>
				<itemizedlist>
					<title>Must Have</title>
					<listitem><emphasis>Browser</emphasis> - Mozile runs in a web browser, so you'll need one of those. Firefox is probably the best one to develop in since it's both free and cross-platform. You'll also have access to tools like the <link href="http://www.mozilla.org/projects/inspector/">DOM Inspector</link>, the <link href="http://www.hacksrus.com/~ginda/venkman/faq/venkman-faq.html">Venkman JavaScript debugger</link>, and helpful extensions like <link href="https://addons.mozilla.org/firefox/1843/">FireBug</link>.</listitem>
					<listitem><emphasis>Text Editor</emphasis> - Except for images and zip files, all of Mozile's files can be edited with a normal text editor. You'll probably want a programmer's text editor, with more features than Windows Notepad, but the choice is entirely up to you.</listitem>
				</itemizedlist>

				<itemizedlist>
					<title>Nice to Have</title>
					<listitem><emphasis>CVS Client</emphasis> - Not strictly necessary, but this will allow you to get the latest code from our CVS repository. There are many free and open-source CVS tools available, either as command-line or GUI applications. See the Mozile <link href="http://mozile.mozdev.org/source.html">source page</link> and <link href="http://mozile.mozdev.org/source.html">contribution page</link>.</listitem>
					<listitem><link href="http://ant.apache.org"><emphasis>Apache Ant</emphasis></link> - Ant is a Java-based build tool, like GNU Make. If you want to compile a custom set of Mozile modules into the <filename>mozile.js</filename> file, Ant makes it easy. See the <link href="#PackagingMozile">packaging Mozile</link> section below.</listitem>
					<listitem><emphasis>IRC Client</emphasis> - Not strictly necessary, but nice if you want to talk to other Mozile developers in our IRC channel: <link href="irc://irc.freenode.net/#mozile">irc://irc.freenode.net/#mozile</link>.</listitem>
				</itemizedlist>

				<para>That's it. Read on for how you can use these tools to help develop Mozile.</para>
			</section>
		</section>
		
		<section id="DocumentObjectModel">
			<title>Document Object Model</title>
			<itemizedlist>
				<title>DOM Resources</title>
				<listitem><link href="http://xulplanet.com/references/objref/">XUL Planet's DOM Object Reference</link> - Includes Mozilla's APIs for DOM, HTML, XML, CSS, SVG, and more.</listitem>
				<listitem><link href="http://developer.mozilla.org/en/docs/Gecko_DOM_Reference">Mozilla's Gecko DOM Reference</link> - More detailed than XUL Planet but less comprehensive.</listitem>
				<listitem><link href="http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/interface.asp">Internet Explorer's HTML Interfaces</link> - Similar to Mozilla in some ways, very different in others.</listitem>
				<listitem><link href="http://www.w3.org/DOM/">W3C DOM Pages</link> - The official DOM specifications.</listitem>
				<listitem>Mozile's DOM related module APIs: <link href="../jsdoc/mozile.dom.html"><code>mozile.dom</code></link>, <link href="../jsdoc/mozile.xml.html"><code>mozile.xml</code></link>.</listitem>
				<listitem>Mozile's <link href="../../KnownIssues">Known Issues</link> page, which notes differences between browsers which affect Mozile.</listitem>
				<listitem>The <link href="http://www.quirksmode.org/">QuirksMode</link> web site, with many resources on cross-browser issues.</listitem>
			</itemizedlist>
			
			<section id="DOM">
				<title>DOM</title>

				<para>All modern web browsers have some support for the W3C's Document Object Model, which describes an HTML or XML document as a tree made up of nodes. The trouble is that every browser differs in the way that it supports the DOM.</para>
				<para>The <code>mozile.dom</code> module contains a number of functions that were written to bridge the gap between DOM implementations. Take a look at Mozile's API documentation for <link href="../jsdoc/mozile.dom.html"><code>mozile.dom</code></link>. The odds are that if some developer took the trouble to write a function for that module, then there must have been some cross-browser annoyance that prompted him to do it. You should take a look and consider using it in your code.</para>
			</section>
			
			<section id="XML">
				<title>XML</title>
				<para>The <link href="../jsdoc/mozile.xml.html"><code>mozile.xml</code></link> module bridges the differences between browsers when it comes to things like parsing and serializing XML. It also holds a list of common XML namespaces in <code>mozile.xml.ns</code>.</para>
				<para>XML namespace support is important to Mozile. However, namespace support is not even as consistent between browsers as the rest of the DOM is. When Mozile is editing HTML documents, no namespaces should ever be used and all nodes should be HTML nodes. When editing XHTML and XML namespaces should always be used, and all nodes should be XML nodes.</para>
				<caution>Mozile's support for XML documents and namespaces is not yet complete.</caution>
			</section>
			
			<section id="XPath">
				<title>XPath</title>
				<para>In the DOM, each node is an object. You can navigate from one node to another. You can move nodes. You can create new nodes and destroy them. But as the structure of the document changes, and nodes move and are replaced, we need a way to access the document by its structure, not just by the nodes. Event if a node has moved from the first paragraph to the fifth paragraph, we still need a way to address the first node of the first paragraph.</para>
				<para>XPath is a way of doing just this. The <link href="http://www.w3.org/TR/xpath">XPath specification</link> is closely tied to the <link href="http://www.w3.org/TR/xsl/">XSL specification</link>, and you can find a good tutorial for bother of them at <link href="http://www.zvon.org/xxl/XSLTreference/Output/index.html">Zvon.org</link>. You can think of XPath as like a file path; it's a hierarchy which starts at a root and leads to a node. For example:</para>
				<example id="SimpleXPathExample">
					<title>Simple XPath example</title>
					<code>/html[1]/body[1]/div[2]/p[3]/span[1]/text()[1]</code>
				</example>
				<para>The numbers inside the square brackets tell us that we want the <emphasis>n</emphasis>th matching node, starting to count at 1. So in this example we want the first <code>html</code> element, and the first <code>body</code> element which is a child of the <code>html</code> element. Then we want the second <code>div</code> in the <code>body</code>, the third <code>p</code> inside that <code>div</code>, the first <code>span</code> in the <code>p</code>, and the first text node in the <code>span</code>.</para>
				<para>XPath can get much more complicated than this. It can select sets of nodes, along different axes, matching different patterns, and using several built-in functions. However in Mozile we only use simple XPaths like these. The only other complication we use in Mozile is namespaces.</para>
				<example id="XPathExampleWithNamespaces">
					<title>XPath example with namespaces</title>
					<code>/xhtml:html[1]/xhtml:body[1]/xhtml:div[2]/mathml:math[3]</code>
				</example>
				<para>In this example we have nodes from the XHTML and MathML namespaces, as indicated by the <code>xhtml</code> and <code>mathml</code> prefixes. That's about as complicated as it gets.</para>
				<para>You can look at the API for the <link href="../jsdoc/mozile.xpath.html"><code>mozile.xpath</code></link> module, but most of the time we just use <code>mozile.xpath.getXPath()</code> to get the XPath address of a node, and <code>mozile.xpath.getNode()</code> to get a node from and XPath address. Any time we need to store a location in the document which might be changed, we store it using XPath.</para>
			</section>
			
			<section id="Mozilla">
				<title>Mozilla</title>
				<para>Mozilla's Gecko rendering engine is known for its standards support. There's also a lot to be said for having access to the Mozilla <link href="http://lxr.mozilla.org/seamonkey/">source code</link>, to see what's happening under the hood. Most of the time, Mozilla's behaviour is the model we follow in Mozile. However, there are also <link href="http://bugzilla.mozilla.org">known bugs</link>. And not all of the browsers built on top of Gecko behave the same way in every situation.</para>
				<para>One particularly problematic aspect of Mozilla is its <code>designMode</code>. By setting <code>document.designMode = "on"</code> an HTML page will enter a mode which allows editing pretty much the same as the old Netscape Composer. Using <code>document.execCommand()</code> you can add <code>font</code> tags, etc. You can also resize images and tables. Most importantly, you get a cursor which you can use for editing the document.</para>
				<para>If the <code>mozile.useDesignMode</code> property is <code>true</code>, then Mozile will use <code>designMode</code> in order to get a cursor. No <code>execCommand()</code> calls are made, and <code>designMode</code> is turned off when outside an editable area.</para>
				<para>The problem is that <code>designMode</code> hasn't seen nearly as much attention as other parts of Mozilla, and it has many bugs. The alternative is to use the "Caret Browsing" mode, which can be activated by the user by pressing <keycap>F7</keycap>, or by setting the browser preference <code>accessibility.browsewithcaret</code>. However, the former must be done manually, and the latter requires the <code>UniversalXPConnect</code> privilege, which is not given to remotely executed scripts (for good reason). Since <code>designMode</code> doesn't work in XHTML and XML documents, Caret Browsing is the only option. For XHTML and XML documents set <code>mozile.useDesignMode = false</code>.</para>
			</section>
			
			<section id="InternetExplorer">
				<title>Internet Explorer</title>
				<para>Internet Explorer is the most popular browser on the Internet by a large margin. Because it's so dominant it's very difficult to ignore. We may be tempted to ignore it, because its poor standards support often makes development difficult. However, IE does support all the features necessary to make Mozile work. Mozile currently has support for IE 6, and we expect to support IE 7 in the future.</para>
				<para>Internet Explorer supports <code>designMode</code>, like Mozilla does, with all of its shortcomings. IE also supports the superior <code>contentEditable</code> attribute, which allows selected elements to be made editable while others are not. Mozile uses <code>contentEditable</code> in order to have a cursor for editing, but it does not use <code>execCommand()</code> to perform any of its editing operations. When <code>contentEditable</code> is enabled, IE uses different white space rules than the standard HTML and XML rules. In most ways this makes editing more convenient, and similar functionality has to be explicitly coded by Mozile for Mozilla.</para>
				<para>There are many differences in the way IE implements the DOM. The difference which has had the single largest effect on the Mozile architecture is the fact that DOM objects cannot be prototyped in IE the way they can in other browsers. In Mozilla you can declare a new method <code>Text.prototype.foo()</code>, and all text nodes will gain the <code>foo()</code> method. <code>Text</code> is treated as a normal JavaScript object with prototype-based inheritance. In IE this is not the case. Although prototyping of some DOM objects seems to be allowed, in general it is not. Prototyping of XML objects, for instance, is not permitted. Mozile 0.7 made extensive use of prototyping, adding methods like <code>Node.isBlock()</code>, which in many ways leads to more readable code. But Mozile 0.8 had to abandon this in favour of functions like <code>mozile.edit.isBlock(node)</code>.</para>
				<para>Most of the DOM Level 1 and DOM Level 2 specifications are implemented similarly by IE and Mozilla. IE has limited namespace support in its DOM methods. It doesn't allow HTML and XML nodes to mix as freely as Mozilla does. IE doesn't provide a <code>TreeWalker</code> object, and so Mozile provides one in <link href="../jsdoc/mozile.dom.TreeWalker.html"><code>mozile.dom.TreeWalker</code></link>.</para>
				<para>The largest gap between Mozilla and Internet Explorer that Mozile developers have had to bridge is between the two range and selection implementations. When the user highlights a range of text, we need to be able to determine the start and end points, and then perform manipulations on that range. Mozilla provides <link href="http://xulplanet.com/references/objref/Range.html"><code>Range</code></link> and <link href="http://xulplanet.com/references/objref/Selection.html"><code>Selection</code></link> objects. End points are specified by the pair of a node and an integer offset; this could be a text node and a number of characters, or an element and a number of child nodes.</para>
				<para>Internet Explorer provides a <link href="http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_textrange.asp"><code>TextRange</code></link> and a <link href="http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_selection.asp"><code>selection</code></link> object. IE does not use pairs of nodes and points to indicate positions, just character offsets within an element. Since selections are critical for editing, a great deal of effort has been put in to wrapping the IE selection system in an interface which is compatible with the Mozilla system. See the <link href="../jsodc/mozile.dom.InternetExplorerRange.html"><code>mozile.dom.InternetExplorerRange</code></link> and <link href="../jsodc/mozile.dom.InternetExplorerSelection.html"><code>mozile.dom.InternetExplorerSelection</code></link> modules. By accessing the window's selection using <code>mozile.dom.selection.get()</code> you can expect the selection object to behave according to the Mozilla model on all supported browsers.</para>
				<caution>The <code>mozile.dom.InternetExplorerRange</code> and <code>mozile.dom.InternetExplorerSelection</code> modules do not yet implement all of the <code>Range</code> and <code>Selection</code> methods. See the API documentation for details.</caution>
				<para>Internet Explorer's CSS support is not equivalent to Mozilla's in many ways. This affects Mozile's GUI code.</para>
				<para>In general, Internet Explorer provides similar features to Mozilla. However be careful of the differences in the way the two implementations work.</para>
			</section>
			
			<section id="Opera">
				<title>Opera</title>
				<para>Opera 9 adds a number of new features which make editing with Mozile possible. In most ways Opera does a very good job of matching Mozilla's behaviour, and so adapting Mozile's code to work with Opera is generally easy. However, "porting" Mozile to another browser is always time consuming, and Opera support will have to wait for someone to find that time.</para>
			</section>
			
			<section id="Safari">
				<title>Safari</title>
				<para>The current release version of Safari for Mac OS X 10.4.6 lacks a number of features which Mozila requires, including Selection and Range objects, and XML namespace support. Development versions, however, include more and more of these requirements, so we expect to be able to support Safari at some point in the future.</para>
				<para>Since Safari and Konqueror are based on the same WebKit/KHTML rendering engine, to a large extent what goes for one goes for the other. However there are important differences, and Konqueror does not always include the latest changes that Safari features.</para>
			</section>


		</section>


		<section id="JavaScript">
			<title>JavaScript</title>
			<itemizedlist>
				<title>JavaScript Resources</title>
				<listitem><link href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference">Core JavaScript 1.5 Reference</link> - Mozilla's JavaScript implementation.</listitem>
				<listitem><link href="http://www.oreilly.com/catalog/jscript4/">JavaScript: The Definitive Guide, Fourth Edition</link> - The best JavaScript book, particularly "Part I: Core JavaScript". Published in 2001, which means it's somewhat dated, but the fifth edition is coming soon.</listitem>
				<listitem>Wikipedia on <link href="http://en.wikipedia.org/wiki/JavaScript">JavaScript</link> and its <link href="http://en.wikipedia.org/wiki/JavaScript_syntax">syntax</link>.</listitem>
				<listitem>The <link href="http://www.jibbering.com/faq/">JavaScript FAQ</link>.</listitem>
				<listitem><link href="http://www.hacksrus.com/~ginda/venkman/faq/venkman-faq.html">Mozilla's Venkman JavaScript Debugger</link> - Takes some getting used to, but it can save a lot of time.</listitem>
				<listitem><link href="http://blogs.msdn.com/ie/archive/2004/10/26/247912.aspx">Notes on IE Script Debugging</link></listitem>
				<listitem><link href="http://dojotoolkit.org/docs/js_style_guide.html">Dojo JavaScript Style Guide</link> - A useful guide to good style.</listitem>
				<listitem><link href="http://www.crockford.com/javascript/index.html">Douglas Crockford's JavaScript Page</link> - Some good articles on the finer points of JavaScript.</listitem>
			</itemizedlist>


			<section id="TheLanguage">
				<title>The Language</title>
				<para>JavaScript is an interesting language. Although it lacks some important features, like library support, the core language is surprisingly elegant. In general it's the ugliness of APIs like the DOM, the frustration of cross-browser variations, and the difficulty debugging code that give JavaScript a bad name.</para>
				<para>It's worthwhile to read the first part of <link href="http://www.oreilly.com/catalog/jscript4/">JavaScript: The Definitive Guide</link> to explore the design of the core language. Here is a brief, biased, incomplete, and probably incorrect overview. The idea here is not to give examples of syntax, but to explain some of the inner workings which are often overlooked.</para>
				<para>JavaScript is an object-based language, but not object-oriented in the same way that Java is. It uses a C-style syntax which looks a fair bit like Java. But JavaScript is <emphasis>not</emphasis> Java, and not even "Java-lite".</para>
				<para>JavaScript is a dynamically-typed interpreted language. It supports UTF-8 and is case sensitive. JavaScript code in web pages is run by the browser inside a sandbox, and so it does not have any access to objects outside the document and browser window.</para>
				<para>The basic data types are <code>null</code>, <code>undefined</code>, <code>Boolean</code>, <code>Number</code> (a double), <code>String</code>, and <code>Object</code>. All of these data types are passed by value, except for <code>Object</code> which is passed by reference. <code>Object</code> is the most interesting type. There are many kinds of objects, including <code>Array</code>, <code>Function</code>, and <code>Error</code>. Objects work like dictionaries in other languages, with keys and values. The values can be any other data type, including other objects and functions. When we talk about an object <code>foo</code> and its "method" <code>foo.bar()</code> we mean that <code>foo</code> has a key named "bar" which has a function as a value. When we talk about <code>foo</code>'s "property" <code>foo.baz</code> we mean that <code>foo</code> has a key "baz" with a value of some other data type (not a function).</para>
				<para>So JavaScript has objects that look a lot like objects in other languages. It also has object inheritance, although it works differently than Java and C++. JavaScript uses <link href="http://en.wikipedia.org/wiki/Prototype-based_programming">prototype-based</link> inheritance. Here's how it works. We have an object <code>foo</code>, and we set <code>foo.a = "A"</code>. When there is no key the value will be <code>undefined</code>, so if we try to access <code>foo.b</code> or <code>foo.c</code> we will get <code>undefined</code>. However, we can create an object <code>fud</code> and set <code>fud.a = "X"</code> and <code>fud.b = "B"</code>. Then we can set the <code>prototype</code> of <code>foo</code> to <code>fud</code> like this: <code>foo.prototype = fud</code>. Now we find that <code>foo.a</code> is <code>"A"</code>, <code>foo.b</code> is <code>"B"</code>, and <code>foo.c</code> is still <code>undefined</code>.</para>
				<para>What's happening in this example? JavaScript checks <code>foo</code>'s dictionary first, and if the desired key is found then the value is returned. If the key is not found, then JavaScript checks <code>foo.prototype</code> for another object and searches that object for the key. JavaScript will keep searching the <code>fud.prototype</code>, and so on along the chain of prototypes, until it finds a matching key or the chain ends. This is how inheritance works in JavaScript.</para>
				<para>There are several built-in objects in JavaScript, all of which inherit from <code>Object</code>: <code>Array</code>, <code>Boolean</code>, <code>Date</code>, <code>Function</code>, <code>Math</code>, <code>Number</code>, <code>RegExp</code>, and <code>String</code>. <code>Array</code> uses numeric indexes instead of (actually in addition to) keys, and works like you expect an array to work. <code>Boolean</code>, <code>Number</code>, and <code>String</code> can be used to create primitive values, and they also provide the properties and methods for the primitive types. Every function in JavaScript is a <code>Function</code> object.</para>
				<para>We've seen objects and inheritance, but we haven't yet seen anything like a Java or C++ class. JavaScript doesn't have classes, but it does have "constructor" functions. Here's an example:</para>
				<example id="JavaScriptConstructor">
					<title>JavaScript constructor</title>
					<programlisting><![CDATA[function Rectangle(width, height) {
  this.width = width;
  this.height = height;					
}
Rectangle.prototype.getArea = function() {
  return this.width * this.height;
}

var r = new Rectangle(2, 3);

if(r.width * r.height == r.getArea()) {
  alert("Test passed.");
}]]></programlisting>
				</example>
				<para>First we define a function named "Rectangle". The <code>this</code> keyword is special; it refers to the object in the current scope (more on scope in a moment). We also add another function named "getArea" to <code>Rectangle.prototype</code>. Then with the line <code>var r = new Rectangle(2, 3)</code> we create a new object and assign it to <code>r</code>. The new object becomes an "instance" of <code>Rectangle</code> because the <code>Rectangle()</code> function is executed as a constructor, assigning new properties to <code>r</code>. We can access <code>r.width</code> to get 2 and <code>r.height</code> to get 3, and we can call <code>r.getArea()</code> to get 6.</para>
				<para>In many ways, <code>Rectangle</code> behaves like a class, and we use the <code>new</code> keyword to create instances of the class. The next step is to show how sub-classes work. We continue the example above, adding the following code:</para>
				<example id="JavaScriptConstructorInheritance">
					<title>JavaScript constructor inheritance</title>
					<programlisting><![CDATA[function Square(width) {
  this.width = width;
  this.height = width;					
}
Square.prototype = new Rectangle;
Square.prototype.constructor = Square;

var s = new Square(4);

if(s.width * s.height == s.getArea()) {
  alert("Test passed.");
}]]></programlisting>
				</example>
				<para>Again we define a constructor function, this time one named "Square". Then we set <code>Square.prototype = new Rectangle</code>, which establishes the chain of prototype-based inheritance. We also set <code>Square.prototype.constructor = Square</code>, to make it clear that the special <code>constructor</code> key, which is supposed to refer to the constructor function, refers to <code>Square</code> instead of <code>Rectangle</code>. After that we can create a new square and access the <code>getArea()</code> method using inheritance.</para>
				<para>There are other ways to make JavaScript objects work like classes; this is the one we generally use in Mozile. But be careful! JavaScript is not Java or C++, and these "classes" are only similar and not the same.</para>
				<para>One last important topic is JavaScript's rules for handling variable scope. The global scope in JavaScript can be considered a function like any other. When working in a browser the global object will have DOM properties, like <code>window</code> and <code>document</code>. When you refer to a variable inside a function, JavaScript will check a chain of scopes which works like the chain of prototypes. Variables are actually properties of the current function, which is an object like any other. First the JavaScript interpreter checks the current function for a variable with the matching name, then it checks the next function and the next until it finds the variable or reaches the global scope. If you declare a variable with the same name as one higher in the scope chain, JavaScript will think you want to use the original variable from the outer scope and overwrite it. </para>
				<para>JavaScript does not include a notion of <emphasis>public</emphasis>, <emphasis>private</emphasis>, and <emphasis>protected</emphasis> visibility for variables and methods. We rely on the scoping rules and the convention that names starting with an underscore, like <code>foo._bar()</code>, are intended to be private and should not be used outside the object that defines them.</para>
			</section>
			
			
			<section id="Debugging">
				<title>Debugging</title>
				<para>Debugging JavaScript is notoriously difficult. Although there are very few differences in the JavaScript language implementation between browsers, there are lots of places where incompatibilities and bugs can slip in.  The fact that Mozile uses <code>eval()</code> to load much of the code aggravates the situation, because it means that the line numbers in most error messages are useless. There's no silver bullet, but there are some things that will make life easier.</para>
				<para><link href="http://en.wikipedia.org/wiki/Test_driven_development">Test-driven development</link> can help. See below for Mozile's <link href="#TestingWithJsUnit">JsUnit</link> test system.</para>
				<para>If you are using a Mozilla browser, set the <code>javascript.options.strict</code> and <code>javascript.options.showInConsole</code> preferences to <code>true</code>. This tells the JavaScript interpreter to send warning messages when it notices undefined variables, or an <code>=</code> where <code>==</code> is expected, or useless expressions. It might help you catch a problem early. You can access the browser preferences by typing <userinput>about:config</userinput> into the address bar of the browser. </para>
				<important>When you make changes to Mozile code, please make sure that they don't cause any unnecessary warnings. We should only see warnings from Mozile if something has actually gone wrong.</important>
				<para>Mozile provides some debugging tools in the <link href="../jsdoc/mozile.debug.html"><code>mozile.debug</code></link> module (in <link href="../../src/core.js"><filename>src/core.js</filename></link>). The <code>mozile.debug.warn()</code>, <code>mozile.debug.inform()</code>, and <code>mozile.debug.debug()</code> functions will log debugging messages and store them in <code>mozile.debug.messages</code>. By changing the <code>mozile.debug.alertLevel</code> and <code>mozile.debug.logLevel</code> you can decide which kinds of messages are displayed using alerts, which are silently logged, and which are ignored. The "Debug" command on the Mozile toolbar (shortcut "Command-D") will open a new window to display logged messages.</para>
				<para>Mozilla's <link href="http://www.hacksrus.com/~ginda/venkman/faq/venkman-faq.html">Venkman JavaScript Debugger</link> is a very powerful tool for tracking down errors. By setting the "Debug - Error Trigger" and "Debug - Throw Trigger" you can catch problems as they occur, look at the values of variables, and step through the code. Be sure to use the "Pretty Print" mode to display the JavaScript code as it appears to Mozilla. This is important for code executed using <code>eval()</code>, which includes almost all of the Mozile code. One difficulty is that most of Mozile's functions are "anonymous" and the name of the object the function belongs to won't be displayed in "Pretty Print" mode. Tracking down the function in the source code may require some clever use of text search like the Unix <code>grep</code> command.</para>
				<para>The Microsoft Script Debugger does many of the same things as Venkman, but does them in different ways.</para>
				<para>The best method when writing new Mozile code is to use just one browser until the code is working, make sure you have good tests written and helpful debugging messages ready, and then "port" the code to other browsers. Just make sure that the changes you make while porting don't break the code in the original browser.</para>
			</section>
			
			<section id="CodingStyle">
				<title>Coding Style</title>
				<para>The <link href="http://dojotoolkit.org/docs/js_style_guide.html">Dojo JavaScript Style Guide</link> is a pretty good place to start. For the sake of the next person to read the code it's nice if there's consistency. But we aren't going to be too strict about style.</para>
				<para>Here's what we would like to see: Please make an effort to use tabs, Unix file endings (LF), and UTF-8 encoded files. Include the license block at the top of all code files. And please make use of the <link href="#DocumentationWithJSDoc">JSDoc</link> system of documentation in comments for all methods and properties.</para>
			</section>
		</section>
		
		<section id="TestingWithJsUnit">
			<title>Testing with JsUnit</title>
			<itemizedlist>
				<title>JsUnit Resources</title>
				<listitem><link href="http://www.jsunit.net/">JsUnit Homepage</link></listitem>
			</itemizedlist>
			<para>JsUnit belongs to the <link href="http://en.wikipedia.org/wiki/XUnit">xUnit</link> family of testing tools, the most popular of which is <link href="http://en.wikipedia.org/wiki/JUnit">JUnit</link> for Java. <link href="http://en.wikipedia.org/wiki/Unit_test">Unit tests</link> are a means of ensuring that small "units" of code function they way that they should by performing automated tests on their interfaces.</para>
			<para>JsUnit tests run inside test pages, which are normal web pages with some extra JavaScript code. The test pages are loaded into a "Test Runner" which controls the tests and keeps track of the results. The test runner calls test functions which do the real work. The test functions call "assertion" functions of all sorts, such as <code>assertTrue()</code> and <code>assertEquals()</code>, which check to make sure that the target code is doing what it should be doing. The test runner displays a green bar, which gets longer as tests are completed. If even one test fails then the bar turns red and a message about the failed test is displayed.</para>
			<para>Testing manually is slow and mind-numbing work. Something will always be missed, and developers will avoid doing it. Automated testing is fast and accurate. When all the tests are "green" you feel good about it. Run the tests after every non-trivial change you make!</para>
			<para>If we have a comprehensive test suite, and all the tests succeed, then we can be fairly confident that the code is working the way it should. The more good tests, the more confident we feel. More importantly, if we make a change to the code and the test suite is still green, then we can be fairly certain that we haven't introduced a bug. We run the test suite before committing new code to CVS, and if it isn't green we stop and find the problem.  This way the code you get from CVS will always be "green", if the test suite fails you can be pretty sure that the changes you made broke something.</para>
			<important>If you see Firefox 1.5 fail any HTML, XHTML, or XML test, or you see Internet Explorer 6 fail any HTML test, make sure that the other developers know about it!</important>
			<para><link href="http://en.wikipedia.org/wiki/Test_driven_development">Test-driven development</link> is the idea that you write the tests before you write the code, and then you write the code to satisfy the test. It's not a bad way of doing things. Programming methodologies like <link href="http://en.wikipedia.org/wiki/Agile_software_development">agile software development</link> and <link href="http://www.extremeprogramming.org/">extreme programming</link> strongly encourage the use of tests. It's hard to argue that tests aren't a good idea. The only reason to skip writing tests is to save time in the short term, but it'll cost you more time later if you miss an important bug for lack of a test. Unit tests also help us port Mozile to new browsers by isolating the problematic differences between them. We aren't going to force you to adopt any particular programming methodology, but we strongly encourage you to write tests for any code you change or add. We're much more likely to accept changes into Mozile if they come with good tests. And we won't accept any changes that cause the test suite to fail.</para>
			<para>Mozile is currently using a slightly modified version of JsUnit 2.2 alpha. The JsUnit files are stored in <link href="../../jsunit/"><filename>www/0.8/jsunit/</filename></link>, while Mozile's test files are stored in <link href="../../tests/"><filename>www/0.8/tests/</filename></link>. The <link href="../../tests/index.xml"><filename>tests/index.xml</filename></link> file provides an index of all the tests, with buttons which trigger test runs, individually and in groups, inside HTML, XHTML, and XML test pages. The <link href="../../tests/shared"><filename>tests/shared</filename></link> directory contains the test pages, which are shared by all the tests. The other subdirectories of the <filename>tests</filename> directory contain the JavaScript test files, organized by module. If a module doesn't have its own subdirectory, the tests will be stored in <link href="../../tests/core/"><filename>tests/core/</filename></link>.</para>
			<para>The steps we take to get from <filename>index.xml</filename> to a test are somewhat complicated. Say we want to run the <code>mozile.dom</code> tests, which are stored in the <link href="../../tests/dom/dom.js"><filename>tests/dom/dom.js</filename></link> file. We point the browser to <filename>index.xml</filename>, which uses <link href="../../tests/index.xsl"><filename>index.xsl</filename></link> and <link href="../../tests/index.js"><filename>index.js</filename></link> to give us a nice dynamic web page. Then find the table row marked "mozile.dom.* :: DOM" and press the corresponding "XHTML" button. A new window opens with the URL <code><![CDATA[file://../jsunit/testRunner.html?autoRun=true&
			testpage=../tests/shared/testpage.html&test=[unit:dom/dom]]]></code> which is the Test Runner page with a bunch of additional arguments. The arguments say that the <link href="../../tests/shared/testpage.html"><filename>tests/shared/testpage.html</filename></link> test page will be used, and the <code>[unit:dom/dom]</code> test will be run. The test runner page loads, and it loads the <filename>tests/shared/testpage.html</filename> test page inside a frame. The <filename>tests/dom/dom.js</filename> test file is then loaded into the <filename>tests/shared/testpage.html</filename> test page. Then the test runner does the work of running the tests.</para>
			<para>It's also possible to run test suites (groups of tests), and tests for HTML, XHTML, and XML documents. Mozile's test system is more complicated than the minimum necessary for JsUnit testing, but it provides a lot of flexibility. All of the Mozile tests can be run, for all of the document types, with a single button press.</para>
			<para>Although the process used to load the tests is fairly complicated, writing the tests is straight-forward. Here is an example from the <filename>tests/dom/dom.js</filename> test file.</para>
			<example id="TestExampleFromDom">
				<title>Test example from <filename>tests/dom/dom.js</filename></title>
				<programlisting><![CDATA[mozile.require("mozile.dom");
mozile.require("mozile.xml");

var name = "mozile.dom.*";

/**
 * Expose the functions named in this array to JsUnit.
 * Make sure to keep it up to date!
 */
function exposeTestFunctionNames() {
  return ["testIsHTML", "testGetters", "testNamespace", "testCreate", 
    "testRemove", "testIgnore", "testIsWhitespace"];
}

/**
 *
 */
function testIsHTML() {
  // HTML Case
  if(document.documentElement.nodeName.toLowerCase() == "html") {
    assertTrue("This is HTML", mozile.dom.isHTML(document));
  }
  // XML Case
  else {
    assertFalse("This is not HTML", mozile.dom.isHTML(document));
  }
}
...]]></programlisting>
			</example>
			<para>The <code>mozile.require("mozile.dom");</code> lines are just the same as in any module. The <code>var name = "mozile.dom.*";</code> line helps identify the test file in failure messages. The <code>exposeTestFunctionNames()</code> function returns an array with the names of all the test functions in this file.</para>
			<important>Make sure the <code>exposeTestFunctionNames()</code> function is up-to-date, or your tests might not run.</important>
			<para>The <code>testIsHTML()</code> test function is simple. It includes a conditional, which determines which test should be run, and then <code>assertTrue()</code> and <code>assertFalse()</code> statements which contain a brief message and tell the test runner what the result of the <code>mozile.dom.isHTML(document)</code> call should be. If the result does not match what is asserted, the test will fail and the test status bar will turn red.</para>
			<para>Most test functions are more complicated that this one, but they all come down to a series of assertion functions. See the JsUnit documentation for a full list of assertion functions.</para>
			<para>There are a few things to keep in mind when running the tests. File paths can be tricky, because the test page is inside a frame of the test runner page, and those files are in different locations. Also, the test functions may be executed in a different page than the one they were loaded into; the file paths can be different inside test functions than they are in the global namespace of the test file.</para>
			<para>The HTML, XHTML, and XML test pages in <link href="../../tests/shared"><filename>tests/shared</filename></link> are structurally very similar, but there are differences in MIME types and namespaces which may cause browsers to fail on one and not the others. This is why we test all three kinds.</para>
			<para>You can make use of the <filename>util.js</filename> files in different places inside the <filename>tests</filename> test directory to store code which is shared between test files.</para>
			<important>Please do your best to update and improve the test suite. Make sure the test suite runs "green" before sending us the changes to your code. We can't add your changes to Mozile until the test suite runs green.</important>
		</section>
		
		<!--
		<section id="RelaxNG">
			<title>RelaxNG</title>
			<itemizedlist>
				<title>RelaxNG Resources</title>
				<listitem><link href="http://relaxng.org/">RelaxNG Homepage</link></listitem>
				<listitem><link href="http://relaxng.org/tutorial-20011203.html">RNG Tutorial</link> - A very complete and informative tutorial.</listitem>
				<listitem><link href="http://relaxng.org/spec-20011203.html">RNG Specification</link></listitem>
			</itemizedlist>
			<para></para>
			<note>This section is incomplete.</note>
		</section>
		
		<section id="Mozile Editing Schema">
			<title>Mozile Editing Schema</title>
			<itemizedlist>
				<title>MES Resources</title>
				<listitem><link href="http://mozile.mozdev.org/0.8/lib/xhtml.rng">Working MES for XHTML</link> - The work in progress on the XHTML editing schema.</listitem>
			</itemizedlist>
			<para></para>
			<note>This section is incomplete.</note>
		</section>
		-->
		
		<section id="DocumentationWithJSDoc">
			<title>Documentation with JSDoc</title>
			<itemizedlist>
				<title>JSDoc Resources</title>
				<listitem><link href="http://jsdoc.sourceforge.net/">JSDoc Homepage</link></listitem>
				<listitem>Mozile's <link href="../jsdoc/index.html">JSDoc documents</link></listitem>
				<listitem><link href="http://java.sun.com/j2se/javadoc/writingdoccomments/index.html">JavaDoc Guide</link></listitem>
			</itemizedlist>
			<note>JSDoc is written in <link href="http://jsdoc.sourceforge.net/">Perl</link>, so you'll need to have <link href="http://www.perl.org/get.html">Perl installed</link> to run it.</note>
			<para>JSDoc documents JavaScript files in the same way that JavaDoc documents Java files. It takes specially formatted comments from the code file and generates HTML documentation files. Here's an example from Mozile:</para>
			<example id="JSDocExample">
				<title>JSDoc example</title>
				<programlisting><![CDATA[/**
 * Inserts the newNode after the refNode in the refNode's parent node.
 * @param {Node} newNode The node to insert.
 * @param {Node} refNode Insert the new node after this node.
 * @type Node
 */
mozile.dom.insertAfter = function(newNode, refNode) {]]></programlisting>
			</example>
			<para>The opening <code>/**</code> indicates to JSDoc that it should pay attention to this comment. The first sentence describes the function. The <code>@param</code> statements indicate parameters (i.e. arguments) for the function. The <code>{Node}</code> entry tells JSDoc that the parameter's type is <code>Node</code>. The parameter also includes a name and description. Finally <code>@type</code> provides the return type of the function. Specifying a type in a dynamically typed language like JavaScript isn't always necessary, but it helps other developers to understand the code. The <link href="http://jsdoc.sourceforge.net/">JSDoc homepage</link> provides a full list of tags and their usage.</para>
			<para>We use JSDoc in all of the files in the <filename>code</filename> directory, as well as in all JavaScript files and most CSS and XML files throughout Mozile. New JSDoc HTML files are generated before every Mozile release, and periodically between releases. You don't need to update the HTML files yourself when you make a change to Mozile, but please keep the JSDoc comments in the source files up to date.</para>
			<para>To generate the JSDoc HTML files, we first remove all the <filename>*.html</filename> files in <link href="../../doc/jsdoc"><filename>doc/jsdoc</filename></link>. Then we run the JSDoc script over all the files in <filename>src</filename>, using the <code>-d</code> option to set the destination directory:</para>
			<programlisting><![CDATA[cd ~/working_copy/mozilewww/0.8/src/
/path/to/jsdoc.pl -d ../doc/jsdoc *]]></programlisting>
			<important>Please write and revise JSDoc comments for the code you're working on, so that the API documentation is always up-to-date.</important>
		</section>
		
		<section id="DocumentationWithDocBook">
			<title>Documentation with DocBook</title>
			<itemizedlist>
				<title>DocBook Resources</title>
				<listitem><link href="http://docbook.org">DocBook Homepage</link></listitem>
				<listitem><link href="http://docbook.sourceforge.net/">The DocBook Project</link> - Includes XSL for transforming DocBook files.</listitem>
				<listitem><link href="http://docbook.org/tdg5/en/html/">DocBook 5.0: The Definitive Guide</link> - A thorough reference. Version 5 is a work in progress.</listitem>
				<listitem><link href="http://www.sagehill.net/docbookxsl/">DocBook XSL: The Complete Guide</link> - A useful guide for transforming DocBook with XSL.</listitem>
			</itemizedlist>
			<para>This document is written using DocBook and translated into HTML using the XSL stylesheets from <link href="http://docbook.sourceforge.net/">The DocBook Project</link>. Please see the <link href="http://www.sagehill.net/docbookxsl/">DocBook XSL: The Complete Guide</link> for information on using XSL to transform DocBook files.</para>
			<para>New HTML files are generated, using the <filename>html/docbook.xsl</filename> and <filename>html/chunk.xsl</filename> stylesheets, before every release and periodically  between releases. You don't have to update the generated files yourself when you make a change, but please update the DocBook file.</para>
			<important>Please update the DocBook version of this documentation as you make changes to Mozile, so that the documentation is always up-to-date.</important>
		</section>
		
		<section id="PackagingMozile">
			<title>Packaging Mozile</title>
			<itemizedlist>
				<title>Packaging Resources