| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- import Model from '../../../src/model/model';
- import Position from '../../../src/model/position';
- import Range from '../../../src/model/range';
- import Selection from '../../../src/model/selection';
- import Element from '../../../src/model/element';
- import deleteContent from '../../../src/model/utils/deletecontent';
- import { setData, getData } from '../../../src/dev-utils/model';
- import { stringify } from '../../../src/dev-utils/view';
- describe( 'DataController utils', () => {
- let model, doc;
- describe( 'deleteContent', () => {
- it( 'should use parent batch', () => {
- model = new Model();
- doc = model.document;
- doc.createRoot();
- model.schema.extend( '$text', { allowIn: '$root' } );
- setData( model, 'x[abc]x' );
- model.change( writer => {
- deleteContent( model, doc.selection );
- expect( writer.batch.operations ).to.length( 1 );
- } );
- } );
- it( 'should not do anything if the selection is already in graveyard', () => {
- model = new Model();
- doc = model.document;
- const gy = model.document.graveyard;
- gy._appendChild( new Element( 'paragraph' ) );
- const baseVersion = model.document.baseVersion;
- model.change( writer => {
- sinon.spy( writer, 'remove' );
- const selection = writer.createSelection( writer.createRangeIn( gy ) );
- deleteContent( model, selection );
- expect( writer.remove.called ).to.be.false;
- } );
- expect( model.document.baseVersion ).to.equal( baseVersion );
- } );
- describe( 'in simple scenarios', () => {
- beforeEach( () => {
- model = new Model();
- doc = model.document;
- doc.createRoot();
- const schema = model.schema;
- schema.register( 'image', { allowWhere: '$text' } );
- schema.extend( '$text', { allowIn: '$root' } );
- } );
- it( 'should be able to delete content at custom selection', () => {
- setData( model, 'a[]bcd' );
- const range = new Range(
- new Position( doc.getRoot(), [ 2 ] ),
- new Position( doc.getRoot(), [ 3 ] )
- );
- const selection = new Selection( [ range ] );
- model.change( () => {
- deleteContent( model, selection );
- expect( getData( model ) ).to.equal( 'a[]bd' );
- } );
- } );
- test(
- 'does nothing on collapsed selection',
- 'f[]oo',
- 'f[]oo'
- );
- test(
- 'deletes single character',
- 'f[o]o',
- 'f[]o'
- );
- it( 'deletes single character (backward selection)', () => {
- setData( model, 'f[o]o', { lastRangeBackward: true } );
- deleteContent( model, doc.selection );
- expect( getData( model ) ).to.equal( 'f[]o' );
- } );
- test(
- 'deletes whole text',
- '[foo]',
- '[]'
- );
- test(
- 'deletes whole text between nodes',
- '<image></image>[foo]<image></image>',
- '<image></image>[]<image></image>'
- );
- test(
- 'deletes an element',
- 'x[<image></image>]y',
- 'x[]y'
- );
- test(
- 'deletes a bunch of nodes',
- 'w[x<image></image>y]z',
- 'w[]z'
- );
- test(
- 'does not break things when option.merge passed',
- 'w[x<image></image>y]z',
- 'w[]z'
- );
- } );
- describe( 'with text attributes', () => {
- beforeEach( () => {
- model = new Model();
- doc = model.document;
- doc.createRoot();
- const schema = model.schema;
- schema.register( 'image', { allowWhere: '$text' } );
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- schema.extend( '$text', {
- allowIn: '$root',
- allowAttributes: [ 'bold', 'italic' ]
- } );
- } );
- it( 'deletes characters (first half has attrs)', () => {
- setData( model, '<$text bold="true">fo[o</$text>b]ar' );
- deleteContent( model, doc.selection );
- expect( getData( model ) ).to.equal( '<$text bold="true">fo[]</$text>ar' );
- expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
- } );
- it( 'deletes characters (2nd half has attrs)', () => {
- setData( model, 'fo[o<$text bold="true">b]ar</$text>' );
- deleteContent( model, doc.selection );
- expect( getData( model ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
- expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
- } );
- it( 'clears selection attrs when emptied content', () => {
- setData( model, '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>' );
- deleteContent( model, doc.selection );
- expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
- expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
- } );
- it( 'leaves selection attributes when text contains them', () => {
- setData(
- model,
- '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>',
- {
- selectionAttributes: {
- bold: true
- }
- }
- );
- deleteContent( model, doc.selection );
- expect( getData( model ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
- expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
- } );
- } );
- // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
- // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
- // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
- // should not happen unless we're talking about lists or tables, but these features will need to cover
- // their scenarios themselves. In all generic scenarios elements are never nested.
- //
- // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
- // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
- // Those case should, again, be handled by their specific implementations.
- describe( 'in multi-element scenarios', () => {
- beforeEach( () => {
- model = new Model();
- doc = model.document;
- doc.createRoot();
- const schema = model.schema;
- schema.register( 'paragraph', {
- inheritAllFrom: '$block',
- allowIn: 'pparent',
- allowAttributes: 'align'
- } );
- schema.register( 'heading1', { inheritAllFrom: '$block' } );
- schema.register( 'image', { inheritAllFrom: '$text' } );
- schema.register( 'pchild', { allowIn: 'paragraph' } );
- schema.register( 'pparent', { allowIn: '$root' } );
- schema.extend( '$text', { allowIn: [ 'pchild', 'pparent' ] } );
- } );
- test(
- 'do not merge when no need to',
- '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
- '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>'
- );
- test(
- 'merges second element into the first one (same name)',
- '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
- '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>'
- );
- test(
- 'does not merge second element into the first one (same name, !option.merge)',
- '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
- '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
- { leaveUnmerged: true }
- );
- test(
- 'merges second element into the first one (different name)',
- '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
- '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>'
- );
- // Note: in all these cases we ignore the direction of merge.
- // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
- // forward and backward delete.
- it( 'merges second element into the first one (different name, backward selection)', () => {
- setData(
- model,
- '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
- { lastRangeBackward: true }
- );
- deleteContent( model, doc.selection );
- expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
- } );
- test(
- 'merges second element into the first one (different attrs)',
- '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
- '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>'
- );
- test(
- 'merges second element to an empty first element',
- '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
- '<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>'
- );
- test(
- 'merges empty element into the first element',
- '<heading1>f[oo</heading1><paragraph>bar]</paragraph><paragraph>x</paragraph>',
- '<heading1>f[]</heading1><paragraph>x</paragraph>'
- );
- test(
- 'leaves just one element when all selected',
- '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
- '<heading1>[]bar</heading1>'
- );
- it( 'uses merge operation even if merged element is empty', () => {
- let mergeSpy;
- setData( model, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
- model.change( writer => {
- mergeSpy = sinon.spy( writer, 'merge' );
- deleteContent( model, doc.selection );
- } );
- expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
- expect( mergeSpy.called ).to.be.true;
- } );
- it( 'uses merge operation even if merged element is empty #2', () => {
- let mergeSpy;
- setData( model, '<paragraph>ab[</paragraph><paragraph>]</paragraph>' );
- model.change( writer => {
- mergeSpy = sinon.spy( writer, 'merge' );
- deleteContent( model, doc.selection );
- } );
- expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
- expect( mergeSpy.called ).to.be.true;
- } );
- it( 'does not try to move the second block if not needed', () => {
- let mergeSpy, moveSpy;
- setData( model, '<paragraph>ab[cd</paragraph><paragraph>ef]gh</paragraph>' );
- model.change( writer => {
- mergeSpy = sinon.spy( writer, 'merge' );
- moveSpy = sinon.spy( writer, 'move' );
- deleteContent( model, doc.selection );
- } );
- expect( getData( model ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
- expect( moveSpy.called ).to.be.false;
- expect( mergeSpy.called ).to.be.true;
- } );
- // Note: in all these cases we ignore the direction of merge.
- // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
- // forward and backward delete.
- describe( 'with nested elements', () => {
- test(
- 'merges elements when deep nested',
- '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
- '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>'
- );
- it( 'merges elements when deep nested (3rd level)', () => {
- const root = doc.getRoot();
- // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
- // <pparent>x<paragraph>x<pchild>fo[o</pchild></paragraph></pparent>
- // <pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>
- root._appendChild(
- new Element( 'pparent', null, [
- 'x',
- new Element( 'paragraph', null, [
- 'x',
- new Element( 'pchild', null, 'foo' )
- ] )
- ] )
- );
- root._appendChild(
- new Element( 'pparent', null, [
- new Element( 'paragraph', null, [
- new Element( 'pchild', null, 'bar' ),
- 'y'
- ] ),
- 'y'
- ] )
- );
- const range = new Range(
- new Position( doc.getRoot(), [ 0, 1, 1, 2 ] ), // fo[o
- new Position( doc.getRoot(), [ 1, 0, 0, 1 ] ) // b]ar
- );
- model.change( writer => {
- writer.setSelection( range );
- } );
- deleteContent( model, doc.selection );
- expect( getData( model ) )
- .to.equal( '<pparent>x<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>y</pparent>' );
- } );
- test(
- 'merges elements when left end deep nested',
- '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph><paragraph>x</paragraph>',
- '<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>'
- );
- test(
- 'merges elements when right end deep nested',
- '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph><pchild>b]ar</pchild>x</paragraph>',
- '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>'
- );
- it( 'merges elements when left end deep nested (3rd level)', () => {
- const root = doc.getRoot();
- // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
- // <pparent>x<paragraph>foo<pchild>ba[r</pchild></paragraph></pparent><paragraph>b]om</paragraph>
- root._appendChild(
- new Element( 'pparent', null, [
- 'x',
- new Element( 'paragraph', null, [
- 'foo',
- new Element( 'pchild', null, 'bar' )
- ] )
- ] )
- );
- root._appendChild(
- new Element( 'paragraph', null, 'bom' )
- );
- const range = new Range(
- new Position( doc.getRoot(), [ 0, 1, 3, 2 ] ), // ba[r
- new Position( doc.getRoot(), [ 1, 1 ] ) // b]om
- );
- model.change( writer => {
- writer.setSelection( range );
- } );
- deleteContent( model, doc.selection );
- expect( getData( model ) )
- .to.equal( '<pparent>x<paragraph>foo<pchild>ba[]om</pchild></paragraph></pparent>' );
- } );
- test(
- 'merges elements when right end deep nested (in an empty container)',
- '<paragraph>fo[o</paragraph><paragraph><pchild>bar]</pchild></paragraph>',
- '<paragraph>fo[]</paragraph>'
- );
- test(
- 'merges elements when left end deep nested (in an empty container)',
- '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
- '<paragraph><pchild>[]ar</pchild></paragraph><paragraph>x</paragraph>'
- );
- it( 'merges elements when right end deep nested (3rd level)', () => {
- const root = doc.getRoot();
- // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
- // <paragraph>fo[o</paragraph><pparent><paragraph><pchild>bar]</pchild></paragraph></pparent>
- root._appendChild(
- new Element( 'paragraph', null, 'foo' )
- );
- root._appendChild(
- new Element( 'pparent', null, [
- new Element( 'paragraph', null, [
- new Element( 'pchild', null, 'bar' )
- ] )
- ] )
- );
- const range = new Range(
- new Position( doc.getRoot(), [ 0, 2 ] ), // f[oo
- new Position( doc.getRoot(), [ 1, 0, 0, 3 ] ) // bar]
- );
- model.change( writer => {
- writer.setSelection( range );
- } );
- deleteContent( model, doc.selection );
- expect( getData( model ) )
- .to.equal( '<paragraph>fo[]</paragraph>' );
- } );
- } );
- describe( 'with object elements', () => {
- beforeEach( () => {
- const schema = model.schema;
- schema.register( 'blockWidget', {
- isObject: true
- } );
- schema.register( 'nestedEditable', {
- isLimit: true
- } );
- schema.extend( 'blockWidget', { allowIn: '$root' } );
- schema.extend( 'nestedEditable', { allowIn: 'blockWidget' } );
- schema.extend( '$text', { allowIn: 'nestedEditable' } );
- } );
- test(
- 'does not merge an object element (if it is first)',
- '<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
- '<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>'
- );
- test(
- 'does not merge an object element (if it is second)',
- '<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
- '<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>'
- );
- } );
- describe( 'filtering out', () => {
- beforeEach( () => {
- const schema = model.schema;
- schema.addAttributeCheck( ( ctx, attributeName ) => {
- // Disallow 'c' on pchild>pchild>$text.
- if ( ctx.endsWith( 'pchild pchild $text' ) && attributeName == 'c' ) {
- return false;
- }
- // Allow 'a' and 'b' on paragraph>$text.
- if ( ctx.endsWith( 'paragraph $text' ) && [ 'a', 'b' ].includes( attributeName ) ) {
- return true;
- }
- // Allow 'b' and 'c' in pchild>$text.
- if ( ctx.endsWith( 'pchild $text' ) && [ 'b', 'c' ].includes( attributeName ) ) {
- return true;
- }
- } );
- schema.extend( 'pchild', { allowIn: 'pchild' } );
- } );
- test(
- 'filters out disallowed attributes after left merge',
- '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>y]<$text a="1" b="1">z</$text></paragraph>',
- '<paragraph>x<pchild>fo[]<$text b="1">z</$text></pchild></paragraph>'
- );
- test(
- 'filters out disallowed attributes from nested nodes after left merge',
- '<paragraph>' +
- 'x' +
- '<pchild>fo[o</pchild>' +
- '</paragraph>' +
- '<paragraph>' +
- 'b]a<$text a="1" b="1">r</$text>' +
- '<pchild>b<$text b="1" c="1">i</$text>z</pchild>' +
- 'y' +
- '</paragraph>',
- '<paragraph>' +
- 'x' +
- '<pchild>' +
- 'fo[]a<$text b="1">r</$text>' +
- '<pchild>b<$text b="1">i</$text>z</pchild>' +
- 'y' +
- '</pchild>' +
- '</paragraph>'
- );
- test(
- 'filters out disallowed attributes after right merge',
- '<paragraph>fo[o</paragraph><paragraph><pchild>x<$text b="1" c="1">y]z</$text></pchild></paragraph>',
- '<paragraph>fo[]<$text b="1">z</$text></paragraph>'
- );
- } );
- } );
- describe( 'in element selections scenarios', () => {
- beforeEach( () => {
- model = new Model();
- doc = model.document;
- // <p> like root.
- doc.createRoot( 'paragraph', 'paragraphRoot' );
- // <body> like root.
- doc.createRoot( '$root', 'bodyRoot' );
- // Special root which allows only blockWidgets inside itself.
- doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
- const schema = model.schema;
- schema.register( 'image', { allowWhere: '$text' } );
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- schema.register( 'heading1', { inheritAllFrom: '$block' } );
- schema.register( 'blockWidget', { isLimit: true } );
- schema.register( 'restrictedRoot', {
- isLimit: true
- } );
- schema.extend( '$block', { allowIn: '$root' } );
- schema.extend( 'blockWidget', { allowIn: '$root' } );
- schema.extend( 'blockWidget', { allowIn: 'restrictedRoot' } );
- } );
- // See also "in simple scenarios => deletes an element".
- it( 'deletes two inline elements', () => {
- model.schema.extend( 'paragraph', {
- isLimit: true
- } );
- setData(
- model,
- 'x[<image></image><image></image>]z',
- { rootName: 'paragraphRoot' }
- );
- deleteContent( model, doc.selection );
- expect( getData( model, { rootName: 'paragraphRoot' } ) )
- .to.equal( 'x[]z' );
- } );
- it( 'moves the (custom) selection to the nearest paragraph', () => {
- setData(
- model,
- '<paragraph>[x]</paragraph><paragraph>yyy</paragraph><paragraph>z</paragraph>',
- { rootName: 'bodyRoot' }
- );
- const root = doc.getRoot( 'bodyRoot' );
- // [<paragraph>yyy</paragraph>]
- const selection = new Selection( [
- new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) )
- ] );
- deleteContent( model, selection );
- expect( getData( model, { rootName: 'bodyRoot' } ) )
- .to.equal( '<paragraph>[x]</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
- expect( stringify( root, selection ) )
- .to.equal( '<$root><paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph></$root>' );
- } );
- it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
- setData(
- model,
- '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
- { rootName: 'bodyRoot' }
- );
- deleteContent( model, doc.selection );
- expect( getData( model, { rootName: 'bodyRoot' } ) )
- .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
- } );
- it( 'creates paragraph when text is not allowed (heading selected)', () => {
- setData(
- model,
- '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>z</paragraph>',
- { rootName: 'bodyRoot' }
- );
- // [<heading1>yyy</heading1>]
- const range = new Range(
- new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
- new Position( doc.getRoot( 'bodyRoot' ), [ 2 ] )
- );
- deleteContent( model, new Selection( range ) );
- expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
- .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
- } );
- it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
- setData(
- model,
- '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>yyy</paragraph><paragraph>z</paragraph>',
- { rootName: 'bodyRoot' }
- );
- // [<heading1>yyy</heading1><paragraph>yyy</paragraph>]
- const range = new Range(
- new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
- new Position( doc.getRoot( 'bodyRoot' ), [ 3 ] )
- );
- deleteContent( model, new Selection( range ) );
- expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
- .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
- } );
- it( 'creates paragraph when text is not allowed (all content selected)', () => {
- setData(
- model,
- '[<heading1>x</heading1><paragraph>z</paragraph>]',
- { rootName: 'bodyRoot' }
- );
- deleteContent( model, doc.selection );
- expect( getData( model, { rootName: 'bodyRoot' } ) )
- .to.equal( '<paragraph>[]</paragraph>' );
- } );
- it( 'does not create a paragraph when it is not allowed', () => {
- setData(
- model,
- '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
- { rootName: 'restrictedRoot' }
- );
- deleteContent( model, doc.selection );
- expect( getData( model, { rootName: 'restrictedRoot' } ) )
- .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
- } );
- it( 'does not create a paragraph when doNotAutoparagraph option is set to true', () => {
- setData(
- model,
- '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
- { rootName: 'bodyRoot' }
- );
- deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
- expect( getData( model, { rootName: 'bodyRoot' } ) )
- .to.equal( '<paragraph>x[]</paragraph><paragraph>z</paragraph>' );
- } );
- it( 'does not create a paragraph when after deletion there is no valid selection range (empty root)', () => {
- setData(
- model,
- '[<blockWidget></blockWidget>]',
- { rootName: 'bodyRoot' }
- );
- deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
- expect( getData( model, { rootName: 'bodyRoot' } ) )
- .to.equal( '[]' );
- } );
- } );
- describe( 'integration with inline limit elements', () => {
- beforeEach( () => {
- model = new Model();
- doc = model.document;
- doc.createRoot();
- const schema = model.schema;
- schema.register( 'inlineLimit', {
- isLimit: true,
- allowIn: '$root'
- } );
- schema.extend( '$text', {
- allowIn: [ 'inlineLimit', '$root', 'x' ]
- } );
- schema.register( 'x', { allowIn: '$root' } );
- } );
- test(
- 'should delete inside inline limit element',
- '<inlineLimit>foo [bar] baz</inlineLimit>',
- '<inlineLimit>foo [] baz</inlineLimit>'
- );
- test(
- 'should delete whole inline limit element',
- 'x[<inlineLimit>foo bar</inlineLimit>]x',
- 'x[]x'
- );
- test(
- 'should delete from two inline limit elements',
- '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
- '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
- );
- test(
- 'merge option should be ignored if both elements are limits',
- '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
- '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
- );
- test(
- 'merge option should be ignored if the first element is a limit',
- '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
- '<inlineLimit>foo []</inlineLimit><x> qux</x>'
- );
- test(
- 'merge option should be ignored if the second element is a limit',
- '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
- '<x>baz []</x><inlineLimit> bar</inlineLimit>'
- );
- } );
- describe( 'integration with block limit elements', () => {
- beforeEach( () => {
- model = new Model();
- doc = model.document;
- doc.createRoot();
- const schema = model.schema;
- schema.register( 'blockLimit', {
- isLimit: true
- } );
- schema.extend( 'blockLimit', { allowIn: '$root' } );
- schema.extend( '$block', { allowIn: 'blockLimit' } );
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- schema.register( 'blockQuote', {
- allowWhere: '$block',
- allowContentOf: '$root'
- } );
- } );
- test(
- 'should delete inside block limit element',
- '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
- '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
- { leaveUnmerged: true }
- );
- test(
- 'should delete inside block limit element (with merge)',
- '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
- '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
- );
- test(
- 'should delete whole block limit element',
- '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
- '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
- );
- test(
- 'should delete from two block limit elements',
- '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
- '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
- );
- test(
- 'merge option should be ignored if any of the elements is a limit',
- '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
- '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
- );
- // See: https://github.com/ckeditor/ckeditor5/issues/1265.
- it( 'should proper merge two elements which are inside limit element', () => {
- setData( model,
- '<blockLimit>' +
- '<blockQuote>' +
- '<paragraph>Foo</paragraph>' +
- '</blockQuote>' +
- '<paragraph>[]Bar</paragraph>' +
- '</blockLimit>'
- );
- model.modifySelection( doc.selection, { direction: 'backward' } );
- deleteContent( model, doc.selection );
- expect( getData( model ) ).to.equal(
- '<blockLimit>' +
- '<blockQuote>' +
- '<paragraph>Foo[]Bar</paragraph>' +
- '</blockQuote>' +
- '</blockLimit>' );
- } );
- it( 'should proper merge elements which are inside limit element (nested elements)', () => {
- setData( model,
- '<blockQuote>' +
- '<blockLimit>' +
- '<blockQuote>' +
- '<paragraph>Foo.</paragraph>' +
- '<blockQuote>' +
- '<paragraph>Foo</paragraph>' +
- '</blockQuote>' +
- '</blockQuote>' +
- '<paragraph>[]Bar</paragraph>' +
- '</blockLimit>' +
- '</blockQuote>'
- );
- model.modifySelection( doc.selection, { direction: 'backward' } );
- deleteContent( model, doc.selection );
- expect( getData( model ) ).to.equal(
- '<blockQuote>' +
- '<blockLimit>' +
- '<blockQuote>' +
- '<paragraph>Foo.</paragraph>' +
- '<blockQuote>' +
- '<paragraph>Foo[]Bar</paragraph>' +
- '</blockQuote>' +
- '</blockQuote>' +
- '</blockLimit>' +
- '</blockQuote>'
- );
- } );
- } );
- describe( 'should leave a paragraph if the entire content was selected', () => {
- beforeEach( () => {
- model = new Model();
- doc = model.document;
- doc.createRoot();
- const schema = model.schema;
- schema.register( 'div', {
- inheritAllFrom: '$block',
- isLimit: true
- } );
- schema.register( 'article', {
- inheritAllFrom: '$block',
- isLimit: true
- } );
- schema.register( 'image', {
- allowWhere: '$text',
- isObject: true
- } );
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- schema.register( 'heading1', { inheritAllFrom: '$block' } );
- schema.register( 'heading2', { inheritAllFrom: '$block' } );
- schema.extend( '$text', { allowIn: '$root' } );
- schema.extend( 'image', { allowIn: '$root' } );
- schema.extend( 'image', { allowIn: 'heading1' } );
- schema.extend( 'heading1', { allowIn: 'div' } );
- schema.extend( 'paragraph', { allowIn: 'div' } );
- schema.extend( 'heading1', { allowIn: 'article' } );
- schema.extend( 'heading2', { allowIn: 'article' } );
- } );
- test(
- 'but not if only one block was selected',
- '<heading1>[xx]</heading1>',
- '<heading1>[]</heading1>'
- );
- test(
- 'when the entire heading and paragraph were selected',
- '<heading1>[xx</heading1><paragraph>yy]</paragraph>',
- '<paragraph>[]</paragraph>'
- );
- test(
- 'when the entire content was selected',
- '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
- '<paragraph>[]</paragraph>'
- );
- test(
- 'inside the limit element when the entire heading and paragraph were inside',
- '<div><heading1>[xx</heading1><paragraph>yy]</paragraph></div>',
- '<div><paragraph>[]</paragraph></div>'
- );
- test(
- 'but not if schema does not accept paragraph in limit element',
- '<article><heading1>[xx</heading1><heading2>yy]</heading2></article>',
- '<article><heading1>[]</heading1></article>'
- );
- test(
- 'but not if selection is not containing the whole content',
- '<image></image><heading1>[xx</heading1><paragraph>yy]</paragraph>',
- '<image></image><heading1>[]</heading1>'
- );
- test(
- 'but not if only single element is selected',
- '<heading1>[<image></image>xx]</heading1>',
- '<heading1>[]</heading1>'
- );
- it( 'when root element was not added as Schema limits work fine as well', () => {
- doc.createRoot( 'paragraph', 'paragraphRoot' );
- setData(
- model,
- 'x[<image></image><image></image>]z',
- { rootName: 'paragraphRoot' }
- );
- deleteContent( model, doc.selection );
- expect( getData( model, { rootName: 'paragraphRoot' } ) )
- .to.equal( 'x[]z' );
- } );
- test(
- 'but not if the flag "doNotResetEntireContent" is set to true',
- '<heading1>[</heading1><paragraph>]</paragraph>',
- '<heading1>[]</heading1>',
- {
- doNotResetEntireContent: true
- }
- );
- } );
- function test( title, input, output, options ) {
- it( title, () => {
- model.enqueueChange( 'transparent', () => {
- setData( model, input );
- deleteContent( model, doc.selection, options );
- } );
- expect( getData( model ) ).to.equal( output );
- } );
- }
- } );
- } );
|