attributeoperation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 coreTestUtils from '/tests/core/_utils/utils.js';
  8. import Document from '/ckeditor5/core/treemodel/document.js';
  9. import Element from '/ckeditor5/core/treemodel/element.js';
  10. import AttributeOperation from '/ckeditor5/core/treemodel/operation/attributeoperation.js';
  11. import Position from '/ckeditor5/core/treemodel/position.js';
  12. import Range from '/ckeditor5/core/treemodel/range.js';
  13. import Character from '/ckeditor5/core/treemodel/character.js';
  14. import Attribute from '/ckeditor5/core/treemodel/attribute.js';
  15. import Text from '/ckeditor5/core/treemodel/text.js';
  16. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  17. const getIteratorCount = coreTestUtils.getIteratorCount;
  18. describe( 'AttributeOperation', () => {
  19. let doc, root;
  20. beforeEach( () => {
  21. doc = new Document();
  22. root = doc.createRoot( 'root' );
  23. } );
  24. it( 'should have proper type', () => {
  25. const op = new AttributeOperation(
  26. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  27. null,
  28. new Attribute( 'isNew', true ),
  29. doc.version
  30. );
  31. expect( op.type ).to.equal( 'attr' );
  32. } );
  33. it( 'should insert attribute to the set of nodes', () => {
  34. let newAttr = new Attribute( 'isNew', true );
  35. root.insertChildren( 0, 'bar' );
  36. doc.applyOperation(
  37. new AttributeOperation(
  38. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  39. null,
  40. newAttr,
  41. doc.version
  42. )
  43. );
  44. expect( doc.version ).to.equal( 1 );
  45. expect( root.getChildCount() ).to.equal( 3 );
  46. expect( root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
  47. expect( root.getChild( 1 ).hasAttr( newAttr ) ).to.be.true;
  48. expect( getIteratorCount( root.getChild( 2 ).getAttrs() ) ).to.equal( 0 );
  49. } );
  50. it( 'should add attribute to the existing attributes', () => {
  51. let newAttr = new Attribute( 'isNew', true );
  52. let fooAttr = new Attribute( 'foo', true );
  53. let barAttr = new Attribute( 'bar', true );
  54. root.insertChildren( 0, new Character( 'x', [ fooAttr, barAttr ] ) );
  55. doc.applyOperation(
  56. new AttributeOperation(
  57. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  58. null,
  59. newAttr,
  60. doc.version
  61. )
  62. );
  63. expect( doc.version ).to.equal( 1 );
  64. expect( root.getChildCount() ).to.equal( 1 );
  65. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 3 );
  66. expect( root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
  67. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  68. expect( root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
  69. } );
  70. it( 'should change attribute to the set of nodes', () => {
  71. let oldAttr = new Attribute( 'isNew', false );
  72. let newAttr = new Attribute( 'isNew', true );
  73. root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
  74. doc.applyOperation(
  75. new AttributeOperation(
  76. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  77. oldAttr,
  78. newAttr,
  79. doc.version
  80. )
  81. );
  82. expect( doc.version ).to.equal( 1 );
  83. expect( root.getChildCount() ).to.equal( 3 );
  84. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 1 );
  85. expect( root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
  86. expect( getIteratorCount( root.getChild( 1 ).getAttrs() ) ).to.equal( 1 );
  87. expect( root.getChild( 1 ).hasAttr( newAttr ) ).to.be.true;
  88. expect( getIteratorCount( root.getChild( 2 ).getAttrs() ) ).to.equal( 1 );
  89. expect( root.getChild( 2 ).hasAttr( oldAttr ) ).to.be.true;
  90. } );
  91. it( 'should change attribute in the middle of existing attributes', () => {
  92. let fooAttr = new Attribute( 'foo', true );
  93. let x1Attr = new Attribute( 'x', 1 );
  94. let x2Attr = new Attribute( 'x', 2 );
  95. let barAttr = new Attribute( 'bar', true );
  96. root.insertChildren( 0, new Character( 'x', [ fooAttr, x1Attr, barAttr ] ) );
  97. doc.applyOperation(
  98. new AttributeOperation(
  99. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  100. x1Attr,
  101. x2Attr,
  102. doc.version
  103. )
  104. );
  105. expect( doc.version ).to.equal( 1 );
  106. expect( root.getChildCount() ).to.equal( 1 );
  107. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 3 );
  108. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  109. expect( root.getChild( 0 ).hasAttr( x2Attr ) ).to.be.true;
  110. expect( root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
  111. } );
  112. it( 'should remove attribute', () => {
  113. let fooAttr = new Attribute( 'foo', true );
  114. let xAttr = new Attribute( 'x', true );
  115. let barAttr = new Attribute( 'bar', true );
  116. root.insertChildren( 0, new Character( 'x', [ fooAttr, xAttr, barAttr ] ) );
  117. doc.applyOperation(
  118. new AttributeOperation(
  119. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  120. xAttr,
  121. null,
  122. doc.version
  123. )
  124. );
  125. expect( doc.version ).to.equal( 1 );
  126. expect( root.getChildCount() ).to.equal( 1 );
  127. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 2 );
  128. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  129. expect( root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
  130. } );
  131. it( 'should create an AttributeOperation as a reverse', () => {
  132. let oldAttr = new Attribute( 'x', 'old' );
  133. let newAttr = new Attribute( 'x', 'new' );
  134. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  135. let operation = new AttributeOperation( range, oldAttr, newAttr, doc.version );
  136. let reverse = operation.getReversed();
  137. expect( reverse ).to.be.an.instanceof( AttributeOperation );
  138. expect( reverse.baseVersion ).to.equal( 1 );
  139. expect( reverse.range.isEqual( range ) ).to.be.true;
  140. expect( reverse.oldAttr ).to.equal( newAttr );
  141. expect( reverse.newAttr ).to.equal( oldAttr );
  142. } );
  143. it( 'should undo adding attribute by applying reverse operation', () => {
  144. let newAttr = new Attribute( 'isNew', true );
  145. root.insertChildren( 0, 'bar' );
  146. let operation = new AttributeOperation(
  147. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  148. null,
  149. newAttr,
  150. doc.version
  151. );
  152. let reverse = operation.getReversed();
  153. doc.applyOperation( operation );
  154. doc.applyOperation( reverse );
  155. expect( doc.version ).to.equal( 2 );
  156. expect( root.getChildCount() ).to.equal( 3 );
  157. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 0 );
  158. expect( getIteratorCount( root.getChild( 1 ).getAttrs() ) ).to.equal( 0 );
  159. expect( getIteratorCount( root.getChild( 2 ).getAttrs() ) ).to.equal( 0 );
  160. } );
  161. it( 'should not set attribute of element if change range starts in the middle of that element', () => {
  162. let fooAttr = new Attribute( 'foo', true );
  163. let eleA = new Element( 'a', [], 'abc' );
  164. let eleB = new Element( 'b', [], 'xyz' );
  165. root.insertChildren( 0, [ eleA, eleB ] );
  166. doc.applyOperation(
  167. new AttributeOperation(
  168. new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ),
  169. null,
  170. fooAttr,
  171. doc.version
  172. )
  173. );
  174. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.false;
  175. } );
  176. it( 'should not remove attribute of element if change range starts in the middle of that element', () => {
  177. let fooAttr = new Attribute( 'foo', true );
  178. let eleA = new Element( 'a', [ fooAttr ], 'abc' );
  179. let eleB = new Element( 'b', [ fooAttr ], 'xyz' );
  180. root.insertChildren( 0, [ eleA, eleB ] );
  181. doc.applyOperation(
  182. new AttributeOperation(
  183. new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 0 ] ) ),
  184. fooAttr,
  185. null,
  186. doc.version
  187. )
  188. );
  189. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  190. } );
  191. it( 'should undo changing attribute by applying reverse operation', () => {
  192. let oldAttr = new Attribute( 'isNew', false );
  193. let newAttr = new Attribute( 'isNew', true );
  194. root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
  195. let operation = new AttributeOperation(
  196. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  197. oldAttr,
  198. newAttr,
  199. doc.version
  200. );
  201. let reverse = operation.getReversed();
  202. doc.applyOperation( operation );
  203. doc.applyOperation( reverse );
  204. expect( doc.version ).to.equal( 2 );
  205. expect( root.getChildCount() ).to.equal( 3 );
  206. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 1 );
  207. expect( root.getChild( 0 ).hasAttr( oldAttr ) ).to.be.true;
  208. expect( getIteratorCount( root.getChild( 1 ).getAttrs() ) ).to.equal( 1 );
  209. expect( root.getChild( 1 ).hasAttr( oldAttr ) ).to.be.true;
  210. expect( getIteratorCount( root.getChild( 2 ).getAttrs() ) ).to.equal( 1 );
  211. expect( root.getChild( 2 ).hasAttr( oldAttr ) ).to.be.true;
  212. } );
  213. it( 'should undo remove attribute by applying reverse operation', () => {
  214. let fooAttr = new Attribute( 'foo', false );
  215. root.insertChildren( 0, new Text( 'bar', [ fooAttr ] ) );
  216. let operation = new AttributeOperation(
  217. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  218. fooAttr,
  219. null,
  220. doc.version
  221. );
  222. let reverse = operation.getReversed();
  223. doc.applyOperation( operation );
  224. doc.applyOperation( reverse );
  225. expect( doc.version ).to.equal( 2 );
  226. expect( root.getChildCount() ).to.equal( 3 );
  227. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 1 );
  228. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  229. expect( getIteratorCount( root.getChild( 1 ).getAttrs() ) ).to.equal( 1 );
  230. expect( root.getChild( 1 ).hasAttr( fooAttr ) ).to.be.true;
  231. expect( getIteratorCount( root.getChild( 2 ).getAttrs() ) ).to.equal( 1 );
  232. expect( root.getChild( 2 ).hasAttr( fooAttr ) ).to.be.true;
  233. } );
  234. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  235. let fooAttr = new Attribute( 'foo', true );
  236. root.insertChildren( 0, 'x' );
  237. expect( () => {
  238. doc.applyOperation(
  239. new AttributeOperation(
  240. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  241. fooAttr,
  242. null,
  243. doc.version
  244. )
  245. );
  246. } ).to.throw( CKEditorError, /operation-attribute-no-attr-to-remove/ );
  247. } );
  248. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  249. let x1Attr = new Attribute( 'x', 1 );
  250. let x2Attr = new Attribute( 'x', 2 );
  251. root.insertChildren( 0, new Character( 'x', [ x1Attr ] ) );
  252. expect( () => {
  253. doc.applyOperation(
  254. new AttributeOperation(
  255. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  256. null,
  257. x2Attr,
  258. doc.version
  259. )
  260. );
  261. } ).to.throw( CKEditorError, /operation-attribute-attr-exists/ );
  262. } );
  263. it( 'should throw an error when one try to change and the new and old attributes have different keys', () => {
  264. let fooAttr = new Attribute( 'foo', true );
  265. let barAttr = new Attribute( 'bar', true );
  266. root.insertChildren( 0, 'x' );
  267. expect( () => {
  268. doc.applyOperation(
  269. new AttributeOperation(
  270. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  271. fooAttr,
  272. barAttr,
  273. doc.version
  274. )
  275. );
  276. } ).to.throw( CKEditorError, /operation-attribute-different-keys/ );
  277. } );
  278. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  279. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  280. let oldAttr = new Attribute( 'foo', 'old' );
  281. let newAttr = new Attribute( 'foo', 'bar' );
  282. let baseVersion = doc.version;
  283. let op = new AttributeOperation( range, oldAttr, newAttr, baseVersion );
  284. let clone = op.clone();
  285. // New instance rather than a pointer to the old instance.
  286. expect( clone ).not.to.be.equal( op );
  287. expect( clone ).to.be.instanceof( AttributeOperation );
  288. expect( clone.range.isEqual( range ) ).to.be.true;
  289. expect( clone.oldAttr.isEqual( oldAttr ) ).to.be.true;
  290. expect( clone.newAttr.isEqual( newAttr ) ).to.be.true;
  291. expect( clone.baseVersion ).to.equal( baseVersion );
  292. } );
  293. } );