wrapoperation.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 Element from '../../../src/model/element';
  7. import Text from '../../../src/model/text';
  8. import WrapOperation from '../../../src/model/operation/wrapoperation';
  9. import UnwrapOperation from '../../../src/model/operation/unwrapoperation';
  10. import Position from '../../../src/model/position';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. describe( 'WrapOperation', () => {
  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 wrap = new WrapOperation( new Position( root, [ 1 ] ), 2, new Element( 'paragraph' ), 1 );
  23. expect( wrap.type ).to.equal( 'wrap' );
  24. } );
  25. it( 'should have proper targetPosition', () => {
  26. const wrap = new WrapOperation( new Position( root, [ 1 ] ), 2, new Element( 'paragraph' ), 1 );
  27. expect( wrap.targetPosition.path ).to.deep.equal( [ 1, 0 ] );
  28. } );
  29. it( 'should have proper wrappedRange', () => {
  30. const wrap = new WrapOperation( new Position( root, [ 1 ] ), 2, new Element( 'paragraph' ), 1 );
  31. expect( wrap.wrappedRange.start.path ).to.deep.equal( [ 1 ] );
  32. expect( wrap.wrappedRange.end.path ).to.deep.equal( [ 3 ] );
  33. } );
  34. it( 'should wrap nodes into an element', () => {
  35. root._insertChild( 0, [
  36. new Element( 'paragraph', null, new Text( 'Foo' ) ),
  37. new Element( 'listItem', null, new Text( 'bar' ) )
  38. ] );
  39. const operation = new WrapOperation( new Position( root, [ 0 ] ), 2, new Element( 'blockQuote' ), doc.version );
  40. model.applyOperation( operation );
  41. expect( doc.version ).to.equal( 1 );
  42. expect( root.maxOffset ).to.equal( 1 );
  43. expect( root.getChild( 0 ).name ).to.equal( 'blockQuote' );
  44. expect( root.getChild( 0 ).maxOffset ).to.equal( 2 );
  45. expect( root.getChild( 0 ).getChild( 0 ).name ).to.equal( 'paragraph' );
  46. expect( root.getChild( 0 ).getChild( 1 ).name ).to.equal( 'listItem' );
  47. } );
  48. it( 'should wrap nodes into an element from graveyard', () => {
  49. root._insertChild( 0, [
  50. new Element( 'paragraph', null, new Text( 'Foo' ) ),
  51. new Element( 'listItem', null, new Text( 'bar' ) )
  52. ] );
  53. gy._insertChild( 0, [ new Element( 'blockQuote' ) ] );
  54. const operation = new WrapOperation( new Position( root, [ 0 ] ), 2, gyPos, doc.version );
  55. model.applyOperation( operation );
  56. expect( doc.version ).to.equal( 1 );
  57. expect( root.maxOffset ).to.equal( 1 );
  58. expect( root.getChild( 0 ).name ).to.equal( 'blockQuote' );
  59. expect( root.getChild( 0 ).maxOffset ).to.equal( 2 );
  60. expect( root.getChild( 0 ).getChild( 0 ).name ).to.equal( 'paragraph' );
  61. expect( root.getChild( 0 ).getChild( 1 ).name ).to.equal( 'listItem' );
  62. expect( gy.maxOffset ).to.equal( 0 );
  63. } );
  64. it( 'should create a proper UnwrapOperation as a reverse', () => {
  65. const operation = new WrapOperation( new Position( root, [ 1 ] ), 2, new Element( 'blockQuote' ), doc.version );
  66. const reverse = operation.getReversed();
  67. expect( reverse ).to.be.an.instanceof( UnwrapOperation );
  68. expect( reverse.baseVersion ).to.equal( 1 );
  69. expect( reverse.position.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  70. expect( reverse.howMany ).to.equal( 2 );
  71. expect( reverse.graveyardPosition.isEqual( gyPos ) ).to.be.true;
  72. } );
  73. it( 'should undo wrap by applying reverse operation', () => {
  74. root._insertChild( 0, [
  75. new Element( 'paragraph', null, new Text( 'Foo' ) ),
  76. new Element( 'listItem', null, new Text( 'bar' ) )
  77. ] );
  78. const operation = new WrapOperation( new Position( root, [ 0 ] ), 2, new Element( 'blockQuote' ), doc.version );
  79. model.applyOperation( operation );
  80. model.applyOperation( operation.getReversed() );
  81. expect( doc.version ).to.equal( 2 );
  82. expect( root.maxOffset ).to.equal( 2 );
  83. expect( root.getChild( 0 ).name ).to.equal( 'paragraph' );
  84. expect( root.getChild( 1 ).name ).to.equal( 'listItem' );
  85. } );
  86. describe( '_validate()', () => {
  87. it( 'should throw an error if position is invalid', () => {
  88. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  89. root._insertChild( 0, [ p1 ] );
  90. const operation = new WrapOperation(
  91. new Position( root, [ 4 ] ),
  92. 3,
  93. new Element( 'blockQuote' ),
  94. doc.version
  95. );
  96. expect( () => operation._validate() ).to.throw( CKEditorError, /wrap-operation-position-invalid/ );
  97. } );
  98. it( 'should throw an error if number of nodes to wrap is invalid', () => {
  99. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  100. root._insertChild( 0, [ p1 ] );
  101. const operation = new WrapOperation(
  102. new Position( root, [ 0 ] ),
  103. 5,
  104. new Element( 'blockQuote' ),
  105. doc.version
  106. );
  107. expect( () => operation._validate() ).to.throw( CKEditorError, /wrap-operation-how-many-invalid/ );
  108. } );
  109. it( 'should throw an error if graveyard position is invalid', () => {
  110. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  111. root._insertChild( 0, [ p1 ] );
  112. const operation = new WrapOperation(
  113. new Position( root, [ 0 ] ),
  114. 1,
  115. gyPos,
  116. doc.version
  117. );
  118. expect( () => operation._validate() ).to.throw( CKEditorError, /wrap-operation-graveyard-position-invalid/ );
  119. } );
  120. } );
  121. it( 'should create WrapOperation with the same parameters when cloned #1', () => {
  122. const position = new Position( root, [ 1, 0 ] );
  123. const howMany = 4;
  124. const baseVersion = doc.version;
  125. const element = new Element( 'blockQuote' );
  126. const op = new WrapOperation( position, howMany, element, baseVersion );
  127. const clone = op.clone();
  128. // New instance rather than a pointer to the old instance.
  129. expect( clone ).not.to.be.equal( op );
  130. expect( clone ).to.be.instanceof( WrapOperation );
  131. expect( clone.position.isEqual( position ) ).to.be.true;
  132. expect( clone.element ).not.to.equal( element );
  133. expect( clone.element ).to.deep.equal( element );
  134. expect( clone.graveyardPosition ).to.be.null;
  135. expect( clone.howMany ).to.equal( howMany );
  136. expect( clone.baseVersion ).to.equal( baseVersion );
  137. } );
  138. it( 'should create WrapOperation with the same parameters when cloned #2', () => {
  139. const position = new Position( root, [ 1, 0 ] );
  140. const howMany = 4;
  141. const baseVersion = doc.version;
  142. const op = new WrapOperation( position, howMany, gyPos, baseVersion );
  143. const clone = op.clone();
  144. expect( clone.position.isEqual( position ) ).to.be.true;
  145. expect( clone.graveyardPosition.isEqual( gyPos ) ).to.be.true;
  146. expect( clone.element ).to.be.null;
  147. expect( clone.howMany ).to.equal( howMany );
  148. expect( clone.baseVersion ).to.equal( baseVersion );
  149. } );
  150. describe( 'toJSON', () => {
  151. it( 'should create proper serialized object #1', () => {
  152. const op = new WrapOperation(
  153. new Position( root, [ 0 ] ),
  154. 1,
  155. new Position( doc.graveyard, [ 0 ] ),
  156. doc.version
  157. );
  158. const serialized = op.toJSON();
  159. expect( serialized ).to.deep.equal( {
  160. __className: 'WrapOperation',
  161. baseVersion: 0,
  162. position: op.position.toJSON(),
  163. graveyardPosition: op.graveyardPosition.toJSON(),
  164. howMany: 1
  165. } );
  166. } );
  167. it( 'should create proper serialized object #2', () => {
  168. const op = new WrapOperation(
  169. new Position( root, [ 0 ] ),
  170. 1,
  171. new Element( 'paragraph' ),
  172. doc.version
  173. );
  174. const serialized = op.toJSON();
  175. expect( serialized ).to.deep.equal( {
  176. __className: 'WrapOperation',
  177. baseVersion: 0,
  178. position: op.position.toJSON(),
  179. element: op.element.toJSON(),
  180. howMany: 1
  181. } );
  182. } );
  183. } );
  184. describe( 'fromJSON', () => {
  185. it( 'should create proper WrapOperation from json object #1', () => {
  186. const op = new WrapOperation(
  187. new Position( root, [ 0 ] ),
  188. 1,
  189. new Position( doc.graveyard, [ 0 ] ),
  190. doc.version
  191. );
  192. const serialized = op.toJSON();
  193. const deserialized = WrapOperation.fromJSON( serialized, doc );
  194. expect( deserialized ).to.deep.equal( op );
  195. } );
  196. it( 'should create proper WrapOperation from json object #2', () => {
  197. const op = new WrapOperation(
  198. new Position( root, [ 0 ] ),
  199. 1,
  200. new Element( 'blockQuote' ),
  201. doc.version
  202. );
  203. const serialized = op.toJSON();
  204. const deserialized = WrapOperation.fromJSON( serialized, doc );
  205. expect( deserialized ).to.deep.equal( op );
  206. } );
  207. } );
  208. } );