0 results found

tinymce.dom.Selection

Contribute to this page
Namespacetinymce.dom
ClassSelection

This class handles text and control selection it's an crossbrowser utility class. Consult the TinyMCE Wiki API for more details and examples on how to use this class.

Example

Public Methods

MethodDefined By
Selection(dom:DOMUtils, win:Window, serializer:Serializer) : Constructs a new selection instance.Selection
collapse(to_start:Boolean):void : Collapse the selection to start or end of range.Selection
getBookmark(type:Number, normalized:Boolean):Object : Returns a bookmark location for the current selection.Selection
getContent(s:Object):String : Returns the selected contents using the DOM serializer passed in to this class.Selection
getEnd():Element : Returns the end element of a selection range.Selection
getNode():Element : Returns the currently selected element or the common ancestor element for both start and end of the selection.Selection
getRng(w3c:Boolean):Range : Returns the browsers internal range object.Selection
getSel():Selection : Returns the browsers internal selection object.Selection
getStart():Element : Returns the start element of a selection range.Selection
isCollapsed():Boolean : Returns true/false if the selection range is collapsed or not.Selection
moveToBookmark(bookmark:Object):Boolean : Restores the selection to the specified bookmark.Selection
select(node:Element, content:Boolean):Element : Selects the specified element.Selection
selectorChanged(selector:String, callback:function):void : Executes callback of the current selection matches the specified selector or not and passes the state and args to the ca...Selection
setContent(content:String, args:Object):void : Sets the current selection to the specified content.Selection
setNode(n:Element):Element : Sets the current selection to the specified DOM element.Selection
setRng(r:Range):void : Changes the selection to the specified DOM range.Selection

Public Events

EventDefined By
onBeforeGetContent(selection:Selection, args:Object) : This event gets executed before contents is inserted into selection.Selection
onBeforeSetContent(selection:Selection, args:Object) : This event gets executed before contents is extracted from the selection.Selection
onGetContent(selection:Selection, args:Object) : This event gets executed when contents is extracted from the selection.Selection
onSetContent(selection:Selection, args:Object) : This event gets executed when contents is inserted into selection.Selection

Method details

Selection

public function Selection(dom:DOMUtils, win:Window, serializer:Serializer)
Constructs a new selection instance.

Parameters

ParamDetail
dom:DOMUtilsDOMUtils object reference.
win:WindowWindow to bind the selection object to.
serializer:SerializerDOM serialization class to use for getContent.

collapse

public function collapse(to_start:Boolean):void
Collapse the selection to start or end of range.

Parameters

ParamDetail
to_start:BooleanOptional boolean state if to collapse to end or not. Defaults to start.

getBookmark

public function getBookmark(type:Number, normalized:Boolean):Object
Returns a bookmark location for the current selection. This bookmark object can then be used to restore the selection after some content modification to the document.

Parameters

ParamDetail
type:NumberOptional state if the bookmark should be simple or not. Default is complex.
normalized:BooleanOptional state that enables you to get a position that it would be after normalization.

Returns
Object - Bookmark object, use moveToBookmark with this object to restore the selection.

Example

getContent

public function getContent(s:Object):String
Returns the selected contents using the DOM serializer passed in to this class.

Parameters

ParamDetail
s:ObjectOptional settings class with for example output format text or html.

Returns
String - Selected contents in for example HTML format.

Example

getEnd

public function getEnd():Element
Returns the end element of a selection range. If the end is in a text node the parent element will be returned.

Returns
Element - End element of selection range.

getNode

public function getNode():Element
Returns the currently selected element or the common ancestor element for both start and end of the selection.

Returns
Element - Currently selected element or common ancestor element.

Example

getRng

public function getRng(w3c:Boolean):Range
Returns the browsers internal range object.

Parameters

ParamDetail
w3c:BooleanForces a compatible W3C range on IE.

Returns
Range - Internal browser range object.

See Also

getSel

public function getSel():Selection
Returns the browsers internal selection object.

Returns
Selection - Internal browser selection object.

getStart

public function getStart():Element
Returns the start element of a selection range. If the start is in a text node the parent element will be returned.

Returns
Element - Start element of selection range.

isCollapsed

public function isCollapsed():Boolean
Returns true/false if the selection range is collapsed or not. Collapsed means if it's a caret or a larger selection.

Returns
Boolean - true/false state if the selection range is collapsed or not. Collapsed means if it's a caret or a larger selection.

moveToBookmark

public function moveToBookmark(bookmark:Object):Boolean
Restores the selection to the specified bookmark.

Parameters

ParamDetail
bookmark:ObjectBookmark to restore selection from.

Returns
Boolean - true/false if it was successful or not.

Example

select

public function select(node:Element, content:Boolean):Element
Selects the specified element. This will place the start and end of the selection range around the element.

Parameters

ParamDetail
node:ElementHMTL DOM element to select.
content:BooleanOptional bool state if the contents should be selected or not on non IE browser.

Returns
Element - Selected element the same element as the one that got passed in.

Example

selectorChanged

public function selectorChanged(selector:String, callback:function):void
Executes callback of the current selection matches the specified selector or not and passes the state and args to the callback.

Parameters

ParamDetail
selector:StringCSS selector to check for.
callback:functionCallback with state and args when the selector is matches or not.

setContent

public function setContent(content:String, args:Object):void
Sets the current selection to the specified content. If any contents is selected it will be replaced with the contents passed in to this function. If there is no selection the contents will be inserted where the caret is placed in the editor/page.

Parameters

ParamDetail
content:StringHTML contents to set could also be other formats depending on settings.
args:ObjectOptional settings object with for example data format.

Example

setNode

public function setNode(n:Element):Element
Sets the current selection to the specified DOM element.

Parameters

ParamDetail
n:ElementElement to set as the contents of the selection.

Returns
Element - Returns the element that got passed in.

Example

setRng

public function setRng(r:Range):void
Changes the selection to the specified DOM range.

Parameters

ParamDetail
r:RangeRange to select.

Event details

onBeforeGetContent

public event onBeforeGetContent(selection:Selection, args:Object)
This event gets executed before contents is inserted into selection.

Parameters

ParamDetail
selection:SelectionSelection object that fired the event.
args:ObjectContains things like the contents that will be inserted.

onBeforeSetContent

public event onBeforeSetContent(selection:Selection, args:Object)
This event gets executed before contents is extracted from the selection.

Parameters

ParamDetail
selection:SelectionSelection object that fired the event.
args:ObjectContains things like the contents that will be returned.

onGetContent

public event onGetContent(selection:Selection, args:Object)
This event gets executed when contents is extracted from the selection.

Parameters

ParamDetail
selection:SelectionSelection object that fired the event.
args:ObjectContains things like the contents that will be returned.

onSetContent

public event onSetContent(selection:Selection, args:Object)
This event gets executed when contents is inserted into selection.

Parameters

ParamDetail
selection:SelectionSelection object that fired the event.
args:ObjectContains things like the contents that will be inserted.

Except as otherwise noted, the content of this page is licensed under the Creative Commons BY-NC-SA 3.0 License, and code samples are licensed under the Apache 2.0 License.