attributeoperation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  6. import Text from '../../../src/model/text';
  7. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  8. import Position from '../../../src/model/position';
  9. import Range from '../../../src/model/range';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import count from '@ckeditor/ckeditor5-utils/src/count';
  12. describe( 'AttributeOperation', () => {
  13. let model, doc, root;
  14. beforeEach( () => {
  15. model = new Model();
  16. doc = model.document;
  17. root = doc.createRoot();
  18. } );
  19. describe( 'type', () => {
  20. it( 'should be addAttribute for adding attribute', () => {
  21. const op = new AttributeOperation(
  22. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  23. 'key',
  24. null,
  25. 'newValue',
  26. doc.version
  27. );
  28. expect( op.type ).to.equal( 'addAttribute' );
  29. } );
  30. it( 'should be removeAttribute for removing attribute', () => {
  31. const op = new AttributeOperation(
  32. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  33. 'key',
  34. 'oldValue',
  35. undefined, // `undefined` should also be accepted as a value, it is same as `null`.
  36. doc.version
  37. );
  38. expect( op.type ).to.equal( 'removeAttribute' );
  39. } );
  40. it( 'should be changeAttribute for removing attribute', () => {
  41. const op = new AttributeOperation(
  42. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  43. 'key',
  44. 'oldValue',
  45. 'newValue',
  46. doc.version
  47. );
  48. expect( op.type ).to.equal( 'changeAttribute' );
  49. } );
  50. } );
  51. it( 'should insert attribute to the set of nodes', () => {
  52. root._insertChild( 0, new Text( 'bar' ) );
  53. model.applyOperation(
  54. new AttributeOperation(
  55. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  56. 'isNew',
  57. undefined, // `undefined` should also be accepted as a value, it is same as `null`.
  58. true,
  59. doc.version
  60. )
  61. );
  62. expect( doc.version ).to.equal( 1 );
  63. expect( root.maxOffset ).to.equal( 3 );
  64. expect( root.getChild( 0 ).hasAttribute( 'isNew' ) ).to.be.true;
  65. expect( root.getChild( 0 ).data ).to.equal( 'ba' );
  66. expect( root.getChild( 1 ).hasAttribute( 'isNew' ) ).to.be.false;
  67. expect( root.getChild( 1 ).data ).to.equal( 'r' );
  68. } );
  69. it( 'should add attribute to the existing attributes', () => {
  70. root._insertChild( 0, new Text( 'x', { foo: true, bar: true } ) );
  71. model.applyOperation(
  72. new AttributeOperation(
  73. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  74. 'isNew',
  75. null,
  76. true,
  77. doc.version
  78. )
  79. );
  80. expect( doc.version ).to.equal( 1 );
  81. expect( root.maxOffset ).to.equal( 1 );
  82. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 3 );
  83. expect( root.getChild( 0 ).hasAttribute( 'isNew' ) ).to.be.true;
  84. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  85. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  86. } );
  87. it( 'should change attribute to the set of nodes', () => {
  88. root._insertChild( 0, new Text( 'bar', { isNew: false } ) );
  89. model.applyOperation(
  90. new AttributeOperation(
  91. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  92. 'isNew',
  93. false,
  94. true,
  95. doc.version
  96. )
  97. );
  98. expect( doc.version ).to.equal( 1 );
  99. expect( root.maxOffset ).to.equal( 3 );
  100. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  101. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.true;
  102. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  103. expect( root.getChild( 1 ).getAttribute( 'isNew' ) ).to.be.false;
  104. } );
  105. it( 'should change attribute in the middle of existing attributes', () => {
  106. root._insertChild( 0, new Text( 'x', { foo: true, x: 1, bar: true } ) );
  107. model.applyOperation(
  108. new AttributeOperation(
  109. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  110. 'x',
  111. 1,
  112. 2,
  113. doc.version
  114. )
  115. );
  116. expect( doc.version ).to.equal( 1 );
  117. expect( root.maxOffset ).to.equal( 1 );
  118. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 3 );
  119. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  120. expect( root.getChild( 0 ).getAttribute( 'x' ) ).to.equal( 2 );
  121. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.be.true;
  122. } );
  123. it( 'should work correctly if old and new value are same', () => {
  124. root._insertChild( 0, new Text( 'bar', { foo: 'bar' } ) );
  125. model.applyOperation(
  126. new AttributeOperation(
  127. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  128. 'foo',
  129. 'bar',
  130. 'bar',
  131. doc.version
  132. )
  133. );
  134. expect( doc.version ).to.equal( 1 );
  135. expect( root.childCount ).to.equal( 1 );
  136. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  137. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 'bar' );
  138. } );
  139. it( 'should remove attribute', () => {
  140. root._insertChild( 0, new Text( 'x', { foo: true, x: true, bar: true } ) );
  141. model.applyOperation(
  142. new AttributeOperation(
  143. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  144. 'x',
  145. true,
  146. null,
  147. doc.version
  148. )
  149. );
  150. expect( doc.version ).to.equal( 1 );
  151. expect( root.maxOffset ).to.equal( 1 );
  152. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 2 );
  153. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  154. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  155. } );
  156. describe( '_validate()', () => {
  157. it( 'should not throw for non-primitive attribute values', () => {
  158. root._insertChild( 0, new Text( 'x', { foo: [ 'bar', 'xyz' ] } ) );
  159. expect( () => {
  160. const operation = new AttributeOperation(
  161. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  162. 'foo',
  163. [ 'bar', 'xyz' ],
  164. true,
  165. doc.version
  166. );
  167. operation._validate();
  168. } ).to.not.throw( Error );
  169. } );
  170. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  171. root._insertChild( 0, new Text( 'x' ) );
  172. expect( () => {
  173. const operation = new AttributeOperation(
  174. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  175. 'foo',
  176. true,
  177. null,
  178. doc.version
  179. );
  180. operation._validate();
  181. } ).to.throw( CKEditorError, /attribute-operation-wrong-old-value/ );
  182. } );
  183. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  184. root._insertChild( 0, new Text( 'x', { x: 1 } ) );
  185. expect( () => {
  186. const operation = new AttributeOperation(
  187. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  188. 'x',
  189. null,
  190. 2,
  191. doc.version
  192. );
  193. operation._validate();
  194. } ).to.throw( CKEditorError, /attribute-operation-attribute-exists/ );
  195. } );
  196. it( 'should not throw when attribute value is the same', () => {
  197. root._insertChild( 0, new Text( 'x', { foo: true } ) );
  198. expect( () => {
  199. const operation = new AttributeOperation(
  200. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  201. 'foo',
  202. true,
  203. true,
  204. doc.version
  205. );
  206. operation._validate();
  207. } ).to.not.throw();
  208. } );
  209. } );
  210. it( 'should create an AttributeOperation as a reverse', () => {
  211. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  212. const operation = new AttributeOperation( range, 'x', 'old', 'new', doc.version );
  213. const reverse = operation.getReversed();
  214. expect( reverse ).to.be.an.instanceof( AttributeOperation );
  215. expect( reverse.baseVersion ).to.equal( 1 );
  216. expect( reverse.range.isEqual( range ) ).to.be.true;
  217. expect( reverse.key ).to.equal( 'x' );
  218. expect( reverse.oldValue ).to.equal( 'new' );
  219. expect( reverse.newValue ).to.equal( 'old' );
  220. } );
  221. it( 'should undo adding attribute by applying reverse operation', () => {
  222. root._insertChild( 0, new Text( 'bar' ) );
  223. const operation = new AttributeOperation(
  224. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  225. 'isNew',
  226. null,
  227. true,
  228. doc.version
  229. );
  230. const reverse = operation.getReversed();
  231. model.applyOperation( operation );
  232. model.applyOperation( reverse );
  233. expect( doc.version ).to.equal( 2 );
  234. expect( root.maxOffset ).to.equal( 3 );
  235. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 0 );
  236. } );
  237. it( 'should undo changing attribute by applying reverse operation', () => {
  238. root._insertChild( 0, new Text( 'bar', { isNew: false } ) );
  239. const operation = new AttributeOperation(
  240. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  241. 'isNew',
  242. false,
  243. true,
  244. doc.version
  245. );
  246. const reverse = operation.getReversed();
  247. model.applyOperation( operation );
  248. model.applyOperation( reverse );
  249. expect( doc.version ).to.equal( 2 );
  250. expect( root.maxOffset ).to.equal( 3 );
  251. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  252. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.false;
  253. } );
  254. it( 'should undo remove attribute by applying reverse operation', () => {
  255. root._insertChild( 0, new Text( 'bar', { foo: true } ) );
  256. const operation = new AttributeOperation(
  257. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  258. 'foo',
  259. true,
  260. null,
  261. doc.version
  262. );
  263. const reverse = operation.getReversed();
  264. model.applyOperation( operation );
  265. model.applyOperation( reverse );
  266. expect( doc.version ).to.equal( 2 );
  267. expect( root.maxOffset ).to.equal( 3 );
  268. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  269. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  270. } );
  271. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  272. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  273. const baseVersion = doc.version;
  274. const op = new AttributeOperation( range, 'foo', 'old', 'new', baseVersion );
  275. const clone = op.clone();
  276. // New instance rather than a pointer to the old instance.
  277. expect( clone ).not.to.be.equal( op );
  278. expect( clone ).to.be.instanceof( AttributeOperation );
  279. expect( clone.range.isEqual( range ) ).to.be.true;
  280. expect( clone.key ).to.equal( 'foo' );
  281. expect( clone.oldValue ).to.equal( 'old' );
  282. expect( clone.newValue ).to.equal( 'new' );
  283. expect( clone.baseVersion ).to.equal( baseVersion );
  284. } );
  285. it( 'should merge characters in node list', () => {
  286. const attrA = { foo: 'a' };
  287. const attrB = { foo: 'b' };
  288. root._insertChild( 0, new Text( 'abc', attrA ) );
  289. root._insertChild( 1, new Text( 'xyz', attrB ) );
  290. model.applyOperation(
  291. new AttributeOperation(
  292. new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ),
  293. 'foo',
  294. 'a',
  295. 'b',
  296. doc.version
  297. )
  298. );
  299. expect( root.getChild( 0 ).data ).to.equal( 'a' );
  300. expect( root.getChild( 1 ).data ).to.equal( 'bcxyz' );
  301. } );
  302. describe( 'toJSON', () => {
  303. it( 'should create proper serialized object', () => {
  304. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  305. const op = new AttributeOperation(
  306. range,
  307. 'key',
  308. null,
  309. 'newValue',
  310. doc.version
  311. );
  312. const serialized = op.toJSON();
  313. expect( serialized.__className ).to.equal( 'engine.model.operation.AttributeOperation' );
  314. expect( serialized ).to.deep.equal( {
  315. __className: 'engine.model.operation.AttributeOperation',
  316. baseVersion: 0,
  317. key: 'key',
  318. newValue: 'newValue',
  319. oldValue: null,
  320. range: range.toJSON()
  321. } );
  322. } );
  323. } );
  324. describe( 'fromJSON', () => {
  325. it( 'should create proper AttributeOperation from json object', () => {
  326. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  327. const op = new AttributeOperation(
  328. range,
  329. 'key',
  330. null,
  331. 'newValue',
  332. doc.version
  333. );
  334. const serialized = op.toJSON();
  335. const deserialized = AttributeOperation.fromJSON( serialized, doc );
  336. expect( deserialized ).to.deep.equal( op );
  337. } );
  338. } );
  339. } );