DomConverter (engine/view)
@ckeditor/ckeditor5-engine/src/view/domconverter
DomConverter is a set of tools to do transformations between DOM nodes and view nodes. It also handles binding these nodes.
DomConverter does not check which nodes should be rendered (use Renderer
), does not keep a
state of a tree nor keeps synchronization between tree view and DOM tree (use Document
).
DomConverter keeps DOM elements to View element bindings, so when the converter will be destroyed, the binding will be lost. Two converters will keep separate binding maps, so one tree view can be bound with two DOM trees.
Filtering
Properties
-
blockElements : Array.<String>
readonly
Tag names of DOM
Element
s which are considered block elements. -
blockFiller : function
readonly
Block filler creator, which is used to create all block fillers during the view to DOM conversion and to recognize block fillers during the DOM to view conversion.
-
preElements : Array.<String>
readonly
Tag names of DOM
Element
s which are considered pre-formatted elements. -
_domToViewMapping : WeakMap
private
DOM to View mapping.
-
_fakeSelectionMapping : WeakMap
private
Holds mapping between fake selection containers and corresponding view selections.
-
_viewToDomMapping : WeakMap
private
View to DOM mapping.
Methods
-
constructor( options = { [options.blockFiller] } )
Creates DOM converter.
Parameters
options : Object
Object with configuration options.
Properties[ options.blockFiller ] : function
Block filler creator.
Defaults to
module:engine/view/filler~BR_FILLER
-
_checkShouldLeftTrimDomText( prevNode )
Helper function which checks if a DOM text node, preceded by the given
prevNode
should be trimmed from the left side.Parameters
prevNode : Node
-
_checkShouldRightTrimDomText( node, prevNode )
Helper function which checks if a DOM text node, succeeded by the given
nextNode
should be trimmed from the right side.Parameters
node : Node
prevNode : Node
-
_getTouchingViewTextNode( node, getNext ) → Text | null
Helper function. For given view text node, it finds previous or next sibling that is contained in the same container element. If there is no such sibling,
null
is returned. -
bindDocumentFragments( domFragment, viewFragment )
Binds DOM and View document fragments, so it will be possible to get corresponding document fragments using
mapDomToView
andmapViewToDom
.Parameters
domFragment : DocumentFragment
DOM document fragment to bind.
viewFragment : DocumentFragment
View document fragment to bind.
-
bindElements( domElement, viewElement )
Binds DOM and View elements, so it will be possible to get corresponding elements using
mapDomToView
andmapViewToDom
.Parameters
domElement : HTMLElement
DOM element to bind.
viewElement : Element
View element to bind.
-
bindFakeSelection( domElement, viewDocumentSelection )
Binds given DOM element that represents fake selection to document selection. Document selection copy is stored and can be retrieved by
fakeSelectionToView
method.Parameters
domElement : HTMLElement
viewDocumentSelection : DocumentSelection
-
domChildrenToView( domElement, options ) → Iterable.<Node>
-
domPositionToView( domParent, domOffset ) → Position
Converts DOM parent and offset to view
Position
.If the position is inside a filler which has no corresponding view node, position of the filler will be converted and returned.
If the position is inside DOM element rendered by
UIElement
that position will be converted to view position before that UIElement.If structures are too different and it is not possible to find corresponding position then
null
will be returned.Parameters
domParent : Node
DOM position parent.
domOffset : Number
DOM position offset.
Returns
Position
viewPosition View position.
-
domRangeToView( domRange ) → Range | null
Converts DOM Range to view
Range
. If the start or end position can not be convertednull
is returned. -
domSelectionToView( domSelection ) → Selection
Converts DOM selection to view
Selection
. Ranges which cannot be converted will be omitted. -
domToView( domNode, [ options ] = { [options.bind], [options.withChildren], [options.keepOriginalCase] } ) → Node | DocumentFragment | null
Converts DOM to view. For all text nodes, not bound elements and document fragments new items will be created. For bound elements and document fragments function will return corresponding items. For fillers
null
will be returned. For all DOM elements rendered byUIElement
that UIElement will be returned.Parameters
domNode : Node | DocumentFragment
DOM node or document fragment to transform.
[ options ] : Object
Conversion options.
Properties[ options.bind ] : Boolean
Determines whether new elements will be bound.
Defaults to
false
[ options.withChildren ] : Boolean
If
true
, node's and document fragment's children will be converted too.Defaults to
true
[ options.keepOriginalCase ] : Boolean
If
false
, node's tag name will be converter to lower case.Defaults to
false
Returns
Node | DocumentFragment | null
Converted node or document fragment or
null
if DOM node is a filler or the given node is an empty text node.
-
fakeSelectionToView( domElement ) → Selection | undefined
Returns view selection instance corresponding to given DOM element that represents fake selection. Returns
undefined
if binding to given DOM element does not exists. -
findCorrespondingDomText( viewText ) → Text | null
Finds corresponding text node. Text nodes are not bound, corresponding text node is returned based on the sibling or parent.
If the directly previous sibling is a bound element, it is used to find the corresponding text node.
If this is a first child in the parent and the parent is a bound element, it is used to find the corresponding text node.
Otherwise
null
is returned.Parameters
viewText : Text
View text node.
Returns
Text | null
Corresponding DOM text node or
null
, if it was not possible to find a corresponding node.
-
findCorrespondingViewText( domText ) → Text | null
Finds corresponding text node. Text nodes are not bound, corresponding text node is returned based on the sibling or parent.
If the directly previous sibling is a bound element, it is used to find the corresponding text node.
If this is a first child in the parent and the parent is a bound element, it is used to find the corresponding text node.
For all text nodes rendered by
UIElement
that UIElement will be returned.Otherwise
null
is returned.Note that for the block or inline filler this method returns
null
.Parameters
domText : Text
DOM text node.
Returns
Text | null
Corresponding view text node or
null
, if it was not possible to find a corresponding node.
-
focus( viewEditable )
Focuses DOM editable that is corresponding to provided
EditableElement
.Parameters
viewEditable : EditableElement
-
getParentUIElement( domNode ) → UIElement | null
Returns parent
UIElement
for provided DOM node. Returnsnull
if there is no parent UIElement. -
isComment( node ) → Boolean
Returns
true
whennode.nodeType
equalsNode.COMMENT_NODE
.Parameters
node : Node
Node to check.
Returns
Boolean
-
isDocumentFragment( node ) → Boolean
Returns
true
whennode.nodeType
equalsNode.DOCUMENT_FRAGMENT_NODE
.Parameters
node : Node
Node to check.
Returns
Boolean
-
isDomSelectionBackward( DOM ) → Boolean
Returns
true
if given selection is a backward selection, that is, if it'sfocus
is beforeanchor
.Parameters
DOM : Selection
Selection instance to check.
Returns
Boolean
-
isDomSelectionCorrect( domSelection ) → Boolean
Checks if given selection's boundaries are at correct places.
The following places are considered as incorrect for selection boundaries:
- before or in the middle of the inline filler sequence,
- inside the DOM element which represents a view ui element.
Parameters
domSelection : Selection
DOM Selection object to be checked.
Returns
Boolean
true
if the given selection is at a correct place,false
otherwise.
-
isElement( node ) → Boolean
Returns
true
whennode.nodeType
equalsNode.ELEMENT_NODE
.Parameters
node : Node
Node to check.
Returns
Boolean
-
mapDomToView( domElementOrDocumentFragment ) → Element | DocumentFragment | undefined
Returns corresponding view Element or
DocumentFragment
for provided DOM element or document fragment. If there is no view item bound to the given DOM -undefined
is returned. For all DOM elements rendered byUIElement
that UIElement will be returned.Parameters
domElementOrDocumentFragment : DocumentFragment | Element
DOM element or document fragment.
Returns
Element | DocumentFragment | undefined
Corresponding view element, document fragment or
undefined
if no element was bound.
-
mapViewToDom( viewNode ) → Node | DocumentFragment | undefined
Returns corresponding DOM item for provided Element or DocumentFragment. To find a corresponding text for view Text instance use
findCorrespondingDomText
.Parameters
viewNode : Element | DocumentFragment
View element or document fragment.
Returns
Node | DocumentFragment | undefined
Corresponding DOM node or document fragment.
-
unbindDomElement( domElement )
Unbinds given
domElement
from the view element it was bound to. Unbinding is deep, meaning that all children ofdomElement
will be unbound too.Parameters
domElement : HTMLElement
DOM element to unbind.
-
viewChildrenToDom( viewElement, domDocument, options ) → Iterable.<Node>
Converts children of the view element to DOM using the
viewToDom
method. Additionally, this method adds block filler to the list of children, if needed.Parameters
viewElement : Element | DocumentFragment
Parent view element.
domDocument : Document
Document which will be used to create DOM nodes.
options : Object
See
viewToDom
options parameter.
Returns
Iterable.<Node>
DOM nodes.
-
viewPositionToDom( viewPosition ) → Object | null
Converts view
Position
to DOM parent and offset.Inline and block fillers are handled during the conversion. If the converted position is directly before inline filler it is moved inside the filler.
Parameters
viewPosition : Position
View position.
Returns
Object | null
position DOM position or
null
if view position could not be converted to DOM.Node
position.parent DOM position parent.
Number
position.offset DOM position offset.
-
viewRangeToDom( viewRange ) → Range
-
viewToDom( viewNode, domDocument, [ options ] = { [options.bind], [options.withChildren] } ) → Node | DocumentFragment
Converts view to DOM. For all text nodes, not bound elements and document fragments new items will be created. For bound elements and document fragments function will return corresponding items.
Parameters
viewNode : Node | DocumentFragment
View node or document fragment to transform.
domDocument : Document
Document which will be used to create DOM nodes.
[ options ] : Object
Conversion options.
Properties[ options.bind ] : Boolean
Determines whether new elements will be bound.
Defaults to
false
[ options.withChildren ] : Boolean
If
true
, node's and document fragment's children will be converted too.Defaults to
true
Returns
Node | DocumentFragment
Converted node or DocumentFragment.
-
_getTouchingInlineDomNode( node, getNext ) → Text | Element | null
private
Helper function. For the given text node, it finds the closest touching node which is either a text node or a
<br>
. The search is terminated at block element boundaries and if a matching node wasn't found so far,null
is returned.In the following DOM structure:
<p>foo<b>bar</b><br>bom</p>
foo
doesn't have its previous touching inline node (null
is returned),foo
's next touching inline node isbar
bar
's next touching inline node is<br>
This method returns text nodes and
<br>
elements because these types of nodes affect how spaces in the given text node need to be converted.Parameters
node : Text
getNext : Boolean
Returns
Text | Element | null
-
_isDomSelectionPositionCorrect( domParent, offset ) → Boolean
private
Checks if the given DOM position is a correct place for selection boundary. See
isDomSelectionCorrect
.Parameters
domParent : Element
Position parent.
offset : Number
Position offset.
Returns
Boolean
true
if given position is at a correct place for selection boundary,false
otherwise.
-
_nodeEndsWithSpace( node ) → Boolean
private
Checks whether given node ends with a space character after changing appropriate space characters to
s.Parameters
node : Text
Node to check.
Returns
Boolean
true
if givennode
ends with space,false
otherwise.
-
_processDataFromDomText( node ) → String
private
Takes text data from native
Text
node and processes it to a correct view text node data.Following changes are done:
- multiple whitespaces are replaced to a single space,
- space at the beginning of a text node is removed if it is the first text node in its container element or if the previous text node ends with a space character,
- space at the end of the text node is removed, if it is the last text node in its container.
Parameters
node : Node
DOM text node to process.
Returns
String
Processed data.
-
_processDataFromViewText( node ) → String
private
Takes text data from a given
data
and processes it so it is correctly displayed in the DOM.Following changes are done:
- a space at the beginning is changed to
if this is the first text node in its container element or if a previous text node ends with a space character, - space at the end of the text node is changed to
if this is the last text node in its container, - remaining spaces are replaced to a chain of spaces and
(e.g.'x x'
becomes'x x'
).
Content of
preElements
is not processed.Parameters
node : Text
View text node to process.
Returns
String
Processed text data.
- a space at the beginning is changed to