@ckeditor/ckeditor5-engine 35.0.1 → 35.2.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 (124) hide show
  1. package/CHANGELOG.md +4 -4
  2. package/package.json +30 -24
  3. package/src/controller/datacontroller.js +467 -561
  4. package/src/controller/editingcontroller.js +168 -204
  5. package/src/conversion/conversion.js +541 -565
  6. package/src/conversion/conversionhelpers.js +24 -28
  7. package/src/conversion/downcastdispatcher.js +457 -686
  8. package/src/conversion/downcasthelpers.js +1583 -1965
  9. package/src/conversion/mapper.js +518 -707
  10. package/src/conversion/modelconsumable.js +240 -283
  11. package/src/conversion/upcastdispatcher.js +372 -718
  12. package/src/conversion/upcasthelpers.js +707 -818
  13. package/src/conversion/viewconsumable.js +524 -581
  14. package/src/dataprocessor/basichtmlwriter.js +12 -16
  15. package/src/dataprocessor/dataprocessor.js +5 -0
  16. package/src/dataprocessor/htmldataprocessor.js +100 -116
  17. package/src/dataprocessor/htmlwriter.js +1 -18
  18. package/src/dataprocessor/xmldataprocessor.js +116 -137
  19. package/src/dev-utils/model.js +260 -352
  20. package/src/dev-utils/operationreplayer.js +106 -126
  21. package/src/dev-utils/utils.js +34 -51
  22. package/src/dev-utils/view.js +632 -753
  23. package/src/index.js +0 -11
  24. package/src/model/batch.js +111 -127
  25. package/src/model/differ.js +988 -1233
  26. package/src/model/document.js +340 -449
  27. package/src/model/documentfragment.js +327 -364
  28. package/src/model/documentselection.js +996 -1189
  29. package/src/model/element.js +306 -410
  30. package/src/model/history.js +224 -262
  31. package/src/model/item.js +5 -0
  32. package/src/model/liveposition.js +84 -145
  33. package/src/model/liverange.js +108 -185
  34. package/src/model/markercollection.js +379 -480
  35. package/src/model/model.js +883 -1034
  36. package/src/model/node.js +419 -463
  37. package/src/model/nodelist.js +176 -201
  38. package/src/model/operation/attributeoperation.js +153 -182
  39. package/src/model/operation/detachoperation.js +64 -83
  40. package/src/model/operation/insertoperation.js +135 -166
  41. package/src/model/operation/markeroperation.js +114 -140
  42. package/src/model/operation/mergeoperation.js +163 -191
  43. package/src/model/operation/moveoperation.js +157 -187
  44. package/src/model/operation/nooperation.js +28 -38
  45. package/src/model/operation/operation.js +106 -125
  46. package/src/model/operation/operationfactory.js +30 -34
  47. package/src/model/operation/renameoperation.js +109 -135
  48. package/src/model/operation/rootattributeoperation.js +155 -188
  49. package/src/model/operation/splitoperation.js +196 -232
  50. package/src/model/operation/transform.js +1833 -2204
  51. package/src/model/operation/utils.js +140 -204
  52. package/src/model/position.js +980 -1053
  53. package/src/model/range.js +910 -1028
  54. package/src/model/rootelement.js +77 -97
  55. package/src/model/schema.js +1189 -1835
  56. package/src/model/selection.js +745 -862
  57. package/src/model/text.js +90 -114
  58. package/src/model/textproxy.js +204 -240
  59. package/src/model/treewalker.js +316 -397
  60. package/src/model/typecheckable.js +16 -0
  61. package/src/model/utils/autoparagraphing.js +32 -44
  62. package/src/model/utils/deletecontent.js +334 -418
  63. package/src/model/utils/findoptimalinsertionrange.js +25 -36
  64. package/src/model/utils/getselectedcontent.js +96 -118
  65. package/src/model/utils/insertcontent.js +757 -773
  66. package/src/model/utils/insertobject.js +96 -119
  67. package/src/model/utils/modifyselection.js +120 -158
  68. package/src/model/utils/selection-post-fixer.js +153 -201
  69. package/src/model/writer.js +1305 -1474
  70. package/src/view/attributeelement.js +189 -225
  71. package/src/view/containerelement.js +75 -85
  72. package/src/view/document.js +172 -215
  73. package/src/view/documentfragment.js +200 -249
  74. package/src/view/documentselection.js +338 -367
  75. package/src/view/domconverter.js +1370 -1617
  76. package/src/view/downcastwriter.js +1747 -2076
  77. package/src/view/editableelement.js +81 -97
  78. package/src/view/element.js +739 -890
  79. package/src/view/elementdefinition.js +5 -0
  80. package/src/view/emptyelement.js +82 -92
  81. package/src/view/filler.js +35 -50
  82. package/src/view/item.js +5 -0
  83. package/src/view/matcher.js +260 -559
  84. package/src/view/node.js +274 -360
  85. package/src/view/observer/arrowkeysobserver.js +19 -28
  86. package/src/view/observer/bubblingemittermixin.js +120 -263
  87. package/src/view/observer/bubblingeventinfo.js +47 -55
  88. package/src/view/observer/clickobserver.js +7 -13
  89. package/src/view/observer/compositionobserver.js +14 -24
  90. package/src/view/observer/domeventdata.js +57 -67
  91. package/src/view/observer/domeventobserver.js +40 -64
  92. package/src/view/observer/fakeselectionobserver.js +81 -96
  93. package/src/view/observer/focusobserver.js +45 -61
  94. package/src/view/observer/inputobserver.js +7 -13
  95. package/src/view/observer/keyobserver.js +17 -27
  96. package/src/view/observer/mouseobserver.js +7 -14
  97. package/src/view/observer/mutationobserver.js +220 -315
  98. package/src/view/observer/observer.js +81 -102
  99. package/src/view/observer/selectionobserver.js +199 -246
  100. package/src/view/observer/tabobserver.js +23 -36
  101. package/src/view/placeholder.js +128 -173
  102. package/src/view/position.js +350 -401
  103. package/src/view/range.js +453 -513
  104. package/src/view/rawelement.js +85 -112
  105. package/src/view/renderer.js +874 -1018
  106. package/src/view/rooteditableelement.js +80 -90
  107. package/src/view/selection.js +608 -689
  108. package/src/view/styles/background.js +43 -44
  109. package/src/view/styles/border.js +220 -276
  110. package/src/view/styles/margin.js +8 -17
  111. package/src/view/styles/padding.js +8 -16
  112. package/src/view/styles/utils.js +127 -160
  113. package/src/view/stylesmap.js +728 -905
  114. package/src/view/text.js +102 -126
  115. package/src/view/textproxy.js +144 -170
  116. package/src/view/treewalker.js +383 -479
  117. package/src/view/typecheckable.js +19 -0
  118. package/src/view/uielement.js +166 -187
  119. package/src/view/upcastwriter.js +395 -449
  120. package/src/view/view.js +569 -664
  121. package/src/dataprocessor/dataprocessor.jsdoc +0 -64
  122. package/src/model/item.jsdoc +0 -14
  123. package/src/view/elementdefinition.jsdoc +0 -59
  124. package/src/view/item.jsdoc +0 -14
@@ -2,15 +2,12 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module engine/model/utils/deletecontent
8
7
  */
9
-
8
+ import DocumentSelection from '../documentselection';
10
9
  import LivePosition from '../liveposition';
11
10
  import Range from '../range';
12
- import DocumentSelection from '../documentselection';
13
-
14
11
  /**
15
12
  * Deletes content of the selection and merge siblings. The resulting selection is always collapsed.
16
13
  *
@@ -57,82 +54,66 @@ import DocumentSelection from '../documentselection';
57
54
  *
58
55
  * `[<imageBlock src="foo.jpg"></imageBlock>]` -> `<paragraph>[]</paragraph>`.
59
56
  */
60
- export default function deleteContent( model, selection, options = {} ) {
61
- if ( selection.isCollapsed ) {
62
- return;
63
- }
64
-
65
- const selRange = selection.getFirstRange();
66
-
67
- // If the selection is already removed, don't do anything.
68
- if ( selRange.root.rootName == '$graveyard' ) {
69
- return;
70
- }
71
-
72
- const schema = model.schema;
73
-
74
- model.change( writer => {
75
- // 1. Replace the entire content with paragraph.
76
- // See: https://github.com/ckeditor/ckeditor5-engine/issues/1012#issuecomment-315017594.
77
- if ( !options.doNotResetEntireContent && shouldEntireContentBeReplacedWithParagraph( schema, selection ) ) {
78
- replaceEntireContentWithParagraph( writer, selection, schema );
79
-
80
- return;
81
- }
82
-
83
- // Collect attributes to copy in case of autoparagraphing.
84
- const attributesForAutoparagraph = {};
85
-
86
- if ( !options.doNotAutoparagraph ) {
87
- const selectedElement = selection.getSelectedElement();
88
-
89
- if ( selectedElement ) {
90
- Object.assign( attributesForAutoparagraph, schema.getAttributesWithProperty( selectedElement, 'copyOnReplace', true ) );
91
- }
92
- }
93
-
94
- // Get the live positions for the range adjusted to span only blocks selected from the user perspective.
95
- const [ startPosition, endPosition ] = getLivePositionsForSelectedBlocks( selRange );
96
-
97
- // 2. Remove the content if there is any.
98
- if ( !startPosition.isTouching( endPosition ) ) {
99
- writer.remove( writer.createRange( startPosition, endPosition ) );
100
- }
101
-
102
- // 3. Merge elements in the right branch to the elements in the left branch.
103
- // The only reasonable (in terms of data and selection correctness) case in which we need to do that is:
104
- //
105
- // <heading type=1>Fo[</heading><paragraph>]ar</paragraph> => <heading type=1>Fo^ar</heading>
106
- //
107
- // However, the algorithm supports also merging deeper structures (up to the depth of the shallower branch),
108
- // as it's hard to imagine what should actually be the default behavior. Usually, specific features will
109
- // want to override that behavior anyway.
110
- if ( !options.leaveUnmerged ) {
111
- mergeBranches( writer, startPosition, endPosition );
112
-
113
- // TMP this will be replaced with a postfixer.
114
- // We need to check and strip disallowed attributes in all nested nodes because after merge
115
- // some attributes could end up in a path where are disallowed.
116
- //
117
- // e.g. bold is disallowed for <H1>
118
- // <h1>Fo{o</h1><p>b}a<b>r</b><p> -> <h1>Fo{}a<b>r</b><h1> -> <h1>Fo{}ar<h1>.
119
- schema.removeDisallowedAttributes( startPosition.parent.getChildren(), writer );
120
- }
121
-
122
- collapseSelectionAt( writer, selection, startPosition );
123
-
124
- // 4. Add a paragraph to set selection in it.
125
- // Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
126
- // If autoparagraphing is off, we assume that you know what you do so we leave the selection wherever it was.
127
- if ( !options.doNotAutoparagraph && shouldAutoparagraph( schema, startPosition ) ) {
128
- insertParagraph( writer, startPosition, selection, attributesForAutoparagraph );
129
- }
130
-
131
- startPosition.detach();
132
- endPosition.detach();
133
- } );
57
+ export default function deleteContent(model, selection, options = {}) {
58
+ if (selection.isCollapsed) {
59
+ return;
60
+ }
61
+ const selRange = selection.getFirstRange();
62
+ // If the selection is already removed, don't do anything.
63
+ if (selRange.root.rootName == '$graveyard') {
64
+ return;
65
+ }
66
+ const schema = model.schema;
67
+ model.change(writer => {
68
+ // 1. Replace the entire content with paragraph.
69
+ // See: https://github.com/ckeditor/ckeditor5-engine/issues/1012#issuecomment-315017594.
70
+ if (!options.doNotResetEntireContent && shouldEntireContentBeReplacedWithParagraph(schema, selection)) {
71
+ replaceEntireContentWithParagraph(writer, selection);
72
+ return;
73
+ }
74
+ // Collect attributes to copy in case of autoparagraphing.
75
+ const attributesForAutoparagraph = {};
76
+ if (!options.doNotAutoparagraph) {
77
+ const selectedElement = selection.getSelectedElement();
78
+ if (selectedElement) {
79
+ Object.assign(attributesForAutoparagraph, schema.getAttributesWithProperty(selectedElement, 'copyOnReplace', true));
80
+ }
81
+ }
82
+ // Get the live positions for the range adjusted to span only blocks selected from the user perspective.
83
+ const [startPosition, endPosition] = getLivePositionsForSelectedBlocks(selRange);
84
+ // 2. Remove the content if there is any.
85
+ if (!startPosition.isTouching(endPosition)) {
86
+ writer.remove(writer.createRange(startPosition, endPosition));
87
+ }
88
+ // 3. Merge elements in the right branch to the elements in the left branch.
89
+ // The only reasonable (in terms of data and selection correctness) case in which we need to do that is:
90
+ //
91
+ // <heading type=1>Fo[</heading><paragraph>]ar</paragraph> => <heading type=1>Fo^ar</heading>
92
+ //
93
+ // However, the algorithm supports also merging deeper structures (up to the depth of the shallower branch),
94
+ // as it's hard to imagine what should actually be the default behavior. Usually, specific features will
95
+ // want to override that behavior anyway.
96
+ if (!options.leaveUnmerged) {
97
+ mergeBranches(writer, startPosition, endPosition);
98
+ // TMP this will be replaced with a postfixer.
99
+ // We need to check and strip disallowed attributes in all nested nodes because after merge
100
+ // some attributes could end up in a path where are disallowed.
101
+ //
102
+ // e.g. bold is disallowed for <H1>
103
+ // <h1>Fo{o</h1><p>b}a<b>r</b><p> -> <h1>Fo{}a<b>r</b><h1> -> <h1>Fo{}ar<h1>.
104
+ schema.removeDisallowedAttributes(startPosition.parent.getChildren(), writer);
105
+ }
106
+ collapseSelectionAt(writer, selection, startPosition);
107
+ // 4. Add a paragraph to set selection in it.
108
+ // Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
109
+ // If autoparagraphing is off, we assume that you know what you do so we leave the selection wherever it was.
110
+ if (!options.doNotAutoparagraph && shouldAutoparagraph(schema, startPosition)) {
111
+ insertParagraph(writer, startPosition, selection, attributesForAutoparagraph);
112
+ }
113
+ startPosition.detach();
114
+ endPosition.detach();
115
+ });
134
116
  }
135
-
136
117
  // Returns the live positions for the range adjusted to span only blocks selected from the user perspective. Example:
137
118
  //
138
119
  // <heading1>[foo</heading1>
@@ -140,122 +121,105 @@ export default function deleteContent( model, selection, options = {} ) {
140
121
  // <heading1>]abc</heading1> <-- this block is not considered as selected
141
122
  //
142
123
  // This is the same behavior as in Selection#getSelectedBlocks() "special case".
143
- function getLivePositionsForSelectedBlocks( range ) {
144
- const model = range.root.document.model;
145
-
146
- const startPosition = range.start;
147
- let endPosition = range.end;
148
-
149
- // If the end of selection is at the start position of last block in the selection, then
150
- // shrink it to not include that trailing block. Note that this should happen only for not empty selection.
151
- if ( model.hasContent( range, { ignoreMarkers: true } ) ) {
152
- const endBlock = getParentBlock( endPosition );
153
-
154
- if ( endBlock && endPosition.isTouching( model.createPositionAt( endBlock, 0 ) ) ) {
155
- // Create forward selection as a probe to find a valid position after excluding last block from the range.
156
- const selection = model.createSelection( range );
157
-
158
- // Modify the forward selection in backward direction to shrink it and remove first position of following block from it.
159
- // This is how modifySelection works and here we are making use of it.
160
- model.modifySelection( selection, { direction: 'backward' } );
161
-
162
- const newEndPosition = selection.getLastPosition();
163
-
164
- // For such a model and selection:
165
- // <paragraph>A[</paragraph><imageBlock></imageBlock><paragraph>]B</paragraph>
166
- //
167
- // After modifySelection(), we would end up with this:
168
- // <paragraph>A[</paragraph>]<imageBlock></imageBlock><paragraph>B</paragraph>
169
- //
170
- // So we need to check if there is no content in the skipped range (because we want to include the <imageBlock>).
171
- const skippedRange = model.createRange( newEndPosition, endPosition );
172
-
173
- if ( !model.hasContent( skippedRange, { ignoreMarkers: true } ) ) {
174
- endPosition = newEndPosition;
175
- }
176
- }
177
- }
178
-
179
- return [
180
- LivePosition.fromPosition( startPosition, 'toPrevious' ),
181
- LivePosition.fromPosition( endPosition, 'toNext' )
182
- ];
124
+ function getLivePositionsForSelectedBlocks(range) {
125
+ const model = range.root.document.model;
126
+ const startPosition = range.start;
127
+ let endPosition = range.end;
128
+ // If the end of selection is at the start position of last block in the selection, then
129
+ // shrink it to not include that trailing block. Note that this should happen only for not empty selection.
130
+ if (model.hasContent(range, { ignoreMarkers: true })) {
131
+ const endBlock = getParentBlock(endPosition);
132
+ if (endBlock && endPosition.isTouching(model.createPositionAt(endBlock, 0))) {
133
+ // Create forward selection as a probe to find a valid position after excluding last block from the range.
134
+ const selection = model.createSelection(range);
135
+ // Modify the forward selection in backward direction to shrink it and remove first position of following block from it.
136
+ // This is how modifySelection works and here we are making use of it.
137
+ model.modifySelection(selection, { direction: 'backward' });
138
+ const newEndPosition = selection.getLastPosition();
139
+ // For such a model and selection:
140
+ // <paragraph>A[</paragraph><imageBlock></imageBlock><paragraph>]B</paragraph>
141
+ //
142
+ // After modifySelection(), we would end up with this:
143
+ // <paragraph>A[</paragraph>]<imageBlock></imageBlock><paragraph>B</paragraph>
144
+ //
145
+ // So we need to check if there is no content in the skipped range (because we want to include the <imageBlock>).
146
+ const skippedRange = model.createRange(newEndPosition, endPosition);
147
+ if (!model.hasContent(skippedRange, { ignoreMarkers: true })) {
148
+ endPosition = newEndPosition;
149
+ }
150
+ }
151
+ }
152
+ return [
153
+ LivePosition.fromPosition(startPosition, 'toPrevious'),
154
+ LivePosition.fromPosition(endPosition, 'toNext')
155
+ ];
183
156
  }
184
-
185
157
  // Finds the lowest element in position's ancestors which is a block.
186
158
  // Returns null if a limit element is encountered before reaching a block element.
187
- function getParentBlock( position ) {
188
- const element = position.parent;
189
- const schema = element.root.document.model.schema;
190
- const ancestors = element.getAncestors( { parentFirst: true, includeSelf: true } );
191
-
192
- for ( const element of ancestors ) {
193
- if ( schema.isLimit( element ) ) {
194
- return null;
195
- }
196
-
197
- if ( schema.isBlock( element ) ) {
198
- return element;
199
- }
200
- }
159
+ function getParentBlock(position) {
160
+ const element = position.parent;
161
+ const schema = element.root.document.model.schema;
162
+ const ancestors = element.getAncestors({ parentFirst: true, includeSelf: true });
163
+ for (const element of ancestors) {
164
+ if (schema.isLimit(element)) {
165
+ return null;
166
+ }
167
+ if (schema.isBlock(element)) {
168
+ return element;
169
+ }
170
+ }
201
171
  }
202
-
203
172
  // This function is a result of reaching the Ballmer's peak for just the right amount of time.
204
173
  // Even I had troubles documenting it after a while and after reading it again I couldn't believe that it really works.
205
- function mergeBranches( writer, startPosition, endPosition ) {
206
- const model = writer.model;
207
-
208
- // Verify if there is a need and possibility to merge.
209
- if ( !checkShouldMerge( writer.model.schema, startPosition, endPosition ) ) {
210
- return;
211
- }
212
-
213
- // If the start element on the common ancestor level is empty, and the end element on the same level is not empty
214
- // then merge those to the right element so that it's properties are preserved (name, attributes).
215
- // Because of OT merging is used instead of removing elements.
216
- //
217
- // Merge left:
218
- // <heading1>foo[</heading1> -> <heading1>foo[]bar</heading1>
219
- // <paragraph>]bar</paragraph> -> --^
220
- //
221
- // Merge right:
222
- // <heading1>[</heading1> ->
223
- // <paragraph>]bar</paragraph> -> <paragraph>[]bar</paragraph>
224
- //
225
- // Merge left:
226
- // <blockQuote> -> <blockQuote>
227
- // <heading1>foo[</heading1> -> <heading1>foo[]bar</heading1>
228
- // <paragraph>]bar</paragraph> -> --^
229
- // </blockQuote> -> </blockQuote>
230
- //
231
- // Merge right:
232
- // <blockQuote> -> <blockQuote>
233
- // <heading1>[</heading1> ->
234
- // <paragraph>]bar</paragraph> -> <paragraph>[]bar</paragraph>
235
- // </blockQuote> -> </blockQuote>
236
-
237
- // Merging should not go deeper than common ancestor.
238
- const [ startAncestor, endAncestor ] = getAncestorsJustBelowCommonAncestor( startPosition, endPosition );
239
-
240
- // Branches can't be merged if one of the positions is directly inside a common ancestor.
241
- //
242
- // Example:
243
- // <blockQuote>
244
- // <paragraph>[foo</paragraph>]
245
- // <table> ... </table>
246
- // <blockQuote>
247
- //
248
- if ( !startAncestor || !endAncestor ) {
249
- return;
250
- }
251
-
252
- if ( !model.hasContent( startAncestor, { ignoreMarkers: true } ) && model.hasContent( endAncestor, { ignoreMarkers: true } ) ) {
253
- mergeBranchesRight( writer, startPosition, endPosition, startAncestor.parent );
254
- } else {
255
- mergeBranchesLeft( writer, startPosition, endPosition, startAncestor.parent );
256
- }
174
+ function mergeBranches(writer, startPosition, endPosition) {
175
+ const model = writer.model;
176
+ // Verify if there is a need and possibility to merge.
177
+ if (!checkShouldMerge(writer.model.schema, startPosition, endPosition)) {
178
+ return;
179
+ }
180
+ // If the start element on the common ancestor level is empty, and the end element on the same level is not empty
181
+ // then merge those to the right element so that it's properties are preserved (name, attributes).
182
+ // Because of OT merging is used instead of removing elements.
183
+ //
184
+ // Merge left:
185
+ // <heading1>foo[</heading1> -> <heading1>foo[]bar</heading1>
186
+ // <paragraph>]bar</paragraph> -> --^
187
+ //
188
+ // Merge right:
189
+ // <heading1>[</heading1> ->
190
+ // <paragraph>]bar</paragraph> -> <paragraph>[]bar</paragraph>
191
+ //
192
+ // Merge left:
193
+ // <blockQuote> -> <blockQuote>
194
+ // <heading1>foo[</heading1> -> <heading1>foo[]bar</heading1>
195
+ // <paragraph>]bar</paragraph> -> --^
196
+ // </blockQuote> -> </blockQuote>
197
+ //
198
+ // Merge right:
199
+ // <blockQuote> -> <blockQuote>
200
+ // <heading1>[</heading1> ->
201
+ // <paragraph>]bar</paragraph> -> <paragraph>[]bar</paragraph>
202
+ // </blockQuote> -> </blockQuote>
203
+ // Merging should not go deeper than common ancestor.
204
+ const [startAncestor, endAncestor] = getAncestorsJustBelowCommonAncestor(startPosition, endPosition);
205
+ // Branches can't be merged if one of the positions is directly inside a common ancestor.
206
+ //
207
+ // Example:
208
+ // <blockQuote>
209
+ // <paragraph>[foo</paragraph>]
210
+ // <table> ... </table>
211
+ // <blockQuote>
212
+ //
213
+ if (!startAncestor || !endAncestor) {
214
+ return;
215
+ }
216
+ if (!model.hasContent(startAncestor, { ignoreMarkers: true }) && model.hasContent(endAncestor, { ignoreMarkers: true })) {
217
+ mergeBranchesRight(writer, startPosition, endPosition, startAncestor.parent);
218
+ }
219
+ else {
220
+ mergeBranchesLeft(writer, startPosition, endPosition, startAncestor.parent);
221
+ }
257
222
  }
258
-
259
223
  // Merging blocks to the left (properties of the left block are preserved).
260
224
  // Simple example:
261
225
  // <heading1>foo[</heading1> -> <heading1>foo[bar</heading1>]
@@ -269,72 +233,62 @@ function mergeBranches( writer, startPosition, endPosition ) {
269
233
  // <paragraph>]bar</paragraph> -> ---
270
234
  // </blockBlock> ->
271
235
  //
272
- function mergeBranchesLeft( writer, startPosition, endPosition, commonAncestor ) {
273
- const startElement = startPosition.parent;
274
- const endElement = endPosition.parent;
275
-
276
- // Merging reached the common ancestor element, stop here.
277
- if ( startElement == commonAncestor || endElement == commonAncestor ) {
278
- return;
279
- }
280
-
281
- // Remember next positions to merge in next recursive step (also used as modification points pointers).
282
- startPosition = writer.createPositionAfter( startElement );
283
- endPosition = writer.createPositionBefore( endElement );
284
-
285
- // Move endElement just after startElement if they aren't siblings.
286
- if ( !endPosition.isEqual( startPosition ) ) {
287
- //
288
- // <blockQuote> -> <blockQuote>
289
- // <heading1>foo[</heading1> -> <heading1>foo</heading1>[<paragraph>bar</paragraph>
290
- // </blockQuote> -> </blockQuote> ^
291
- // <blockBlock> -> <blockBlock> |
292
- // <paragraph>]bar</paragraph> -> ] ---
293
- // </blockBlock> -> </blockBlock>
294
- //
295
- writer.insert( endElement, startPosition );
296
- }
297
-
298
- // Merge two siblings (nodes on sides of startPosition):
299
- //
300
- // <blockQuote> -> <blockQuote>
301
- // <heading1>foo</heading1>[<paragraph>bar</paragraph> -> <heading1>foo[bar</heading1>
302
- // </blockQuote> -> </blockQuote>
303
- // <blockBlock> -> <blockBlock>
304
- // ] -> ]
305
- // </blockBlock> -> </blockBlock>
306
- //
307
- // Or in simple case (without moving elements in above if):
308
- // <heading1>foo</heading1>[<paragraph>bar</paragraph>] -> <heading1>foo[bar</heading1>]
309
- //
310
- writer.merge( startPosition );
311
-
312
- // Remove empty end ancestors:
313
- //
314
- // <blockQuote> -> <blockQuote>
315
- // <heading1>foo[bar</heading1> -> <heading1>foo[bar</heading1>
316
- // </blockQuote> -> </blockQuote>
317
- // <blockBlock> ->
318
- // ] -> ]
319
- // </blockBlock> ->
320
- //
321
- while ( endPosition.parent.isEmpty ) {
322
- const parentToRemove = endPosition.parent;
323
-
324
- endPosition = writer.createPositionBefore( parentToRemove );
325
-
326
- writer.remove( parentToRemove );
327
- }
328
-
329
- // Verify if there is a need and possibility to merge next level.
330
- if ( !checkShouldMerge( writer.model.schema, startPosition, endPosition ) ) {
331
- return;
332
- }
333
-
334
- // Continue merging next level (blockQuote with blockBlock in the examples above if it would not be empty and got removed).
335
- mergeBranchesLeft( writer, startPosition, endPosition, commonAncestor );
236
+ function mergeBranchesLeft(writer, startPosition, endPosition, commonAncestor) {
237
+ const startElement = startPosition.parent;
238
+ const endElement = endPosition.parent;
239
+ // Merging reached the common ancestor element, stop here.
240
+ if (startElement == commonAncestor || endElement == commonAncestor) {
241
+ return;
242
+ }
243
+ // Remember next positions to merge in next recursive step (also used as modification points pointers).
244
+ startPosition = writer.createPositionAfter(startElement);
245
+ endPosition = writer.createPositionBefore(endElement);
246
+ // Move endElement just after startElement if they aren't siblings.
247
+ if (!endPosition.isEqual(startPosition)) {
248
+ //
249
+ // <blockQuote> -> <blockQuote>
250
+ // <heading1>foo[</heading1> -> <heading1>foo</heading1>[<paragraph>bar</paragraph>
251
+ // </blockQuote> -> </blockQuote> ^
252
+ // <blockBlock> -> <blockBlock> |
253
+ // <paragraph>]bar</paragraph> -> ] ---
254
+ // </blockBlock> -> </blockBlock>
255
+ //
256
+ writer.insert(endElement, startPosition);
257
+ }
258
+ // Merge two siblings (nodes on sides of startPosition):
259
+ //
260
+ // <blockQuote> -> <blockQuote>
261
+ // <heading1>foo</heading1>[<paragraph>bar</paragraph> -> <heading1>foo[bar</heading1>
262
+ // </blockQuote> -> </blockQuote>
263
+ // <blockBlock> -> <blockBlock>
264
+ // ] -> ]
265
+ // </blockBlock> -> </blockBlock>
266
+ //
267
+ // Or in simple case (without moving elements in above if):
268
+ // <heading1>foo</heading1>[<paragraph>bar</paragraph>] -> <heading1>foo[bar</heading1>]
269
+ //
270
+ writer.merge(startPosition);
271
+ // Remove empty end ancestors:
272
+ //
273
+ // <blockQuote> -> <blockQuote>
274
+ // <heading1>foo[bar</heading1> -> <heading1>foo[bar</heading1>
275
+ // </blockQuote> -> </blockQuote>
276
+ // <blockBlock> ->
277
+ // ] -> ]
278
+ // </blockBlock> ->
279
+ //
280
+ while (endPosition.parent.isEmpty) {
281
+ const parentToRemove = endPosition.parent;
282
+ endPosition = writer.createPositionBefore(parentToRemove);
283
+ writer.remove(parentToRemove);
284
+ }
285
+ // Verify if there is a need and possibility to merge next level.
286
+ if (!checkShouldMerge(writer.model.schema, startPosition, endPosition)) {
287
+ return;
288
+ }
289
+ // Continue merging next level (blockQuote with blockBlock in the examples above if it would not be empty and got removed).
290
+ mergeBranchesLeft(writer, startPosition, endPosition, commonAncestor);
336
291
  }
337
-
338
292
  // Merging blocks to the right (properties of the right block are preserved).
339
293
  // Simple example:
340
294
  // <heading1>foo[</heading1> -> --v
@@ -348,194 +302,156 @@ function mergeBranchesLeft( writer, startPosition, endPosition, commonAncestor )
348
302
  // <paragraph>]bar</paragraph> -> <paragraph>foo]bar</paragraph>
349
303
  // </blockBlock> -> </blockBlock>
350
304
  //
351
- function mergeBranchesRight( writer, startPosition, endPosition, commonAncestor ) {
352
- const startElement = startPosition.parent;
353
- const endElement = endPosition.parent;
354
-
355
- // Merging reached the common ancestor element, stop here.
356
- if ( startElement == commonAncestor || endElement == commonAncestor ) {
357
- return;
358
- }
359
-
360
- // Remember next positions to merge in next recursive step (also used as modification points pointers).
361
- startPosition = writer.createPositionAfter( startElement );
362
- endPosition = writer.createPositionBefore( endElement );
363
-
364
- // Move startElement just before endElement if they aren't siblings.
365
- if ( !endPosition.isEqual( startPosition ) ) {
366
- //
367
- // <blockQuote> -> <blockQuote>
368
- // <heading1>foo[</heading1> -> [ ---
369
- // </blockQuote> -> </blockQuote> |
370
- // <blockBlock> -> <blockBlock> v
371
- // <paragraph>]bar</paragraph> -> <heading1>foo</heading1>]<paragraph>bar</paragraph>
372
- // </blockBlock> -> </blockBlock>
373
- //
374
- writer.insert( startElement, endPosition );
375
- }
376
-
377
- // Remove empty end ancestors:
378
- //
379
- // <blockQuote> ->
380
- // [ -> [
381
- // </blockQuote> ->
382
- // <blockBlock> -> <blockBlock>
383
- // <heading1>foo</heading1>]<paragraph>bar</paragraph> -> <heading1>foo</heading1>]<paragraph>bar</paragraph>
384
- // </blockBlock> -> </blockBlock>
385
- //
386
- while ( startPosition.parent.isEmpty ) {
387
- const parentToRemove = startPosition.parent;
388
-
389
- startPosition = writer.createPositionBefore( parentToRemove );
390
-
391
- writer.remove( parentToRemove );
392
- }
393
-
394
- // Update endPosition after inserting and removing elements.
395
- endPosition = writer.createPositionBefore( endElement );
396
-
397
- // Merge right two siblings (nodes on sides of endPosition):
398
- // ->
399
- // [ -> [
400
- // ->
401
- // <blockBlock> -> <blockBlock>
402
- // <heading1>foo</heading1>]<paragraph>bar</paragraph> -> <paragraph>foo]bar</paragraph>
403
- // </blockBlock> -> </blockBlock>
404
- //
405
- // Or in simple case (without moving elements in above if):
406
- // [<heading1>foo</heading1>]<paragraph>bar</paragraph> -> [<heading1>foo]bar</heading1>
407
- //
408
- mergeRight( writer, endPosition );
409
-
410
- // Verify if there is a need and possibility to merge next level.
411
- if ( !checkShouldMerge( writer.model.schema, startPosition, endPosition ) ) {
412
- return;
413
- }
414
-
415
- // Continue merging next level (blockQuote with blockBlock in the examples above if it would not be empty and got removed).
416
- mergeBranchesRight( writer, startPosition, endPosition, commonAncestor );
305
+ function mergeBranchesRight(writer, startPosition, endPosition, commonAncestor) {
306
+ const startElement = startPosition.parent;
307
+ const endElement = endPosition.parent;
308
+ // Merging reached the common ancestor element, stop here.
309
+ if (startElement == commonAncestor || endElement == commonAncestor) {
310
+ return;
311
+ }
312
+ // Remember next positions to merge in next recursive step (also used as modification points pointers).
313
+ startPosition = writer.createPositionAfter(startElement);
314
+ endPosition = writer.createPositionBefore(endElement);
315
+ // Move startElement just before endElement if they aren't siblings.
316
+ if (!endPosition.isEqual(startPosition)) {
317
+ //
318
+ // <blockQuote> -> <blockQuote>
319
+ // <heading1>foo[</heading1> -> [ ---
320
+ // </blockQuote> -> </blockQuote> |
321
+ // <blockBlock> -> <blockBlock> v
322
+ // <paragraph>]bar</paragraph> -> <heading1>foo</heading1>]<paragraph>bar</paragraph>
323
+ // </blockBlock> -> </blockBlock>
324
+ //
325
+ writer.insert(startElement, endPosition);
326
+ }
327
+ // Remove empty end ancestors:
328
+ //
329
+ // <blockQuote> ->
330
+ // [ -> [
331
+ // </blockQuote> ->
332
+ // <blockBlock> -> <blockBlock>
333
+ // <heading1>foo</heading1>]<paragraph>bar</paragraph> -> <heading1>foo</heading1>]<paragraph>bar</paragraph>
334
+ // </blockBlock> -> </blockBlock>
335
+ //
336
+ while (startPosition.parent.isEmpty) {
337
+ const parentToRemove = startPosition.parent;
338
+ startPosition = writer.createPositionBefore(parentToRemove);
339
+ writer.remove(parentToRemove);
340
+ }
341
+ // Update endPosition after inserting and removing elements.
342
+ endPosition = writer.createPositionBefore(endElement);
343
+ // Merge right two siblings (nodes on sides of endPosition):
344
+ // ->
345
+ // [ -> [
346
+ // ->
347
+ // <blockBlock> -> <blockBlock>
348
+ // <heading1>foo</heading1>]<paragraph>bar</paragraph> -> <paragraph>foo]bar</paragraph>
349
+ // </blockBlock> -> </blockBlock>
350
+ //
351
+ // Or in simple case (without moving elements in above if):
352
+ // [<heading1>foo</heading1>]<paragraph>bar</paragraph> -> [<heading1>foo]bar</heading1>
353
+ //
354
+ mergeRight(writer, endPosition);
355
+ // Verify if there is a need and possibility to merge next level.
356
+ if (!checkShouldMerge(writer.model.schema, startPosition, endPosition)) {
357
+ return;
358
+ }
359
+ // Continue merging next level (blockQuote with blockBlock in the examples above if it would not be empty and got removed).
360
+ mergeBranchesRight(writer, startPosition, endPosition, commonAncestor);
417
361
  }
418
-
419
362
  // There is no right merge operation so we need to simulate it.
420
- function mergeRight( writer, position ) {
421
- const startElement = position.nodeBefore;
422
- const endElement = position.nodeAfter;
423
-
424
- if ( startElement.name != endElement.name ) {
425
- writer.rename( startElement, endElement.name );
426
- }
427
-
428
- writer.clearAttributes( startElement );
429
- writer.setAttributes( Object.fromEntries( endElement.getAttributes() ), startElement );
430
-
431
- writer.merge( position );
363
+ function mergeRight(writer, position) {
364
+ const startElement = position.nodeBefore;
365
+ const endElement = position.nodeAfter;
366
+ if (startElement.name != endElement.name) {
367
+ writer.rename(startElement, endElement.name);
368
+ }
369
+ writer.clearAttributes(startElement);
370
+ writer.setAttributes(Object.fromEntries(endElement.getAttributes()), startElement);
371
+ writer.merge(position);
432
372
  }
433
-
434
373
  // Verifies if merging is needed and possible. It's not needed if both positions are in the same element
435
374
  // and it's not possible if some element is a limit or the range crosses a limit element.
436
- function checkShouldMerge( schema, startPosition, endPosition ) {
437
- const startElement = startPosition.parent;
438
- const endElement = endPosition.parent;
439
-
440
- // If both positions ended up in the same parent, then there's nothing more to merge:
441
- // <$root><p>x[</p><p>]y</p></$root> => <$root><p>xy</p>[]</$root>
442
- if ( startElement == endElement ) {
443
- return false;
444
- }
445
-
446
- // If one of the positions is a limit element, then there's nothing to merge because we don't want to cross the limit boundaries.
447
- if ( schema.isLimit( startElement ) || schema.isLimit( endElement ) ) {
448
- return false;
449
- }
450
-
451
- // Check if operations we'll need to do won't need to cross object or limit boundaries.
452
- // E.g., we can't merge endElement into startElement in this case:
453
- // <limit><startElement>x[</startElement></limit><endElement>]</endElement>
454
- return isCrossingLimitElement( startPosition, endPosition, schema );
375
+ function checkShouldMerge(schema, startPosition, endPosition) {
376
+ const startElement = startPosition.parent;
377
+ const endElement = endPosition.parent;
378
+ // If both positions ended up in the same parent, then there's nothing more to merge:
379
+ // <$root><p>x[</p><p>]y</p></$root> => <$root><p>xy</p>[]</$root>
380
+ if (startElement == endElement) {
381
+ return false;
382
+ }
383
+ // If one of the positions is a limit element, then there's nothing to merge because we don't want to cross the limit boundaries.
384
+ if (schema.isLimit(startElement) || schema.isLimit(endElement)) {
385
+ return false;
386
+ }
387
+ // Check if operations we'll need to do won't need to cross object or limit boundaries.
388
+ // E.g., we can't merge endElement into startElement in this case:
389
+ // <limit><startElement>x[</startElement></limit><endElement>]</endElement>
390
+ return isCrossingLimitElement(startPosition, endPosition, schema);
455
391
  }
456
-
457
392
  // Returns the elements that are the ancestors of the provided positions that are direct children of the common ancestor.
458
- function getAncestorsJustBelowCommonAncestor( positionA, positionB ) {
459
- const ancestorsA = positionA.getAncestors();
460
- const ancestorsB = positionB.getAncestors();
461
-
462
- let i = 0;
463
-
464
- while ( ancestorsA[ i ] && ancestorsA[ i ] == ancestorsB[ i ] ) {
465
- i++;
466
- }
467
-
468
- return [ ancestorsA[ i ], ancestorsB[ i ] ];
393
+ function getAncestorsJustBelowCommonAncestor(positionA, positionB) {
394
+ const ancestorsA = positionA.getAncestors();
395
+ const ancestorsB = positionB.getAncestors();
396
+ let i = 0;
397
+ while (ancestorsA[i] && ancestorsA[i] == ancestorsB[i]) {
398
+ i++;
399
+ }
400
+ return [ancestorsA[i], ancestorsB[i]];
469
401
  }
470
-
471
- function shouldAutoparagraph( schema, position ) {
472
- const isTextAllowed = schema.checkChild( position, '$text' );
473
- const isParagraphAllowed = schema.checkChild( position, 'paragraph' );
474
-
475
- return !isTextAllowed && isParagraphAllowed;
402
+ function shouldAutoparagraph(schema, position) {
403
+ const isTextAllowed = schema.checkChild(position, '$text');
404
+ const isParagraphAllowed = schema.checkChild(position, 'paragraph');
405
+ return !isTextAllowed && isParagraphAllowed;
476
406
  }
477
-
478
407
  // Check if parents of two positions can be merged by checking if there are no limit/object
479
408
  // boundaries between those two positions.
480
409
  //
481
410
  // E.g. in <bQ><p>x[]</p></bQ><widget><caption>{}</caption></widget>
482
411
  // we'll check <p>, <bQ>, <widget> and <caption>.
483
412
  // Usually, widget and caption are marked as objects/limits in the schema, so in this case merging will be blocked.
484
- function isCrossingLimitElement( leftPos, rightPos, schema ) {
485
- const rangeToCheck = new Range( leftPos, rightPos );
486
-
487
- for ( const value of rangeToCheck.getWalker() ) {
488
- if ( schema.isLimit( value.item ) ) {
489
- return false;
490
- }
491
- }
492
-
493
- return true;
413
+ function isCrossingLimitElement(leftPos, rightPos, schema) {
414
+ const rangeToCheck = new Range(leftPos, rightPos);
415
+ for (const value of rangeToCheck.getWalker()) {
416
+ if (schema.isLimit(value.item)) {
417
+ return false;
418
+ }
419
+ }
420
+ return true;
494
421
  }
495
-
496
- function insertParagraph( writer, position, selection, attributes = {} ) {
497
- const paragraph = writer.createElement( 'paragraph' );
498
-
499
- writer.model.schema.setAllowedAttributes( paragraph, attributes, writer );
500
-
501
- writer.insert( paragraph, position );
502
-
503
- collapseSelectionAt( writer, selection, writer.createPositionAt( paragraph, 0 ) );
422
+ function insertParagraph(writer, position, selection, attributes = {}) {
423
+ const paragraph = writer.createElement('paragraph');
424
+ writer.model.schema.setAllowedAttributes(paragraph, attributes, writer);
425
+ writer.insert(paragraph, position);
426
+ collapseSelectionAt(writer, selection, writer.createPositionAt(paragraph, 0));
504
427
  }
505
-
506
- function replaceEntireContentWithParagraph( writer, selection ) {
507
- const limitElement = writer.model.schema.getLimitElement( selection );
508
-
509
- writer.remove( writer.createRangeIn( limitElement ) );
510
- insertParagraph( writer, writer.createPositionAt( limitElement, 0 ), selection );
428
+ function replaceEntireContentWithParagraph(writer, selection) {
429
+ const limitElement = writer.model.schema.getLimitElement(selection);
430
+ writer.remove(writer.createRangeIn(limitElement));
431
+ insertParagraph(writer, writer.createPositionAt(limitElement, 0), selection);
511
432
  }
512
-
513
433
  // We want to replace the entire content with a paragraph when:
514
434
  // * the entire content is selected,
515
435
  // * selection contains at least two elements,
516
436
  // * whether the paragraph is allowed in schema in the common ancestor.
517
- function shouldEntireContentBeReplacedWithParagraph( schema, selection ) {
518
- const limitElement = schema.getLimitElement( selection );
519
-
520
- if ( !selection.containsEntireContent( limitElement ) ) {
521
- return false;
522
- }
523
-
524
- const range = selection.getFirstRange();
525
-
526
- if ( range.start.parent == range.end.parent ) {
527
- return false;
528
- }
529
-
530
- return schema.checkChild( limitElement, 'paragraph' );
437
+ function shouldEntireContentBeReplacedWithParagraph(schema, selection) {
438
+ const limitElement = schema.getLimitElement(selection);
439
+ if (!selection.containsEntireContent(limitElement)) {
440
+ return false;
441
+ }
442
+ const range = selection.getFirstRange();
443
+ if (range.start.parent == range.end.parent) {
444
+ return false;
445
+ }
446
+ return schema.checkChild(limitElement, 'paragraph');
531
447
  }
532
-
533
448
  // Helper function that sets the selection. Depending whether given `selection` is a document selection or not,
534
449
  // uses a different method to set it.
535
- function collapseSelectionAt( writer, selection, positionOrRange ) {
536
- if ( selection instanceof DocumentSelection ) {
537
- writer.setSelection( positionOrRange );
538
- } else {
539
- selection.setTo( positionOrRange );
540
- }
450
+ function collapseSelectionAt(writer, selection, positionOrRange) {
451
+ if (selection instanceof DocumentSelection) {
452
+ writer.setSelection(positionOrRange);
453
+ }
454
+ else {
455
+ selection.setTo(positionOrRange);
456
+ }
541
457
  }