Procházet zdrojové kódy

Changed the rule of selecting markers.

Oskar Wróbel před 7 roky
rodič
revize
1172e4f5e8

+ 3 - 1
packages/ckeditor5-engine/src/model/documentselection.js

@@ -813,8 +813,10 @@ class LiveSelection extends Selection {
 		const markers = [];
 
 		for ( const marker of this._model.markers ) {
+			const markerRange = marker.getRange();
+
 			for ( const selectionRange of this.getRanges() ) {
-				if ( marker.getRange().containsRange( selectionRange ) ) {
+				if ( markerRange.containsRange( selectionRange, !selectionRange.isCollapsed ) ) {
 					markers.push( marker );
 				}
 			}

+ 75 - 1
packages/ckeditor5-engine/tests/model/documentselection.js

@@ -244,7 +244,7 @@ describe( 'DocumentSelection', () => {
 				} );
 			} );
 
-			expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-3' ] );
+			expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-2', 'marker-3' ] );
 		} );
 
 		it( 'should update markers after selection change', () => {
@@ -347,6 +347,80 @@ describe( 'DocumentSelection', () => {
 
 			expect( selection.markers.map( marker => marker.name ), 2 ).to.have.members( [ 'marker-3' ] );
 		} );
+
+		it( 'should not add marker when collapsed selection is on the marker left bound', () => {
+			model.change( writer => {
+				writer.setSelection( writer.createRange(
+					writer.createPositionFromPath( root, [ 2, 2 ] ),
+					writer.createPositionFromPath( root, [ 2, 4 ] )
+				) );
+
+				writer.addMarker( 'marker', {
+					range: writer.createRange(
+						writer.createPositionFromPath( root, [ 2, 2 ] )
+					),
+					usingOperation: false
+				} );
+			} );
+
+			expect( selection.markers ).to.length( 0 );
+		} );
+
+		it( 'should not add marker when collapsed selection is on the marker right bound', () => {
+			model.change( writer => {
+				writer.setSelection( writer.createRange(
+					writer.createPositionFromPath( root, [ 2, 4 ] )
+				) );
+
+				writer.addMarker( 'marker', {
+					range: writer.createRange(
+						writer.createPositionFromPath( root, [ 2, 2 ] ),
+						writer.createPositionFromPath( root, [ 2, 4 ] )
+					),
+					usingOperation: false
+				} );
+			} );
+
+			expect( selection.markers ).to.length( 0 );
+		} );
+
+		it( 'should add marker when non-collapsed selection is inside a marker and touches the left bound', () => {
+			model.change( writer => {
+				writer.setSelection( writer.createRange(
+					writer.createPositionFromPath( root, [ 2, 1 ] ),
+					writer.createPositionFromPath( root, [ 2, 3 ] )
+				) );
+
+				writer.addMarker( 'marker', {
+					range: writer.createRange(
+						writer.createPositionFromPath( root, [ 2, 1 ] ),
+						writer.createPositionFromPath( root, [ 2, 5 ] )
+					),
+					usingOperation: false
+				} );
+			} );
+
+			expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
+		} );
+
+		it( 'should add marker when non-collapsed selection is inside a marker and touches the right bound', () => {
+			model.change( writer => {
+				writer.setSelection( writer.createRange(
+					writer.createPositionFromPath( root, [ 2, 2 ] ),
+					writer.createPositionFromPath( root, [ 2, 5 ] )
+				) );
+
+				writer.addMarker( 'marker', {
+					range: writer.createRange(
+						writer.createPositionFromPath( root, [ 2, 1 ] ),
+						writer.createPositionFromPath( root, [ 2, 5 ] )
+					),
+					usingOperation: false
+				} );
+			} );
+
+			expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
+		} );
 	} );
 
 	describe( 'destroy()', () => {