@@ -12,6 +12,7 @@ import buildModelConverter from '../../src/conversion/buildmodelconverter';
1212
1313import ModelDocumentFragment from '../../src/model/documentfragment' ;
1414import ModelText from '../../src/model/text' ;
15+ import ModelRange from '../../src/model/range' ;
1516import ModelSelection from '../../src/model/selection' ;
1617
1718import ViewDocumentFragment from '../../src/view/documentfragment' ;
@@ -439,4 +440,87 @@ describe( 'DataController', () => {
439440 expect ( stringify ( content ) ) . to . equal ( 'ob' ) ;
440441 } ) ;
441442 } ) ;
443+
444+ describe ( 'hasContent' , ( ) => {
445+ let root ;
446+
447+ beforeEach ( ( ) => {
448+ schema . registerItem ( 'paragraph' , '$block' ) ;
449+ schema . registerItem ( 'div' , '$block' ) ;
450+ schema . allow ( { name : '$block' , inside : 'div' } ) ;
451+ schema . registerItem ( 'image' ) ;
452+ schema . allow ( { name : 'image' , inside : 'div' } ) ;
453+ schema . objects . add ( 'image' ) ;
454+
455+ setData (
456+ modelDocument ,
457+
458+ '<div>' +
459+ '<paragraph></paragraph>' +
460+ '</div>' +
461+ '<paragraph>foo</paragraph>' +
462+ '<div>' +
463+ '<image></image>' +
464+ '</div>'
465+ ) ;
466+
467+ root = modelDocument . getRoot ( ) ;
468+ } ) ;
469+
470+ it ( 'should return true if given element has text node' , ( ) => {
471+ const pFoo = root . getChild ( 1 ) ;
472+
473+ expect ( data . hasContent ( pFoo ) ) . to . be . true ;
474+ } ) ;
475+
476+ it ( 'should return true if given element has element that is an object' , ( ) => {
477+ const divImg = root . getChild ( 2 ) ;
478+
479+ expect ( data . hasContent ( divImg ) ) . to . be . true ;
480+ } ) ;
481+
482+ it ( 'should return false if given element has no elements' , ( ) => {
483+ const pEmpty = root . getChild ( 0 ) . getChild ( 0 ) ;
484+
485+ expect ( data . hasContent ( pEmpty ) ) . to . be . false ;
486+ } ) ;
487+
488+ it ( 'should return false if given element has only elements that are not objects' , ( ) => {
489+ const divP = root . getChild ( 0 ) ;
490+
491+ expect ( data . hasContent ( divP ) ) . to . be . false ;
492+ } ) ;
493+
494+ it ( 'should return true if there is a text node in given range' , ( ) => {
495+ const range = ModelRange . createFromParentsAndOffsets ( root , 1 , root , 2 ) ;
496+
497+ expect ( data . hasContent ( range ) ) . to . be . true ;
498+ } ) ;
499+
500+ it ( 'should return true if there is a part of text node in given range' , ( ) => {
501+ const pFoo = root . getChild ( 1 ) ;
502+ const range = ModelRange . createFromParentsAndOffsets ( pFoo , 1 , pFoo , 2 ) ;
503+
504+ expect ( data . hasContent ( range ) ) . to . be . true ;
505+ } ) ;
506+
507+ it ( 'should return true if there is element that is an object in given range' , ( ) => {
508+ const divImg = root . getChild ( 2 ) ;
509+ const range = ModelRange . createFromParentsAndOffsets ( divImg , 0 , divImg , 1 ) ;
510+
511+ expect ( data . hasContent ( range ) ) . to . be . true ;
512+ } ) ;
513+
514+ it ( 'should return false if range is collapsed' , ( ) => {
515+ const range = ModelRange . createFromParentsAndOffsets ( root , 1 , root , 1 ) ;
516+
517+ expect ( data . hasContent ( range ) ) . to . be . false ;
518+ } ) ;
519+
520+ it ( 'should return false if range has only elements that are not objects' , ( ) => {
521+ const range = ModelRange . createFromParentsAndOffsets ( root , 0 , root , 1 ) ;
522+
523+ expect ( data . hasContent ( range ) ) . to . be . false ;
524+ } ) ;
525+ } ) ;
442526} ) ;
0 commit comments