rootattributeoperation.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, operation */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import RootAttributeOperation from '/ckeditor5/core/treemodel/operation/rootattributeoperation.js';
  9. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  10. describe( 'RootAttributeOperation', () => {
  11. let doc, root;
  12. beforeEach( () => {
  13. doc = new Document();
  14. root = doc.createRoot( 'root' );
  15. } );
  16. it( 'should have proper type', () => {
  17. const op = new RootAttributeOperation(
  18. root,
  19. 'isNew',
  20. null,
  21. true,
  22. doc.version
  23. );
  24. expect( op.type ).to.equal( 'rootattribute' );
  25. } );
  26. it( 'should add attribute on the root element', () => {
  27. doc.applyOperation(
  28. new RootAttributeOperation(
  29. root,
  30. 'isNew',
  31. null,
  32. true,
  33. doc.version
  34. )
  35. );
  36. expect( doc.version ).to.equal( 1 );
  37. expect( root.hasAttribute( 'isNew' ) ).to.be.true;
  38. } );
  39. it( 'should change attribute on the root element', () => {
  40. root.setAttribute( 'isNew', false );
  41. doc.applyOperation(
  42. new RootAttributeOperation(
  43. root,
  44. 'isNew',
  45. false,
  46. true,
  47. doc.version
  48. )
  49. );
  50. expect( doc.version ).to.equal( 1 );
  51. expect( root.getAttribute( 'isNew' ) ).to.be.true;
  52. } );
  53. it( 'should remove attribute from the root element', () => {
  54. root.setAttribute( 'x', true );
  55. doc.applyOperation(
  56. new RootAttributeOperation(
  57. root,
  58. 'x',
  59. true,
  60. null,
  61. doc.version
  62. )
  63. );
  64. expect( doc.version ).to.equal( 1 );
  65. expect( root.hasAttribute( 'x' ) ).to.be.false;
  66. } );
  67. it( 'should create a RootAttributeOperation as a reverse', () => {
  68. let operation = new RootAttributeOperation( root, 'x', 'old', 'new', doc.version );
  69. let reverse = operation.getReversed();
  70. expect( reverse ).to.be.an.instanceof( RootAttributeOperation );
  71. expect( reverse.baseVersion ).to.equal( 1 );
  72. expect( reverse.root ).to.equal( root );
  73. expect( reverse.key ).to.equal( 'x' );
  74. expect( reverse.oldValue ).to.equal( 'new' );
  75. expect( reverse.newValue ).to.equal( 'old' );
  76. } );
  77. it( 'should undo adding attribute by applying reverse operation', () => {
  78. let operation = new RootAttributeOperation(
  79. root,
  80. 'isNew',
  81. null,
  82. true,
  83. doc.version
  84. );
  85. let reverse = operation.getReversed();
  86. doc.applyOperation( operation );
  87. doc.applyOperation( reverse );
  88. expect( doc.version ).to.equal( 2 );
  89. expect( root.hasAttribute( 'x' ) ).to.be.false;
  90. } );
  91. it( 'should undo changing attribute by applying reverse operation', () => {
  92. root.setAttribute( 'isNew', false );
  93. let operation = new RootAttributeOperation(
  94. root,
  95. 'isNew',
  96. false,
  97. true,
  98. doc.version
  99. );
  100. let reverse = operation.getReversed();
  101. doc.applyOperation( operation );
  102. doc.applyOperation( reverse );
  103. expect( doc.version ).to.equal( 2 );
  104. expect( root.getAttribute( 'isNew' ) ).to.be.false;
  105. } );
  106. it( 'should undo remove attribute by applying reverse operation', () => {
  107. root.setAttribute( 'foo', true );
  108. let operation = new RootAttributeOperation(
  109. root,
  110. 'foo',
  111. true,
  112. null,
  113. doc.version
  114. );
  115. let reverse = operation.getReversed();
  116. doc.applyOperation( operation );
  117. doc.applyOperation( reverse );
  118. expect( doc.version ).to.equal( 2 );
  119. expect( root.getAttribute( 'foo' ) ).to.be.true;
  120. } );
  121. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  122. expect( () => {
  123. doc.applyOperation(
  124. new RootAttributeOperation(
  125. root,
  126. 'foo',
  127. true,
  128. null,
  129. doc.version
  130. )
  131. );
  132. } ).to.throw( CKEditorError, /operation-rootattribute-no-attr-to-remove/ );
  133. } );
  134. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  135. root.setAttribute( 'x', 1 );
  136. expect( () => {
  137. doc.applyOperation(
  138. new RootAttributeOperation(
  139. root,
  140. 'x',
  141. null,
  142. 2,
  143. doc.version
  144. )
  145. );
  146. } ).to.throw( CKEditorError, /operation-rootattribute-attr-exists/ );
  147. } );
  148. it( 'should create a RootAttributeOperation with the same parameters when cloned', () => {
  149. let baseVersion = doc.version;
  150. let op = new RootAttributeOperation( root, 'foo', 'old', 'new', baseVersion );
  151. let clone = op.clone();
  152. // New instance rather than a pointer to the old instance.
  153. expect( clone ).not.to.be.equal( op );
  154. expect( clone ).to.be.instanceof( RootAttributeOperation );
  155. expect( clone.root ).to.equal( root );
  156. expect( clone.key ).to.equal( 'foo' );
  157. expect( clone.oldValue ).to.equal( 'old' );
  158. expect( clone.newValue ).to.equal( 'new' );
  159. expect( clone.baseVersion ).to.equal( baseVersion );
  160. } );
  161. } );