rootattributeoperation.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 RootAttributeOperation from '../../../src/model/operation/rootattributeoperation';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import { jsonParseStringify, wrapInDelta } from '../../../tests/model/_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. describe( 'isDocumentOperation', () => {
  50. it( 'should be true when root is in the document', () => {
  51. const operation = new RootAttributeOperation(
  52. root,
  53. 'isNew',
  54. null,
  55. true,
  56. doc.version
  57. );
  58. expect( operation.isDocumentOperation ).to.true;
  59. } );
  60. it( 'should be false when root is not in the document', () => {
  61. const element = new Element( 'element' );
  62. const operation = new RootAttributeOperation(
  63. element,
  64. 'isNew',
  65. null,
  66. true,
  67. doc.version
  68. );
  69. expect( operation.isDocumentOperation ).to.false;
  70. } );
  71. } );
  72. it( 'should add attribute on the root element', () => {
  73. model.applyOperation( wrapInDelta(
  74. new RootAttributeOperation(
  75. root,
  76. 'isNew',
  77. null,
  78. true,
  79. doc.version
  80. )
  81. ) );
  82. expect( doc.version ).to.equal( 1 );
  83. expect( root.hasAttribute( 'isNew' ) ).to.be.true;
  84. } );
  85. it( 'should change attribute on the root element', () => {
  86. root.setAttribute( 'isNew', false );
  87. model.applyOperation( wrapInDelta(
  88. new RootAttributeOperation(
  89. root,
  90. 'isNew',
  91. false,
  92. true,
  93. doc.version
  94. )
  95. ) );
  96. expect( doc.version ).to.equal( 1 );
  97. expect( root.getAttribute( 'isNew' ) ).to.be.true;
  98. } );
  99. it( 'should remove attribute from the root element', () => {
  100. root.setAttribute( 'x', true );
  101. model.applyOperation( wrapInDelta(
  102. new RootAttributeOperation(
  103. root,
  104. 'x',
  105. true,
  106. null,
  107. doc.version
  108. )
  109. ) );
  110. expect( doc.version ).to.equal( 1 );
  111. expect( root.hasAttribute( 'x' ) ).to.be.false;
  112. } );
  113. it( 'should create a RootAttributeOperation as a reverse', () => {
  114. const operation = new RootAttributeOperation( root, 'x', 'old', 'new', doc.version );
  115. const reverse = operation.getReversed();
  116. expect( reverse ).to.be.an.instanceof( RootAttributeOperation );
  117. expect( reverse.baseVersion ).to.equal( 1 );
  118. expect( reverse.root ).to.equal( root );
  119. expect( reverse.key ).to.equal( 'x' );
  120. expect( reverse.oldValue ).to.equal( 'new' );
  121. expect( reverse.newValue ).to.equal( 'old' );
  122. } );
  123. it( 'should undo adding attribute by applying reverse operation', () => {
  124. const operation = new RootAttributeOperation(
  125. root,
  126. 'isNew',
  127. null,
  128. true,
  129. doc.version
  130. );
  131. const reverse = operation.getReversed();
  132. model.applyOperation( wrapInDelta( operation ) );
  133. model.applyOperation( wrapInDelta( reverse ) );
  134. expect( doc.version ).to.equal( 2 );
  135. expect( root.hasAttribute( 'x' ) ).to.be.false;
  136. } );
  137. it( 'should undo changing attribute by applying reverse operation', () => {
  138. root.setAttribute( 'isNew', false );
  139. const operation = new RootAttributeOperation(
  140. root,
  141. 'isNew',
  142. false,
  143. true,
  144. doc.version
  145. );
  146. const reverse = operation.getReversed();
  147. model.applyOperation( wrapInDelta( operation ) );
  148. model.applyOperation( wrapInDelta( reverse ) );
  149. expect( doc.version ).to.equal( 2 );
  150. expect( root.getAttribute( 'isNew' ) ).to.be.false;
  151. } );
  152. it( 'should undo remove attribute by applying reverse operation', () => {
  153. root.setAttribute( 'foo', true );
  154. const operation = new RootAttributeOperation(
  155. root,
  156. 'foo',
  157. true,
  158. null,
  159. doc.version
  160. );
  161. const reverse = operation.getReversed();
  162. model.applyOperation( wrapInDelta( operation ) );
  163. model.applyOperation( wrapInDelta( reverse ) );
  164. expect( doc.version ).to.equal( 2 );
  165. expect( root.getAttribute( 'foo' ) ).to.be.true;
  166. } );
  167. describe( '_validate()', () => {
  168. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  169. expect( () => {
  170. const op = new RootAttributeOperation(
  171. root,
  172. 'foo',
  173. true,
  174. null,
  175. doc.version
  176. );
  177. op._validate();
  178. } ).to.throw( CKEditorError, /rootattribute-operation-wrong-old-value/ );
  179. } );
  180. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  181. root.setAttribute( 'x', 1 );
  182. expect( () => {
  183. const op = new RootAttributeOperation(
  184. root,
  185. 'x',
  186. null,
  187. 2,
  188. doc.version
  189. );
  190. op._validate();
  191. } ).to.throw( CKEditorError, /rootattribute-operation-attribute-exists/ );
  192. } );
  193. } );
  194. it( 'should create a RootAttributeOperation with the same parameters when cloned', () => {
  195. const baseVersion = doc.version;
  196. const op = new RootAttributeOperation( root, 'foo', 'old', 'new', baseVersion );
  197. const clone = op.clone();
  198. // New instance rather than a pointer to the old instance.
  199. expect( clone ).not.to.be.equal( op );
  200. expect( clone ).to.be.instanceof( RootAttributeOperation );
  201. expect( clone.root ).to.equal( root );
  202. expect( clone.key ).to.equal( 'foo' );
  203. expect( clone.oldValue ).to.equal( 'old' );
  204. expect( clone.newValue ).to.equal( 'new' );
  205. expect( clone.baseVersion ).to.equal( baseVersion );
  206. } );
  207. describe( 'toJSON', () => {
  208. it( 'should create proper serialized object', () => {
  209. const op = new RootAttributeOperation(
  210. root,
  211. 'key',
  212. null,
  213. 'newValue',
  214. doc.version
  215. );
  216. const serialized = jsonParseStringify( op );
  217. expect( serialized.__className ).to.equal( 'engine.model.operation.RootAttributeOperation' );
  218. expect( serialized ).to.deep.equal( {
  219. __className: 'engine.model.operation.RootAttributeOperation',
  220. baseVersion: 0,
  221. key: 'key',
  222. newValue: 'newValue',
  223. oldValue: null,
  224. root: 'main'
  225. } );
  226. } );
  227. } );
  228. describe( 'fromJSON', () => {
  229. it( 'should create proper RootAttributeOperation from json object', () => {
  230. const op = new RootAttributeOperation( root, 'key', null, 'newValue', doc.version );
  231. const serialized = jsonParseStringify( op );
  232. const deserialized = RootAttributeOperation.fromJSON( serialized, doc );
  233. expect( deserialized ).to.deep.equal( op );
  234. } );
  235. it( 'should throw an error when root does not exists', () => {
  236. const op = new RootAttributeOperation(
  237. root,
  238. 'key',
  239. null,
  240. 'newValue',
  241. doc.version
  242. );
  243. const serialized = jsonParseStringify( op );
  244. serialized.root = 'no-root';
  245. expect( () => {
  246. RootAttributeOperation.fromJSON( serialized, doc );
  247. } ).to.throw( CKEditorError, /rootattribute-operation-fromjson-no-root/ );
  248. } );
  249. } );
  250. } );