@@ -641,4 +641,100 @@ describe( 'Schema', () => {
641641 expect ( result [ 1 ] . end . path ) . to . members ( [ 1 ] ) ;
642642 } ) ;
643643 } ) ;
644+
645+ describe ( 'getLimitElement()' , ( ) => {
646+ let doc , root ;
647+
648+ beforeEach ( ( ) => {
649+ doc = new Document ( ) ;
650+ schema = doc . schema ;
651+ root = doc . createRoot ( ) ;
652+
653+ schema . registerItem ( 'div' , '$block' ) ;
654+ schema . registerItem ( 'article' , '$block' ) ;
655+ schema . registerItem ( 'section' , '$block' ) ;
656+ schema . registerItem ( 'paragraph' , '$block' ) ;
657+ schema . registerItem ( 'widget' , '$block' ) ;
658+ schema . registerItem ( 'image' , '$block' ) ;
659+ schema . registerItem ( 'caption' , '$block' ) ;
660+ schema . allow ( { name : 'image' , inside : 'widget' } ) ;
661+ schema . allow ( { name : 'caption' , inside : 'image' } ) ;
662+ schema . allow ( { name : 'paragraph' , inside : 'article' } ) ;
663+ schema . allow ( { name : 'article' , inside : 'section' } ) ;
664+ schema . allow ( { name : 'section' , inside : 'div' } ) ;
665+ schema . allow ( { name : 'widget' , inside : 'div' } ) ;
666+ } ) ;
667+
668+ it ( 'always returns $root element if any other limit was not defined' , ( ) => {
669+ schema . limits . clear ( ) ;
670+
671+ setData ( doc , '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' ) ;
672+ expect ( schema . getLimitElement ( doc . selection ) ) . to . equal ( root ) ;
673+ } ) ;
674+
675+ it ( 'returns the limit element which is the closest element to common ancestor for collapsed selection' , ( ) => {
676+ schema . limits . add ( 'article' ) ;
677+ schema . limits . add ( 'section' ) ;
678+
679+ setData ( doc , '<div><section><article><paragraph>foo[]bar</paragraph></article></section></div>' ) ;
680+
681+ const article = root . getNodeByPath ( [ 0 , 0 , 0 ] ) ;
682+
683+ expect ( schema . getLimitElement ( doc . selection ) ) . to . equal ( article ) ;
684+ } ) ;
685+
686+ it ( 'returns the limit element which is the closest element to common ancestor for non-collapsed selection' , ( ) => {
687+ schema . limits . add ( 'article' ) ;
688+ schema . limits . add ( 'section' ) ;
689+
690+ setData ( doc , '<div><section><article>[foo</article><article>bar]</article></section></div>' ) ;
691+
692+ const section = root . getNodeByPath ( [ 0 , 0 ] ) ;
693+
694+ expect ( schema . getLimitElement ( doc . selection ) ) . to . equal ( section ) ;
695+ } ) ;
696+
697+ it ( 'works fine with multi-range selections' , ( ) => {
698+ schema . limits . add ( 'article' ) ;
699+ schema . limits . add ( 'widget' ) ;
700+ schema . limits . add ( 'div' ) ;
701+
702+ setData (
703+ doc ,
704+ '<div>' +
705+ '<section>' +
706+ '<article>' +
707+ '<paragraph>[foo]</paragraph>' +
708+ '</article>' +
709+ '</section>' +
710+ '<widget>' +
711+ '<image>' +
712+ '<caption>b[a]r</caption>' +
713+ '</image>' +
714+ '</widget>' +
715+ '</div>'
716+ ) ;
717+
718+ const div = root . getNodeByPath ( [ 0 ] ) ;
719+ expect ( schema . getLimitElement ( doc . selection ) ) . to . equal ( div ) ;
720+ } ) ;
721+
722+ it ( 'works fine with multi-range selections even if limit elements are not defined' , ( ) => {
723+ schema . limits . clear ( ) ;
724+
725+ setData (
726+ doc ,
727+ '<div>' +
728+ '<section>' +
729+ '<article>' +
730+ '<paragraph>[foo]</paragraph>' +
731+ '</article>' +
732+ '</section>' +
733+ '</div>' +
734+ '<section>b[]ar</section>'
735+ ) ;
736+
737+ expect ( schema . getLimitElement ( doc . selection ) ) . to . equal ( root ) ;
738+ } ) ;
739+ } ) ;
644740} ) ;
0 commit comments