Răsfoiți Sursa

Merge pull request #1454 from ckeditor/t/ckeditor5/1064

Fix: Expand selection mechanism will work correctly with the inline elements. Closes ckeditor/ckeditor5#1064.
Piotrek Koszuliński 7 ani în urmă
părinte
comite
44e0cad72e

+ 2 - 1
packages/ckeditor5-engine/src/model/utils/modifyselection.js

@@ -172,7 +172,8 @@ function getCorrectWordBreakPosition( walker, isForward ) {
 			// should expand to : 'foofoo [bar<$text bold="true">bar</$text>] bazbaz'.
 			// should expand to : 'foofoo [bar<$text bold="true">bar</$text>] bazbaz'.
 			const nextNode = isForward ? walker.position.nodeAfter : walker.position.nodeBefore;
 			const nextNode = isForward ? walker.position.nodeAfter : walker.position.nodeBefore;
 
 
-			if ( nextNode ) {
+			// Scan only text nodes. Ignore inline elements (like `<softBreak>`).
+			if ( nextNode && nextNode.is( 'text' ) ) {
 				// Check boundary char of an adjacent text node.
 				// Check boundary char of an adjacent text node.
 				const boundaryChar = nextNode.data.charAt( isForward ? 0 : nextNode.data.length - 1 );
 				const boundaryChar = nextNode.data.charAt( isForward ? 0 : nextNode.data.length - 1 );
 
 

+ 43 - 0
packages/ckeditor5-engine/tests/model/utils/modifyselection.js

@@ -18,6 +18,7 @@ describe( 'DataController utils', () => {
 		model.schema.register( 'p', { inheritAllFrom: '$block' } );
 		model.schema.register( 'p', { inheritAllFrom: '$block' } );
 		model.schema.register( 'x', { inheritAllFrom: '$block' } );
 		model.schema.register( 'x', { inheritAllFrom: '$block' } );
 		model.schema.extend( 'x', { allowIn: 'p' } );
 		model.schema.extend( 'x', { allowIn: 'p' } );
+		model.schema.register( 'br', { allowWhere: '$text' } );
 
 
 		doc.createRoot();
 		doc.createRoot();
 	} );
 	} );
@@ -554,6 +555,48 @@ describe( 'DataController utils', () => {
 					expect( doc.selection.isBackward ).to.true;
 					expect( doc.selection.isBackward ).to.true;
 				} );
 				} );
 
 
+				it( 'expands backward selection to the word begin in the paragraph with soft break', () => {
+					setData( model, '<p>Foo<br></br>Bar[]</p>' );
+
+					modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
+
+					expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>Foo<br></br>[Bar]</p>' );
+				} );
+
+				it( 'expands backward selection to the whole paragraph in the paragraph with soft break', () => {
+					setData( model, '<p>Foo<br></br>Bar[]</p>' );
+
+					modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
+					modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
+
+					expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>Foo[<br></br>Bar]</p>' );
+
+					modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
+
+					expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>[Foo<br></br>Bar]</p>' );
+				} );
+
+				it( 'expands forward selection to the word end in the paragraph with soft break', () => {
+					setData( model, '<p>[]Foo<br></br>Bar</p>' );
+
+					modifySelection( model, doc.selection, { unit: 'word', direction: 'forward' } );
+
+					expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>[Foo]<br></br>Bar</p>' );
+				} );
+
+				it( 'expands forward selection to the whole paragraph in the paragraph with soft break', () => {
+					setData( model, '<p>[]Foo<br></br>Bar</p>' );
+
+					modifySelection( model, doc.selection, { unit: 'word', direction: 'forward' } );
+					modifySelection( model, doc.selection, { unit: 'word', direction: 'forward' } );
+
+					expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>[Foo<br></br>]Bar</p>' );
+
+					modifySelection( model, doc.selection, { unit: 'word', direction: 'forward' } );
+
+					expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>[Foo<br></br>Bar]</p>' );
+				} );
+
 				function testStopCharacter( stopCharacter ) {
 				function testStopCharacter( stopCharacter ) {
 					describe( `stop character: "${ stopCharacter }"`, () => {
 					describe( `stop character: "${ stopCharacter }"`, () => {
 						test(
 						test(