1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#######################
Editor module structure
#######################
This document explains the structure of the editor module and overview of classes.
Introduction
============
This module implements the builtin editors of editable elements or documents, and this does **not**
implement the interface with DOM API and visual feedback of the editing UI. In other words, this
module implements DOM tree editors.
Directories
===========
composer
--------
Previously, this directory contained "Composer" UI related code. However, currently, this
directory contains ``nsEditingSession`` and ``ComposerCommandsUpdater``.
libeditor
---------
This is the main directory which contains "core" implementation of editors.
spellchecker
------------
Despite of the directory name, implementation of the spellchecker is **not** here. This directory
contains only a bridge between editor classes and the spellchecker and serialized text of editable
content for spellchecking.
txmgr
-----
This directory contains transaction items and transaction classes. They were designed for generic
use cases, e.g., managing undo/redo of bookmarks/history of browser, etc, but they are used only by
the editor.
Main classes
============
EditorBase
----------
``EditorBase`` class is an abstract class of editors. This inherits ``nsIEditor`` XPCOM interface,
implement common features which work with instance of classes, and exposed by
``mozilla/EditorBase.h``.
TextEditor
----------
``TextEditor`` class is the implementation of plaintext editor which works with ``<input>`` and
``<textarea>``. Its exposed root is the host HTML elements, however, the editable root is an
anonymous ``<div>`` created in a native anonymous subtree under the exposed root elements. This
creates a ``Text`` node as the first child of the anonymous ``<div>`` and modify its data. If the text
data ends with a line-break, i.e., the last line is empty, append a ``<br>`` element for making the
empty last line visible.
This also implements password editor. It works almost same as normal text editor, but each character
may be masked by masked character such as "●" or "*" by the layout module for the privacy.
Therefore, this manages masked/unmasked range of password and maybe making typed character
automatically after a while for mobile devices.
This is exposed with ``mozilla/TextEditor.h``.
Selection in TextEditor
^^^^^^^^^^^^^^^^^^^^^^^
Independent ``Selection`` and ``nsFrameSelection`` per ``<input>`` or ``<textarea>``.
Lifetime of TextEditor
^^^^^^^^^^^^^^^^^^^^^^
Created when an editable ``<textarea>`` is created or a text-editable ``<input>`` element gets focus.
Note that the initialization may run asynchronously if it's requested when it's not safe to run
script. Destroyed when the element becomes invisible. Note that ``TextEditor`` is recreated when
every reframe of the host element. This means that when the size of ``<input>`` or ``<textarea>``
is changed for example, ``TextEditor`` is recreated and forget undo/redo transactions, but takes
over the value, selection ranges and composition of IME from the previous instance.
HTMLEditor
----------
``HTMLEditor`` class is the implementation of rich text editor which works with ``contenteditable``,
``Document.designMode`` and XUL ``<editor>``. Its instance is created per document even if the
document has multiple elements having ``contenteditable`` attribute. Therefore, undo/redo
transactions are shared in all editable regions.
This is exposed with ``mozilla/HTMLEditor.h``.
Selection in HTMLEditor
^^^^^^^^^^^^^^^^^^^^^^^
The instance for the ``Document`` and ``Window``. When an editable element gets focus, ``HTMLEditor``
sets the ancestor limit of ``Selection`` to the focused element or the ``<body>`` of the ``Document``.
Then, ``Selection`` cannot cross boundary of the limiter element.
Lifetime of HTMLEditor
^^^^^^^^^^^^^^^^^^^^^^
Created when first editable region is created in the ``Document``. Destroyed when last editable
region becomes non-editable.
Currently, even while ``HTMLEditor`` is handling an edit command/operation (called edit action in
editor classes), each DOM mutation can be tracked with legacy DOM mutation events synchronously.
Thus, after changing the DOM tree from ``HTMLEditor``, any state could occur, e.g., the editor
itself may have been destroyed, the DOM tree have been modified, the ``Selection`` have been
modified, etc. This issue is tracked in
`bug 1710784 <https://bugzilla.mozilla.org/show_bug.cgi?id=1710784>`__.
EditorUtils
-----------
This class has only static utility methods which are used by ``EditorBase`` or ``TextEditor`` and
may be used by ``HTMLEditor`` too. I.e., the utility methods which are used **not** only by
``HTMLEditor`` should be implemented in this class.
Typically, sateless methods should be implemented as ``static`` methods of utility classes because
editor classes have too many methods and fields.
This class is not exposed.
HTMLEditUtils
-------------
This class has only static utility methods which are used only by ``HTMLEditor``.
This class is not exposed.
AutoRangeArray
--------------
This class is a stack only class and intended to copy of normal selection ranges. In the new code,
`Selection` shouldn't be referred directly, instead, methods should take reference to this instance
and modify it. Finally, root caller should apply the ranges to `Selection`. Then, `HTMLEditor`
does not need to take care of unexpected `Selection` updates by legacy DOM mutation event listeners.
This class is not exposed.
EditorDOMPoint, EditorRawDOMPoint, EditorDOMPointInText, EditorRawDOMPointInText
--------------------------------------------------------------------------------
It represents a point in a DOM tree with one of the following:
* Container node and offset in it
* Container node and child node in it
* Container node and both offset and child node in it
In most cases, instances are initialized with a container and only offset or child node. Then,
when ``Offset()`` or ``GetChild()`` is called, the last one is "fixed". After inserting new child
node before the offset and/or the child node, ``IsSetAndValid()`` will return ``false`` since the
child node is not the child at the offset.
If you want to keep using after modifying the DOM tree, you can make the instance forget offset or
child node with ``AutoEditorDOMPointChildInvalidator`` and ``AutoEditorDOMRangeChildrenInvalidator``.
The reason why the forgetting methods are not simply exposed is, ``Offset()`` and ``GetChild()``
are available even after the DOM tree is modified to get the cached offset and child node,
additionally, which method may modify the DOM tree may be not clear for developers. Therefore,
creating a block only for these helper classes makes the updating point clearer.
These classes are exposed with ``mozilla/EditorDOMPoint.h``.
EditorDOMRange, EditorRawDOMRange, EditorDOMRangeInTexts, EditorRawDOMRangeInTexts
----------------------------------------------------------------------------------
It represents 2 points in a DOM tree with 2 ``Editor*DOMPoint(InText)``. Different from ``nsRange``,
the instances do not track the DOM tree changes. Therefore, the initialization is much faster than
``nsRange`` and can be in the stack.
These classes are exposed with ``mozilla/EditorDOMPoint.h``.
AutoTrackDOMPoint, AutoTrackDOMRange
------------------------------------
These methods updates ``Editor*DOMPoint(InText)`` or ``Editor*DOMRange(InTexts)`` at destruction
with applying the changes caused by the editor instance. In other words, they don't track the DOM
tree changes by the web apps like changes from legacy DOM mutation event listeners.
These classes are currently exposed with ``mozilla/SelectionState.h``, but we should stop exposing
them.
WSRunScanner
------------
A helper class of ``HTMLEditor``. This class scans previous or (inclusive) next visible thing from
a DOM point or a DOM node. This is typically useful for considering whether a `<br>` is visible or
invisible due to near a block element boundary, finding nearest editable character from caret
position, etc. However, the running cost is **not** cheap, thus if you find another way to consider
it simpler, use it instead, and also this does not check the actual style of the nodes (visible vs.
invisible, block vs. inline), thus you'd get unexpected result in tricky cases.
This class is not exposed.
WhiteSpaceVisibilityKeeper
--------------------------
A helper class of ``HTMLEditor`` to handle collapsible white-spaces as what user expected. This
class currently handles white-space normalization (e.g., when user inputs multiple collapsible
white-spaces, this replaces some of them to NBSPs), but the behavior is different from the other
browsers. We should re-implement this with emulating the other browsers' behavior as far as possible,
but currently it's put off due to not affecting UX (tracked in
`bug 1658699 <https://bugzilla.mozilla.org/show_bug.cgi?id=1658699>`__.
This class is not exposed.
\*Transaction
-------------
``*Transaction`` classes represents a small transaction of updating the DOM tree and implements
"do", "undo" and "redo" of the update.
Note that each class instance is created too many (one edit action may cause multiple transactions).
Therefore, each instance must be smaller as far as possible, and if you have an idea to collapse
multiple instances to one instance, you should fix it. Then, users can run Firefox with smaller
memory devices especially if the transaction is used in ``TextEditor``.
|