attributeoperation.js 11 KB

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