rootattributeoperation.js 7.4 KB

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