|
@@ -7,9 +7,11 @@ import { default as Schema, SchemaItem } from '../../../src/model/schema';
|
|
|
import Document from '../../../src/model/document';
|
|
import Document from '../../../src/model/document';
|
|
|
import Element from '../../../src/model/element';
|
|
import Element from '../../../src/model/element';
|
|
|
import Position from '../../../src/model/position';
|
|
import Position from '../../../src/model/position';
|
|
|
|
|
+import Range from '../../../src/model/range';
|
|
|
|
|
+import Selection from '../../../src/model/selection';
|
|
|
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
|
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
|
|
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
|
|
|
-import { setData } from '../../../src/dev-utils/model';
|
|
|
|
|
|
|
+import { setData, stringify } from '../../../src/dev-utils/model';
|
|
|
|
|
|
|
|
testUtils.createSinonSandbox();
|
|
testUtils.createSinonSandbox();
|
|
|
|
|
|
|
@@ -540,4 +542,77 @@ describe( 'Schema', () => {
|
|
|
} );
|
|
} );
|
|
|
} );
|
|
} );
|
|
|
} );
|
|
} );
|
|
|
|
|
+
|
|
|
|
|
+ describe( 'getValidRanges()', () => {
|
|
|
|
|
+ const attribute = 'bold';
|
|
|
|
|
+ let document, root, schema, ranges;
|
|
|
|
|
+
|
|
|
|
|
+ beforeEach( () => {
|
|
|
|
|
+ document = new Document();
|
|
|
|
|
+ schema = document.schema;
|
|
|
|
|
+ root = document.createRoot();
|
|
|
|
|
+
|
|
|
|
|
+ schema.registerItem( 'p', '$block' );
|
|
|
|
|
+ schema.registerItem( 'h1', '$block' );
|
|
|
|
|
+ schema.registerItem( 'img', '$inline' );
|
|
|
|
|
+
|
|
|
|
|
+ schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
|
|
|
|
|
+ schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );
|
|
|
|
|
+
|
|
|
|
|
+ setData( document, '<p>foo<img />bar</p>' );
|
|
|
|
|
+ ranges = [ Range.createOn( root.getChild( 0 ) ) ];
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ it( 'should return unmodified ranges when attribute is allowed on each item (text is not allowed in img)', () => {
|
|
|
|
|
+ schema.allow( { name: 'img', attributes: 'bold', inside: 'p' } );
|
|
|
|
|
+
|
|
|
|
|
+ expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ it( 'should return unmodified ranges when attribute is allowed on each item (text is allowed in img)', () => {
|
|
|
|
|
+ schema.allow( { name: 'img', attributes: 'bold', inside: 'p' } );
|
|
|
|
|
+ schema.allow( { name: '$text', inside: 'img' } );
|
|
|
|
|
+
|
|
|
|
|
+ expect( schema.getValidRanges( ranges, attribute ) ).to.deep.equal( ranges );
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ it( 'should return two ranges when attribute is not allowed on one item', () => {
|
|
|
|
|
+ schema.allow( { name: 'img', attributes: 'bold', inside: 'p' } );
|
|
|
|
|
+ schema.allow( { name: '$text', inside: 'img' } );
|
|
|
|
|
+
|
|
|
|
|
+ setData( document, '<p>foo<img>xxx</img>bar</p>' );
|
|
|
|
|
+
|
|
|
|
|
+ const validRanges = schema.getValidRanges( ranges, attribute );
|
|
|
|
|
+ const sel = new Selection();
|
|
|
|
|
+ sel.setRanges( validRanges );
|
|
|
|
|
+
|
|
|
|
|
+ expect( stringify( root, sel ) ).to.equal( '[<p>foo<img>]xxx[</img>bar</p>]' );
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ it( 'should return three ranges when attribute is not allowed on one element but is allowed on its child', () => {
|
|
|
|
|
+ schema.allow( { name: '$text', inside: 'img' } );
|
|
|
|
|
+ schema.allow( { name: '$text', attributes: 'bold', inside: 'img' } );
|
|
|
|
|
+
|
|
|
|
|
+ setData( document, '<p>foo<img>xxx</img>bar</p>' );
|
|
|
|
|
+
|
|
|
|
|
+ const validRanges = schema.getValidRanges( ranges, attribute );
|
|
|
|
|
+ const sel = new Selection();
|
|
|
|
|
+ sel.setRanges( validRanges );
|
|
|
|
|
+
|
|
|
|
|
+ expect( stringify( root, sel ) ).to.equal( '[<p>foo]<img>[xxx]</img>[bar</p>]' );
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ it( 'should split range into two ranges and omit disallowed element', () => {
|
|
|
|
|
+ // Disallow bold on img.
|
|
|
|
|
+ document.schema.disallow( { name: 'img', attributes: 'bold', inside: 'p' } );
|
|
|
|
|
+
|
|
|
|
|
+ const result = schema.getValidRanges( ranges, attribute );
|
|
|
|
|
+
|
|
|
|
|
+ expect( result ).to.length( 2 );
|
|
|
|
|
+ expect( result[ 0 ].start.path ).to.members( [ 0 ] );
|
|
|
|
|
+ expect( result[ 0 ].end.path ).to.members( [ 0, 3 ] );
|
|
|
|
|
+ expect( result[ 1 ].start.path ).to.members( [ 0, 4 ] );
|
|
|
|
|
+ expect( result[ 1 ].end.path ).to.members( [ 1 ] );
|
|
|
|
|
+ } );
|
|
|
|
|
+ } );
|
|
|
} );
|
|
} );
|