| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /**
- * @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, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
- viewDocument.render();
- viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: viewDocument.domRoots.get( 'main' ) } );
- const domSelection = document.getSelection();
- expect( domSelection.anchorNode.data ).to.equal( 'foo' );
- expect( domSelection.anchorOffset ).to.equal( 3 );
- expect( domSelection.isCollapsed ).to.be.true;
- } );
- it( 'should do nothing when another key is pressed', () => {
- setData( viewDocument, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
- 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, '<container:p>foo<attribute:b>{x}</attribute:b>bar</container:p>' );
- 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, '<container:p>foo<attribute:b>{}x</attribute:b>bar</container:p>' );
- // 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, '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
- viewDocument.render();
- // Insert a letter to the <b>: '<container:p>foo<attribute:b>x{}</attribute:b>bar</container:p>'
- // 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;
- } );
- } );
- } );
|