attributeoperation.js 12 KB

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