layout/doc/Layout_Overview.html

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 <!-- This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 - License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
michael@0 4
michael@0 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
michael@0 6 <html>
michael@0 7 <head>
michael@0 8 <title>Layout Overview</title>
michael@0 9 <meta http-equiv="content-type"
michael@0 10 content="text/html; charset=ISO-8859-1">
michael@0 11 </head>
michael@0 12 <body>
michael@0 13 <h1>Layout System Overview</h1>
michael@0 14 <br>
michael@0 15 <h3>Layout's Job: Provide the Presentation</h3>
michael@0 16 Layout is primarily concerned with providing a presentation to an HTML or
michael@0 17 XML document. This presentation is typically formatted in accordance with
michael@0 18 the requirements of the <a href="http://www.w3.org/TR/REC-CSS1">CSS1</a>
michael@0 19 and <a href="http://www.w3.org/TR/REC-CSS2/">CSS2</a> specifications from
michael@0 20 the W3C. Presentation formatting is also required to provide compatibility
michael@0 21 with legacy browsers (Microsoft Internet Explorer and Netscape Navigator
michael@0 22 4.x). The decision about when to apply CSS-specified formatting and when
michael@0 23 to apply legacy formatting is controlled by the document's DOCTYPE specification.
michael@0 24 These layout modes are referred to as 'Standards' and 'NavQuirks' modes. (DOCTYPE
michael@0 25 and modes are explained in more detail <a
michael@0 26 href="http://www.mozilla.org/quality/help/bugzilla-helper.html">here</a>).<br>
michael@0 27 <br>
michael@0 28 The presentation generally is constrained by the width of the window in which
michael@0 29 the presentation is to be displayed, and a height that extends as far as
michael@0 30 necessary. This is referred to as the Galley Mode presentation, and is what
michael@0 31 one expects from a typical browser. Additionally, layout must support a paginated
michael@0 32 presentation, where the width of the presentation is constrained by the dimensions
michael@0 33 of the printed output (paper) and the height of each page is fixed. This
michael@0 34 paged presentation presents several challenges not present in the galley
michael@0 35 presentation, namely how to break up elements that are larger than a single
michael@0 36 page, and how to handle changes to page dimensions.<br>
michael@0 37 <br>
michael@0 38 The original design of the Layout system allowed for multiple, possibly different,
michael@0 39 presentations to be supported simultaneously from the same content model.
michael@0 40 In other words, the same HTML or XML document could be viewed as a normal
michael@0 41 galley presentation in a browser window, while simultaneously being presented
michael@0 42 in a paged presentation to a printer, or even an aural presentation to a
michael@0 43 speech-synthesizer. To date the only real use of this multiple presentation
michael@0 44 ability is seen in printing, where multiple presentations are managed, all
michael@0 45 connected to the same content model. (note: it is unclear if this is really
michael@0 46 a benefit - it may have been better to use a copy of the content model for
michael@0 47 each presentation, and to remove the complexity of managing separate presentations
michael@0 48 - analysis is needed here). The idea of supporting a non-visual presentation
michael@0 49 is interesting. Layout's support for aural presentations is undeveloped,
michael@0 50 though conceptually, it is possible and supported by the architecture.<br>
michael@0 51 <br>
michael@0 52 <h3>How Layout Does its Job: Frames and Reflow</h3>
michael@0 53 So, layout is concerned with providing a presentation, in galley or paged
michael@0 54 mode. Given a content model, how does the layout system actually create the
michael@0 55 presentation? Through the creation and management of frames. Frames are an
michael@0 56 encapsulation of a region on the screen, a region that contains geometry
michael@0 57 (size and location, stacking order). Generally frames correspond to the content
michael@0 58 elements, though there is often a one-to-many correspondence between content
michael@0 59 elements and frames. Layout creates frames for content based on either the
michael@0 60 specific HTML rules for an element or based on the CSS display type of the
michael@0 61 element. In the case of the HTML-specific elements, the frame types that
michael@0 62 correspond to the element are hard-coded, but in the more general case where
michael@0 63 the display type is needes, the layout system must determine that display
michael@0 64 type by using the Style System. A content element is passed to the Style
michael@0 65 System and a request is made to resolve the style for that element. This
michael@0 66 causes the Style System to apply all style rules that correspond to the element
michael@0 67 and results in a resolved Style Context - the style data specific to that
michael@0 68 element. The Layout module looks at the 'display' field of the style context
michael@0 69 to determine what kind of frame to create (block, inline, table, etc.). The
michael@0 70 style context is associated with the frame via a reference because it is
michael@0 71 needed for many other computations during the formatting of the frames.<br>
michael@0 72 <br>
michael@0 73 Once a frame is created for a content element, it must be formatted. We refer
michael@0 74 to this as 'laying out' the frame, or as 'reflowing' the frame. Each frame
michael@0 75 implements a Reflow method to compute its position and size, among other
michael@0 76 things. For more details on the Reflow mechanism, see the Reflow Overview
michael@0 77 document... &nbsp;The CSS formatting requirements present two distinct layout
michael@0 78 models: 1) the in-flow model, where the geometry of an element is influenced
michael@0 79 by the geometry of the elements that precede it, and 2) the positioned model,
michael@0 80 where the geometry of an element is not influenced by the geometry of the
michael@0 81 elements that precede it, or in any case, is influenced more locally. The
michael@0 82 in-flow cased is considered the 'normal' case, and corresponds to normal
michael@0 83 HTML formatting. The later case, called 'out of flow' puts the document author
michael@0 84 in control of the layout, and the author must specify the locations and sizes
michael@0 85 of all of the elements that are positioned. There is, of course, some complexity
michael@0 86 involved with managing these two models simultanelusly...<br>
michael@0 87 <br>
michael@0 88 So far the general flow of layout looks like this:<br>
michael@0 89 <br>
michael@0 90 1) Obtain a document's content model<br>
michael@0 91 2) Utilize the Style System to resolve the style of each element in the content
michael@0 92 model<br>
michael@0 93 3) Construct the frames that correspond to the content model, according to
michael@0 94 the resolved style data.<br>
michael@0 95 4) Perform the initial layout, or initial reflow, on the newly constructed
michael@0 96 frame.<br>
michael@0 97 <br>
michael@0 98 This is pretty straight-forward, but is complicated somewhat by the notion
michael@0 99 of <i>incrementalism</i>. One of the goals of the Layout system's design
michael@0 100 is to create parts of the presentation as they become available, rather than
michael@0 101 waiting for the entire document to be read, parsed, and then presented. This
michael@0 102 is a major benefit for large documents because the user does not have to wait
michael@0 103 for the 200th page of text to be read in before the first page can be displayed
michael@0 104 - they can start reading something right away. So really, this sequence of
michael@0 105 operations Resolve Style, Create Frame, Layout Frame, gets repeated many
michael@0 106 times as the content becomes available. In the normal in-flow case this is
michael@0 107 quite natural because the sequential addition of new content results in sequential
michael@0 108 addition of new frames, and because everything is in-flow, the new frames
michael@0 109 do not influence the geometry of the frames that have already been formatted.
michael@0 110 When out-of-flow frames are present this is a more difficult problem, however.
michael@0 111 Sometimes a content element comes in incrementally, and invalidates the formatting
michael@0 112 of some of the frames that precede it, frame that have already been formatted.
michael@0 113 In this case the Layout System has to detect that impact and reflow again
michael@0 114 the impacted frames. This is referred to as an <i>incremental reflow</i>.<br>
michael@0 115 <br>
michael@0 116 <a name="DHTML_interaction"></a>Another responsibility of layout is to manage
michael@0 117 dynamic changes to the content model, changes that occur after the document
michael@0 118 has been loaded and (possibly) presented. These dynamic changes are caused
michael@0 119 by manipulations of the content model via the&nbsp;<acronym
michael@0 120 title="Document Object Model">DOM<acronym></acronym></acronym> (generally
michael@0 121 through java script). When a content element is modified, added or removed,
michael@0 122 Layout is notified. If content elements have been inserted, new frames are
michael@0 123 created and formatted (and impacts to other frames are managed by performing
michael@0 124 further incremental reflows). If content is removed, the corresponding frames
michael@0 125 are destroyed (again, impacts to other elements are managed). If a content
michael@0 126 element is modified, layout must determine if the chage influences the formatting
michael@0 127 of that or other elements' presentations, and must then reformat, or re-reflow,
michael@0 128 the impacted elements. In all cases, the determination of the impact is critical
michael@0 129 to avoid either the problem of not updating impacted elements, thus presenting
michael@0 130 an invalid presentation, or updating too much of the presentation and thus
michael@0 131 doing too much work, potentially causing performance problems.<br>
michael@0 132 <br>
michael@0 133 One very special case of dynamic content manipulation is the HTML Editor.
michael@0 134 Layout is used to implement both a full-blown WYSIWYG HTML editor, and a single
michael@0 135 and multi-line text entry control. In both cases, the content is manipulated
michael@0 136 by the user (via the DOM) and the resulting visual impacts must be shown as
michael@0 137 quickly as possible, without disconcerting flicker or other artifacts that
michael@0 138 might bother the user. Consider a text entry field: the user types text into
michael@0 139 a form on the web. As the user types a new character it is inserted into
michael@0 140 the content model. This causes layout to be notified that a new piece of
michael@0 141 content has been entered, which causes Layout to create a new frame and format
michael@0 142 it. This must happen very fast, so the user's typing is not delayed. In the
michael@0 143 case of the WYSIWYG HTML editor, the user expects that the modifications
michael@0 144 they make to the document will apear immediately, not seconds later. This
michael@0 145 is especially critical when the user is typing into the document: it would
michael@0 146 be quite unusable if typing a character at the end of a document in the HTML
michael@0 147 editor caused the entire document to be reformated - it would be too slow,
michael@0 148 at least on low-end machines. Thus the HTML editor and text controls put
michael@0 149 considerable performance requirements on the layout system's handling of dynamic
michael@0 150 content manipulation.<br>
michael@0 151 <h3>The Fundamentals of Frames: Block and Line</h3>
michael@0 152 There are many types of frames that are designed to model various formatting
michael@0 153 requirements of different HTML and XML elements. CSS2 defines several (block,
michael@0 154 inline, list-item, marker, run-in, compact, and various table types) and
michael@0 155 the standard HTML form controls require their own special frame types to
michael@0 156 be formatted as expected. The most essential frame types are Block and Inline,
michael@0 157 and these correspond to the most important Layout concepts, the Block and
michael@0 158 Line.<br>
michael@0 159 <br>
michael@0 160 A block is a rectangular region that is composed of one or more lines. A
michael@0 161 line is a single row of text or other presentational elements. All layout
michael@0 162 happens in the context of a block, and all of the contents of a block are
michael@0 163 formatted into lines within that block. As the width of a block is changed,
michael@0 164 the contents of the lines must be reformatted. consider for example a large
michael@0 165 paragraph of text sitting in paragraph:<br>
michael@0 166 <br>
michael@0 167 <pre>&lt;p&gt;<br> We need documentation for users, web developers, and developers working
michael@0 168 on Mozilla. If you write your own code, document it. Much of the
michael@0 169 existing code &lt;b&gt;isn&#8217;t very well documented&lt;/b&gt;. In the process of figuring
michael@0 170 things out, try and document your discoveries.
michael@0 171 If you&#8217;d like to contribute, let us know.<br>&lt;/p&gt;</pre>
michael@0 172 There is one block that corresponds to the &lt;p&gt; element, and then a number
michael@0 173 of lines of text that correspond to the text. As the width of the block changes
michael@0 174 (due to the window being resized, for example) the length of the lines within
michael@0 175 it changes, and thus more or less text appears on each line. The block is
michael@0 176 responsible for managing the lines. Note that lines may contain only inline
michael@0 177 elements, whereas block may contain both inline elements and other blocks.<br>
michael@0 178 <h3>Other Layout Models: XUL</h3>
michael@0 179 In addition to managing CSS-defined formatting, the layout system provides
michael@0 180 a way to integrate other layout schemes into the presentation. Currently
michael@0 181 layout supports the formatting of XUL elements, which utilize a constraint-based
michael@0 182 layout language. The Box is introduced into the layout system's frame model
michael@0 183 via an adapter (BoxToBlockAdapter) that translates the normal layout model
michael@0 184 into the box formatting model. Conceptually, this could be used to introduce
michael@0 185 other layout systems, but it might be worth noting that there was no specific
michael@0 186 facility designed into the layout system to accommodate this. Layout deals
michael@0 187 with frames, but as long as the layout system being considered has no need
michael@0 188 to influence presentational elements from other layout systems, it can be
michael@0 189 adapted using a frame-based adapter, ala XUL.<br>
michael@0 190 <br>
michael@0 191 <h2>Core Classes</h2>
michael@0 192 At the highest level, the layout system is a group of classes that manages
michael@0 193 the presentation within a fixed width and either unlimited height (galley
michael@0 194 presentation) or discrete page heights (paged presentation). Digging just
michael@0 195 a tiny bit deeper into the system we find that the complexity (and interest)
michael@0 196 mushrooms very rapidly. The idea of formatting text and graphics to fit within
michael@0 197 a given screen area sounds simple, but the interaction of in-flow and out-of-flow
michael@0 198 elements, the considerations of incremental page rendering, and the performance
michael@0 199 concerns of dynamic content changes makes for a system that has a lot of
michael@0 200 work to do, and a lot of data to manage. Here are the high-level classes
michael@0 201 that make up the layout system. Of course this is a small percentage of the
michael@0 202 total clases in layout (see the detailed design documents for the details
michael@0 203 on all of the classes, in the context of their actual role).<br>
michael@0 204 <h3>Presentation Shell / Presentation Context</h3>
michael@0 205 Together the presentation shell and the presentation context provide the
michael@0 206 root of the current presentation. The original design provided for a single
michael@0 207 Presentation Shell to manage multiple Presentation Contexts, to allow a single
michael@0 208 shell to handle multiple presentations. It is unclear if this is really possible,
michael@0 209 however, and in general it is assumed that there is a one-to-one correspondence
michael@0 210 between a presentation shell and a presentation context. The two classes
michael@0 211 should probably be folded into one, or the distinction between them formalized
michael@0 212 and utilized in the code. The Presentation Shell currently owns a controlling
michael@0 213 reference to the Presentation Context. Further references to the Presentation
michael@0 214 Shell and Presentation Context will be made by using the term Presentation
michael@0 215 Shell.<br>
michael@0 216 <br>
michael@0 217 The Presentation Shell is the root of the presentation, and as such it owns
michael@0 218 and manges a lot of layout objects that are used to create and maintain a
michael@0 219 presentation (<font color="#990000">note that the DocumentViewer is the owner
michael@0 220 of the Presentation Shell, and in some cases the creator of the objects
michael@0 221 used by the Presentation Shell to manage the presentation. More details
michael@0 222 of the Document Viewer are needed here...</font>). The Presentation Shell,
michael@0 223 or PresShell, is first and foremost the owner of the formatting objects,
michael@0 224 the frames. Management of the frames is facilitated by the Frame Manager,
michael@0 225 an instance of which the PresShell owns. Additionally, the PresShell provides
michael@0 226 a specialized storage heap for frames, called an Arena, that is used to make
michael@0 227 allocation and deallocation of frames faster and less likely to fragment
michael@0 228 the global heap. <br>
michael@0 229 <br>
michael@0 230 The Presentation Shell also owns the root of the Style System, the Style
michael@0 231 Set. In many cases the Presentation Shell provides pass-through methods to
michael@0 232 the Style Set, and generally uses the Style Set to do style resolution and
michael@0 233 Style Sheet management.<br>
michael@0 234 <br>
michael@0 235 One of the critical aspects of the Presentation Shell is the handling of
michael@0 236 frame formatting, or reflow. The Presentation Shell owns and maintains a
michael@0 237 Reflow Queue where requests for reflow are held until it is time to perform
michael@0 238 a reflow, and then pulled out and executed.<br>
michael@0 239 <br>
michael@0 240 It is also important to see the Presentation Shell as an observer of many
michael@0 241 kinds of events in the system. For example, the Presentation Shell receives
michael@0 242 notifications of document load events, which are used to trigger updates
michael@0 243 to the formatting of the frames in some cases. The Presentation Shell also
michael@0 244 receives notifications about changes in cursor and focus states, whereby
michael@0 245 the selection and caret updates can be made visible.<br>
michael@0 246 <br>
michael@0 247 There are dozens of other data objects managed by the Presentation Shell
michael@0 248 and Presentation Context, all necessary for the internal implementation.
michael@0 249 These data members and their roles will be discussed in the Presentation
michael@0 250 Shell design documents. For this overview, the Frames, Style Set, and Reflow
michael@0 251 Queue are the most important high-level parts of the Presentation Shell.<br>
michael@0 252 <h3>Frame Manager</h3>
michael@0 253 The Frame Manager is used to, well, manage frames. Frames are basic formatting
michael@0 254 objects used in layout, and the Frame Manager is responsible for making frames
michael@0 255 available to clients. There are several collections of frames maintained
michael@0 256 by the Frame Manager. The most basic is a list of all of the frames starting
michael@0 257 at the root frame. Clients generally do not want to incur the expense of
michael@0 258 traversing all of the frames from the root to find the frame they are interested
michael@0 259 in, so the Frame Manager provides some other mappings based on the needs of
michael@0 260 the clients.<br>
michael@0 261 <br>
michael@0 262 The most crucial mapping is the Primary Frame Map. This collection provides
michael@0 263 access to a frame that is designated as the primary frame for a piece of
michael@0 264 content. When a frame is created for a piece of content, it may be the 'primary
michael@0 265 frame' for that content element (content elements that require multiple
michael@0 266 frames have primary and secondary frames; only the primary frame is mapped).
michael@0 267 The Frame Manager is then instructed to store the mapping from a content
michael@0 268 element to the primary frame. This mapping facilitates updates to frames
michael@0 269 that result in changes to content (see <a href="#DHTML_interaction">discussion
michael@0 270 above</a>).<br>
michael@0 271 <br>
michael@0 272 Another important mapping maintained by the Frame Manager is that of the
michael@0 273 undisplayed content. When a content element is defined as having no display
michael@0 274 (via the CSS property 'display:none') it is noted by a special entry in the
michael@0 275 undisplayed map. This is important because no frame is generated for these
michael@0 276 elements yet changes to their style values and to the content elements still
michael@0 277 need to be handled by layout, in case their display state changes from 'none'
michael@0 278 to something else. The Undisplayed Map keeps track of all content and style
michael@0 279 data for elements that currently have no frames. (<font color="#990000">note:
michael@0 280 the original architecture of the layout system included the creation of frames
michael@0 281 for elements with no display. This changed somewhere along the line, but
michael@0 282 there is no indication of why the change was made. Presumably it is more
michael@0 283 time and space-efficient to prevent frame creation for elements with no display.)</font><br>
michael@0 284 <br>
michael@0 285 The Frame Manager also maintains a list of Forms and Form Controls, as <i>content
michael@0 286 nodes</i>. This is presumably related to the fact that layout is responsible
michael@0 287 for form submission, but this is a design flaw that is being corrected by
michael@0 288 moving form submission into the content world. These collections of Forms
michael@0 289 and Form Controls should be removed eventually.<br>
michael@0 290 <br>
michael@0 291 <h3>CSS Frame Constructor</h3>
michael@0 292 The Frame Constructor is responsible for resolving style values for content
michael@0 293 elements and creating the appropriate frames corresponding to that element.
michael@0 294 In addition to managing the creation of frames, the Frame Constructor is
michael@0 295 responsible for managing changes to the frames. Frame Construction is generally
michael@0 296 achieved by the use of stateless methods, but in some cases there is the
michael@0 297 need to provide context to frames created as children of a container. The
michael@0 298 Frame Manager uses the Frame Constructor State class to manage the important
michael@0 299 information about the container of a frame being created (<font
michael@0 300 color="#990000">and lots of other state-stuff too - need to describe more
michael@0 301 fully</font>).<br>
michael@0 302 <h3>Frame</h3>
michael@0 303 The Frame is the most fundamental layout object. The class nsFrame is the
michael@0 304 base class for all frames, and it inherits from the base class nsIFrame (note:
michael@0 305 nsIFrame is NOT an interface, it is an abstract base class. It was once an
michael@0 306 interface but was changed to be a base class when the Style System was modified
michael@0 307 - the name was not changed to reflect that it is not an interface). The Frame
michael@0 308 provides generic functionality that can be used by subclasses but cannot
michael@0 309 itself be instantiated.<br>
michael@0 310 <br>
michael@0 311 nsFrame:<br>
michael@0 312 The Frame provides a mechanism to navigate to a parent frame as well as child
michael@0 313 frames. All frames have a parent except for the root frame. The Frame is
michael@0 314 able to provide a reference to its parent and to its children upon request.
michael@0 315 The basic data that all frames maintain include: a rectangle describing the
michael@0 316 dimensions of the frame, a pointer to the content that the frame is representing,
michael@0 317 the style context representing all of the stylistic data corresponding to
michael@0 318 the frame, a parent frame pointer, a sibling frame pointer, and a series
michael@0 319 of state bits.<br>
michael@0 320 <br>
michael@0 321 Frames are chained primarily by the sibling links. Given a frame, one can
michael@0 322 walk the sibling of that frame, and can also navigate back up to the parent
michael@0 323 frame. Specializations of the frame also allow for the management of child
michael@0 324 frames; this functionality is provided by the Container Frame.<br>
michael@0 325 <br>
michael@0 326 Container Frames:<br>
michael@0 327 The Container Frame is a specialization of the base frame class that introduces
michael@0 328 the ability to manage a list of child frames. All frames that need to manage
michael@0 329 child frames (e.g. frames that are not themselves leaf frames) derive from
michael@0 330 Container Frame.<br>
michael@0 331 <br>
michael@0 332 <br>
michael@0 333 <br>
michael@0 334 <h3>Block Frame<br>
michael@0 335 </h3>
michael@0 336 <h3>Reflow State</h3>
michael@0 337 <h3>Reflow Metrics<br>
michael@0 338 </h3>
michael@0 339 <h3>Space Manager<br>
michael@0 340 </h3>
michael@0 341 <h3>StyleSet</h3>
michael@0 342 <h3>StyleContext</h3>
michael@0 343 <br>
michael@0 344 <br>
michael@0 345 <hr width="100%" size="2"><br>
michael@0 346 <b>Document History:<br>
michael@0 347 </b>
michael@0 348 <ul>
michael@0 349 <li>05/20/2002 - Marc Attinasi: &nbsp;created, wrote highest level introduction
michael@0 350 to general layout concepts, links to relevant specs and existing documents.<br>
michael@0 351 </li>
michael@0 352 </ul>
michael@0 353 </body>
michael@0 354 </html>

mercurial