/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* globals document */
import ViewDocument from '../../../src/view/document';
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';
describe( 'Document', () => {
let viewDocument, domRoot, domSelection;
beforeEach( () => {
domRoot = createElement( document, 'div', {
contenteditable: 'true'
} );
document.body.appendChild( domRoot );
viewDocument = new ViewDocument();
viewDocument.createRoot( domRoot );
domSelection = document.getSelection();
domSelection.removeAllRanges();
viewDocument.isFocused = true;
} );
afterEach( () => {
viewDocument.destroy();
domRoot.parentElement.removeChild( domRoot );
} );
function prepare( view, options ) {
setViewData( viewDocument, view );
viewDocument.render();
const eventData = Object.assign( { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) }, options );
viewDocument.fire( 'keydown', eventData );
}
function check( anchorNode, anchorOffset, focusNode, focusOffset ) {
const anchor = domSelection.anchorNode.data ? domSelection.anchorNode.data : domSelection.anchorNode.nodeName.toUpperCase();
expect( anchor, 'anchorNode' ).to.equal( anchorNode );
expect( domSelection.anchorOffset, 'anchorOffset' ).to.equal( anchorOffset );
if ( focusNode ) {
const focus = domSelection.focusNode.data ? domSelection.focusNode.data : domSelection.focusNode.nodeName.toUpperCase();
expect( focus, 'focusNode' ).to.equal( focusNode );
expect( domSelection.focusOffset, 'focusOffset' ).to.equal( focusOffset );
} else {
expect( domSelection.isCollapsed, 'isCollapsed' ).to.be.true;
}
}
describe( 'jump over ui element handler', () => {
describe( 'collapsed selection', () => {
it( 'do nothing when another key is pressed', () => {
prepare( 'foo{}bar', { keyCode: keyCodes.arrowleft } );
check( 'bar', 0 );
} );
it( 'jump over ui element when right arrow is pressed before ui element - directly before ui element', () => {
prepare( 'foo[]bar' );
check( 'P', 2 );
} );
it( 'jump over ui element when right arrow is pressed before ui element - not directly before ui element', () => {
prepare( 'foo{}bar' );
check( 'P', 2 );
} );
it( 'jump over multiple ui elements when right arrow is pressed before ui element', () => {
prepare( 'foo{}bar' );
check( 'P', 3 );
} );
it( 'jump over ui elements at the end of container element', () => {
prepare( 'foo{}' );
check( 'P', 3 );
} );
it( 'jump over ui element if selection is in attribute element - case 1', () => {
prepare( 'foo{}bar' );
check( 'P', 2 );
} );
it( 'jump over ui element if selection is in attribute element - case 2', () => {
prepare( 'foo{}bar' );
check( 'P', 2 );
} );
it( 'jump over ui element if selection is in multiple attribute elements', () => {
prepare( 'foo{}bar' );
check( 'P', 2 );
} );
it( 'jump over empty attribute elements and ui elements', () => {
prepare(
'' +
'foo{}bar' +
''
);
check( 'P', 5 );
} );
it( 'jump over empty attribute elements and ui elements if shift key is pressed', () => {
prepare(
'' +
'foo{}bar' +
'',
{ shiftKey: true }
);
check( 'P', 5 );
} );
it( 'do nothing if selection is not directly before ui element', () => {
prepare( 'fo{}obar' );
check( 'foo', 2 );
} );
it( 'do nothing if selection is in attribute element but not before ui element', () => {
prepare( 'foo{}bar' );
check( 'foo', 3 );
} );
it( 'do nothing if selection is before non-empty attribute element', () => {
prepare( 'fo{}obar' );
check( 'fo', 2 );
} );
it( 'do nothing if selection is before container element - case 1', () => {
prepare( 'foo{}bar' );
check( 'foo', 3 );
} );
it( 'do nothing if selection is before container element - case 2', () => {
prepare( 'foo{}' );
check( 'foo', 3 );
} );
it( 'do nothing if selection is at the end of last container element', () => {
prepare( 'foo{}' );
check( 'foo', 3 );
} );
} );
describe( 'non-collapsed selection', () => {
it( 'should do nothing', () => {
prepare( 'f{oo}bar' );
check( 'foo', 1, 'foo', 3 );
} );
it( 'should do nothing if selection is not before ui element - shift key pressed', () => {
prepare( 'f{o}obar', { shiftKey: true } );
check( 'foo', 1, 'foo', 2 );
} );
it( 'jump over ui element if shift key is pressed', () => {
prepare( 'fo{o}bar', { shiftKey: true } );
check( 'foo', 2, 'P', 2 );
} );
it( 'jump over ui element if selection is in multiple attribute elements', () => {
prepare(
'fo{o}bar',
{ shiftKey: true }
);
check( 'foo', 2, 'P', 2 );
} );
it( 'jump over empty attribute elements and ui elements if shift key is pressed', () => {
prepare(
'' +
'fo{o}bar' +
'',
{ shiftKey: true }
);
check( 'foo', 2, 'P', 5 );
} );
} );
it( 'should do nothing if dom position cannot be converted to view position', () => {
const newDiv = document.createElement( 'div' );
const domSelection = document.getSelection();
document.body.appendChild( newDiv );
domSelection.collapse( newDiv, 0 );
viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
} );
} );
} );