movedelta.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, delta */
  6. 'use strict';
  7. const modules = bender.amd.require(
  8. 'treemodel/document',
  9. 'treemodel/position',
  10. 'treemodel/range',
  11. 'treemodel/element'
  12. );
  13. describe( 'Batch', () => {
  14. let Document, Position, Range, Element;
  15. let doc, root, div, p, batch, chain;
  16. before( () => {
  17. Document = modules[ 'treemodel/document' ];
  18. Position = modules[ 'treemodel/position' ];
  19. Range = modules[ 'treemodel/range' ];
  20. Element = modules[ 'treemodel/element' ];
  21. } );
  22. beforeEach( () => {
  23. doc = new Document();
  24. root = doc.createRoot( 'root' );
  25. div = new Element( 'div', [], 'foobar' );
  26. p = new Element( 'p', [], 'abcxyz' );
  27. root.insertChildren( 0, [ div, p ] );
  28. batch = doc.batch();
  29. } );
  30. function getNodesAndText( element ) {
  31. let range = Range.createFromElement( element );
  32. let txt = '';
  33. for ( let step of range ) {
  34. let node = step.node;
  35. if ( node.character ) {
  36. txt += node.character.toLowerCase();
  37. } else if ( node.name ) {
  38. txt += node.name.toUpperCase();
  39. }
  40. }
  41. return txt;
  42. }
  43. describe( 'moveNode', () => {
  44. beforeEach( () => {
  45. chain = batch.moveNode( div, new Position( root, [ 2 ] ) );
  46. } );
  47. it( 'should move specified node', () => {
  48. expect( root.getChildCount() ).to.equal( 2 );
  49. expect( getNodesAndText( root.getChild( 0 ) ) ).to.equal( 'abcxyz' );
  50. expect( getNodesAndText( root.getChild( 1 ) ) ).to.equal( 'foobar' );
  51. } );
  52. it( 'should be chainable', () => {
  53. expect( chain ).to.equal( batch );
  54. } );
  55. } );
  56. describe( 'move', () => {
  57. beforeEach( () => {
  58. div.insertChildren( 4, [ new Element( 'p', [], 'gggg' ) ] );
  59. div.insertChildren( 2, [ new Element( 'p', [], 'hhhh' ) ] );
  60. // Range starts in ROOT > DIV > P > gg|gg.
  61. // Range ends in ROOT > DIV > ...|ar.
  62. let range = new Range( new Position( root, [ 0, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
  63. chain = batch.move( range, new Position( root, [ 1, 3 ] ) );
  64. } );
  65. it( 'should move any range of nodes', () => {
  66. expect( getNodesAndText( root.getChild( 0 ) ) ).to.equal( 'foPhhPar' );
  67. expect( getNodesAndText( root.getChild( 1 ) ) ).to.equal( 'abchhobPggggPxyz' );
  68. } );
  69. it( 'should create minimal number of operations', () => {
  70. expect( batch.deltas.length ).to.equal( 1 );
  71. expect( batch.deltas[ 0 ].operations.length ).to.equal( 2 );
  72. } );
  73. it( 'should be chainable', () => {
  74. expect( chain ).to.equal( batch );
  75. } );
  76. } );
  77. } );