| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: model */
- import Document from '/ckeditor5/engine/model/document.js';
- import Element from '/ckeditor5/engine/model/element.js';
- import Text from '/ckeditor5/engine/model/text.js';
- import Range from '/ckeditor5/engine/model/range.js';
- import Position from '/ckeditor5/engine/model/position.js';
- import LiveRange from '/ckeditor5/engine/model/liverange.js';
- import LiveSelection from '/ckeditor5/engine/model/liveselection.js';
- import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
- import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
- import AttributeOperation from '/ckeditor5/engine/model/operation/attributeoperation.js';
- import count from '/ckeditor5/utils/count.js';
- import testUtils from '/tests/core/_utils/utils.js';
- import { wrapInDelta } from '/tests/engine/model/_utils/utils.js';
- testUtils.createSinonSandbox();
- describe( 'LiveSelection', () => {
- let attrFooBar;
- before( () => {
- attrFooBar = { foo: 'bar' };
- } );
- let doc, root, selection, liveRange, range;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- root.appendChildren( [
- new Element( 'p' ),
- new Element( 'p' ),
- new Element( 'p', [], new Text( 'foobar' ) ),
- new Element( 'p' ),
- new Element( 'p' ),
- new Element( 'p' ),
- new Element( 'p', [], new Text( 'foobar' ) )
- ] );
- selection = doc.selection;
- doc.schema.registerItem( 'p', '$block' );
- liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
- range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
- } );
- afterEach( () => {
- doc.destroy();
- liveRange.detach();
- } );
- describe( 'default range', () => {
- it( 'should go to the first editable element', () => {
- const ranges = Array.from( selection.getRanges() );
- expect( ranges.length ).to.equal( 1 );
- expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
- expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
- expect( selection ).to.have.property( 'isBackward', false );
- } );
- it( 'should be set to the beginning of the doc if there is no editable element', () => {
- doc = new Document();
- root = doc.createRoot();
- root.insertChildren( 0, new Text( 'foobar' ) );
- selection = doc.selection;
- const ranges = Array.from( selection.getRanges() );
- expect( ranges.length ).to.equal( 1 );
- expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
- expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
- expect( selection ).to.have.property( 'isBackward', false );
- expect( count( selection.getAttributes() ) ).to.equal( 0 );
- } );
- it( 'should skip element when you can not put selection', () => {
- doc = new Document();
- root = doc.createRoot();
- root.insertChildren( 0, [
- new Element( 'img' ),
- new Element( 'p', [], new Text( 'foobar' ) )
- ] );
- doc.schema.registerItem( 'img' );
- doc.schema.registerItem( 'p', '$block' );
- selection = doc.selection;
- const ranges = Array.from( selection.getRanges() );
- expect( ranges.length ).to.equal( 1 );
- expect( selection.anchor.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
- expect( selection.focus.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
- expect( selection ).to.have.property( 'isBackward', false );
- expect( count( selection.getAttributes() ) ).to.equal( 0 );
- } );
- } );
- describe( 'isCollapsed', () => {
- it( 'should return true for default range', () => {
- expect( selection.isCollapsed ).to.be.true;
- } );
- } );
- describe( 'rangeCount', () => {
- it( 'should return proper range count', () => {
- expect( selection.rangeCount ).to.equal( 1 );
- selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
- expect( selection.rangeCount ).to.equal( 1 );
- selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
- expect( selection.rangeCount ).to.equal( 2 );
- } );
- } );
- describe( 'addRange', () => {
- it( 'should convert added Range to LiveRange', () => {
- selection.addRange( range );
- const ranges = selection._ranges;
- expect( ranges[ 0 ] ).to.be.instanceof( LiveRange );
- } );
- } );
- describe( 'collapse', () => {
- it( 'detaches all existing ranges', () => {
- selection.addRange( range );
- selection.addRange( liveRange );
- const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
- selection.collapse( root );
- expect( spy.calledTwice ).to.be.true;
- } );
- } );
- describe( 'destroy', () => {
- it( 'should unbind all events', () => {
- selection.addRange( liveRange );
- selection.addRange( range );
- const ranges = selection._ranges;
- sinon.spy( ranges[ 0 ], 'detach' );
- sinon.spy( ranges[ 1 ], 'detach' );
- selection.destroy();
- expect( ranges[ 0 ].detach.called ).to.be.true;
- expect( ranges[ 1 ].detach.called ).to.be.true;
- ranges[ 0 ].detach.restore();
- ranges[ 1 ].detach.restore();
- } );
- } );
- describe( 'setFocus', () => {
- it( 'modifies default range', () => {
- const startPos = selection.getFirstPosition();
- const endPos = Position.createAt( root, 'end' );
- selection.setFocus( endPos );
- expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
- expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
- } );
- it( 'detaches the range it replaces', () => {
- const startPos = Position.createAt( root, 1 );
- const endPos = Position.createAt( root, 2 );
- const newEndPos = Position.createAt( root, 4 );
- const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
- selection.addRange( new Range( startPos, endPos ) );
- selection.setFocus( newEndPos );
- expect( spy.calledOnce ).to.be.true;
- } );
- } );
- describe( 'removeAllRanges', () => {
- let spy, ranges;
- beforeEach( () => {
- selection.addRange( liveRange );
- selection.addRange( range );
- spy = sinon.spy();
- selection.on( 'change:range', spy );
- ranges = Array.from( selection._ranges );
- sinon.spy( ranges[ 0 ], 'detach' );
- sinon.spy( ranges[ 1 ], 'detach' );
- selection.removeAllRanges();
- } );
- afterEach( () => {
- ranges[ 0 ].detach.restore();
- ranges[ 1 ].detach.restore();
- } );
- it( 'should remove all stored ranges (and reset to default range)', () => {
- expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
- expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
- expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
- } );
- it( 'should detach ranges', () => {
- expect( ranges[ 0 ].detach.called ).to.be.true;
- expect( ranges[ 1 ].detach.called ).to.be.true;
- } );
- } );
- describe( 'setRanges', () => {
- let newRanges, spy, oldRanges;
- before( () => {
- newRanges = [
- new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
- new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
- ];
- } );
- beforeEach( () => {
- selection.addRange( liveRange );
- selection.addRange( range );
- spy = sinon.spy();
- selection.on( 'change:range', spy );
- oldRanges = Array.from( selection._ranges );
- sinon.spy( oldRanges[ 0 ], 'detach' );
- sinon.spy( oldRanges[ 1 ], 'detach' );
- } );
- afterEach( () => {
- oldRanges[ 0 ].detach.restore();
- oldRanges[ 1 ].detach.restore();
- } );
- it( 'should detach removed ranges', () => {
- selection.setRanges( newRanges );
- expect( oldRanges[ 0 ].detach.called ).to.be.true;
- expect( oldRanges[ 1 ].detach.called ).to.be.true;
- } );
- } );
- describe( 'getFirstRange', () => {
- it( 'should return default range if no ranges were added', () => {
- const firstRange = selection.getFirstRange();
- expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
- expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
- } );
- } );
- describe( 'getLastRange', () => {
- it( 'should return default range if no ranges were added', () => {
- const lastRange = selection.getLastRange();
- expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
- expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
- } );
- } );
- describe( 'createFromSelection', () => {
- it( 'should return a LiveSelection instance', () => {
- selection.addRange( range, true );
- expect( LiveSelection.createFromSelection( selection ) ).to.be.instanceof( LiveSelection );
- } );
- } );
- // LiveSelection uses LiveRanges so here are only simple test to see if integration is
- // working well, without getting into complicated corner cases.
- describe( 'after applying an operation should get updated and fire events', () => {
- let spyRange;
- beforeEach( () => {
- root.insertChildren( 0, [
- new Element( 'ul', [], new Text( 'abcdef' ) ),
- new Element( 'p', [], new Text( 'foobar' ) ),
- new Text( 'xyz' )
- ] );
- selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
- spyRange = sinon.spy();
- selection.on( 'change:range', spyRange );
- } );
- describe( 'InsertOperation', () => {
- it( 'before selection', () => {
- selection.on( 'change:range', ( evt, data ) => {
- expect( data.directChange ).to.be.false;
- } );
- doc.applyOperation( wrapInDelta(
- new InsertOperation(
- new Position( root, [ 0, 1 ] ),
- 'xyz',
- doc.version
- )
- ) );
- let range = selection.getFirstRange();
- expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
- expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
- expect( spyRange.calledOnce ).to.be.true;
- } );
- it( 'inside selection', () => {
- selection.on( 'change:range', ( evt, data ) => {
- expect( data.directChange ).to.be.false;
- } );
- doc.applyOperation( wrapInDelta(
- new InsertOperation(
- new Position( root, [ 1, 0 ] ),
- 'xyz',
- doc.version
- )
- ) );
- let range = selection.getFirstRange();
- expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
- expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
- expect( spyRange.calledOnce ).to.be.true;
- } );
- } );
- describe( 'MoveOperation', () => {
- it( 'move range from before a selection', () => {
- selection.on( 'change:range', ( evt, data ) => {
- expect( data.directChange ).to.be.false;
- } );
- doc.applyOperation( wrapInDelta(
- new MoveOperation(
- new Position( root, [ 0, 0 ] ),
- 2,
- new Position( root, [ 2 ] ),
- doc.version
- )
- ) );
- let range = selection.getFirstRange();
- expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
- expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
- expect( spyRange.calledOnce ).to.be.true;
- } );
- it( 'moved into before a selection', () => {
- selection.on( 'change:range', ( evt, data ) => {
- expect( data.directChange ).to.be.false;
- } );
- doc.applyOperation( wrapInDelta(
- new MoveOperation(
- new Position( root, [ 2 ] ),
- 2,
- new Position( root, [ 0, 0 ] ),
- doc.version
- )
- ) );
- let range = selection.getFirstRange();
- expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
- expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
- expect( spyRange.calledOnce ).to.be.true;
- } );
- it( 'move range from inside of selection', () => {
- selection.on( 'change:range', ( evt, data ) => {
- expect( data.directChange ).to.be.false;
- } );
- doc.applyOperation( wrapInDelta(
- new MoveOperation(
- new Position( root, [ 1, 0 ] ),
- 2,
- new Position( root, [ 2 ] ),
- doc.version
- )
- ) );
- let range = selection.getFirstRange();
- expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
- expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
- expect( spyRange.calledOnce ).to.be.true;
- } );
- it( 'moved range intersects with selection', () => {
- selection.on( 'change:range', ( evt, data ) => {
- expect( data.directChange ).to.be.false;
- } );
- doc.applyOperation( wrapInDelta(
- new MoveOperation(
- new Position( root, [ 1, 3 ] ),
- 2,
- new Position( root, [ 4 ] ),
- doc.version
- )
- ) );
- let range = selection.getFirstRange();
- expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
- expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
- expect( spyRange.calledOnce ).to.be.true;
- } );
- it( 'split inside selection (do not break selection)', () => {
- selection.on( 'change:range', ( evt, data ) => {
- expect( data.directChange ).to.be.false;
- } );
- doc.applyOperation( wrapInDelta(
- new InsertOperation(
- new Position( root, [ 2 ] ),
- new Element( 'p' ),
- doc.version
- )
- ) );
- doc.applyOperation( wrapInDelta(
- new MoveOperation(
- new Position( root, [ 1, 2 ] ),
- 4,
- new Position( root, [ 2, 0 ] ),
- doc.version
- )
- ) );
- let range = selection.getFirstRange();
- expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
- expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
- expect( spyRange.calledOnce ).to.be.true;
- } );
- } );
- describe( 'AttributeOperation', () => {
- it( 'changed range includes selection anchor', () => {
- const spyAttribute = sinon.spy();
- selection.on( 'change:attribute', spyAttribute );
- selection.on( 'change:attribute', ( evt, data ) => {
- expect( data.directChange ).to.be.false;
- expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
- } );
- doc.applyOperation( wrapInDelta(
- new AttributeOperation(
- new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
- 'foo',
- null,
- 'bar',
- doc.version
- )
- ) );
- expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
- expect( spyAttribute.calledOnce ).to.be.true;
- } );
- it( 'should not overwrite previously set attributes', () => {
- selection.setAttribute( 'foo', 'xyz' );
- const spyAttribute = sinon.spy();
- selection.on( 'change:attribute', spyAttribute );
- doc.applyOperation( wrapInDelta(
- new AttributeOperation(
- new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
- 'foo',
- null,
- 'bar',
- doc.version
- )
- ) );
- expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
- expect( spyAttribute.called ).to.be.false;
- } );
- it( 'should not overwrite previously removed attributes', () => {
- selection.setAttribute( 'foo', 'xyz' );
- selection.removeAttribute( 'foo' );
- const spyAttribute = sinon.spy();
- selection.on( 'change:attribute', spyAttribute );
- doc.applyOperation( wrapInDelta(
- new AttributeOperation(
- new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
- 'foo',
- null,
- 'bar',
- doc.version
- )
- ) );
- expect( selection.hasAttribute( 'foo' ) ).to.be.false;
- expect( spyAttribute.called ).to.be.false;
- } );
- } );
- } );
- describe( 'attributes interface', () => {
- let fullP, emptyP, rangeInFullP, rangeInEmptyP;
- beforeEach( () => {
- root.insertChildren( 0, [
- new Element( 'p', [], new Text( 'foobar' ) ),
- new Element( 'p', [], [] )
- ] );
- fullP = root.getChild( 0 );
- emptyP = root.getChild( 1 );
- rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
- rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
- } );
- describe( 'setAttribute', () => {
- it( 'should store attribute if the selection is in empty node', () => {
- selection.setRanges( [ rangeInEmptyP ] );
- selection.setAttribute( 'foo', 'bar' );
- expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
- expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
- } );
- } );
- describe( 'setAttributesTo', () => {
- it( 'should fire change:attribute event with correct parameters', ( done ) => {
- selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
- selection.on( 'change:attribute', ( evt, data ) => {
- expect( data.directChange ).to.be.true;
- expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
- done();
- } );
- selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
- } );
- it( 'should not fire change:attribute event if same attributes are set', () => {
- selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
- const spy = sinon.spy();
- selection.on( 'change:attribute', spy );
- selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
- expect( spy.called ).to.be.false;
- } );
- it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
- selection.setRanges( [ rangeInEmptyP ] );
- selection.setAttribute( 'abc', 'xyz' );
- selection.setAttributesTo( { foo: 'bar' } );
- expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
- expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
- expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
- expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
- } );
- } );
- describe( 'removeAttribute', () => {
- it( 'should remove attribute set on the text fragment', () => {
- selection.setRanges( [ rangeInFullP ] );
- selection.setAttribute( 'foo', 'bar' );
- selection.removeAttribute( 'foo' );
- expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
- expect( fullP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
- } );
- it( 'should remove stored attribute if the selection is in empty node', () => {
- selection.setRanges( [ rangeInEmptyP ] );
- selection.setAttribute( 'foo', 'bar' );
- selection.removeAttribute( 'foo' );
- expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
- expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
- } );
- } );
- describe( 'clearAttributes', () => {
- it( 'should remove all stored attributes if the selection is in empty node', () => {
- selection.setRanges( [ rangeInEmptyP ] );
- selection.setAttribute( 'foo', 'bar' );
- selection.setAttribute( 'abc', 'xyz' );
- selection.clearAttributes();
- expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
- expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
- expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
- expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
- } );
- } );
- } );
- describe( 'update attributes on direct range change', () => {
- beforeEach( () => {
- root.insertChildren( 0, [
- new Element( 'p', { p: true } ),
- new Text( 'a', { a: true } ),
- new Element( 'p', { p: true } ),
- new Text( 'b', { b: true } ),
- new Text( 'c', { c: true } ),
- new Element( 'p', [], [
- new Text( 'd', { d: true } )
- ] ),
- new Element( 'p', { p: true } ),
- new Text( 'e', { e: true } )
- ] );
- } );
- it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
- selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
- // Step into elements when looking for first character:
- selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
- } );
- it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
- // Take styles from character before selection.
- selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
- // If there are none,
- // Take styles from character after selection.
- selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
- // If there are none,
- // Look from the selection position to the beginning of node looking for character to take attributes from.
- selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
- // If there are none,
- // Look from the selection position to the end of node looking for character to take attributes from.
- selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
- // If there are no characters to copy attributes from, use stored attributes.
- selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
- } );
- it( 'should overwrite any previously set attributes', () => {
- selection.collapse( new Position( root, [ 5, 0 ] ) );
- selection.setAttribute( 'x', true );
- selection.setAttribute( 'y', true );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
- selection.collapse( new Position( root, [ 1 ] ) );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
- } );
- it( 'should fire change:attribute event', () => {
- let spy = sinon.spy();
- selection.on( 'change:attribute', spy );
- selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'should not fire change:attribute event if attributes did not change', () => {
- selection.collapse( new Position( root, [ 5, 0 ] ) );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
- let spy = sinon.spy();
- selection.on( 'change:attribute', spy );
- selection.collapse( new Position( root, [ 5, 1 ] ) );
- expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
- expect( spy.called ).to.be.false;
- } );
- } );
- describe( '_getStoredAttributes', () => {
- it( 'should return no values if there are no ranges in selection', () => {
- let values = Array.from( selection._getStoredAttributes() );
- expect( values ).to.deep.equal( [] );
- } );
- } );
- } );
|