| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import View from '../../src/view/view';
- import ViewDocumentSelection from '../../src/view/documentselection';
- import ViewRange from '../../src/view/range';
- import createViewRoot from '../view/_utils/createroot';
- import Model from '../../src/model/model';
- import Mapper from '../../src/conversion/mapper';
- import { convertSelectionChange } from '../../src/conversion/upcast-selection-converters';
- import { setData as modelSetData, getData as modelGetData } from '../../src/dev-utils/model';
- import { setData as viewSetData } from '../../src/dev-utils/view';
- describe( 'convertSelectionChange', () => {
- let model, view, viewDocument, mapper, convertSelection, modelRoot, viewRoot;
- beforeEach( () => {
- model = new Model();
- modelRoot = model.document.createRoot();
- model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- modelSetData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
- view = new View();
- viewDocument = view.document;
- viewRoot = createViewRoot( viewDocument, 'div', 'main' );
- viewSetData( view, '<p>foo</p><p>bar</p>' );
- mapper = new Mapper();
- mapper.bindElements( modelRoot, viewRoot );
- mapper.bindElements( modelRoot.getChild( 0 ), viewRoot.getChild( 0 ) );
- mapper.bindElements( modelRoot.getChild( 1 ), viewRoot.getChild( 1 ) );
- convertSelection = convertSelectionChange( model, mapper );
- } );
- afterEach( () => {
- view.destroy();
- } );
- it( 'should convert collapsed selection', () => {
- const viewSelection = new ViewDocumentSelection();
- viewSelection._setTo( ViewRange.createFromParentsAndOffsets(
- viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 1 ) );
- convertSelection( null, { newSelection: viewSelection } );
- expect( modelGetData( model ) ).to.equals( '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
- expect( modelGetData( model ) ).to.equal( '<paragraph>f[]oo</paragraph><paragraph>bar</paragraph>' );
- } );
- it( 'should support unicode', () => {
- modelSetData( model, '<paragraph>நிலைக்கு</paragraph>' );
- viewSetData( view, '<p>நிலைக்கு</p>' );
- // Re-bind elements that were just re-set.
- mapper.bindElements( modelRoot.getChild( 0 ), viewRoot.getChild( 0 ) );
- const viewSelection = new ViewDocumentSelection( [
- ViewRange.createFromParentsAndOffsets( viewRoot.getChild( 0 ).getChild( 0 ), 2, viewRoot.getChild( 0 ).getChild( 0 ), 6 )
- ] );
- convertSelection( null, { newSelection: viewSelection } );
- expect( modelGetData( model ) ).to.equal( '<paragraph>நி[லைக்]கு</paragraph>' );
- } );
- it( 'should convert multi ranges selection', () => {
- const viewSelection = new ViewDocumentSelection( [
- ViewRange.createFromParentsAndOffsets(
- viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 2 ),
- ViewRange.createFromParentsAndOffsets(
- viewRoot.getChild( 1 ).getChild( 0 ), 1, viewRoot.getChild( 1 ).getChild( 0 ), 2 )
- ] );
- convertSelection( null, { newSelection: viewSelection } );
- expect( modelGetData( model ) ).to.equal(
- '<paragraph>f[o]o</paragraph><paragraph>b[a]r</paragraph>' );
- const ranges = Array.from( model.document.selection.getRanges() );
- expect( ranges.length ).to.equal( 2 );
- expect( ranges[ 0 ].start.parent ).to.equal( modelRoot.getChild( 0 ) );
- expect( ranges[ 0 ].start.offset ).to.equal( 1 );
- expect( ranges[ 0 ].end.parent ).to.equal( modelRoot.getChild( 0 ) );
- expect( ranges[ 0 ].end.offset ).to.equal( 2 );
- expect( ranges[ 1 ].start.parent ).to.equal( modelRoot.getChild( 1 ) );
- expect( ranges[ 1 ].start.offset ).to.equal( 1 );
- expect( ranges[ 1 ].end.parent ).to.equal( modelRoot.getChild( 1 ) );
- expect( ranges[ 1 ].end.offset ).to.equal( 2 );
- } );
- it( 'should convert reverse selection', () => {
- const viewSelection = new ViewDocumentSelection( [
- ViewRange.createFromParentsAndOffsets(
- viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 2 ),
- ViewRange.createFromParentsAndOffsets(
- viewRoot.getChild( 1 ).getChild( 0 ), 1, viewRoot.getChild( 1 ).getChild( 0 ), 2 )
- ], { backward: true } );
- convertSelection( null, { newSelection: viewSelection } );
- expect( modelGetData( model ) ).to.equal( '<paragraph>f[o]o</paragraph><paragraph>b[a]r</paragraph>' );
- expect( model.document.selection.isBackward ).to.true;
- } );
- it( 'should not enqueue changes if selection has not changed', () => {
- const viewSelection = new ViewDocumentSelection( [
- ViewRange.createFromParentsAndOffsets(
- viewRoot.getChild( 0 ).getChild( 0 ), 1, viewRoot.getChild( 0 ).getChild( 0 ), 1 )
- ] );
- convertSelection( null, { newSelection: viewSelection } );
- const spy = sinon.spy();
- model.on( 'change', spy );
- convertSelection( null, { newSelection: viewSelection } );
- expect( spy.called ).to.be.false;
- } );
- } );
|