/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* globals document */
import ViewRange from '../../../src/view/range';
import ViewDocument from '../../../src/view/document';
import { INLINE_FILLER_LENGTH, isInlineFiller, startsWithFiller } from '../../../src/view/filler';
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
import { parse, setData } from '../../../src/dev-utils/view';
describe( 'Document', () => {
let viewDocument, domRoot;
beforeEach( () => {
domRoot = createElement( document, 'div', {
contenteditable: 'true'
} );
document.body.appendChild( domRoot );
viewDocument = new ViewDocument();
viewDocument.createRoot( domRoot );
document.getSelection().removeAllRanges();
viewDocument.isFocused = true;
} );
afterEach( () => {
viewDocument.destroy();
domRoot.parentElement.removeChild( domRoot );
} );
describe( 'jump over inline filler hack', () => {
it( 'should jump over inline filler when left arrow is pressed after inline filler', () => {
setData( viewDocument, 'foo[]bar' );
viewDocument.render();
viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
const domSelection = document.getSelection();
// There's a problem now. We expect that the selection was moved to "foo^FILLER", but Safari
// will render it on "foo^...". Both options are correct.
if ( domSelection.anchorNode.data == 'foo' ) {
expect( domSelection.anchorNode.data ).to.equal( 'foo' );
expect( domSelection.anchorOffset ).to.equal( 3 );
} else {
expect( isInlineFiller( domSelection.anchorNode ) ).to.be.true;
expect( domSelection.anchorOffset ).to.equal( 0 );
}
expect( domSelection.isCollapsed ).to.be.true;
} );
it( 'should do nothing when another key is pressed', () => {
setData( viewDocument, 'foo[]bar' );
viewDocument.render();
viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: viewDocument.domRoots.get( 'main' ) } );
const domSelection = document.getSelection();
expect( isInlineFiller( domSelection.anchorNode ) ).to.be.true;
expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH );
expect( domSelection.isCollapsed ).to.be.true;
} );
it( 'should do nothing if range is not collapsed', () => {
setData( viewDocument, 'foo{x}bar' );
viewDocument.render();
viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
const domSelection = document.getSelection();
expect( domSelection.anchorNode.data ).to.equal( 'x' );
expect( domSelection.anchorOffset ).to.equal( 0 );
expect( domSelection.focusNode.data ).to.equal( 'x' );
expect( domSelection.focusOffset ).to.equal( 1 );
} );
// See #664
// it( 'should do nothing if node does not start with the filler', () => {
// setData( viewDocument, 'foo{}xbar' );
// viewDocument.render();
// viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
// const domSelection = document.getSelection();
// expect( domSelection.anchorNode.data ).to.equal( 'x' );
// expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH );
// expect( domSelection.isCollapsed ).to.be.true;
// } );
it( 'should do nothing if caret is not directly before the filler', () => {
setData( viewDocument, 'foo[]bar' );
viewDocument.render();
// Insert a letter to the : 'foox{}bar'
// Do this both in the view and in the DOM to simulate typing and to avoid rendering (which would remove the filler).
const viewB = viewDocument.selection.getFirstPosition().parent;
const viewTextX = parse( 'x' );
viewB.appendChildren( viewTextX );
viewDocument.selection.removeAllRanges();
viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewTextX, 1, viewTextX, 1 ) );
const domB = viewDocument.getDomRoot( 'main' ).querySelector( 'b' );
const domSelection = document.getSelection();
domB.childNodes[ 0 ].data += 'x';
const domRange = document.createRange();
domSelection.removeAllRanges();
domRange.setStart( domB.childNodes[ 0 ], INLINE_FILLER_LENGTH + 1 );
domRange.collapse( true );
domSelection.addRange( domRange );
viewDocument.render();
viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
expect( startsWithFiller( domSelection.anchorNode ) ).to.be.true;
expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH + 1 );
expect( domSelection.isCollapsed ).to.be.true;
} );
} );
} );