rootattributeoperation.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 DocumentFragment from '../../../src/model/documentfragment';
  7. import Element from '../../../src/model/element';
  8. import RootAttributeOperation from '../../../src/model/operation/rootattributeoperation';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import { jsonParseStringify, wrapInDelta } from '../../../tests/model/_utils/utils';
  11. describe( 'RootAttributeOperation', () => {
  12. let model, doc, root;
  13. beforeEach( () => {
  14. model = new Model();
  15. doc = model.document;
  16. root = doc.createRoot();
  17. } );
  18. describe( 'type', () => {
  19. it( 'should be addRootAttribute for adding attribute', () => {
  20. const op = new RootAttributeOperation(
  21. root,
  22. 'key',
  23. null,
  24. 'newValue',
  25. doc.version
  26. );
  27. expect( op.type ).to.equal( 'addRootAttribute' );
  28. } );
  29. it( 'should be removeRootAttribute for removing attribute', () => {
  30. const op = new RootAttributeOperation(
  31. root,
  32. 'key',
  33. 'oldValue',
  34. null,
  35. doc.version
  36. );
  37. expect( op.type ).to.equal( 'removeRootAttribute' );
  38. } );
  39. it( 'should be changeRootAttribute for removing attribute', () => {
  40. const op = new RootAttributeOperation(
  41. root,
  42. 'key',
  43. 'oldValue',
  44. 'newValue',
  45. doc.version
  46. );
  47. expect( op.type ).to.equal( 'changeRootAttribute' );
  48. } );
  49. } );
  50. it( 'should add attribute on the root element', () => {
  51. model.applyOperation( wrapInDelta(
  52. new RootAttributeOperation(
  53. root,
  54. 'isNew',
  55. null,
  56. true,
  57. doc.version
  58. )
  59. ) );
  60. expect( doc.version ).to.equal( 1 );
  61. expect( root.hasAttribute( 'isNew' ) ).to.be.true;
  62. } );
  63. it( 'should change attribute on the root element', () => {
  64. root._setAttribute( 'isNew', false );
  65. model.applyOperation( wrapInDelta(
  66. new RootAttributeOperation(
  67. root,
  68. 'isNew',
  69. false,
  70. true,
  71. doc.version
  72. )
  73. ) );
  74. expect( doc.version ).to.equal( 1 );
  75. expect( root.getAttribute( 'isNew' ) ).to.be.true;
  76. } );
  77. it( 'should remove attribute from the root element', () => {
  78. root._setAttribute( 'x', true );
  79. model.applyOperation( wrapInDelta(
  80. new RootAttributeOperation(
  81. root,
  82. 'x',
  83. true,
  84. null,
  85. doc.version
  86. )
  87. ) );
  88. expect( doc.version ).to.equal( 1 );
  89. expect( root.hasAttribute( 'x' ) ).to.be.false;
  90. } );
  91. it( 'should create a RootAttributeOperation as a reverse', () => {
  92. const operation = new RootAttributeOperation( root, 'x', 'old', 'new', doc.version );
  93. const reverse = operation.getReversed();
  94. expect( reverse ).to.be.an.instanceof( RootAttributeOperation );
  95. expect( reverse.baseVersion ).to.equal( 1 );
  96. expect( reverse.root ).to.equal( root );
  97. expect( reverse.key ).to.equal( 'x' );
  98. expect( reverse.oldValue ).to.equal( 'new' );
  99. expect( reverse.newValue ).to.equal( 'old' );
  100. } );
  101. it( 'should undo adding attribute by applying reverse operation', () => {
  102. const operation = new RootAttributeOperation(
  103. root,
  104. 'isNew',
  105. null,
  106. true,
  107. doc.version
  108. );
  109. const reverse = operation.getReversed();
  110. model.applyOperation( wrapInDelta( operation ) );
  111. model.applyOperation( wrapInDelta( reverse ) );
  112. expect( doc.version ).to.equal( 2 );
  113. expect( root.hasAttribute( 'x' ) ).to.be.false;
  114. } );
  115. it( 'should undo changing attribute by applying reverse operation', () => {
  116. root._setAttribute( 'isNew', false );
  117. const operation = new RootAttributeOperation(
  118. root,
  119. 'isNew',
  120. false,
  121. true,
  122. doc.version
  123. );
  124. const reverse = operation.getReversed();
  125. model.applyOperation( wrapInDelta( operation ) );
  126. model.applyOperation( wrapInDelta( reverse ) );
  127. expect( doc.version ).to.equal( 2 );
  128. expect( root.getAttribute( 'isNew' ) ).to.be.false;
  129. } );
  130. it( 'should undo remove attribute by applying reverse operation', () => {
  131. root._setAttribute( 'foo', true );
  132. const operation = new RootAttributeOperation(
  133. root,
  134. 'foo',
  135. true,
  136. null,
  137. doc.version
  138. );
  139. const reverse = operation.getReversed();
  140. model.applyOperation( wrapInDelta( operation ) );
  141. model.applyOperation( wrapInDelta( reverse ) );
  142. expect( doc.version ).to.equal( 2 );
  143. expect( root.getAttribute( 'foo' ) ).to.be.true;
  144. } );
  145. describe( '_validate()', () => {
  146. it( 'should throw an error when trying to change non-root element', () => {
  147. const child = new Element( 'p' );
  148. const parent = new Element( 'p' );
  149. parent._appendChild( child );
  150. expect( () => {
  151. const op = new RootAttributeOperation(
  152. child,
  153. 'foo',
  154. null,
  155. 'bar',
  156. null
  157. );
  158. op._validate();
  159. } ).to.throw( CKEditorError, /rootattribute-operation-not-a-root/ );
  160. } );
  161. it( 'should throw an error when trying to change document fragment', () => {
  162. expect( () => {
  163. const op = new RootAttributeOperation(
  164. new DocumentFragment(),
  165. 'foo',
  166. null,
  167. 'bar',
  168. null
  169. );
  170. op._validate();
  171. } ).to.throw( CKEditorError, /rootattribute-operation-not-a-root/ );
  172. } );
  173. it( 'should throw an error when trying to remove an attribute that does not exists', () => {
  174. expect( () => {
  175. const op = new RootAttributeOperation(
  176. root,
  177. 'foo',
  178. true,
  179. null,
  180. doc.version
  181. );
  182. op._validate();
  183. } ).to.throw( CKEditorError, /rootattribute-operation-wrong-old-value/ );
  184. } );
  185. it( 'should throw an error when trying to add an attribute that already exists', () => {
  186. root._setAttribute( 'x', 1 );
  187. expect( () => {
  188. const op = new RootAttributeOperation(
  189. root,
  190. 'x',
  191. null,
  192. 2,
  193. doc.version
  194. );
  195. op._validate();
  196. } ).to.throw( CKEditorError, /rootattribute-operation-attribute-exists/ );
  197. } );
  198. } );
  199. it( 'should create a RootAttributeOperation with the same parameters when cloned', () => {
  200. const baseVersion = doc.version;
  201. const op = new RootAttributeOperation( root, 'foo', 'old', 'new', baseVersion );
  202. const clone = op.clone();
  203. // New instance rather than a pointer to the old instance.
  204. expect( clone ).not.to.be.equal( op );
  205. expect( clone ).to.be.instanceof( RootAttributeOperation );
  206. expect( clone.root ).to.equal( root );
  207. expect( clone.key ).to.equal( 'foo' );
  208. expect( clone.oldValue ).to.equal( 'old' );
  209. expect( clone.newValue ).to.equal( 'new' );
  210. expect( clone.baseVersion ).to.equal( baseVersion );
  211. } );
  212. describe( 'toJSON', () => {
  213. it( 'should create proper serialized object', () => {
  214. const op = new RootAttributeOperation(
  215. root,
  216. 'key',
  217. null,
  218. 'newValue',
  219. doc.version
  220. );
  221. const serialized = jsonParseStringify( op );
  222. expect( serialized.__className ).to.equal( 'engine.model.operation.RootAttributeOperation' );
  223. expect( serialized ).to.deep.equal( {
  224. __className: 'engine.model.operation.RootAttributeOperation',
  225. baseVersion: 0,
  226. key: 'key',
  227. newValue: 'newValue',
  228. oldValue: null,
  229. root: 'main'
  230. } );
  231. } );
  232. } );
  233. describe( 'fromJSON', () => {
  234. it( 'should create proper RootAttributeOperation from json object', () => {
  235. const op = new RootAttributeOperation( root, 'key', null, 'newValue', doc.version );
  236. const serialized = jsonParseStringify( op );
  237. const deserialized = RootAttributeOperation.fromJSON( serialized, doc );
  238. expect( deserialized ).to.deep.equal( op );
  239. } );
  240. it( 'should throw an error when root does not exists', () => {
  241. const op = new RootAttributeOperation(
  242. root,
  243. 'key',
  244. null,
  245. 'newValue',
  246. doc.version
  247. );
  248. const serialized = jsonParseStringify( op );
  249. serialized.root = 'no-root';
  250. expect( () => {
  251. RootAttributeOperation.fromJSON( serialized, doc );
  252. } ).to.throw( CKEditorError, /rootattribute-operation-fromjson-no-root/ );
  253. } );
  254. } );
  255. } );