Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 56347d1

Browse files
authored
Merge pull request #983 from ckeditor/t/982
Other: Changed the `merge` option of `DataController.deleteContent()` to `leaveUnmerged`. The default value stayed `false`, so the default behavior of the function was changed to merge blocks. Closes #982. BREAKING CHANGE: The `DataController.deleteContent()` option was renamed from `merge` to `leaveUnmerged` and the default behavior of the function was changed to merge blocks.
2 parents d3e7afa + 945d7aa commit 56347d1

File tree

3 files changed

+40
-58
lines changed

3 files changed

+40
-58
lines changed

src/controller/deletecontent.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ import Element from '../model/element';
1818
* @param {module:engine/model/selection~Selection} selection Selection of which the content should be deleted.
1919
* @param {module:engine/model/batch~Batch} batch Batch to which the deltas will be added.
2020
* @param {Object} [options]
21-
* @param {Boolean} [options.merge=false] Merge elements after removing the contents of the selection.
22-
* For example, `<h>x[x</h><p>y]y</p>` will become: `<h>x^y</h>` with the option enabled
23-
* and: `<h>x^</h><p>y</p>` without it.
21+
* @param {Boolean} [options.leaveUnmerged=false] Whether to merge elements after removing the content of the selection.
22+
*
23+
* For example `<h>x[x</h><p>y]y</p>` will become:
24+
* * `<h>x^y</h>` with the option disabled (`leaveUnmerged == false`)
25+
* * `<h>x^</h><p>y</p>` with enabled (`leaveUnmerged == true`).
26+
*
2427
* Note: {@link module:engine/model/schema~Schema#objects object} and {@link module:engine/model/schema~Schema#limits limit}
2528
* elements will not be merged.
2629
*/
@@ -34,7 +37,7 @@ export default function deleteContent( selection, batch, options = {} ) {
3437
const startPos = selRange.start;
3538
const endPos = LivePosition.createFromPosition( selRange.end );
3639

37-
// 1. Remove the contents if there are any.
40+
// 1. Remove the content if there is any.
3841
if ( !selRange.start.isTouching( selRange.end ) ) {
3942
batch.remove( selRange );
4043
}
@@ -47,7 +50,7 @@ export default function deleteContent( selection, batch, options = {} ) {
4750
// However, the algorithm supports also merging deeper structures (up to the depth of the shallower branch),
4851
// as it's hard to imagine what should actually be the default behavior. Usually, specific features will
4952
// want to override that behavior anyway.
50-
if ( options.merge ) {
53+
if ( !options.leaveUnmerged ) {
5154
mergeBranches( batch, startPos, endPos );
5255
}
5356

src/controller/insertcontent.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ export default function insertContent( dataController, content, selection, batch
3535
}
3636

3737
if ( !selection.isCollapsed ) {
38-
dataController.deleteContent( selection, batch, {
39-
merge: true
40-
} );
38+
dataController.deleteContent( selection, batch );
4139
}
4240

4341
const insertion = new Insertion( dataController, batch, selection.anchor );

tests/controller/deletecontent.js

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ describe( 'DataController', () => {
7474
test(
7575
'does not break things when option.merge passed',
7676
'w[x<image></image>y]z',
77-
'w[]z',
78-
{ merge: true }
77+
'w[]z'
7978
);
8079
} );
8180

@@ -173,35 +172,32 @@ describe( 'DataController', () => {
173172
test(
174173
'do not merge when no need to',
175174
'<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
176-
'<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>',
177-
{ merge: true }
175+
'<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>'
178176
);
179177

180178
test(
181179
'merges second element into the first one (same name)',
182180
'<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
183-
'<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
184-
{ merge: true }
181+
'<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>'
185182
);
186183

187184
test(
188185
'does not merge second element into the first one (same name, !option.merge)',
189186
'<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
190-
'<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>'
187+
'<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
188+
{ leaveUnmerged: true }
191189
);
192190

193191
test(
194192
'merges second element into the first one (same name)',
195193
'<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
196-
'<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
197-
{ merge: true }
194+
'<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>'
198195
);
199196

200197
test(
201198
'merges second element into the first one (different name)',
202199
'<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
203-
'<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>',
204-
{ merge: true }
200+
'<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>'
205201
);
206202

207203
// Note: in all these cases we ignore the direction of merge.
@@ -214,37 +210,33 @@ describe( 'DataController', () => {
214210
{ lastRangeBackward: true }
215211
);
216212

217-
deleteContent( doc.selection, doc.batch(), { merge: true } );
213+
deleteContent( doc.selection, doc.batch() );
218214

219215
expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
220216
} );
221217

222218
test(
223219
'merges second element into the first one (different attrs)',
224220
'<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
225-
'<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>',
226-
{ merge: true }
221+
'<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>'
227222
);
228223

229224
test(
230225
'merges second element to an empty first element',
231226
'<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
232-
'<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>',
233-
{ merge: true }
227+
'<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>'
234228
);
235229

236230
test(
237231
'merges empty element into the first element',
238232
'<heading1>f[oo</heading1><paragraph>bar]</paragraph><paragraph>x</paragraph>',
239-
'<heading1>f[]</heading1><paragraph>x</paragraph>',
240-
{ merge: true }
233+
'<heading1>f[]</heading1><paragraph>x</paragraph>'
241234
);
242235

243236
test(
244237
'leaves just one element when all selected',
245238
'<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
246-
'<heading1>[]</heading1>',
247-
{ merge: true }
239+
'<heading1>[]</heading1>'
248240
);
249241

250242
it( 'uses remove delta instead of merge delta if merged element is empty', () => {
@@ -254,7 +246,7 @@ describe( 'DataController', () => {
254246
const spyMerge = sinon.spy( batch, 'merge' );
255247
const spyRemove = sinon.spy( batch, 'remove' );
256248

257-
deleteContent( doc.selection, batch, { merge: true } );
249+
deleteContent( doc.selection, batch );
258250

259251
expect( getData( doc ) ).to.equal( '<paragraph>ab[]</paragraph>' );
260252

@@ -269,7 +261,7 @@ describe( 'DataController', () => {
269261
const spyMerge = sinon.spy( batch, 'merge' );
270262
const spyMove = sinon.spy( batch, 'move' );
271263

272-
deleteContent( doc.selection, batch, { merge: true } );
264+
deleteContent( doc.selection, batch );
273265

274266
expect( getData( doc ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
275267

@@ -284,8 +276,7 @@ describe( 'DataController', () => {
284276
test(
285277
'merges elements when deep nested',
286278
'<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
287-
'<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>',
288-
{ merge: true }
279+
'<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>'
289280
);
290281

291282
it( 'merges elements when deep nested (3rd level)', () => {
@@ -322,7 +313,7 @@ describe( 'DataController', () => {
322313

323314
doc.selection.setRanges( [ range ] );
324315

325-
deleteContent( doc.selection, doc.batch(), { merge: true } );
316+
deleteContent( doc.selection, doc.batch() );
326317

327318
expect( getData( doc ) )
328319
.to.equal( '<pparent>x<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>y</pparent>' );
@@ -331,15 +322,13 @@ describe( 'DataController', () => {
331322
test(
332323
'merges elements when left end deep nested',
333324
'<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph><paragraph>x</paragraph>',
334-
'<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>',
335-
{ merge: true }
325+
'<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>'
336326
);
337327

338328
test(
339329
'merges elements when right end deep nested',
340330
'<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph><pchild>b]ar</pchild>x</paragraph>',
341-
'<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>',
342-
{ merge: true }
331+
'<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>'
343332
);
344333

345334
it( 'merges elements when left end deep nested (3rd level)', () => {
@@ -369,7 +358,7 @@ describe( 'DataController', () => {
369358

370359
doc.selection.setRanges( [ range ] );
371360

372-
deleteContent( doc.selection, doc.batch(), { merge: true } );
361+
deleteContent( doc.selection, doc.batch() );
373362

374363
expect( getData( doc ) )
375364
.to.equal( '<pparent>x<paragraph>foo<pchild>ba[]om</pchild></paragraph></pparent>' );
@@ -378,15 +367,13 @@ describe( 'DataController', () => {
378367
test(
379368
'merges elements when right end deep nested (in an empty container)',
380369
'<paragraph>fo[o</paragraph><paragraph><pchild>bar]</pchild></paragraph>',
381-
'<paragraph>fo[]</paragraph>',
382-
{ merge: true }
370+
'<paragraph>fo[]</paragraph>'
383371
);
384372

385373
test(
386374
'merges elements when left end deep nested (in an empty container)',
387375
'<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
388-
'<paragraph><pchild>[]ar</pchild></paragraph><paragraph>x</paragraph>',
389-
{ merge: true }
376+
'<paragraph><pchild>[]ar</pchild></paragraph><paragraph>x</paragraph>'
390377
);
391378

392379
it( 'merges elements when left end deep nested (3rd level)', () => {
@@ -414,7 +401,7 @@ describe( 'DataController', () => {
414401

415402
doc.selection.setRanges( [ range ] );
416403

417-
deleteContent( doc.selection, doc.batch(), { merge: true } );
404+
deleteContent( doc.selection, doc.batch() );
418405

419406
expect( getData( doc ) )
420407
.to.equal( '<paragraph>fo[]</paragraph>' );
@@ -440,15 +427,13 @@ describe( 'DataController', () => {
440427
test(
441428
'does not merge an object element (if it is first)',
442429
'<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
443-
'<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>',
444-
{ merge: true }
430+
'<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>'
445431
);
446432

447433
test(
448434
'does not merge an object element (if it is second)',
449435
'<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
450-
'<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>',
451-
{ merge: true }
436+
'<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>'
452437
);
453438
} );
454439
} );
@@ -611,22 +596,19 @@ describe( 'DataController', () => {
611596
test(
612597
'merge option should be ignored if both elements are limits',
613598
'<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
614-
'<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>',
615-
{ merge: true }
599+
'<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
616600
);
617601

618602
test(
619603
'merge option should be ignored if the first element is a limit',
620604
'<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
621-
'<inlineLimit>foo []</inlineLimit><x> qux</x>',
622-
{ merge: true }
605+
'<inlineLimit>foo []</inlineLimit><x> qux</x>'
623606
);
624607

625608
test(
626609
'merge option should be ignored if the second element is a limit',
627610
'<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
628-
'<x>baz []</x><inlineLimit> bar</inlineLimit>',
629-
{ merge: true }
611+
'<x>baz []</x><inlineLimit> bar</inlineLimit>'
630612
);
631613
} );
632614

@@ -648,14 +630,14 @@ describe( 'DataController', () => {
648630
test(
649631
'should delete inside block limit element',
650632
'<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
651-
'<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>'
633+
'<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
634+
{ leaveUnmerged: true }
652635
);
653636

654637
test(
655638
'should delete inside block limit element (with merge)',
656639
'<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
657-
'<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>',
658-
{ merge: true }
640+
'<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
659641
);
660642

661643
test(
@@ -673,8 +655,7 @@ describe( 'DataController', () => {
673655
test(
674656
'merge option should be ignored if any of the elements is a limit',
675657
'<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
676-
'<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>',
677-
{ merge: true }
658+
'<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
678659
);
679660
} );
680661

0 commit comments

Comments
 (0)