unwrapoperation.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  6. import UnwrapOperation from '../../../src/model/operation/unwrapoperation';
  7. import WrapOperation from '../../../src/model/operation/wrapoperation';
  8. import Position from '../../../src/model/position';
  9. import Element from '../../../src/model/element';
  10. import Text from '../../../src/model/text';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. describe( 'UnwrapOperation', () => {
  13. let model, doc, root, gy, gyPos;
  14. beforeEach( () => {
  15. model = new Model();
  16. doc = model.document;
  17. root = doc.createRoot();
  18. gy = doc.graveyard;
  19. gyPos = new Position( gy, [ 0 ] );
  20. } );
  21. it( 'should have proper type', () => {
  22. const unwrap = new UnwrapOperation( new Position( root, [ 1, 0 ] ), 2, gyPos, 1 );
  23. expect( unwrap.type ).to.equal( 'unwrap' );
  24. } );
  25. it( 'should have proper targetPosition', () => {
  26. const unwrap = new UnwrapOperation( new Position( root, [ 1, 0 ] ), 2, gyPos, 1 );
  27. expect( unwrap.targetPosition.path ).to.deep.equal( [ 1 ] );
  28. } );
  29. it( 'should have proper unwrappedRange', () => {
  30. const unwrap = new UnwrapOperation( new Position( root, [ 1, 0 ] ), 2, gyPos, 1 );
  31. expect( unwrap.unwrappedRange.start.path ).to.deep.equal( [ 1, 0 ] );
  32. expect( unwrap.unwrappedRange.end.path ).to.deep.equal( [ 1, 2 ] );
  33. } );
  34. it( 'should unwrap an element', () => {
  35. const bq = new Element( 'blockQuote', null, [
  36. new Element( 'paragraph', null, new Text( 'Foo' ) ),
  37. new Element( 'listItem', null, new Text( 'bar' ) )
  38. ] );
  39. root._insertChild( 0, [ bq ] );
  40. const operation = new UnwrapOperation( new Position( root, [ 0, 0 ] ), 2, gyPos, doc.version );
  41. model.applyOperation( operation );
  42. expect( doc.version ).to.equal( 1 );
  43. expect( root.maxOffset ).to.equal( 2 );
  44. expect( root.getChild( 0 ).name ).to.equal( 'paragraph' );
  45. expect( root.getChild( 1 ).name ).to.equal( 'listItem' );
  46. } );
  47. it( 'should create a proper WrapOperation as a reverse', () => {
  48. const operation = new UnwrapOperation( new Position( root, [ 1, 0 ] ), 2, gyPos, doc.version );
  49. const reverse = operation.getReversed();
  50. expect( reverse ).to.be.an.instanceof( WrapOperation );
  51. expect( reverse.baseVersion ).to.equal( 1 );
  52. expect( reverse.position.isEqual( new Position( root, [ 1 ] ) ) ).to.be.true;
  53. expect( reverse.howMany ).to.equal( 2 );
  54. expect( reverse.element ).to.be.null;
  55. expect( reverse.graveyardPosition.isEqual( gyPos ) ).to.be.true;
  56. } );
  57. it( 'should undo unwrap by applying reverse operation', () => {
  58. const bq = new Element( 'blockQuote', null, [
  59. new Element( 'paragraph', null, new Text( 'Foo' ) ),
  60. new Element( 'listItem', null, new Text( 'bar' ) )
  61. ] );
  62. root._insertChild( 0, [ bq ] );
  63. const operation = new UnwrapOperation( new Position( root, [ 0, 0 ] ), 2, gyPos, doc.version );
  64. model.applyOperation( operation );
  65. model.applyOperation( operation.getReversed() );
  66. expect( doc.version ).to.equal( 2 );
  67. expect( root.maxOffset ).to.equal( 1 );
  68. expect( root.getChild( 0 ).name ).to.equal( 'blockQuote' );
  69. expect( root.getChild( 0 ).maxOffset ).to.equal( 2 );
  70. } );
  71. describe( '_validate()', () => {
  72. it( 'should throw an error if position is invalid', () => {
  73. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  74. root._insertChild( 0, [ p1 ] );
  75. const operation = new UnwrapOperation(
  76. new Position( root, [ 1, 0 ] ),
  77. 3,
  78. gyPos,
  79. doc.version
  80. );
  81. expect( () => operation._validate() ).to.throw( CKEditorError, /unwrap-operation-position-invalid/ );
  82. } );
  83. it( 'should throw an error if number of nodes to unwrap is invalid', () => {
  84. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  85. root._insertChild( 0, [ p1 ] );
  86. const operation = new UnwrapOperation(
  87. new Position( root, [ 0, 0 ] ),
  88. 5,
  89. gyPos,
  90. doc.version
  91. );
  92. expect( () => operation._validate() ).to.throw( CKEditorError, /unwrap-operation-how-many-invalid/ );
  93. } );
  94. } );
  95. it( 'should create UnwrapOperation with the same parameters when cloned', () => {
  96. const position = new Position( root, [ 1, 0 ] );
  97. const howMany = 4;
  98. const baseVersion = doc.version;
  99. const op = new UnwrapOperation( position, howMany, gyPos, baseVersion );
  100. const clone = op.clone();
  101. // New instance rather than a pointer to the old instance.
  102. expect( clone ).not.to.be.equal( op );
  103. expect( clone ).to.be.instanceof( UnwrapOperation );
  104. expect( clone.position.isEqual( position ) ).to.be.true;
  105. expect( clone.howMany ).to.equal( howMany );
  106. expect( clone.graveyardPosition.isEqual( gyPos ) ).to.be.true;
  107. expect( clone.baseVersion ).to.equal( baseVersion );
  108. } );
  109. describe( 'toJSON', () => {
  110. it( 'should create proper json object', () => {
  111. const position = new Position( root, [ 1, 0 ] );
  112. const op = new UnwrapOperation( position, 4, gyPos, doc.version );
  113. const serialized = op.toJSON();
  114. expect( serialized ).to.deep.equal( {
  115. __className: 'UnwrapOperation',
  116. baseVersion: 0,
  117. howMany: 4,
  118. position: op.position.toJSON(),
  119. graveyardPosition: gyPos.toJSON()
  120. } );
  121. } );
  122. } );
  123. describe( 'fromJSON', () => {
  124. it( 'should create proper UnwrapOperation from json object', () => {
  125. const position = new Position( root, [ 1, 0 ] );
  126. const op = new UnwrapOperation( position, 4, gyPos, doc.version );
  127. const serialized = op.toJSON();
  128. const deserialized = UnwrapOperation.fromJSON( serialized, doc );
  129. expect( deserialized ).to.deep.equal( op );
  130. } );
  131. } );
  132. } );