@ckeditor/ckeditor5-engine 41.0.0 → 41.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-engine",
3
- "version": "41.0.0",
3
+ "version": "41.2.0",
4
4
  "description": "The editing engine of CKEditor 5 – the best browser-based rich text editor.",
5
5
  "keywords": [
6
6
  "wysiwyg",
@@ -24,7 +24,7 @@
24
24
  "type": "module",
25
25
  "main": "src/index.js",
26
26
  "dependencies": {
27
- "@ckeditor/ckeditor5-utils": "41.0.0",
27
+ "@ckeditor/ckeditor5-utils": "41.2.0",
28
28
  "lodash-es": "4.17.21"
29
29
  },
30
30
  "author": "CKSource (http://cksource.com/)",
@@ -194,6 +194,7 @@ export default class DataController extends DataController_base {
194
194
  */
195
195
  set(data: string | Record<string, string>, options?: {
196
196
  batchType?: BatchType;
197
+ [key: string]: unknown;
197
198
  }): void;
198
199
  /**
199
200
  * Returns the data parsed by the {@link #processor data processor} and then converted by upcast converters
@@ -14,6 +14,7 @@ import ModelElement from '../model/element.js';
14
14
  import ModelPosition from '../model/position.js';
15
15
  import ViewAttributeElement from '../view/attributeelement.js';
16
16
  import ConversionHelpers from './conversionhelpers.js';
17
+ import StylesMap from '../view/stylesmap.js';
17
18
  import { CKEditorError, toArray } from '@ckeditor/ckeditor5-utils';
18
19
  import { cloneDeep } from 'lodash-es';
19
20
  /**
@@ -1353,15 +1354,24 @@ function changeAttribute(attributeCreator) {
1353
1354
  // First remove the old attribute if there was one.
1354
1355
  if (data.attributeOldValue !== null && oldAttribute) {
1355
1356
  if (oldAttribute.key == 'class') {
1356
- const classes = toArray(oldAttribute.value);
1357
+ const classes = typeof oldAttribute.value == 'string' ? oldAttribute.value.split(/\s+/) : oldAttribute.value;
1357
1358
  for (const className of classes) {
1358
1359
  viewWriter.removeClass(className, viewElement);
1359
1360
  }
1360
1361
  }
1361
1362
  else if (oldAttribute.key == 'style') {
1362
- const keys = Object.keys(oldAttribute.value);
1363
- for (const key of keys) {
1364
- viewWriter.removeStyle(key, viewElement);
1363
+ if (typeof oldAttribute.value == 'string') {
1364
+ const styles = new StylesMap(viewWriter.document.stylesProcessor);
1365
+ styles.setTo(oldAttribute.value);
1366
+ for (const [key] of styles.getStylesEntries()) {
1367
+ viewWriter.removeStyle(key, viewElement);
1368
+ }
1369
+ }
1370
+ else {
1371
+ const keys = Object.keys(oldAttribute.value);
1372
+ for (const key of keys) {
1373
+ viewWriter.removeStyle(key, viewElement);
1374
+ }
1365
1375
  }
1366
1376
  }
1367
1377
  else {
@@ -1371,15 +1381,24 @@ function changeAttribute(attributeCreator) {
1371
1381
  // Then set the new attribute.
1372
1382
  if (data.attributeNewValue !== null && newAttribute) {
1373
1383
  if (newAttribute.key == 'class') {
1374
- const classes = toArray(newAttribute.value);
1384
+ const classes = typeof newAttribute.value == 'string' ? newAttribute.value.split(/\s+/) : newAttribute.value;
1375
1385
  for (const className of classes) {
1376
1386
  viewWriter.addClass(className, viewElement);
1377
1387
  }
1378
1388
  }
1379
1389
  else if (newAttribute.key == 'style') {
1380
- const keys = Object.keys(newAttribute.value);
1381
- for (const key of keys) {
1382
- viewWriter.setStyle(key, newAttribute.value[key], viewElement);
1390
+ if (typeof newAttribute.value == 'string') {
1391
+ const styles = new StylesMap(viewWriter.document.stylesProcessor);
1392
+ styles.setTo(newAttribute.value);
1393
+ for (const [key, value] of styles.getStylesEntries()) {
1394
+ viewWriter.setStyle(key, value, viewElement);
1395
+ }
1396
+ }
1397
+ else {
1398
+ const keys = Object.keys(newAttribute.value);
1399
+ for (const key of keys) {
1400
+ viewWriter.setStyle(key, newAttribute.value[key], viewElement);
1401
+ }
1383
1402
  }
1384
1403
  }
1385
1404
  else {
@@ -776,15 +776,15 @@ function normalizeViewAttributeKeyValueConfig(config) {
776
776
  config.view = { key: config.view };
777
777
  }
778
778
  const key = config.view.key;
779
+ const value = typeof config.view.value == 'undefined' ? /[\s\S]*/ : config.view.value;
779
780
  let normalized;
780
781
  if (key == 'class' || key == 'style') {
781
782
  const keyName = key == 'class' ? 'classes' : 'styles';
782
783
  normalized = {
783
- [keyName]: config.view.value
784
+ [keyName]: value
784
785
  };
785
786
  }
786
787
  else {
787
- const value = typeof config.view.value == 'undefined' ? /[\s\S]*/ : config.view.value;
788
788
  normalized = {
789
789
  attributes: {
790
790
  [key]: value
@@ -5,8 +5,7 @@
5
5
  /**
6
6
  * @module engine/conversion/viewconsumable
7
7
  */
8
- import { CKEditorError } from '@ckeditor/ckeditor5-utils';
9
- import { isArray } from 'lodash-es';
8
+ import { CKEditorError, toArray } from '@ckeditor/ckeditor5-utils';
10
9
  /**
11
10
  * Class used for handling consumption of view {@link module:engine/view/element~Element elements},
12
11
  * {@link module:engine/view/text~Text text nodes} and {@link module:engine/view/documentfragment~DocumentFragment document fragments}.
@@ -420,7 +419,7 @@ export class ViewElementConsumables {
420
419
  * @param item Consumable item or array of items.
421
420
  */
422
421
  _add(type, item) {
423
- const items = isArray(item) ? item : [item];
422
+ const items = toArray(item);
424
423
  const consumables = this._consumables[type];
425
424
  for (const name of items) {
426
425
  if (type === 'attributes' && (name === 'class' || name === 'style')) {
@@ -461,7 +460,7 @@ export class ViewElementConsumables {
461
460
  * consumed and `false` when one of the items is already consumed.
462
461
  */
463
462
  _test(type, item) {
464
- const items = isArray(item) ? item : [item];
463
+ const items = toArray(item);
465
464
  const consumables = this._consumables[type];
466
465
  for (const name of items) {
467
466
  if (type === 'attributes' && (name === 'class' || name === 'style')) {
@@ -492,7 +491,7 @@ export class ViewElementConsumables {
492
491
  * @param item Consumable item or array of items.
493
492
  */
494
493
  _consume(type, item) {
495
- const items = isArray(item) ? item : [item];
494
+ const items = toArray(item);
496
495
  const consumables = this._consumables[type];
497
496
  for (const name of items) {
498
497
  if (type === 'attributes' && (name === 'class' || name === 'style')) {
@@ -517,7 +516,7 @@ export class ViewElementConsumables {
517
516
  * @param item Consumable item or array of items.
518
517
  */
519
518
  _revert(type, item) {
520
- const items = isArray(item) ? item : [item];
519
+ const items = toArray(item);
521
520
  const consumables = this._consumables[type];
522
521
  for (const name of items) {
523
522
  if (type === 'attributes' && (name === 'class' || name === 'style')) {
package/src/index.d.ts CHANGED
@@ -105,7 +105,7 @@ export type { ViewDocumentTabEvent } from './view/observer/tabobserver.js';
105
105
  export type { ViewDocumentClickEvent } from './view/observer/clickobserver.js';
106
106
  export type { ViewDocumentSelectionChangeEvent } from './view/observer/selectionobserver.js';
107
107
  export type { ViewRenderEvent, ViewScrollToTheSelectionEvent } from './view/view.js';
108
- export { StylesProcessor, type BoxSides } from './view/stylesmap.js';
108
+ export { default as StylesMap, StylesProcessor, type BoxSides } from './view/stylesmap.js';
109
109
  export * from './view/styles/background.js';
110
110
  export * from './view/styles/border.js';
111
111
  export * from './view/styles/margin.js';
package/src/index.js CHANGED
@@ -69,7 +69,7 @@ export { default as Matcher } from './view/matcher.js';
69
69
  export { default as BubblingEventInfo } from './view/observer/bubblingeventinfo.js';
70
70
  export { default as DomEventData } from './view/observer/domeventdata.js';
71
71
  // View / Styles.
72
- export { StylesProcessor } from './view/stylesmap.js';
72
+ export { default as StylesMap, StylesProcessor } from './view/stylesmap.js';
73
73
  export * from './view/styles/background.js';
74
74
  export * from './view/styles/border.js';
75
75
  export * from './view/styles/margin.js';
@@ -460,6 +460,14 @@ class ContextFactory {
460
460
  this._setRelation(opA, opB, 'splitAtSource');
461
461
  }
462
462
  }
463
+ else if (opB instanceof MoveOperation && opB.howMany > 0) {
464
+ if (opA.sourcePosition.isEqual(opB.sourcePosition.getShiftedBy(opB.howMany))) {
465
+ this._setRelation(opA, opB, 'mergeSourceAffected');
466
+ }
467
+ if (opA.targetPosition.isEqual(opB.sourcePosition)) {
468
+ this._setRelation(opA, opB, 'mergeTargetWasBefore');
469
+ }
470
+ }
463
471
  }
464
472
  else if (opA instanceof MarkerOperation) {
465
473
  const markerRange = opA.newRange;
@@ -1103,16 +1111,68 @@ setTransformation(MergeOperation, MoveOperation, (a, b, context) => {
1103
1111
  return [new NoOperation(0)];
1104
1112
  }
1105
1113
  }
1106
- // The default case.
1114
+ // In most cases we want `sourcePosition` to stick to previous and `targetPosition` to stick to next.
1115
+ // Usually, `sourcePosition` is at the beginning of the merged element and `targetPosition` is at the end of the merge-target element.
1107
1116
  //
1108
- if (a.sourcePosition.hasSameParentAs(b.targetPosition)) {
1109
- a.howMany += b.howMany;
1117
+ // However, `sourcePosition` and `targetPosition` may end up in the middle of an element due to some OT magic that happens during undo.
1118
+ // It is expected and used in `MergeOperation` x `SplitOperation` transformation.
1119
+ //
1120
+ // But when these positions are in the middle, it messes up the regular `MergeOperation` x `MoveOperation` transformation because
1121
+ // these positions may "follow" some moved elements. And we want them stick in the original elements.
1122
+ //
1123
+ // This is why we add two extra cases: (1) and (2).
1124
+ //
1125
+ // But after this `MergeOperation` is transformed by "this" move (which is undone), we also need to define extra cases for
1126
+ // the operation undoing previous move. These are (3) and (4).
1127
+ //
1128
+ // (1). Note that this case is also added to `updateRelations()` and sets `mergeSourceAffected` relation.
1129
+ //
1130
+ // [] is move operation, } is merge source position (sticks to previous by default):
1131
+ // <p>A[b]}c</p> -> <p>A}c</p>
1132
+ //
1133
+ if (b.sourcePosition.getShiftedBy(b.howMany).isEqual(a.sourcePosition)) {
1134
+ a.sourcePosition.stickiness = 'toNone';
1135
+ }
1136
+ // (3). This is the transformation for undoing operation of the above case.
1137
+ //
1138
+ // [] is move operation, } is merge source position (sticks to previous by default):
1139
+ // <p>A}c</p> -> <p>A[b]}c</p> (instead of <p>A}[b]c</p>)
1140
+ //
1141
+ else if (b.targetPosition.isEqual(a.sourcePosition) && context.abRelation == 'mergeSourceAffected') {
1142
+ a.sourcePosition.stickiness = 'toNext';
1110
1143
  }
1111
- if (a.sourcePosition.hasSameParentAs(b.sourcePosition)) {
1144
+ // (2). Note that this case is also added to `updateRelations()` and sets `mergeTargetWasBefore` relation.
1145
+ //
1146
+ // [] is move operation, { is merge target position (sticks to next by default):
1147
+ // <p>A{[b]c</p> -> <p>A{c</p>
1148
+ //
1149
+ else if (b.sourcePosition.isEqual(a.targetPosition)) {
1150
+ a.targetPosition.stickiness = 'toNone';
1112
1151
  a.howMany -= b.howMany;
1113
1152
  }
1153
+ // (4). This is the transformation for undoing operation of the above case.
1154
+ //
1155
+ // [] is move operation, { is merge target position (sticks to next by default):
1156
+ // <p>A{c</p> -> <p>A{[b]c</p> (instead of <p>A[b]{c</p>)
1157
+ //
1158
+ else if (b.targetPosition.isEqual(a.targetPosition) && context.abRelation == 'mergeTargetWasBefore') {
1159
+ a.targetPosition.stickiness = 'toPrevious';
1160
+ a.howMany += b.howMany;
1161
+ }
1162
+ // The default case.
1163
+ else {
1164
+ if (a.sourcePosition.hasSameParentAs(b.targetPosition)) {
1165
+ a.howMany += b.howMany;
1166
+ }
1167
+ if (a.sourcePosition.hasSameParentAs(b.sourcePosition)) {
1168
+ a.howMany -= b.howMany;
1169
+ }
1170
+ }
1114
1171
  a.sourcePosition = a.sourcePosition._getTransformedByMoveOperation(b);
1115
1172
  a.targetPosition = a.targetPosition._getTransformedByMoveOperation(b);
1173
+ // After transformations are done, make sure to revert stickiness in case if (1) - (4) scenario happened.
1174
+ a.sourcePosition.stickiness = 'toPrevious';
1175
+ a.targetPosition.stickiness = 'toNext';
1116
1176
  // `MergeOperation` graveyard position is like `MoveOperation` target position. It is a position where element(s) will
1117
1177
  // be moved. Like in other similar cases, we need to consider the scenario when those positions are same.
1118
1178
  // Here, we will treat `MergeOperation` like it is always strong (see `InsertOperation` x `InsertOperation` for comparison).
@@ -149,7 +149,7 @@ export default function insertContent(model, content, selectable) {
149
149
  for (const [name, [start, end]] of Object.entries(markersData)) {
150
150
  // For now, we ignore markers if they are included in the filtered-out content.
151
151
  // In the future implementation we will improve that case to create markers that are not filtered out completely.
152
- if (start && end && start.root === end.root) {
152
+ if (start && end && start.root === end.root && start.root.document) {
153
153
  writer.addMarker(name, {
154
154
  usingOperation: true,
155
155
  affectsData: true,
@@ -519,7 +519,7 @@ export default class Writer {
519
519
  *
520
520
  * The `options.affectsData` parameter, which defaults to `false`, allows you to define if a marker affects the data. It should be
521
521
  * `true` when the marker change changes the data returned by the
522
- * {@link module:core/editor/utils/dataapimixin~DataApi#getData `editor.getData()`} method.
522
+ * {@link module:core/editor/editor~Editor#getData `editor.getData()`} method.
523
523
  * When set to `true` it fires the {@link module:engine/model/document~Document#event:change:data `change:data`} event.
524
524
  * When set to `false` it fires the {@link module:engine/model/document~Document#event:change `change`} event.
525
525
  *
@@ -573,7 +573,7 @@ export default class Writer {
573
573
  *
574
574
  * The `options.affectsData` parameter, which defaults to `false`, allows you to define if a marker affects the data. It should be
575
575
  * `true` when the marker change changes the data returned by
576
- * the {@link module:core/editor/utils/dataapimixin~DataApi#getData `editor.getData()`} method.
576
+ * the {@link module:core/editor/editor~Editor#getData `editor.getData()`} method.
577
577
  * When set to `true` it fires the {@link module:engine/model/document~Document#event:change:data `change:data`} event.
578
578
  * When set to `false` it fires the {@link module:engine/model/document~Document#event:change `change`} event.
579
579
  *
@@ -705,7 +705,7 @@ export default class Writer {
705
705
  *
706
706
  * The `options.affectsData` parameter, which defaults to `false`, allows you to define if a marker affects the data. It should be
707
707
  * `true` when the marker change changes the data returned by the
708
- * {@link module:core/editor/utils/dataapimixin~DataApi#getData `editor.getData()`} method.
708
+ * {@link module:core/editor/editor~Editor#getData `editor.getData()`} method.
709
709
  * When set to `true` it fires the {@link module:engine/model/document~Document#event:change:data `change:data`} event.
710
710
  * When set to `false` it fires the {@link module:engine/model/document~Document#event:change `change`} event.
711
711
  *
@@ -789,7 +789,7 @@ export default class Writer {
789
789
  *
790
790
  * The `options.affectsData` parameter, which defaults to `false`, allows you to define if a marker affects the data. It should be
791
791
  * `true` when the marker change changes the data returned by
792
- * the {@link module:core/editor/utils/dataapimixin~DataApi#getData `editor.getData()`} method.
792
+ * the {@link module:core/editor/editor~Editor#getData `editor.getData()`} method.
793
793
  * When set to `true` it fires the {@link module:engine/model/document~Document#event:change:data `change:data`} event.
794
794
  * When set to `false` it fires the {@link module:engine/model/document~Document#event:change `change`} event.
795
795
  *
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import type { StylesProcessor } from '../stylesmap.js';
9
9
  /**
10
- * Adds a margin CSS styles processing rules.
10
+ * Adds a padding CSS styles processing rules.
11
11
  *
12
12
  * ```ts
13
13
  * editor.data.addStyleProcessorRules( addPaddingRules );
@@ -4,7 +4,7 @@
4
4
  */
5
5
  import { getPositionShorthandNormalizer, getBoxSidesValueReducer } from './utils.js';
6
6
  /**
7
- * Adds a margin CSS styles processing rules.
7
+ * Adds a padding CSS styles processing rules.
8
8
  *
9
9
  * ```ts
10
10
  * editor.data.addStyleProcessorRules( addPaddingRules );
@@ -4,8 +4,6 @@
4
4
  */
5
5
  /**
6
6
  * Styles map. Allows handling (adding, removing, retrieving) a set of style rules (usually, of an element).
7
- *
8
- * The styles map is capable of normalizing style names so e.g. the following operations are possible:
9
7
  */
10
8
  export default class StylesMap {
11
9
  /**
@@ -312,7 +310,7 @@ export default class StylesMap {
312
310
  /**
313
311
  * Returns normalized styles entries for further processing.
314
312
  */
315
- private _getStylesEntries;
313
+ getStylesEntries(): Array<PropertyDescriptor>;
316
314
  /**
317
315
  * Removes empty objects upon removing an entry from internal object.
318
316
  */
@@ -8,8 +8,6 @@
8
8
  import { get, isObject, merge, set, unset } from 'lodash-es';
9
9
  /**
10
10
  * Styles map. Allows handling (adding, removing, retrieving) a set of style rules (usually, of an element).
11
- *
12
- * The styles map is capable of normalizing style names so e.g. the following operations are possible:
13
11
  */
14
12
  export default class StylesMap {
15
13
  /**
@@ -190,7 +188,7 @@ export default class StylesMap {
190
188
  if (this.isEmpty) {
191
189
  return '';
192
190
  }
193
- return this._getStylesEntries()
191
+ return this.getStylesEntries()
194
192
  .map(arr => arr.join(':'))
195
193
  .sort()
196
194
  .join(';') + ';';
@@ -290,7 +288,7 @@ export default class StylesMap {
290
288
  if (expand) {
291
289
  return this._styleProcessor.getStyleNames(this._styles);
292
290
  }
293
- const entries = this._getStylesEntries();
291
+ const entries = this.getStylesEntries();
294
292
  return entries.map(([key]) => key);
295
293
  }
296
294
  /**
@@ -302,7 +300,7 @@ export default class StylesMap {
302
300
  /**
303
301
  * Returns normalized styles entries for further processing.
304
302
  */
305
- _getStylesEntries() {
303
+ getStylesEntries() {
306
304
  const parsed = [];
307
305
  const keys = Object.keys(this._styles);
308
306
  for (const key of keys) {