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

Commit 050a415

Browse files
authored
Merge pull request #1276 from ckeditor/t/1275
Fix: `Schema#getLimitElement()` will return a proper limit element (the root element) if one of the selection's ranges have the root element as the limit element. Closes #1275.
2 parents 699da2b + 6a0c7af commit 050a415

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/model/schema.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,14 @@ export default class Schema {
581581
getLimitElement( selection ) {
582582
// Find the common ancestor for all selection's ranges.
583583
let element = Array.from( selection.getRanges() )
584-
.reduce( ( node, range ) => {
585-
if ( !node ) {
586-
return range.getCommonAncestor();
584+
.reduce( ( element, range ) => {
585+
const rangeCommonAncestor = range.getCommonAncestor();
586+
587+
if ( !element ) {
588+
return rangeCommonAncestor;
587589
}
588590

589-
return node.getCommonAncestor( range.getCommonAncestor() );
591+
return element.getCommonAncestor( rangeCommonAncestor, { includeSelf: true } );
590592
}, null );
591593

592594
while ( !this.isLimit( element ) ) {

tests/model/schema.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,38 @@ describe( 'Schema', () => {
933933

934934
expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
935935
} );
936+
937+
it( 'works fine with multi-range selections if the first range has the root element as a limit element', () => {
938+
setData(
939+
model,
940+
'<image>' +
941+
'<caption>[Foo</caption>' +
942+
'</image>' +
943+
'<article>' +
944+
'<paragraph>Paragraph in article]</paragraph>' +
945+
'</article>' +
946+
'<paragraph>Paragraph item 1</paragraph>' +
947+
'<paragraph>Paragraph [item 2]</paragraph>'
948+
);
949+
950+
expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
951+
} );
952+
953+
it( 'works fine with multi-range selections if the last range has the root element as a limit element', () => {
954+
setData(
955+
model,
956+
'<paragraph>Paragraph item 1</paragraph>' +
957+
'<paragraph>Paragraph [item 2]</paragraph>' +
958+
'<image>' +
959+
'<caption>[Foo</caption>' +
960+
'</image>' +
961+
'<article>' +
962+
'<paragraph>Paragraph in article]</paragraph>' +
963+
'</article>'
964+
);
965+
966+
expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
967+
} );
936968
} );
937969

938970
describe( 'checkAttributeInSelection()', () => {

0 commit comments

Comments
 (0)