Selection object

Represents textual (and block) selection inside element that has either htmlarea or richtext behaviors attached.

To access the selection object of the element use its selection property.

Properties

type
null | symbol, read-only property. Reports type of the selection: #text, #cells, #blocks.
isCollapsed
true | false, read-only property. true if the selection is represented by only caret so selection.start == selection.end.
anchor
null | bookmark, read-only property. DOM position of selection anchor.
caret
null | bookmark, read-only property. DOM position of selection caret.
start
null | bookmark, read-only property. DOM position of either caret or anchor, whichever is lower in DOM order.
end
null | bookmark, read-only property. DOM position of either caret or anchor, whichever is greater in DOM order.
text
string, read-only property. text contained in the selection.
html
string, read-only property. selected html fragment.

Enumeration

for ... in
for(var (pos,char) in element. selection) { /* loop body */ }

TBD. (enumerates caret positions and characters in the selection)

Methods

collapse
( towhat:symbol ) returns: bookmark.

Collapse selection range to single caret position. towhat is one of: #toCaret, #toAnchor,#toStart or #toEnd

select
( caret:bookmark [ , anchor:bookmark ] ) returns: true | false

Set's selection to the range or single caret position.

select
( el:element [ , how: #content | #element ] ) returns: true | false

Set's selection to either the element el itself (what is #element) or to its content - its first and last caret positions.

contains
( selector: string ) returns: true | false

Returns true if selection covers at least one of elements matching the CSS selector.

elements
( selector: string ) returns: Array of elements

Returns list of DOM elements covered by the selection. It returns only elements satisfying the selector. Note that selector may contain list of comma separated selectors.

Example:

var paragraphs = editor.selection.elements("p,div");

Here paragraphs will contain list of <p> and <div> elements that are covered by current selection.

advance
( step:symbol [, target:symbol] ) returns: true | false.

Advances target to new position by step. The target defines what end of selection to advance, can be one of:

The step is one of:

applyMark
(start: bookmark, end: bookmark, markName: string): returns: integer

marks region of text from start to end by mark name that can be styled in CSS by ::mark(markName) { ... } selector/rule.

The function returns number of changes made.

clearMark
(start: bookmark, end: bookmark, markName: string): returns: integer

clears mentioned mark from the region.

The function returns number of changes made.

box
(part: symbol)

Returns view relative coordinates of:

part - one of symbolic constants #left, #top, #right, #bottom, #width or #height. Defines part of box (rectangle) to return.

Bookmark

Some methods above use so called bookmark object that is a tuple (of length 3) having tag #bookmark. Usually you will get ready to use bookmarks from various properties or  methods but if needed bookmarks can be constructed literally as:

var bm = [bookmark: node, index, after];

Where: