| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: document */
- 'use strict';
- var modules = bender.amd.require(
- 'document/document',
- 'document/changeoperation',
- 'document/position',
- 'document/range',
- 'document/character',
- 'document/attribute',
- 'document/nodelist',
- 'document/text',
- 'ckeditorerror' );
- describe( 'ChangeOperation', function() {
- it( 'should insert attribute to the set of nodes', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var newAttr = new Attribute( 'isNew', true );
- doc.root.insertChildren( 0, 'bar' );
- doc.applyOperation( new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 2 ], doc.root ) ),
- null,
- newAttr,
- doc.version ) );
- expect( doc.version ).to.be.equal( 1 );
- expect( doc.root.getChildCount() ).to.be.equal( 3 );
- expect( doc.root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
- expect( doc.root.getChild( 1 ).hasAttr( newAttr ) ).to.be.true;
- expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 0 );
- } );
- it( 'should insert attribute to multiple ranges', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var newAttr = new Attribute( 'isNew', true );
- doc.root.insertChildren( 0, 'bar' );
- doc.applyOperation( new ChangeOperation(
- [
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
- new Range( new Position( [ 2 ], doc.root ), new Position( [ 3 ], doc.root ) )
- ],
- null,
- newAttr,
- doc.version ) );
- expect( doc.version ).to.be.equal( 1 );
- expect( doc.root.getChildCount() ).to.be.equal( 3 );
- expect( doc.root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
- expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 0 );
- expect( doc.root.getChild( 2 ).hasAttr( newAttr ) ).to.be.true;
- } );
- it( 'should add attribute to the existing attributes', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Character = modules[ 'document/character' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var newAttr = new Attribute( 'isNew', true );
- var fooAttr = new Attribute( 'foo', true );
- var barAttr = new Attribute( 'bar', true );
- doc.root.insertChildren( 0, new Character( 'x', [ fooAttr, barAttr ] ) );
- doc.applyOperation( new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
- null,
- newAttr,
- doc.version ) );
- expect( doc.version ).to.be.equal( 1 );
- expect( doc.root.getChildCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 3 );
- expect( doc.root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
- expect( doc.root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
- expect( doc.root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
- } );
- it( 'should change attributes on multiple ranges', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Text = modules[ 'document/text' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var oldAttr = new Attribute( 'isNew', false );
- var newAttr = new Attribute( 'isNew', true );
- doc.root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
- doc.applyOperation( new ChangeOperation(
- [
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
- new Range( new Position( [ 2 ], doc.root ), new Position( [ 3 ], doc.root ) )
- ],
- oldAttr,
- newAttr,
- doc.version ) );
- expect( doc.version ).to.be.equal( 1 );
- expect( doc.root.getChildCount() ).to.be.equal( 3 );
- expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
- expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 1 ).hasAttr( oldAttr ) ).to.be.true;
- expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 2 ).hasAttr( newAttr ) ).to.be.true;
- } );
- it( 'should change attribute to the set of nodes', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Text = modules[ 'document/text' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var oldAttr = new Attribute( 'isNew', false );
- var newAttr = new Attribute( 'isNew', true );
- doc.root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
- doc.applyOperation( new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 2 ], doc.root ) ),
- oldAttr,
- newAttr,
- doc.version ) );
- expect( doc.version ).to.be.equal( 1 );
- expect( doc.root.getChildCount() ).to.be.equal( 3 );
- expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
- expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 1 ).hasAttr( newAttr ) ).to.be.true;
- expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 2 ).hasAttr( oldAttr ) ).to.be.true;
- } );
- it( 'should change attribute in the middle of existing attributes', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Character = modules[ 'document/character' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var fooAttr = new Attribute( 'foo', true );
- var x1Attr = new Attribute( 'x', 1 );
- var x2Attr = new Attribute( 'x', 2 );
- var barAttr = new Attribute( 'bar', true );
- doc.root.insertChildren( 0, new Character( 'x', [ fooAttr, x1Attr, barAttr ] ) );
- doc.applyOperation( new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
- x1Attr,
- x2Attr,
- doc.version ) );
- expect( doc.version ).to.be.equal( 1 );
- expect( doc.root.getChildCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 3 );
- expect( doc.root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
- expect( doc.root.getChild( 0 ).hasAttr( x2Attr ) ).to.be.true;
- expect( doc.root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
- } );
- it( 'should remove attribute', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Character = modules[ 'document/character' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var fooAttr = new Attribute( 'foo', true );
- var xAttr = new Attribute( 'x', true );
- var barAttr = new Attribute( 'bar', true );
- doc.root.insertChildren( 0, new Character( 'x', [ fooAttr, xAttr, barAttr ] ) );
- doc.applyOperation( new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
- xAttr,
- null,
- doc.version ) );
- expect( doc.version ).to.be.equal( 1 );
- expect( doc.root.getChildCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 2 );
- expect( doc.root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
- expect( doc.root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
- } );
- it( 'should remove attributes on multiple ranges', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Text = modules[ 'document/text' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var fooAttr = new Attribute( 'foo', true );
- doc.root.insertChildren( 0, new Text( 'bar', [ fooAttr ] ) );
- doc.applyOperation( new ChangeOperation(
- [
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
- new Range( new Position( [ 2 ], doc.root ), new Position( [ 3 ], doc.root ) )
- ],
- fooAttr,
- null,
- doc.version ) );
- expect( doc.version ).to.be.equal( 1 );
- expect( doc.root.getChildCount() ).to.be.equal( 3 );
- expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 0 );
- expect( doc.root.getChild( 1 ).hasAttr( fooAttr ) ).to.be.true;
- expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 0 );
- } );
- it( 'should create a change operation as a reverse', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var oldAttr = new Attribute( 'x', 'old' );
- var newAttr = new Attribute( 'x', 'new' );
- var ranges = [ new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ) ];
- var oppertaion = new ChangeOperation( ranges, oldAttr, newAttr, doc.version );
- var reverse = oppertaion.reverseOperation();
- expect( reverse ).to.be.an.instanceof( ChangeOperation );
- expect( reverse.baseVersion ).to.be.equals( 1 );
- expect( reverse.ranges ).to.be.equals( ranges );
- expect( reverse.oldAttr ).to.be.equals( newAttr );
- expect( reverse.newAttr ).to.be.equals( oldAttr );
- } );
- it( 'should undo insert attribute by applying reverse operation', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var newAttr = new Attribute( 'isNew', true );
- doc.root.insertChildren( 0, 'bar' );
- var oppertaion = new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),
- null,
- newAttr,
- doc.version );
- var reverse = oppertaion.reverseOperation();
- doc.applyOperation( oppertaion );
- doc.applyOperation( reverse );
- expect( doc.version ).to.be.equal( 2 );
- expect( doc.root.getChildCount() ).to.be.equal( 3 );
- expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 0 );
- expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 0 );
- expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 0 );
- } );
- it( 'should undo change attribute by applying reverse operation', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Text = modules[ 'document/text' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var oldAttr = new Attribute( 'isNew', false );
- var newAttr = new Attribute( 'isNew', true );
- doc.root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
- var oppertaion = new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),
- oldAttr,
- newAttr,
- doc.version );
- var reverse = oppertaion.reverseOperation();
- doc.applyOperation( oppertaion );
- doc.applyOperation( reverse );
- expect( doc.version ).to.be.equal( 2 );
- expect( doc.root.getChildCount() ).to.be.equal( 3 );
- expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 0 ).hasAttr( oldAttr ) ).to.be.true;
- expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 1 ).hasAttr( oldAttr ) ).to.be.true;
- expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 2 ).hasAttr( oldAttr ) ).to.be.true;
- } );
- it( 'should undo remove attribute by applying reverse operation', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Text = modules[ 'document/text' ];
- var Attribute = modules[ 'document/attribute' ];
- var doc = new Document();
- var fooAttr = new Attribute( 'foo', false );
- doc.root.insertChildren( 0, new Text( 'bar', [ fooAttr ] ) );
- var oppertaion = new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),
- fooAttr,
- null,
- doc.version );
- var reverse = oppertaion.reverseOperation();
- doc.applyOperation( oppertaion );
- doc.applyOperation( reverse );
- expect( doc.version ).to.be.equal( 2 );
- expect( doc.root.getChildCount() ).to.be.equal( 3 );
- expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
- expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 1 ).hasAttr( fooAttr ) ).to.be.true;
- expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 1 );
- expect( doc.root.getChild( 2 ).hasAttr( fooAttr ) ).to.be.true;
- } );
- it( 'should throw an error when one try to remove and the attribute does not exists', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Attribute = modules[ 'document/attribute' ];
- var CKEditorError = modules.ckeditorerror;
- var doc = new Document();
- var fooAttr = new Attribute( 'foo', true );
- doc.root.insertChildren( 0, 'x' );
- expect( function() {
- doc.applyOperation( new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
- fooAttr,
- null,
- doc.version ) );
- } ).to.throw( CKEditorError, /operation-change-no-attr-to-remove/ );
- } );
- it( 'should throw an error when one try to insert and the attribute already exists', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Character = modules[ 'document/character' ];
- var Attribute = modules[ 'document/attribute' ];
- var CKEditorError = modules.ckeditorerror;
- var doc = new Document();
- var x1Attr = new Attribute( 'x', 1 );
- var x2Attr = new Attribute( 'x', 2 );
- doc.root.insertChildren( 0, new Character( 'x', [ x1Attr ] ) );
- expect( function() {
- doc.applyOperation( new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
- null,
- x2Attr,
- doc.version ) );
- } ).to.throw( CKEditorError, /operation-change-attr-exists/ );
- } );
- it( 'should throw an error when one try to change and the new and old attributes have different keys', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Attribute = modules[ 'document/attribute' ];
- var CKEditorError = modules.ckeditorerror;
- var doc = new Document();
- var fooAttr = new Attribute( 'foo', true );
- var barAttr = new Attribute( 'bar', true );
- doc.root.insertChildren( 0, 'x' );
- expect( function() {
- doc.applyOperation( new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
- fooAttr,
- barAttr,
- doc.version ) );
- } ).to.throw( CKEditorError, /operation-change-different-keys/ );
- } );
- it( 'should throw an error when one try to change and the old attribute does not exists', function() {
- var Document = modules[ 'document/document' ];
- var ChangeOperation = modules[ 'document/changeoperation' ];
- var Position = modules[ 'document/position' ];
- var Range = modules[ 'document/range' ];
- var Attribute = modules[ 'document/attribute' ];
- var CKEditorError = modules.ckeditorerror;
- var doc = new Document();
- var x1Attr = new Attribute( 'x', 1 );
- var x2Attr = new Attribute( 'x', 2 );
- doc.root.insertChildren( 0, 'x' );
- expect( function() {
- doc.applyOperation( new ChangeOperation(
- new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
- x1Attr,
- x2Attr,
- doc.version ) );
- } ).to.throw( CKEditorError, /operation-change-no-attr-to-change/ );
- } );
- } );
|