VisualEditor/API/Data Model/Surface

A surface contains a document, selection and history of transactions that have been processed over time. While the user interface provides a familiar affordance for working with a document, everything that is being changed is done through the surface model.

Selection
In the user interface, a user describes where in the document they want to change by selecting. Although rendered very differently, a blinking cursor is just a zero-length selection. This selection mechanism is used throughout VisualEditor, and especially when working with documents.

Offsets
Offsets describe locations in a document. While only the offsets where content can be inserted are available in the user interface, no such restriction exists at the model level.


 * An offset describes a position within the document between two elements, characters or extents.
 * All elements in a document are wrapped in a pair of elements with the exception of text and the document itself. Due to this wrapping, in a document that contains a single paragraph, the offset before the first character is offset 1.


 * Text is not wrapped with elements, the boundaries are implied.
 * If the cursor is inside of an element that can contain content, it's effectively inside a text node, even if the element is empty.

Ranges
A range is a pair of offsets, from and to. A selection can be drawn either in the reading direction, or backwards, so it's common to work with the start and end values of a range, which are always in ascending order respectively, rather than the from and to values which are always in the original order that the selection was drawn.


 * To select the contents of an element, a range must start and end inside of the element.
 * Even if the range covers one of the element ends (the opening or closing) the content is the only thing that is being worked on. Given the scenario of removal, selecting a part of an element will cause the element to be truncated.


 * To select the element itself, the range must completely contain the element.
 * If a range starts before the element and ends after it, the entire element is consider covered. Given the scenario of removal, selecting an entire element will remove it and it's contents.

To create a new range, the ve.Range class can be instantiated with from and to arguments, each an offset within a document.

A common way that a range object is obtained is when getting the current selection.

You can also modify the selection of a surface by providing a range object as an argument. When the selection is changed on the model, the user interface responds by selecting the corresponding content in the DOM.

When working with a node in the document's model tree, you can get the range or outer range (which includes wrappers if appropriate) directly.

Fragments
When working with a surface, a surface fragment object is used to abstract away the complexity of getting information about a surface and making changes to it while keeping everything in sync. Interactions with this API are similar to working with a jQuery selection in many ways, which is done intentionally to make it easier to learn.

Chaining
Once you have a fragment, you can get information about the surface using a getter function, get another fragment based on the one you have, or make changes to the document. The latter two of these classes of operations are chainable.

Null fragments
Sometimes getting a new fragment based on the one you have results in an invalid fragment, such as asking to expand the range to the nearest paragraph while in a pre-formatted node. In this case a null fragment is returned, from which all getters return empty values, only null fragments can be created and changes to the document are ignored.

Ranges
Each fragment's range will automatically be updated to remain relevant any time a document is modified. This is done internally by using the ve.dm.Transaction.translateRange feature.

ve.dm.SurfaceFragment( surface, selection )
Selected portion of a surface.
 * surface {ve.dm.Surface}
 * Target surface


 * selection {ve.dm.Selection} [optional]
 * Selection within target document, current selection used by default


 * isNull
 * Responds to transactions being processed on the document
 * Returns  {Boolean}  Fragment is a null fragment


 * adjustLinearSelection( start, end )
 * Gets a new fragment with an adjusted position
 * Returns  {ve.dm.SurfaceFragment}  Adjusted fragment


 * start {Number} [optional]
 * Adjustment for start position


 * end {Number} [optional]
 * Adjustment for end position