8
0

rootattributeoperation.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model, operation */
  6. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import RootAttributeOperation from '/ckeditor5/engine/model/operation/rootattributeoperation.js';
  9. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  10. import { jsonParseStringify, wrapInDelta } from '/tests/engine/model/_utils/utils.js';
  11. describe( 'RootAttributeOperation', () => {
  12. let doc, root;
  13. beforeEach( () => {
  14. doc = new 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. doc.applyOperation( wrapInDelta(
  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. doc.applyOperation( wrapInDelta(
  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. doc.applyOperation( wrapInDelta(
  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. let operation = new RootAttributeOperation( root, 'x', 'old', 'new', doc.version );
  92. let 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. let operation = new RootAttributeOperation(
  102. root,
  103. 'isNew',
  104. null,
  105. true,
  106. doc.version
  107. );
  108. let reverse = operation.getReversed();
  109. doc.applyOperation( wrapInDelta( operation ) );
  110. doc.applyOperation( wrapInDelta( 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. let operation = new RootAttributeOperation(
  117. root,
  118. 'isNew',
  119. false,
  120. true,
  121. doc.version
  122. );
  123. let reverse = operation.getReversed();
  124. doc.applyOperation( wrapInDelta( operation ) );
  125. doc.applyOperation( wrapInDelta( 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. let operation = new RootAttributeOperation(
  132. root,
  133. 'foo',
  134. true,
  135. null,
  136. doc.version
  137. );
  138. let reverse = operation.getReversed();
  139. doc.applyOperation( wrapInDelta( operation ) );
  140. doc.applyOperation( wrapInDelta( reverse ) );
  141. expect( doc.version ).to.equal( 2 );
  142. expect( root.getAttribute( 'foo' ) ).to.be.true;
  143. } );
  144. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  145. expect( () => {
  146. doc.applyOperation( wrapInDelta(
  147. new RootAttributeOperation(
  148. root,
  149. 'foo',
  150. true,
  151. null,
  152. doc.version
  153. )
  154. ) );
  155. } ).to.throw( CKEditorError, /operation-rootattribute-no-attr-to-remove/ );
  156. } );
  157. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  158. root.setAttribute( 'x', 1 );
  159. expect( () => {
  160. doc.applyOperation( wrapInDelta(
  161. new RootAttributeOperation(
  162. root,
  163. 'x',
  164. null,
  165. 2,
  166. doc.version
  167. )
  168. ) );
  169. } ).to.throw( CKEditorError, /operation-rootattribute-attr-exists/ );
  170. } );
  171. it( 'should create a RootAttributeOperation with the same parameters when cloned', () => {
  172. let baseVersion = doc.version;
  173. let op = new RootAttributeOperation( root, 'foo', 'old', 'new', baseVersion );
  174. let clone = op.clone();
  175. // New instance rather than a pointer to the old instance.
  176. expect( clone ).not.to.be.equal( op );
  177. expect( clone ).to.be.instanceof( RootAttributeOperation );
  178. expect( clone.root ).to.equal( root );
  179. expect( clone.key ).to.equal( 'foo' );
  180. expect( clone.oldValue ).to.equal( 'old' );
  181. expect( clone.newValue ).to.equal( 'new' );
  182. expect( clone.baseVersion ).to.equal( baseVersion );
  183. } );
  184. describe( 'toJSON', () => {
  185. it( 'should create proper serialized object', () => {
  186. const op = new RootAttributeOperation(
  187. root,
  188. 'key',
  189. null,
  190. 'newValue',
  191. doc.version
  192. );
  193. const serialized = jsonParseStringify( op );
  194. expect( serialized.__className ).to.equal( 'engine.model.operation.RootAttributeOperation' );
  195. expect( serialized ).to.deep.equal( {
  196. __className: 'engine.model.operation.RootAttributeOperation',
  197. baseVersion: 0,
  198. key: 'key',
  199. newValue: 'newValue',
  200. oldValue: null,
  201. root: 'main'
  202. } );
  203. } );
  204. } );
  205. describe( 'fromJSON', () => {
  206. it( 'should create proper RootAttributeOperation from json object', () => {
  207. const op = new RootAttributeOperation( root, 'key', null, 'newValue', doc.version );
  208. const serialized = jsonParseStringify( op );
  209. const deserialized = RootAttributeOperation.fromJSON( serialized, doc );
  210. expect( deserialized ).to.deep.equal( op );
  211. } );
  212. it( 'should throw an error when root does not exists', () => {
  213. const op = new RootAttributeOperation(
  214. root,
  215. 'key',
  216. null,
  217. 'newValue',
  218. doc.version
  219. );
  220. const serialized = jsonParseStringify( op );
  221. serialized.root = 'no-root';
  222. expect( () => {
  223. RootAttributeOperation.fromJSON( serialized, doc );
  224. } ).to.throw( CKEditorError, /rootattributeoperation-fromjson-no-root/ );
  225. } );
  226. } );
  227. } );