Browse Source

Fixed tests failing on Safari connected to jumping over UIElements.

Szymon Kupś 8 years ago
parent
commit
f5e02c7bf5
1 changed files with 280 additions and 54 deletions
  1. 280 54
      packages/ckeditor5-engine/tests/view/document/jumpoveruielement.js

+ 280 - 54
packages/ckeditor5-engine/tests/view/document/jumpoveruielement.js

@@ -6,12 +6,27 @@
 /* globals document */
 
 import ViewDocument from '../../../src/view/document';
+import UIElement from '../../../src/view/uielement';
+import ViewContainerElement from '../../../src/view/containerelement';
+import ViewAttribtueElement from '../../../src/view/attributeelement';
+import ViewText from '../../../src/view/text';
+import ViewRange from '../../../src/view/range';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
 import { setData as setViewData } from '../../../src/dev-utils/view';
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 
 describe( 'Document', () => {
-	let viewDocument, domRoot, domSelection;
+	let viewDocument, domRoot, domSelection, viewRoot, foo, bar, ui, ui2;
+
+	class MyUIElement extends UIElement {
+		render( domDocument ) {
+			const element = super.render( domDocument );
+			element.innerText = this.contents;
+
+			return element;
+		}
+	}
 
 	beforeEach( () => {
 		domRoot = createElement( document, 'div', {
@@ -20,12 +35,20 @@ describe( 'Document', () => {
 		document.body.appendChild( domRoot );
 
 		viewDocument = new ViewDocument();
-		viewDocument.createRoot( domRoot );
+		viewRoot = viewDocument.createRoot( domRoot );
 
 		domSelection = document.getSelection();
 		domSelection.removeAllRanges();
 
 		viewDocument.isFocused = true;
+
+		foo = new ViewText( 'foo' );
+		bar = new ViewText( 'bar' );
+		ui = new MyUIElement( 'span' );
+		ui.contents = 'xxx';
+
+		ui2 = new MyUIElement( 'span' );
+		ui2.contents = 'yyy';
 	} );
 
 	afterEach( () => {
@@ -34,8 +57,7 @@ describe( 'Document', () => {
 		domRoot.parentElement.removeChild( domRoot );
 	} );
 
-	function prepare( view, options ) {
-		setViewData( viewDocument, view );
+	function renderAndFireKeydownEvent( options ) {
 		viewDocument.render();
 
 		const eventData = Object.assign( { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) }, options );
@@ -61,130 +83,334 @@ describe( 'Document', () => {
 	describe( 'jump over ui element handler', () => {
 		describe( 'collapsed selection', () => {
 			it( 'do nothing when another key is pressed', () => {
-				prepare( '<container:p>foo<ui:span></ui:span>{}bar</container:p>', { keyCode: keyCodes.arrowleft } );
-				check( 'bar', 0 );
+				// <container:p>foo<ui:span>xxx</ui:span>{}bar</container:p>
+				const p = new ViewContainerElement( 'p', null, [ foo, ui, bar ] );
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( bar, 0, bar, 0 ) ] );
+
+				renderAndFireKeydownEvent( { keyCode: keyCodes.arrowleft } );
+
+				testUtils.checkAssertions(
+					() => check( 'bar', 0 ),
+					// Safari renders selection at the end of the text node.
+					() => check( 'xxx', 3 )
+				);
 			} );
 
 			it( 'jump over ui element when right arrow is pressed before ui element - directly before ui element', () => {
-				prepare( '<container:p>foo[]<ui:span></ui:span>bar</container:p>' );
-				check( 'P', 2 );
+				// <container:p>foo[]<ui:span>xxx</ui:span>bar</container:p>
+				const p = new ViewContainerElement( 'p', null, [ foo, ui, bar ] );
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( p, 1, p, 1 ) ] );
+
+				renderAndFireKeydownEvent();
+
+				testUtils.checkAssertions(
+					// <p>foo<span>xxx</span>[]bar</p>
+					() => check( 'P', 2 ),
+					// Safari renders selection at the end of the text node.
+					// <p>foo<span>xxx{}</span>bar</p>
+					() => check( 'xxx', 3 )
+				);
 			} );
 
 			it( 'jump over ui element when right arrow is pressed before ui element - not directly before ui element', () => {
-				prepare( '<container:p>foo{}<ui:span></ui:span>bar</container:p>' );
-				check( 'P', 2 );
+				// <container:p>foo{}<ui:span>xxx</ui:span>bar</container:p>
+				const p = new ViewContainerElement( 'p', null, [ foo, ui, bar ] );
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( foo, 3, foo, 3 ) ] );
+
+				renderAndFireKeydownEvent();
+
+				testUtils.checkAssertions(
+					// <p>foo<span>xxx</span>[]bar</p>
+					() => check( 'P', 2 ),
+					// Safari renders selection at the end of the text node.
+					// <p>foo<span>xxx{}</span>bar</p>
+					() => check( 'xxx', 3 )
+				);
 			} );
 
 			it( 'jump over multiple ui elements when right arrow is pressed before ui element', () => {
-				prepare( '<container:p>foo{}<ui:span></ui:span><ui:span></ui:span>bar</container:p>' );
-				check( 'P', 3 );
+				// <container:p>foo{}<ui:span>xxx</ui:span><ui:span>yyy</ui:span>bar</container:p>'
+				const p = new ViewContainerElement( 'p', null, [ foo, ui, ui2, bar ] );
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( foo, 3, foo, 3 ) ] );
+
+				renderAndFireKeydownEvent();
+
+				testUtils.checkAssertions(
+					// <p>foo<span>xxx</span><span>yyy</span>[]bar</p>
+					() => check( 'P', 3 ),
+					// Safari renders selection at the end of the text node.
+					// <p>foo<span>xxx</span><span>yyy{}</span>bar</p>
+					() => check( 'yyy', 3 )
+				);
 			} );
 
 			it( 'jump over ui elements at the end of container element', () => {
-				prepare( '<container:p>foo{}<ui:span></ui:span><ui:span></ui:span></container:p><container:div></container:div>' );
-				check( 'P', 3 );
+				// <container:p>foo{}<ui:span>xxx</ui:span><ui:span>yyy</ui:span></container:p><container:div></container:div>
+				const p = new ViewContainerElement( 'p', null, [ foo, ui, ui2 ] );
+				const div = new ViewContainerElement( 'div' );
+				viewRoot.appendChildren( p );
+				viewRoot.appendChildren( div );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( foo, 3, foo, 3 ) ] );
+
+				renderAndFireKeydownEvent();
+
+				testUtils.checkAssertions(
+					// <p>foo<span>xxx</span><span>yyy</span>[]</p><div></div>
+					() => check( 'P', 3 ),
+					// Safari renders selection at the end of the text node.
+					// <p>foo<span>xxx</span><span>yyy{}</span></p><div></div>
+					() => check( 'yyy', 3 )
+				);
 			} );
 
 			it( 'jump over ui element if selection is in attribute element - case 1', () => {
-				prepare( '<container:p><attribute:b>foo{}</attribute:b><ui:span></ui:span>bar</container:p>' );
-				check( 'P', 2 );
+				// <container:p><attribute:b>foo{}</attribute:b><ui:span>xxx</ui:span>bar</container:p>
+				const b = new ViewAttribtueElement( 'b', null, foo );
+				const p = new ViewContainerElement( 'p', null, [ b, ui, bar ] );
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( foo, 3, foo, 3 ) ] );
+
+				renderAndFireKeydownEvent();
+
+				testUtils.checkAssertions(
+					// <p><b>foo</b><span>xxx</span>[]bar</p>
+					() => check( 'P', 2 ),
+					// Safari renders selection at the end of the text node.
+					// <p><b>foo</b><span>xxx{}</span>bar</p>
+					() => check( 'xxx', 3 )
+				);
 			} );
 
 			it( 'jump over ui element if selection is in attribute element - case 2', () => {
-				prepare( '<container:p><attribute:b>foo{}</attribute:b><ui:span></ui:span>bar</container:p>' );
-				check( 'P', 2 );
+				// <container:p><attribute:b>foo[]</attribute:b><ui:span>xxx</ui:span>bar</container:p>
+				const b = new ViewAttribtueElement( 'b', null, foo );
+				const p = new ViewContainerElement( 'p', null, [ b, ui, bar ] );
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( b, 1, b, 1 ) ] );
+
+				renderAndFireKeydownEvent();
+
+				testUtils.checkAssertions(
+					// <p><b>foo</b><span>xxx</span>[]bar</p>
+					() => check( 'P', 2 ),
+					// Safari renders selection at the end of the text node.
+					// <p><b>foo</b><span>xxx{}</span>bar</p>
+					() => check( 'xxx', 3 )
+				);
 			} );
 
 			it( 'jump over ui element if selection is in multiple attribute elements', () => {
-				prepare( '<container:p><attribute:i><attribute:b>foo{}</attribute:b></attribute:i><ui:span></ui:span>bar</container:p>' );
-				check( 'P', 2 );
+				// <container:p>
+				// 		<attribute:i>
+				// 			<attribute:b>foo{}</attribute:b>
+				// 		</attribute:i>
+				// 		<ui:span>
+				//			xxx
+				// 		</ui:span>
+				// 		bar
+				// </container:p>
+				const b = new ViewAttribtueElement( 'b', null, foo );
+				const i = new ViewAttribtueElement( 'i', null, b );
+				const p = new ViewContainerElement( 'p', null, [ i, ui, bar ] );
+
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( foo, 3, foo, 3 ) ] );
+
+				renderAndFireKeydownEvent();
+
+				testUtils.checkAssertions(
+					// <p><i><b>foo</b></i><span>xxx</span>[]bar</p>
+					() => check( 'P', 2 ),
+					// Safari renders selection at the end of the text node.
+					// <p><i><b>foo</b></i><span>xxx{}</span>bar</p>
+					() => check( 'xxx', 3 )
+				);
 			} );
 
 			it( 'jump over empty attribute elements and ui elements', () => {
-				prepare(
-					'<container:p>' +
-						'foo{}<attribute:b></attribute:b><ui:span></ui:span><ui:span></ui:span><attribute:b></attribute:b>bar' +
-					'</container:p>'
+				// <container:p>' +
+				// 		foo{}
+				// 		<attribute:b></attribute:b>
+				// 		<ui:span>xxx</ui:span>
+				// 		<ui:span>yyy</ui:span>
+				// 		<attribute:b></attribute:b>
+				// 		bar
+				// </container:p>
+				const b1 = new ViewAttribtueElement( 'b' );
+				const b2 = new ViewAttribtueElement( 'b' );
+				const p = new ViewContainerElement( 'p', null, [ foo, b1, ui, ui2, b2, bar ] );
+
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( foo, 3, foo, 3 ) ] );
+
+				renderAndFireKeydownEvent();
+
+				testUtils.checkAssertions(
+					// <p>foo<b></b><span>xxx</span><span>yyy</span>[]bar</p>
+					() => check( 'P', 5 ),
+					// Safari renders selection at the end of the text node.
+					// <p>foo<b></b><span>xxx</span><span>yyy{}</span>bar</p>
+					() => check( 'yyy', 3 )
 				);
-
-				check( 'P', 5 );
 			} );
 
 			it( 'jump over empty attribute elements and ui elements if shift key is pressed', () => {
-				prepare(
-					'<container:p>' +
-						'foo{}<attribute:b></attribute:b><ui:span></ui:span><ui:span></ui:span><attribute:b></attribute:b>bar' +
-					'</container:p>',
-					{ shiftKey: true }
+				// <container:p>
+				// 		foo{}
+				// 		<attribute:b></attribute:b>
+				// 		<ui:span>xxx</ui:span>
+				// 		<ui:span>yyy</ui:span>
+				// 		<attribute:b></attribute:b>
+				// 		bar
+				// </container:p>
+
+				const b1 = new ViewAttribtueElement( 'b' );
+				const b2 = new ViewAttribtueElement( 'b' );
+				const p = new ViewContainerElement( 'p', null, [ foo, b1, ui, ui2, b2, bar ] );
+
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( foo, 3, foo, 3 ) ] );
+
+				renderAndFireKeydownEvent( { shiftKey: true } );
+
+				testUtils.checkAssertions(
+					// <p>foo<b></b><span>xxx</span><span>yyy</span><b><b>[]bar</p>
+					() => check( 'P', 5 ),
+					// Safari renders selection at the end of the text node.
+					// <p>foo<b></b><span>xxx</span><span>yyy{}</span><b><b>bar</p>
+					() => check( 'yyy', 3 )
 				);
-
-				check( 'P', 5 );
 			} );
 
 			it( 'do nothing if selection is not directly before ui element', () => {
-				prepare( '<container:p>fo{}o<ui:span></ui:span>bar</container:p>' );
+				setViewData( viewDocument, '<container:p>fo{}o<ui:span></ui:span>bar</container:p>' );
+				renderAndFireKeydownEvent();
+
 				check( 'foo', 2 );
 			} );
 
 			it( 'do nothing if selection is in attribute element but not before ui element', () => {
-				prepare( '<container:p><attribute:b>foo{}</attribute:b>bar</container:p>' );
+				setViewData( viewDocument, '<container:p><attribute:b>foo{}</attribute:b>bar</container:p>' );
+				renderAndFireKeydownEvent();
+
 				check( 'foo', 3 );
 			} );
 
 			it( 'do nothing if selection is before non-empty attribute element', () => {
-				prepare( '<container:p>fo{}<attribute:b>o</attribute:b><ui:span></ui:span>bar</container:p>' );
+				setViewData( viewDocument, '<container:p>fo{}<attribute:b>o</attribute:b><ui:span></ui:span>bar</container:p>' );
+				renderAndFireKeydownEvent();
+
 				check( 'fo', 2 );
 			} );
 
 			it( 'do nothing if selection is before container element - case 1', () => {
-				prepare( '<container:p>foo{}</container:p><ui:span></ui:span><container:div>bar</container:div>' );
+				setViewData( viewDocument, '<container:p>foo{}</container:p><ui:span></ui:span><container:div>bar</container:div>' );
+				renderAndFireKeydownEvent();
+
 				check( 'foo', 3 );
 			} );
 
 			it( 'do nothing if selection is before container element - case 2', () => {
-				prepare( '<container:div>foo{}<container:p></container:p><ui:span></ui:span></container:div>' );
+				setViewData( viewDocument, '<container:div>foo{}<container:p></container:p><ui:span></ui:span></container:div>' );
+				renderAndFireKeydownEvent();
+
 				check( 'foo', 3 );
 			} );
 
 			it( 'do nothing if selection is at the end of last container element', () => {
-				prepare( '<container:p>foo{}</container:p>' );
+				setViewData( viewDocument, '<container:p>foo{}</container:p>' );
+				renderAndFireKeydownEvent();
+
 				check( 'foo', 3 );
 			} );
 		} );
 
 		describe( 'non-collapsed selection', () => {
 			it( 'should do nothing', () => {
-				prepare( '<container:p>f{oo}<ui:span></ui:span>bar</container:p>' );
+				setViewData( viewDocument, '<container:p>f{oo}<ui:span></ui:span>bar</container:p>' );
+				renderAndFireKeydownEvent();
+
 				check( 'foo', 1, 'foo', 3 );
 			} );
 
 			it( 'should do nothing if selection is not before ui element - shift key pressed', () => {
-				prepare( '<container:p>f{o}o<ui:span></ui:span>bar</container:p>', { shiftKey: true } );
+				setViewData( viewDocument, '<container:p>f{o}o<ui:span></ui:span>bar</container:p>' );
+				renderAndFireKeydownEvent( { shiftKey: true } );
+
 				check( 'foo', 1, 'foo', 2 );
 			} );
 
 			it( 'jump over ui element if shift key is pressed', () => {
-				prepare( '<container:p>fo{o}<ui:span></ui:span>bar</container:p>', { shiftKey: true } );
-				check( 'foo', 2, 'P', 2 );
+				// <container:p>fo{o}<ui:span>xxx</ui:span>bar</container:p>
+				const p = new ViewContainerElement( 'p', null, [ foo, ui, bar ] );
+
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( foo, 2, foo, 3 ) ] );
+
+				renderAndFireKeydownEvent( { shiftKey: true } );
+
+				testUtils.checkAssertions(
+					// <p>fo{o<span>xxx</span>]bar</p>
+					() => check( 'foo', 2, 'P', 2 ),
+					// Safari renders selection at the end of the previous text node.
+					// <p>fo{o<span>xxx}</span>bar</p>
+					() => check( 'foo', 2, 'xxx', 3 )
+				);
 			} );
 
 			it( 'jump over ui element if selection is in multiple attribute elements', () => {
-				prepare(
-					'<container:p><attribute:i><attribute:b>fo{o}</attribute:b></attribute:i><ui:span></ui:span>bar</container:p>',
-					{ shiftKey: true }
+				// <container:p>
+				// 		<attribute:i>
+				// 			<attribute:b>fo{o}</attribute:b>
+				// 		</attribute:i>
+				// 		<ui:span>xxx</ui:span>
+				// 		bar
+				// </container:p>
+				const b = new ViewAttribtueElement( 'b', null, foo );
+				const i = new ViewAttribtueElement( 'i', null, b );
+				const p = new ViewContainerElement( 'p', null, [ i, ui, bar ] );
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( foo, 2, foo, 3 ) ] );
+
+				renderAndFireKeydownEvent( { shiftKey: true } );
+
+				testUtils.checkAssertions(
+					// <p><i><b>fo{o</b></i><span>xxx</span>]bar</p>
+					() => check( 'foo', 2, 'P', 2 ),
+					// Safari renders selection at the end of the previous text node.
+					// <p><i><b>fo{o</b></i><span>xxx}</span>bar</p>
+					() => check( 'foo', 2, 'xxx', 3 )
 				);
-				check( 'foo', 2, 'P', 2 );
 			} );
 
 			it( 'jump over empty attribute elements and ui elements if shift key is pressed', () => {
-				prepare(
-					'<container:p>' +
-						'fo{o}<attribute:b></attribute:b><ui:span></ui:span><ui:span></ui:span><attribute:b></attribute:b>bar' +
-					'</container:p>',
-					{ shiftKey: true }
+				// <container:p>
+				// 		fo{o}
+				// 		<attribute:b></attribute:b>
+				// 		<ui:span>xxx</ui:span>
+				// 		<ui:span>yyy</ui:span>
+				// 		<attribute:b></attribute:b>
+				// 		bar
+				// </container:p>
+				const b1 = new ViewAttribtueElement( 'b' );
+				const b2 = new ViewAttribtueElement( 'b' );
+				const p = new ViewContainerElement( 'p', null, [ foo, b1, ui, ui2, b2, bar ] );
+				viewRoot.appendChildren( p );
+				viewDocument.selection.setRanges( [ ViewRange.createFromParentsAndOffsets( foo, 2, foo, 3 ) ] );
+
+				renderAndFireKeydownEvent( { shiftKey: true } );
+
+				testUtils.checkAssertions(
+					// <p>fo{o<b></b><span>xxx</span><span>yyy</span><b></b>]bar</p>
+					() => check( 'foo', 2, 'P', 5 ),
+					// Safari renders selection at the end of the previous text node.
+					// <p>fo{o<b></b><span>xxx</span><span>yyy}</span><b></b>bar</p>
+					() => check( 'foo', 2, 'yyy', 3 )
 				);
-
-				check( 'foo', 2, 'P', 5 );
 			} );
 		} );