浏览代码

Support for multi-ranges selection.

Kamil Piechaczek 8 年之前
父节点
当前提交
c56fe6aa1d
共有 2 个文件被更改,包括 56 次插入1 次删除
  1. 9 1
      packages/ckeditor5-engine/src/model/schema.js
  2. 47 0
      packages/ckeditor5-engine/tests/model/schema/schema.js

+ 9 - 1
packages/ckeditor5-engine/src/model/schema.js

@@ -389,7 +389,15 @@ export default class Schema {
 	 * @returns {module:engine/model/element~Element}
 	 */
 	getLimitElement( selection ) {
-		let element = selection.getFirstRange().getCommonAncestor();
+		// Find the common ancestor for all selection's ranges.
+		let element = Array.from( selection.getRanges() )
+			.reduce( ( node, range ) => {
+				if ( !node ) {
+					return range.getCommonAncestor();
+				}
+
+				return node.getCommonAncestor( range.getCommonAncestor() );
+			}, null );
 
 		while ( !this.limits.has( element.name ) ) {
 			if ( element.parent ) {

+ 47 - 0
packages/ckeditor5-engine/tests/model/schema/schema.js

@@ -676,5 +676,52 @@ describe( 'Schema', () => {
 
 			expect( schema.getLimitElement( doc.selection ) ).to.equal( article );
 		} );
+
+		it( 'returns the limit element which is the closest element to common ancestor for non-collapsed selection', () => {
+			schema.limits.add( 'section' );
+
+			setData( doc, '<div><section><article><paragraph>fo[o</paragraph></article>b]ar</section></div>' );
+
+			const section = root.getNodeByPath( [ 0, 0 ] );
+
+			expect( schema.getLimitElement( doc.selection ) ).to.equal( section );
+		} );
+
+		it( 'works fine with multi-range selections', () => {
+			schema.limits.add( 'div' );
+
+			setData(
+				doc,
+				'<div>' +
+					'<section>' +
+						'<article>' +
+							'<paragraph>[foo]</paragraph>' +
+						'</article>' +
+					'</section>' +
+					'<section>b[]ar</section>' +
+				'</div>'
+			);
+
+			const div = root.getNodeByPath( [ 0 ] );
+			expect( schema.getLimitElement( doc.selection ) ).to.equal( div );
+		} );
+
+		it( 'works fine with multi-range selections even if limit elements are not defined', () => {
+			schema.limits.clear();
+
+			setData(
+				doc,
+				'<div>' +
+					'<section>' +
+						'<article>' +
+							'<paragraph>[foo]</paragraph>' +
+						'</article>' +
+					'</section>' +
+				'</div>' +
+				'<section>b[]ar</section>'
+			);
+
+			expect( schema.getLimitElement( doc.selection ) ).to.equal( root );
+		} );
 	} );
 } );