@ckeditor/ckeditor5-engine 30.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/LICENSE.md +17 -0
  2. package/README.md +30 -0
  3. package/package.json +70 -0
  4. package/src/controller/datacontroller.js +563 -0
  5. package/src/controller/editingcontroller.js +149 -0
  6. package/src/conversion/conversion.js +644 -0
  7. package/src/conversion/conversionhelpers.js +40 -0
  8. package/src/conversion/downcastdispatcher.js +914 -0
  9. package/src/conversion/downcasthelpers.js +1706 -0
  10. package/src/conversion/mapper.js +696 -0
  11. package/src/conversion/modelconsumable.js +329 -0
  12. package/src/conversion/upcastdispatcher.js +807 -0
  13. package/src/conversion/upcasthelpers.js +997 -0
  14. package/src/conversion/viewconsumable.js +623 -0
  15. package/src/dataprocessor/basichtmlwriter.js +32 -0
  16. package/src/dataprocessor/dataprocessor.jsdoc +64 -0
  17. package/src/dataprocessor/htmldataprocessor.js +159 -0
  18. package/src/dataprocessor/htmlwriter.js +22 -0
  19. package/src/dataprocessor/xmldataprocessor.js +161 -0
  20. package/src/dev-utils/model.js +482 -0
  21. package/src/dev-utils/operationreplayer.js +140 -0
  22. package/src/dev-utils/utils.js +103 -0
  23. package/src/dev-utils/view.js +1091 -0
  24. package/src/index.js +52 -0
  25. package/src/model/batch.js +82 -0
  26. package/src/model/differ.js +1282 -0
  27. package/src/model/document.js +483 -0
  28. package/src/model/documentfragment.js +390 -0
  29. package/src/model/documentselection.js +1261 -0
  30. package/src/model/element.js +438 -0
  31. package/src/model/history.js +138 -0
  32. package/src/model/item.jsdoc +14 -0
  33. package/src/model/liveposition.js +182 -0
  34. package/src/model/liverange.js +221 -0
  35. package/src/model/markercollection.js +553 -0
  36. package/src/model/model.js +934 -0
  37. package/src/model/node.js +507 -0
  38. package/src/model/nodelist.js +217 -0
  39. package/src/model/operation/attributeoperation.js +202 -0
  40. package/src/model/operation/detachoperation.js +103 -0
  41. package/src/model/operation/insertoperation.js +188 -0
  42. package/src/model/operation/markeroperation.js +154 -0
  43. package/src/model/operation/mergeoperation.js +216 -0
  44. package/src/model/operation/moveoperation.js +209 -0
  45. package/src/model/operation/nooperation.js +58 -0
  46. package/src/model/operation/operation.js +139 -0
  47. package/src/model/operation/operationfactory.js +49 -0
  48. package/src/model/operation/renameoperation.js +155 -0
  49. package/src/model/operation/rootattributeoperation.js +211 -0
  50. package/src/model/operation/splitoperation.js +254 -0
  51. package/src/model/operation/transform.js +2389 -0
  52. package/src/model/operation/utils.js +292 -0
  53. package/src/model/position.js +1164 -0
  54. package/src/model/range.js +1049 -0
  55. package/src/model/rootelement.js +111 -0
  56. package/src/model/schema.js +1851 -0
  57. package/src/model/selection.js +902 -0
  58. package/src/model/text.js +138 -0
  59. package/src/model/textproxy.js +279 -0
  60. package/src/model/treewalker.js +414 -0
  61. package/src/model/utils/autoparagraphing.js +77 -0
  62. package/src/model/utils/deletecontent.js +528 -0
  63. package/src/model/utils/getselectedcontent.js +150 -0
  64. package/src/model/utils/insertcontent.js +824 -0
  65. package/src/model/utils/modifyselection.js +229 -0
  66. package/src/model/utils/selection-post-fixer.js +297 -0
  67. package/src/model/writer.js +1574 -0
  68. package/src/view/attributeelement.js +274 -0
  69. package/src/view/containerelement.js +123 -0
  70. package/src/view/document.js +221 -0
  71. package/src/view/documentfragment.js +273 -0
  72. package/src/view/documentselection.js +387 -0
  73. package/src/view/domconverter.js +1437 -0
  74. package/src/view/downcastwriter.js +2121 -0
  75. package/src/view/editableelement.js +118 -0
  76. package/src/view/element.js +945 -0
  77. package/src/view/elementdefinition.jsdoc +59 -0
  78. package/src/view/emptyelement.js +119 -0
  79. package/src/view/filler.js +161 -0
  80. package/src/view/item.jsdoc +14 -0
  81. package/src/view/matcher.js +776 -0
  82. package/src/view/node.js +391 -0
  83. package/src/view/observer/arrowkeysobserver.js +58 -0
  84. package/src/view/observer/bubblingemittermixin.js +307 -0
  85. package/src/view/observer/bubblingeventinfo.js +71 -0
  86. package/src/view/observer/clickobserver.js +46 -0
  87. package/src/view/observer/compositionobserver.js +79 -0
  88. package/src/view/observer/domeventdata.js +82 -0
  89. package/src/view/observer/domeventobserver.js +99 -0
  90. package/src/view/observer/fakeselectionobserver.js +118 -0
  91. package/src/view/observer/focusobserver.js +106 -0
  92. package/src/view/observer/inputobserver.js +44 -0
  93. package/src/view/observer/keyobserver.js +83 -0
  94. package/src/view/observer/mouseobserver.js +56 -0
  95. package/src/view/observer/mutationobserver.js +345 -0
  96. package/src/view/observer/observer.js +118 -0
  97. package/src/view/observer/selectionobserver.js +242 -0
  98. package/src/view/placeholder.js +285 -0
  99. package/src/view/position.js +426 -0
  100. package/src/view/range.js +533 -0
  101. package/src/view/rawelement.js +148 -0
  102. package/src/view/renderer.js +1037 -0
  103. package/src/view/rooteditableelement.js +107 -0
  104. package/src/view/selection.js +718 -0
  105. package/src/view/styles/background.js +73 -0
  106. package/src/view/styles/border.js +362 -0
  107. package/src/view/styles/margin.js +41 -0
  108. package/src/view/styles/padding.js +40 -0
  109. package/src/view/styles/utils.js +277 -0
  110. package/src/view/stylesmap.js +938 -0
  111. package/src/view/text.js +147 -0
  112. package/src/view/textproxy.js +199 -0
  113. package/src/view/treewalker.js +496 -0
  114. package/src/view/uielement.js +238 -0
  115. package/src/view/upcastwriter.js +484 -0
  116. package/src/view/view.js +721 -0
  117. package/theme/placeholder.css +27 -0
@@ -0,0 +1,285 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/view/placeholder
8
+ */
9
+
10
+ import '../../theme/placeholder.css';
11
+
12
+ // Each document stores information about its placeholder elements and check functions.
13
+ const documentPlaceholders = new WeakMap();
14
+
15
+ /**
16
+ * A helper that enables a placeholder on the provided view element (also updates its visibility).
17
+ * The placeholder is a CSS pseudo–element (with a text content) attached to the element.
18
+ *
19
+ * To change the placeholder text, simply call this method again with new options.
20
+ *
21
+ * To disable the placeholder, use {@link module:engine/view/placeholder~disablePlaceholder `disablePlaceholder()`} helper.
22
+ *
23
+ * @param {Object} [options] Configuration options of the placeholder.
24
+ * @param {module:engine/view/view~View} options.view Editing view instance.
25
+ * @param {module:engine/view/element~Element} options.element Element that will gain a placeholder.
26
+ * See `options.isDirectHost` to learn more.
27
+ * @param {String} options.text Placeholder text.
28
+ * @param {Boolean} [options.isDirectHost=true] If set `false`, the placeholder will not be enabled directly
29
+ * in the passed `element` but in one of its children (selected automatically, i.e. a first empty child element).
30
+ * Useful when attaching placeholders to elements that can host other elements (not just text), for instance,
31
+ * editable root elements.
32
+ * @param {Boolean} [options.keepOnFocus=false] If set `true`, the placeholder stay visible when the host element is focused.
33
+ */
34
+ export function enablePlaceholder( options ) {
35
+ const { view, element, text, isDirectHost = true, keepOnFocus = false } = options;
36
+ const doc = view.document;
37
+
38
+ // Use a single a single post fixer per—document to update all placeholders.
39
+ if ( !documentPlaceholders.has( doc ) ) {
40
+ documentPlaceholders.set( doc, new Map() );
41
+
42
+ // If a post-fixer callback makes a change, it should return `true` so other post–fixers
43
+ // can re–evaluate the document again.
44
+ doc.registerPostFixer( writer => updateDocumentPlaceholders( doc, writer ) );
45
+ }
46
+
47
+ // Store information about the element placeholder under its document.
48
+ documentPlaceholders.get( doc ).set( element, {
49
+ text,
50
+ isDirectHost,
51
+ keepOnFocus,
52
+ hostElement: isDirectHost ? element : null
53
+ } );
54
+
55
+ // Update the placeholders right away.
56
+ view.change( writer => updateDocumentPlaceholders( doc, writer ) );
57
+ }
58
+
59
+ /**
60
+ * Disables the placeholder functionality from a given element.
61
+ *
62
+ * See {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} to learn more.
63
+ *
64
+ * @param {module:engine/view/view~View} view
65
+ * @param {module:engine/view/element~Element} element
66
+ */
67
+ export function disablePlaceholder( view, element ) {
68
+ const doc = element.document;
69
+
70
+ view.change( writer => {
71
+ if ( !documentPlaceholders.has( doc ) ) {
72
+ return;
73
+ }
74
+
75
+ const placeholders = documentPlaceholders.get( doc );
76
+ const config = placeholders.get( element );
77
+
78
+ writer.removeAttribute( 'data-placeholder', config.hostElement );
79
+ hidePlaceholder( writer, config.hostElement );
80
+
81
+ placeholders.delete( element );
82
+ } );
83
+ }
84
+
85
+ /**
86
+ * Shows a placeholder in the provided element by changing related attributes and CSS classes.
87
+ *
88
+ * **Note**: This helper will not update the placeholder visibility nor manage the
89
+ * it in any way in the future. What it does is a one–time state change of an element. Use
90
+ * {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} and
91
+ * {@link module:engine/view/placeholder~disablePlaceholder `disablePlaceholder()`} for full
92
+ * placeholder functionality.
93
+ *
94
+ * **Note**: This helper will blindly show the placeholder directly in the root editable element if
95
+ * one is passed, which could result in a visual clash if the editable element has some children
96
+ * (for instance, an empty paragraph). Use {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`}
97
+ * in that case or make sure the correct element is passed to the helper.
98
+ *
99
+ * @param {module:engine/view/downcastwriter~DowncastWriter} writer
100
+ * @param {module:engine/view/element~Element} element
101
+ * @returns {Boolean} `true`, if any changes were made to the `element`.
102
+ */
103
+ export function showPlaceholder( writer, element ) {
104
+ if ( !element.hasClass( 'ck-placeholder' ) ) {
105
+ writer.addClass( 'ck-placeholder', element );
106
+
107
+ return true;
108
+ }
109
+
110
+ return false;
111
+ }
112
+
113
+ /**
114
+ * Hides a placeholder in the element by changing related attributes and CSS classes.
115
+ *
116
+ * **Note**: This helper will not update the placeholder visibility nor manage the
117
+ * it in any way in the future. What it does is a one–time state change of an element. Use
118
+ * {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} and
119
+ * {@link module:engine/view/placeholder~disablePlaceholder `disablePlaceholder()`} for full
120
+ * placeholder functionality.
121
+ *
122
+ * @param {module:engine/view/downcastwriter~DowncastWriter} writer
123
+ * @param {module:engine/view/element~Element} element
124
+ * @returns {Boolean} `true`, if any changes were made to the `element`.
125
+ */
126
+ export function hidePlaceholder( writer, element ) {
127
+ if ( element.hasClass( 'ck-placeholder' ) ) {
128
+ writer.removeClass( 'ck-placeholder', element );
129
+
130
+ return true;
131
+ }
132
+
133
+ return false;
134
+ }
135
+
136
+ /**
137
+ * Checks if a placeholder should be displayed in the element.
138
+ *
139
+ * **Note**: This helper will blindly check the possibility of showing a placeholder directly in the
140
+ * root editable element if one is passed, which may not be the expected result. If an element can
141
+ * host other elements (not just text), most likely one of its children should be checked instead
142
+ * because it will be the final host for the placeholder. Use
143
+ * {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} in that case or make
144
+ * sure the correct element is passed to the helper.
145
+ *
146
+ * @param {module:engine/view/element~Element} element Element that holds the placeholder.
147
+ * @param {Boolean} keepOnFocus Focusing the element will keep the placeholder visible.
148
+ * @returns {Boolean}
149
+ */
150
+ export function needsPlaceholder( element, keepOnFocus ) {
151
+ if ( !element.isAttached() ) {
152
+ return false;
153
+ }
154
+
155
+ // Anything but uiElement(s) counts as content.
156
+ const hasContent = Array.from( element.getChildren() )
157
+ .some( element => !element.is( 'uiElement' ) );
158
+
159
+ if ( hasContent ) {
160
+ return false;
161
+ }
162
+
163
+ // Skip the focus check and make the placeholder visible already regardless of document focus state.
164
+ if ( keepOnFocus ) {
165
+ return true;
166
+ }
167
+
168
+ const doc = element.document;
169
+
170
+ // If the document is blurred.
171
+ if ( !doc.isFocused ) {
172
+ return true;
173
+ }
174
+
175
+ const viewSelection = doc.selection;
176
+ const selectionAnchor = viewSelection.anchor;
177
+
178
+ // If document is focused and the element is empty but the selection is not anchored inside it.
179
+ return selectionAnchor && selectionAnchor.parent !== element;
180
+ }
181
+
182
+ // Updates all placeholders associated with a document in a post–fixer callback.
183
+ //
184
+ // @private
185
+ // @param { module:engine/view/document~Document} doc
186
+ // @param {module:engine/view/downcastwriter~DowncastWriter} writer
187
+ // @returns {Boolean} True if any changes were made to the view document.
188
+ function updateDocumentPlaceholders( doc, writer ) {
189
+ const placeholders = documentPlaceholders.get( doc );
190
+ const directHostElements = [];
191
+ let wasViewModified = false;
192
+
193
+ // First set placeholders on the direct hosts.
194
+ for ( const [ element, config ] of placeholders ) {
195
+ if ( config.isDirectHost ) {
196
+ directHostElements.push( element );
197
+
198
+ if ( updatePlaceholder( writer, element, config ) ) {
199
+ wasViewModified = true;
200
+ }
201
+ }
202
+ }
203
+
204
+ // Then set placeholders on the indirect hosts but only on those that does not already have an direct host placeholder.
205
+ for ( const [ element, config ] of placeholders ) {
206
+ if ( config.isDirectHost ) {
207
+ continue;
208
+ }
209
+
210
+ const hostElement = getChildPlaceholderHostSubstitute( element );
211
+
212
+ // When not a direct host, it could happen that there is no child element
213
+ // capable of displaying a placeholder.
214
+ if ( !hostElement ) {
215
+ continue;
216
+ }
217
+
218
+ // Don't override placeholder if the host element already has some direct placeholder.
219
+ if ( directHostElements.includes( hostElement ) ) {
220
+ continue;
221
+ }
222
+
223
+ // Update the host element (used for setting and removing the placeholder).
224
+ config.hostElement = hostElement;
225
+
226
+ if ( updatePlaceholder( writer, element, config ) ) {
227
+ wasViewModified = true;
228
+ }
229
+ }
230
+
231
+ return wasViewModified;
232
+ }
233
+
234
+ // Updates a single placeholder in a post–fixer callback.
235
+ //
236
+ // @private
237
+ // @param {module:engine/view/downcastwriter~DowncastWriter} writer
238
+ // @param {module:engine/view/element~Element} element
239
+ // @param {Object} config Configuration of the placeholder
240
+ // @param {String} config.text
241
+ // @param {Boolean} config.isDirectHost
242
+ // @returns {Boolean} True if any changes were made to the view document.
243
+ function updatePlaceholder( writer, element, config ) {
244
+ const { text, isDirectHost, hostElement } = config;
245
+
246
+ let wasViewModified = false;
247
+
248
+ // This may be necessary when updating the placeholder text to something else.
249
+ if ( hostElement.getAttribute( 'data-placeholder' ) !== text ) {
250
+ writer.setAttribute( 'data-placeholder', text, hostElement );
251
+ wasViewModified = true;
252
+ }
253
+
254
+ // If the host element is not a direct host then placeholder is needed only when there is only one element.
255
+ const isOnlyChild = isDirectHost || element.childCount == 1;
256
+
257
+ if ( isOnlyChild && needsPlaceholder( hostElement, config.keepOnFocus ) ) {
258
+ if ( showPlaceholder( writer, hostElement ) ) {
259
+ wasViewModified = true;
260
+ }
261
+ } else if ( hidePlaceholder( writer, hostElement ) ) {
262
+ wasViewModified = true;
263
+ }
264
+
265
+ return wasViewModified;
266
+ }
267
+
268
+ // Gets a child element capable of displaying a placeholder if a parent element can host more
269
+ // than just text (for instance, when it is a root editable element). The child element
270
+ // can then be used in other placeholder helpers as a substitute of its parent.
271
+ //
272
+ // @private
273
+ // @param {module:engine/view/element~Element} parent
274
+ // @returns {module:engine/view/element~Element|null}
275
+ function getChildPlaceholderHostSubstitute( parent ) {
276
+ if ( parent.childCount ) {
277
+ const firstChild = parent.getChild( 0 );
278
+
279
+ if ( firstChild.is( 'element' ) && !firstChild.is( 'uiElement' ) ) {
280
+ return firstChild;
281
+ }
282
+ }
283
+
284
+ return null;
285
+ }
@@ -0,0 +1,426 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/view/position
8
+ */
9
+
10
+ import TreeWalker from './treewalker';
11
+
12
+ import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
13
+ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
14
+ import EditableElement from './editableelement';
15
+
16
+ // To check if component is loaded more than once.
17
+ import '@ckeditor/ckeditor5-utils/src/version';
18
+
19
+ /**
20
+ * Position in the view tree. Position is represented by its parent node and an offset in this parent.
21
+ *
22
+ * In order to create a new position instance use the `createPosition*()` factory methods available in:
23
+ *
24
+ * * {@link module:engine/view/view~View}
25
+ * * {@link module:engine/view/downcastwriter~DowncastWriter}
26
+ * * {@link module:engine/view/upcastwriter~UpcastWriter}
27
+ */
28
+ export default class Position {
29
+ /**
30
+ * Creates a position.
31
+ *
32
+ * @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} parent Position parent.
33
+ * @param {Number} offset Position offset.
34
+ */
35
+ constructor( parent, offset ) {
36
+ /**
37
+ * Position parent.
38
+ *
39
+ * @readonly
40
+ * @member {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
41
+ * module:engine/view/position~Position#parent
42
+ */
43
+ this.parent = parent;
44
+
45
+ /**
46
+ * Position offset.
47
+ *
48
+ * @readonly
49
+ * @member {Number} module:engine/view/position~Position#offset
50
+ */
51
+ this.offset = offset;
52
+ }
53
+
54
+ /**
55
+ * Node directly after the position. Equals `null` when there is no node after position or position is located
56
+ * inside text node.
57
+ *
58
+ * @readonly
59
+ * @type {module:engine/view/node~Node|null}
60
+ */
61
+ get nodeAfter() {
62
+ if ( this.parent.is( '$text' ) ) {
63
+ return null;
64
+ }
65
+
66
+ return this.parent.getChild( this.offset ) || null;
67
+ }
68
+
69
+ /**
70
+ * Node directly before the position. Equals `null` when there is no node before position or position is located
71
+ * inside text node.
72
+ *
73
+ * @readonly
74
+ * @type {module:engine/view/node~Node|null}
75
+ */
76
+ get nodeBefore() {
77
+ if ( this.parent.is( '$text' ) ) {
78
+ return null;
79
+ }
80
+
81
+ return this.parent.getChild( this.offset - 1 ) || null;
82
+ }
83
+
84
+ /**
85
+ * Is `true` if position is at the beginning of its {@link module:engine/view/position~Position#parent parent}, `false` otherwise.
86
+ *
87
+ * @readonly
88
+ * @type {Boolean}
89
+ */
90
+ get isAtStart() {
91
+ return this.offset === 0;
92
+ }
93
+
94
+ /**
95
+ * Is `true` if position is at the end of its {@link module:engine/view/position~Position#parent parent}, `false` otherwise.
96
+ *
97
+ * @readonly
98
+ * @type {Boolean}
99
+ */
100
+ get isAtEnd() {
101
+ const endOffset = this.parent.is( '$text' ) ? this.parent.data.length : this.parent.childCount;
102
+
103
+ return this.offset === endOffset;
104
+ }
105
+
106
+ /**
107
+ * Position's root, that is the root of the position's parent element.
108
+ *
109
+ * @readonly
110
+ * @type {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
111
+ */
112
+ get root() {
113
+ return this.parent.root;
114
+ }
115
+
116
+ /**
117
+ * {@link module:engine/view/editableelement~EditableElement EditableElement} instance that contains this position, or `null` if
118
+ * position is not inside an editable element.
119
+ *
120
+ * @type {module:engine/view/editableelement~EditableElement|null}
121
+ */
122
+ get editableElement() {
123
+ let editable = this.parent;
124
+
125
+ while ( !( editable instanceof EditableElement ) ) {
126
+ if ( editable.parent ) {
127
+ editable = editable.parent;
128
+ } else {
129
+ return null;
130
+ }
131
+ }
132
+
133
+ return editable;
134
+ }
135
+
136
+ /**
137
+ * Returns a new instance of Position with offset incremented by `shift` value.
138
+ *
139
+ * @param {Number} shift How position offset should get changed. Accepts negative values.
140
+ * @returns {module:engine/view/position~Position} Shifted position.
141
+ */
142
+ getShiftedBy( shift ) {
143
+ const shifted = Position._createAt( this );
144
+
145
+ const offset = shifted.offset + shift;
146
+ shifted.offset = offset < 0 ? 0 : offset;
147
+
148
+ return shifted;
149
+ }
150
+
151
+ /**
152
+ * Gets the farthest position which matches the callback using
153
+ * {@link module:engine/view/treewalker~TreeWalker TreeWalker}.
154
+ *
155
+ * For example:
156
+ *
157
+ * getLastMatchingPosition( value => value.type == 'text' ); // <p>{}foo</p> -> <p>foo[]</p>
158
+ * getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } ); // <p>foo[]</p> -> <p>{}foo</p>
159
+ * getLastMatchingPosition( value => false ); // Do not move the position.
160
+ *
161
+ * @param {Function} skip Callback function. Gets {@link module:engine/view/treewalker~TreeWalkerValue} and should
162
+ * return `true` if the value should be skipped or `false` if not.
163
+ * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
164
+ *
165
+ * @returns {module:engine/view/position~Position} The position after the last item which matches the `skip` callback test.
166
+ */
167
+ getLastMatchingPosition( skip, options = {} ) {
168
+ options.startPosition = this;
169
+
170
+ const treeWalker = new TreeWalker( options );
171
+ treeWalker.skip( skip );
172
+
173
+ return treeWalker.position;
174
+ }
175
+
176
+ /**
177
+ * Returns ancestors array of this position, that is this position's parent and it's ancestors.
178
+ *
179
+ * @returns {Array} Array with ancestors.
180
+ */
181
+ getAncestors() {
182
+ if ( this.parent.is( 'documentFragment' ) ) {
183
+ return [ this.parent ];
184
+ } else {
185
+ return this.parent.getAncestors( { includeSelf: true } );
186
+ }
187
+ }
188
+
189
+ /**
190
+ * Returns a {@link module:engine/view/node~Node} or {@link module:engine/view/documentfragment~DocumentFragment}
191
+ * which is a common ancestor of both positions.
192
+ *
193
+ * @param {module:engine/view/position~Position} position
194
+ * @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null}
195
+ */
196
+ getCommonAncestor( position ) {
197
+ const ancestorsA = this.getAncestors();
198
+ const ancestorsB = position.getAncestors();
199
+
200
+ let i = 0;
201
+
202
+ while ( ancestorsA[ i ] == ancestorsB[ i ] && ancestorsA[ i ] ) {
203
+ i++;
204
+ }
205
+
206
+ return i === 0 ? null : ancestorsA[ i - 1 ];
207
+ }
208
+
209
+ /**
210
+ * Checks whether this object is of the given type.
211
+ *
212
+ * position.is( 'position' ); // -> true
213
+ * position.is( 'view:position' ); // -> true
214
+ *
215
+ * position.is( 'model:position' ); // -> false
216
+ * position.is( 'element' ); // -> false
217
+ * position.is( 'range' ); // -> false
218
+ *
219
+ * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
220
+ *
221
+ * @param {String} type
222
+ * @returns {Boolean}
223
+ */
224
+ is( type ) {
225
+ return type === 'position' || type === 'view:position';
226
+ }
227
+
228
+ /**
229
+ * Checks whether this position equals given position.
230
+ *
231
+ * @param {module:engine/view/position~Position} otherPosition Position to compare with.
232
+ * @returns {Boolean} True if positions are same.
233
+ */
234
+ isEqual( otherPosition ) {
235
+ return ( this.parent == otherPosition.parent && this.offset == otherPosition.offset );
236
+ }
237
+
238
+ /**
239
+ * Checks whether this position is located before given position. When method returns `false` it does not mean that
240
+ * this position is after give one. Two positions may be located inside separate roots and in that situation this
241
+ * method will still return `false`.
242
+ *
243
+ * @see module:engine/view/position~Position#isAfter
244
+ * @see module:engine/view/position~Position#compareWith
245
+ * @param {module:engine/view/position~Position} otherPosition Position to compare with.
246
+ * @returns {Boolean} Returns `true` if this position is before given position.
247
+ */
248
+ isBefore( otherPosition ) {
249
+ return this.compareWith( otherPosition ) == 'before';
250
+ }
251
+
252
+ /**
253
+ * Checks whether this position is located after given position. When method returns `false` it does not mean that
254
+ * this position is before give one. Two positions may be located inside separate roots and in that situation this
255
+ * method will still return `false`.
256
+ *
257
+ * @see module:engine/view/position~Position#isBefore
258
+ * @see module:engine/view/position~Position#compareWith
259
+ * @param {module:engine/view/position~Position} otherPosition Position to compare with.
260
+ * @returns {Boolean} Returns `true` if this position is after given position.
261
+ */
262
+ isAfter( otherPosition ) {
263
+ return this.compareWith( otherPosition ) == 'after';
264
+ }
265
+
266
+ /**
267
+ * Checks whether this position is before, after or in same position that other position. Two positions may be also
268
+ * different when they are located in separate roots.
269
+ *
270
+ * @param {module:engine/view/position~Position} otherPosition Position to compare with.
271
+ * @returns {module:engine/view/position~PositionRelation}
272
+ */
273
+ compareWith( otherPosition ) {
274
+ if ( this.root !== otherPosition.root ) {
275
+ return 'different';
276
+ }
277
+
278
+ if ( this.isEqual( otherPosition ) ) {
279
+ return 'same';
280
+ }
281
+
282
+ // Get path from root to position's parent element.
283
+ const thisPath = this.parent.is( 'node' ) ? this.parent.getPath() : [];
284
+ const otherPath = otherPosition.parent.is( 'node' ) ? otherPosition.parent.getPath() : [];
285
+
286
+ // Add the positions' offsets to the parents offsets.
287
+ thisPath.push( this.offset );
288
+ otherPath.push( otherPosition.offset );
289
+
290
+ // Compare both path arrays to find common ancestor.
291
+ const result = compareArrays( thisPath, otherPath );
292
+
293
+ switch ( result ) {
294
+ case 'prefix':
295
+ return 'before';
296
+
297
+ case 'extension':
298
+ return 'after';
299
+
300
+ default:
301
+ return thisPath[ result ] < otherPath[ result ] ? 'before' : 'after';
302
+ }
303
+ }
304
+
305
+ /**
306
+ * Creates a {@link module:engine/view/treewalker~TreeWalker TreeWalker} instance with this positions as a start position.
307
+ *
308
+ * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}
309
+ * @param {module:engine/view/range~Range} [options.boundaries=null] Range to define boundaries of the iterator.
310
+ * @param {Boolean} [options.singleCharacters=false]
311
+ * @param {Boolean} [options.shallow=false]
312
+ * @param {Boolean} [options.ignoreElementEnd=false]
313
+ */
314
+ getWalker( options = {} ) {
315
+ options.startPosition = this;
316
+
317
+ return new TreeWalker( options );
318
+ }
319
+
320
+ clone() {
321
+ return new Position( this.parent, this.offset );
322
+ }
323
+
324
+ /**
325
+ * Creates position at the given location. The location can be specified as:
326
+ *
327
+ * * a {@link module:engine/view/position~Position position},
328
+ * * parent element and offset (offset defaults to `0`),
329
+ * * parent element and `'end'` (sets position at the end of that element),
330
+ * * {@link module:engine/view/item~Item view item} and `'before'` or `'after'` (sets position before or after given view item).
331
+ *
332
+ * This method is a shortcut to other constructors such as:
333
+ *
334
+ * * {@link module:engine/view/position~Position._createBefore},
335
+ * * {@link module:engine/view/position~Position._createAfter}.
336
+ *
337
+ * @protected
338
+ * @param {module:engine/view/item~Item|module:engine/view/position~Position} itemOrPosition
339
+ * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
340
+ * first parameter is a {@link module:engine/view/item~Item view item}.
341
+ */
342
+ static _createAt( itemOrPosition, offset ) {
343
+ if ( itemOrPosition instanceof Position ) {
344
+ return new this( itemOrPosition.parent, itemOrPosition.offset );
345
+ } else {
346
+ const node = itemOrPosition;
347
+
348
+ if ( offset == 'end' ) {
349
+ offset = node.is( '$text' ) ? node.data.length : node.childCount;
350
+ } else if ( offset == 'before' ) {
351
+ return this._createBefore( node );
352
+ } else if ( offset == 'after' ) {
353
+ return this._createAfter( node );
354
+ } else if ( offset !== 0 && !offset ) {
355
+ /**
356
+ * {@link module:engine/view/view~View#createPositionAt `View#createPositionAt()`}
357
+ * requires the offset to be specified when the first parameter is a view item.
358
+ *
359
+ * @error view-createpositionat-offset-required
360
+ */
361
+ throw new CKEditorError( 'view-createpositionat-offset-required', node );
362
+ }
363
+
364
+ return new Position( node, offset );
365
+ }
366
+ }
367
+
368
+ /**
369
+ * Creates a new position after given view item.
370
+ *
371
+ * @protected
372
+ * @param {module:engine/view/item~Item} item View item after which the position should be located.
373
+ * @returns {module:engine/view/position~Position}
374
+ */
375
+ static _createAfter( item ) {
376
+ // TextProxy is not a instance of Node so we need do handle it in specific way.
377
+ if ( item.is( '$textProxy' ) ) {
378
+ return new Position( item.textNode, item.offsetInText + item.data.length );
379
+ }
380
+
381
+ if ( !item.parent ) {
382
+ /**
383
+ * You can not make a position after a root.
384
+ *
385
+ * @error view-position-after-root
386
+ * @param {module:engine/view/node~Node} root
387
+ */
388
+ throw new CKEditorError( 'view-position-after-root', item, { root: item } );
389
+ }
390
+
391
+ return new Position( item.parent, item.index + 1 );
392
+ }
393
+
394
+ /**
395
+ * Creates a new position before given view item.
396
+ *
397
+ * @protected
398
+ * @param {module:engine/view/item~Item} item View item before which the position should be located.
399
+ * @returns {module:engine/view/position~Position}
400
+ */
401
+ static _createBefore( item ) {
402
+ // TextProxy is not a instance of Node so we need do handle it in specific way.
403
+ if ( item.is( '$textProxy' ) ) {
404
+ return new Position( item.textNode, item.offsetInText );
405
+ }
406
+
407
+ if ( !item.parent ) {
408
+ /**
409
+ * You cannot make a position before a root.
410
+ *
411
+ * @error view-position-before-root
412
+ * @param {module:engine/view/node~Node} root
413
+ */
414
+ throw new CKEditorError( 'view-position-before-root', item, { root: item } );
415
+ }
416
+
417
+ return new Position( item.parent, item.index );
418
+ }
419
+ }
420
+
421
+ /**
422
+ * A flag indicating whether this position is `'before'` or `'after'` or `'same'` as given position.
423
+ * If positions are in different roots `'different'` flag is returned.
424
+ *
425
+ * @typedef {String} module:engine/view/position~PositionRelation
426
+ */