@ckeditor/ckeditor5-engine 35.0.1 → 35.1.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 +175 -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 +899 -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 +654 -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 +191 -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,138 +2,119 @@
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/operation/operation
8
7
  */
9
-
10
8
  /**
11
9
  * Abstract base operation class.
12
10
  *
13
11
  * @abstract
14
12
  */
15
13
  export default class Operation {
16
- /**
17
- * Base operation constructor.
18
- *
19
- * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
20
- * can be applied or `null` if the operation operates on detached (non-document) tree.
21
- */
22
- constructor( baseVersion ) {
23
- /**
24
- * {@link module:engine/model/document~Document#version} on which operation can be applied. If you try to
25
- * {@link module:engine/model/model~Model#applyOperation apply} operation with different base version than the
26
- * {@link module:engine/model/document~Document#version document version} the
27
- * {@link module:utils/ckeditorerror~CKEditorError model-document-applyOperation-wrong-version} error is thrown.
28
- *
29
- * @member {Number}
30
- */
31
- this.baseVersion = baseVersion;
32
-
33
- /**
34
- * Defines whether operation is executed on attached or detached {@link module:engine/model/item~Item items}.
35
- *
36
- * @readonly
37
- * @member {Boolean} #isDocumentOperation
38
- */
39
- this.isDocumentOperation = this.baseVersion !== null;
40
-
41
- /**
42
- * {@link module:engine/model/batch~Batch Batch} to which the operation is added or `null` if the operation is not
43
- * added to any batch yet.
44
- *
45
- * @member {module:engine/model/batch~Batch|null} #batch
46
- */
47
- this.batch = null;
48
-
49
- /**
50
- * Operation type.
51
- *
52
- * @readonly
53
- * @member {String} #type
54
- */
55
-
56
- /**
57
- * Creates and returns an operation that has the same parameters as this operation.
58
- *
59
- * @method #clone
60
- * @returns {module:engine/model/operation/operation~Operation} Clone of this operation.
61
- */
62
-
63
- /**
64
- * Creates and returns a reverse operation. Reverse operation when executed right after
65
- * the original operation will bring back tree model state to the point before the original
66
- * operation execution. In other words, it reverses changes done by the original operation.
67
- *
68
- * Keep in mind that tree model state may change since executing the original operation,
69
- * so reverse operation will be "outdated". In that case you will need to transform it by
70
- * all operations that were executed after the original operation.
71
- *
72
- * @method #getReversed
73
- * @returns {module:engine/model/operation/operation~Operation} Reversed operation.
74
- */
75
-
76
- /**
77
- * Executes the operation - modifications described by the operation properties will be applied to the model tree.
78
- *
79
- * @protected
80
- * @method #_execute
81
- */
82
- }
83
-
84
- /**
85
- * Checks whether the operation's parameters are correct and the operation can be correctly executed. Throws
86
- * an error if operation is not valid.
87
- *
88
- * @protected
89
- * @method #_validate
90
- */
91
- _validate() {
92
- }
93
-
94
- /**
95
- * Custom toJSON method to solve child-parent circular dependencies.
96
- *
97
- * @method #toJSON
98
- * @returns {Object} Clone of this object with the operation property replaced with string.
99
- */
100
- toJSON() {
101
- // This method creates only a shallow copy, all nested objects should be defined separately.
102
- // See https://github.com/ckeditor/ckeditor5-engine/issues/1477.
103
- const json = Object.assign( {}, this );
104
-
105
- json.__className = this.constructor.className;
106
-
107
- // Remove reference to the parent `Batch` to avoid circular dependencies.
108
- delete json.batch;
109
-
110
- // Only document operations are shared with other clients so it is not necessary to keep this information.
111
- delete json.isDocumentOperation;
112
-
113
- return json;
114
- }
115
-
116
- /**
117
- * Name of the operation class used for serialization.
118
- *
119
- * @type {String}
120
- */
121
- static get className() {
122
- return 'Operation';
123
- }
124
-
125
- /**
126
- * Creates Operation object from deserilized object, i.e. from parsed JSON string.
127
- *
128
- * @param {Object} json Deserialized JSON object.
129
- * @param {module:engine/model/document~Document} doc Document on which this operation will be applied.
130
- * @returns {module:engine/model/operation/operation~Operation}
131
- */
132
- static fromJSON( json ) {
133
- return new this( json.baseVersion );
134
- }
135
-
136
- // @if CK_DEBUG_ENGINE // log() {
137
- // @if CK_DEBUG_ENGINE // console.log( this.toString() );
138
- // @if CK_DEBUG_ENGINE // }
14
+ /**
15
+ * Base operation constructor.
16
+ *
17
+ * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
18
+ * can be applied or `null` if the operation operates on detached (non-document) tree.
19
+ */
20
+ constructor(baseVersion) {
21
+ /**
22
+ * {@link module:engine/model/document~Document#version} on which operation can be applied. If you try to
23
+ * {@link module:engine/model/model~Model#applyOperation apply} operation with different base version than the
24
+ * {@link module:engine/model/document~Document#version document version} the
25
+ * {@link module:utils/ckeditorerror~CKEditorError model-document-applyOperation-wrong-version} error is thrown.
26
+ *
27
+ * @member {Number|null}
28
+ */
29
+ this.baseVersion = baseVersion;
30
+ /**
31
+ * Defines whether operation is executed on attached or detached {@link module:engine/model/item~Item items}.
32
+ *
33
+ * @readonly
34
+ * @member {Boolean} #isDocumentOperation
35
+ */
36
+ this.isDocumentOperation = this.baseVersion !== null;
37
+ /**
38
+ * {@link module:engine/model/batch~Batch Batch} to which the operation is added or `null` if the operation is not
39
+ * added to any batch yet.
40
+ *
41
+ * @member {module:engine/model/batch~Batch|null} #batch
42
+ */
43
+ this.batch = null;
44
+ /**
45
+ * Operation type.
46
+ *
47
+ * @readonly
48
+ * @member {String} #type
49
+ */
50
+ /**
51
+ * Creates and returns an operation that has the same parameters as this operation.
52
+ *
53
+ * @method #clone
54
+ * @returns {module:engine/model/operation/operation~Operation} Clone of this operation.
55
+ */
56
+ /**
57
+ * Creates and returns a reverse operation. Reverse operation when executed right after
58
+ * the original operation will bring back tree model state to the point before the original
59
+ * operation execution. In other words, it reverses changes done by the original operation.
60
+ *
61
+ * Keep in mind that tree model state may change since executing the original operation,
62
+ * so reverse operation will be "outdated". In that case you will need to transform it by
63
+ * all operations that were executed after the original operation.
64
+ *
65
+ * @method #getReversed
66
+ * @returns {module:engine/model/operation/operation~Operation} Reversed operation.
67
+ */
68
+ /**
69
+ * Executes the operation - modifications described by the operation properties will be applied to the model tree.
70
+ *
71
+ * @protected
72
+ * @method #_execute
73
+ */
74
+ }
75
+ /**
76
+ * Checks whether the operation's parameters are correct and the operation can be correctly executed. Throws
77
+ * an error if operation is not valid.
78
+ *
79
+ * @internal
80
+ * @protected
81
+ * @method #_validate
82
+ */
83
+ _validate() {
84
+ }
85
+ /**
86
+ * Custom toJSON method to solve child-parent circular dependencies.
87
+ *
88
+ * @method #toJSON
89
+ * @returns {Object} Clone of this object with the operation property replaced with string.
90
+ */
91
+ toJSON() {
92
+ // This method creates only a shallow copy, all nested objects should be defined separately.
93
+ // See https://github.com/ckeditor/ckeditor5-engine/issues/1477.
94
+ const json = Object.assign({}, this);
95
+ json.__className = this.constructor.className;
96
+ // Remove reference to the parent `Batch` to avoid circular dependencies.
97
+ delete json.batch;
98
+ // Only document operations are shared with other clients so it is not necessary to keep this information.
99
+ delete json.isDocumentOperation;
100
+ return json;
101
+ }
102
+ /**
103
+ * Name of the operation class used for serialization.
104
+ *
105
+ * @type {String}
106
+ */
107
+ static get className() {
108
+ return 'Operation';
109
+ }
110
+ /**
111
+ * Creates Operation object from deserilized object, i.e. from parsed JSON string.
112
+ *
113
+ * @param {Object} json Deserialized JSON object.
114
+ * @param {module:engine/model/document~Document} doc Document on which this operation will be applied.
115
+ * @returns {module:engine/model/operation/operation~Operation}
116
+ */
117
+ static fromJSON(json, document) {
118
+ return new this(json.baseVersion);
119
+ }
139
120
  }
@@ -2,48 +2,44 @@
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/operation/operationfactory
8
7
  */
9
-
10
- import AttributeOperation from '../operation/attributeoperation';
11
- import InsertOperation from '../operation/insertoperation';
12
- import MarkerOperation from '../operation/markeroperation';
13
- import MoveOperation from '../operation/moveoperation';
14
- import NoOperation from '../operation/nooperation';
15
- import Operation from '../operation/operation';
16
- import RenameOperation from '../operation/renameoperation';
17
- import RootAttributeOperation from '../operation/rootattributeoperation';
18
- import SplitOperation from '../operation/splitoperation';
19
- import MergeOperation from '../operation/mergeoperation';
20
-
8
+ import AttributeOperation from './attributeoperation';
9
+ import InsertOperation from './insertoperation';
10
+ import MarkerOperation from './markeroperation';
11
+ import MoveOperation from './moveoperation';
12
+ import NoOperation from './nooperation';
13
+ import Operation from './operation';
14
+ import RenameOperation from './renameoperation';
15
+ import RootAttributeOperation from './rootattributeoperation';
16
+ import SplitOperation from './splitoperation';
17
+ import MergeOperation from './mergeoperation';
21
18
  const operations = {};
22
- operations[ AttributeOperation.className ] = AttributeOperation;
23
- operations[ InsertOperation.className ] = InsertOperation;
24
- operations[ MarkerOperation.className ] = MarkerOperation;
25
- operations[ MoveOperation.className ] = MoveOperation;
26
- operations[ NoOperation.className ] = NoOperation;
27
- operations[ Operation.className ] = Operation;
28
- operations[ RenameOperation.className ] = RenameOperation;
29
- operations[ RootAttributeOperation.className ] = RootAttributeOperation;
30
- operations[ SplitOperation.className ] = SplitOperation;
31
- operations[ MergeOperation.className ] = MergeOperation;
32
-
19
+ operations[AttributeOperation.className] = AttributeOperation;
20
+ operations[InsertOperation.className] = InsertOperation;
21
+ operations[MarkerOperation.className] = MarkerOperation;
22
+ operations[MoveOperation.className] = MoveOperation;
23
+ operations[NoOperation.className] = NoOperation;
24
+ operations[Operation.className] = Operation;
25
+ operations[RenameOperation.className] = RenameOperation;
26
+ operations[RootAttributeOperation.className] = RootAttributeOperation;
27
+ operations[SplitOperation.className] = SplitOperation;
28
+ operations[MergeOperation.className] = MergeOperation;
33
29
  /**
34
30
  * A factory class for creating operations.
35
31
  *
36
32
  * @abstract
37
33
  */
38
34
  export default class OperationFactory {
39
- /**
40
- * Creates an operation instance from a JSON object (parsed JSON string).
41
- *
42
- * @param {Object} json Deserialized JSON object.
43
- * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
44
- * @returns {module:engine/model/operation/operation~Operation}
45
- */
46
- static fromJSON( json, document ) {
47
- return operations[ json.__className ].fromJSON( json, document );
48
- }
35
+ /**
36
+ * Creates an operation instance from a JSON object (parsed JSON string).
37
+ *
38
+ * @param {Object} json Deserialized JSON object.
39
+ * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
40
+ * @returns {module:engine/model/operation/operation~Operation}
41
+ */
42
+ static fromJSON(json, document) {
43
+ return operations[json.__className].fromJSON(json, document);
44
+ }
49
45
  }
@@ -2,16 +2,13 @@
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/operation/renameoperation
8
7
  */
9
-
10
8
  import Operation from './operation';
11
9
  import Element from '../element';
12
10
  import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
13
11
  import Position from '../position';
14
-
15
12
  /**
16
13
  * Operation to change element's name.
17
14
  *
@@ -20,136 +17,113 @@ import Position from '../position';
20
17
  * @extends module:engine/model/operation/operation~Operation
21
18
  */
22
19
  export default class RenameOperation extends Operation {
23
- /**
24
- * Creates an operation that changes element's name.
25
- *
26
- * @param {module:engine/model/position~Position} position Position before an element to change.
27
- * @param {String} oldName Current name of the element.
28
- * @param {String} newName New name for the element.
29
- * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
30
- * can be applied or `null` if the operation operates on detached (non-document) tree.
31
- */
32
- constructor( position, oldName, newName, baseVersion ) {
33
- super( baseVersion );
34
-
35
- /**
36
- * Position before an element to change.
37
- *
38
- * @member {module:engine/model/position~Position} module:engine/model/operation/renameoperation~RenameOperation#position
39
- */
40
- this.position = position;
41
- // This position sticks to the next node because it is a position before the node that we want to change.
42
- this.position.stickiness = 'toNext';
43
-
44
- /**
45
- * Current name of the element.
46
- *
47
- * @member {String} module:engine/model/operation/renameoperation~RenameOperation#oldName
48
- */
49
- this.oldName = oldName;
50
-
51
- /**
52
- * New name for the element.
53
- *
54
- * @member {String} module:engine/model/operation/renameoperation~RenameOperation#newName
55
- */
56
- this.newName = newName;
57
- }
58
-
59
- /**
60
- * @inheritDoc
61
- */
62
- get type() {
63
- return 'rename';
64
- }
65
-
66
- /**
67
- * Creates and returns an operation that has the same parameters as this operation.
68
- *
69
- * @returns {module:engine/model/operation/renameoperation~RenameOperation} Clone of this operation.
70
- */
71
- clone() {
72
- return new RenameOperation( this.position.clone(), this.oldName, this.newName, this.baseVersion );
73
- }
74
-
75
- /**
76
- * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
77
- *
78
- * @returns {module:engine/model/operation/renameoperation~RenameOperation}
79
- */
80
- getReversed() {
81
- return new RenameOperation( this.position.clone(), this.newName, this.oldName, this.baseVersion + 1 );
82
- }
83
-
84
- /**
85
- * @inheritDoc
86
- */
87
- _validate() {
88
- const element = this.position.nodeAfter;
89
-
90
- if ( !( element instanceof Element ) ) {
91
- /**
92
- * Given position is invalid or node after it is not instance of Element.
93
- *
94
- * @error rename-operation-wrong-position
95
- */
96
- throw new CKEditorError(
97
- 'rename-operation-wrong-position',
98
- this
99
- );
100
- } else if ( element.name !== this.oldName ) {
101
- /**
102
- * Element to change has different name than operation's old name.
103
- *
104
- * @error rename-operation-wrong-name
105
- */
106
- throw new CKEditorError(
107
- 'rename-operation-wrong-name',
108
- this
109
- );
110
- }
111
- }
112
-
113
- /**
114
- * @inheritDoc
115
- */
116
- _execute() {
117
- const element = this.position.nodeAfter;
118
-
119
- element.name = this.newName;
120
- }
121
-
122
- /**
123
- * @inheritDoc
124
- */
125
- toJSON() {
126
- const json = super.toJSON();
127
-
128
- json.position = this.position.toJSON();
129
-
130
- return json;
131
- }
132
-
133
- /**
134
- * @inheritDoc
135
- */
136
- static get className() {
137
- return 'RenameOperation';
138
- }
139
-
140
- /**
141
- * Creates `RenameOperation` object from deserialized object, i.e. from parsed JSON string.
142
- *
143
- * @param {Object} json Deserialized JSON object.
144
- * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
145
- * @returns {module:engine/model/operation/attributeoperation~AttributeOperation}
146
- */
147
- static fromJSON( json, document ) {
148
- return new RenameOperation( Position.fromJSON( json.position, document ), json.oldName, json.newName, json.baseVersion );
149
- }
150
-
151
- // @if CK_DEBUG_ENGINE // toString() {
152
- // @if CK_DEBUG_ENGINE // return `RenameOperation( ${ this.baseVersion } ): ` +
153
- // @if CK_DEBUG_ENGINE // `${ this.position }: "${ this.oldName }" -> "${ this.newName }"`;
154
- // @if CK_DEBUG_ENGINE // }
20
+ /**
21
+ * Creates an operation that changes element's name.
22
+ *
23
+ * @param {module:engine/model/position~Position} position Position before an element to change.
24
+ * @param {String} oldName Current name of the element.
25
+ * @param {String} newName New name for the element.
26
+ * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
27
+ * can be applied or `null` if the operation operates on detached (non-document) tree.
28
+ */
29
+ constructor(position, oldName, newName, baseVersion) {
30
+ super(baseVersion);
31
+ /**
32
+ * Position before an element to change.
33
+ *
34
+ * @member {module:engine/model/position~Position} module:engine/model/operation/renameoperation~RenameOperation#position
35
+ */
36
+ this.position = position;
37
+ // This position sticks to the next node because it is a position before the node that we want to change.
38
+ this.position.stickiness = 'toNext';
39
+ /**
40
+ * Current name of the element.
41
+ *
42
+ * @member {String} module:engine/model/operation/renameoperation~RenameOperation#oldName
43
+ */
44
+ this.oldName = oldName;
45
+ /**
46
+ * New name for the element.
47
+ *
48
+ * @member {String} module:engine/model/operation/renameoperation~RenameOperation#newName
49
+ */
50
+ this.newName = newName;
51
+ }
52
+ /**
53
+ * @inheritDoc
54
+ */
55
+ get type() {
56
+ return 'rename';
57
+ }
58
+ /**
59
+ * Creates and returns an operation that has the same parameters as this operation.
60
+ *
61
+ * @returns {module:engine/model/operation/renameoperation~RenameOperation} Clone of this operation.
62
+ */
63
+ clone() {
64
+ return new RenameOperation(this.position.clone(), this.oldName, this.newName, this.baseVersion);
65
+ }
66
+ /**
67
+ * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
68
+ *
69
+ * @returns {module:engine/model/operation/renameoperation~RenameOperation}
70
+ */
71
+ getReversed() {
72
+ return new RenameOperation(this.position.clone(), this.newName, this.oldName, this.baseVersion + 1);
73
+ }
74
+ /**
75
+ * @inheritDoc
76
+ * @internal
77
+ */
78
+ _validate() {
79
+ const element = this.position.nodeAfter;
80
+ if (!(element instanceof Element)) {
81
+ /**
82
+ * Given position is invalid or node after it is not instance of Element.
83
+ *
84
+ * @error rename-operation-wrong-position
85
+ */
86
+ throw new CKEditorError('rename-operation-wrong-position', this);
87
+ }
88
+ else if (element.name !== this.oldName) {
89
+ /**
90
+ * Element to change has different name than operation's old name.
91
+ *
92
+ * @error rename-operation-wrong-name
93
+ */
94
+ throw new CKEditorError('rename-operation-wrong-name', this);
95
+ }
96
+ }
97
+ /**
98
+ * @inheritDoc
99
+ * @internal
100
+ */
101
+ _execute() {
102
+ const element = this.position.nodeAfter;
103
+ element.name = this.newName;
104
+ }
105
+ /**
106
+ * @inheritDoc
107
+ */
108
+ toJSON() {
109
+ const json = super.toJSON();
110
+ json.position = this.position.toJSON();
111
+ return json;
112
+ }
113
+ /**
114
+ * @inheritDoc
115
+ */
116
+ static get className() {
117
+ return 'RenameOperation';
118
+ }
119
+ /**
120
+ * Creates `RenameOperation` object from deserialized object, i.e. from parsed JSON string.
121
+ *
122
+ * @param {Object} json Deserialized JSON object.
123
+ * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
124
+ * @returns {module:engine/model/operation/attributeoperation~AttributeOperation}
125
+ */
126
+ static fromJSON(json, document) {
127
+ return new RenameOperation(Position.fromJSON(json.position, document), json.oldName, json.newName, json.baseVersion);
128
+ }
155
129
  }