Răsfoiți Sursa

Merge branch 'master' into i/6120

Maciej Gołaszewski 5 ani în urmă
părinte
comite
9b2f0d5050

+ 31 - 14
packages/ckeditor5-engine/tests/view/view/view.js

@@ -16,6 +16,7 @@ import FocusObserver from '../../../src/view/observer/focusobserver';
 import CompositionObserver from '../../../src/view/observer/compositionobserver';
 import ViewRange from '../../../src/view/range';
 import ViewElement from '../../../src/view/element';
+import ViewContainerElement from '../../../src/view/containerelement';
 import ViewPosition from '../../../src/view/position';
 import ViewSelection from '../../../src/view/selection';
 import { StylesProcessor } from '../../../src/view/stylesmap';
@@ -527,7 +528,10 @@ describe( 'view', () => {
 
 			view.attachDomRoot( domRoot );
 
-			viewRoot._appendChild( new ViewElement( viewDocument, 'p' ) );
+			// It must be a container element to be rendered with the bogus <br> inside which ensures
+			// that the browser sees a selection position inside (empty <p> may not be selectable).
+			// May help resolving https://github.com/ckeditor/ckeditor5/issues/6655.
+			viewRoot._appendChild( new ViewContainerElement( viewDocument, 'p' ) );
 			view.forceRender();
 
 			domElement = createElement( document, 'div', { contenteditable: 'true' } );
@@ -542,27 +546,40 @@ describe( 'view', () => {
 		} );
 
 		it( 'should be true if selection is inside a DOM root element', done => {
-			domSelection.collapse( domP, 0 );
+			domRoot.focus();
 
-			// Wait for async selectionchange event on DOM document.
 			setTimeout( () => {
-				expect( view.hasDomSelection ).to.be.true;
+				domSelection.collapse( domP, 0 );
 
-				done();
-			}, 100 );
+				// Wait for async selectionchange event on DOM document.
+				setTimeout( () => {
+					expect( view.hasDomSelection ).to.be.true;
+
+					done();
+				}, 10 );
+			}, 10 );
 		} );
 
 		it( 'should be true if selection is inside a DOM root element - no focus', done => {
-			domSelection.collapse( domP, 0 );
-			domRoot.blur();
+			domRoot.focus();
 
-			// Wait for async selectionchange event on DOM document.
 			setTimeout( () => {
-				expect( view.hasDomSelection ).to.be.true;
-				expect( view.document.isFocused ).to.be.false;
-
-				done();
-			}, 100 );
+				domSelection.collapse( domP, 0 );
+
+				setTimeout( () => {
+					// We could also do domRoot.blur() here but it's always better to know where the focus went.
+					// E.g. if it went to some <input>, the selection would disappear from the editor and the test would fail.
+					domRoot.blur();
+
+					// Wait for async selectionchange event on DOM document.
+					setTimeout( () => {
+						expect( view.hasDomSelection ).to.be.true;
+						expect( view.document.isFocused ).to.be.false;
+
+						done();
+					}, 10 );
+				}, 10 );
+			}, 10 );
 		} );
 
 		it( 'should be false if selection is outside DOM root element', done => {

+ 1 - 1
packages/ckeditor5-table/src/tableselection.js

@@ -231,7 +231,7 @@ export default class TableSelection extends Plugin {
 				return;
 			}
 
-			const anchorCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
+			const anchorCell = this.getAnchorCell() || getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
 
 			if ( !anchorCell ) {
 				return;

+ 33 - 0
packages/ckeditor5-table/tests/tableselection.js

@@ -227,6 +227,39 @@ describe( 'table selection', () => {
 			expect( preventDefault.called ).to.equal( true );
 		} );
 
+		it( 'should use the anchor cell from the selection if possible', () => {
+			const preventDefault = sinon.spy();
+
+			const domEventDataMock = new DomEventData( view, {
+				shiftKey: true,
+				target: view.domConverter.mapViewToDom(
+					// figure > table > tbody > tr > td
+					viewDocument.getRoot().getChild( 0 ).getChild( 1 ).getChild( 0 ).getChild( 0 ).getChild( 2 )
+				),
+				preventDefault
+			} );
+
+			tableSelection.setCellSelection(
+				modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
+				modelRoot.getNodeByPath( [ 0, 2, 1 ] )
+			);
+			assertSelectedCells( model, [
+				[ 0, 0, 0 ],
+				[ 1, 1, 0 ],
+				[ 1, 1, 0 ]
+			] );
+
+			viewDocument.fire( 'mousedown', domEventDataMock );
+
+			assertSelectedCells( model, [
+				[ 1, 1, 1 ],
+				[ 1, 1, 1 ],
+				[ 0, 0, 0 ]
+			] );
+
+			expect( preventDefault.called ).to.equal( true );
+		} );
+
 		it( 'should ignore `selectionChange` event when selecting cells', () => {
 			const consoleLog = sinon.stub( console, 'log' );
 			const preventDefault = sinon.spy();