attributeoperation.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. describe( 'is()', () => {
  53. let operation;
  54. before( () => {
  55. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  56. operation = new AttributeOperation(
  57. range,
  58. 'key',
  59. null,
  60. 'newValue',
  61. doc.version
  62. );
  63. } );
  64. it( 'should return true for all valid names of "attribute" operation', () => {
  65. expect( operation.is( 'operation' ) ).to.be.true;
  66. expect( operation.is( 'model:operation' ) ).to.be.true;
  67. expect( operation.is( 'attributeOperation' ) ).to.be.true;
  68. expect( operation.is( 'model:operation:attribute' ) ).to.be.true;
  69. } );
  70. it( 'should return false for invalid parameters', () => {
  71. expect( operation.is( 'operation:attribute' ) ).to.be.false;
  72. expect( operation.is( 'model:operation:insert' ) ).to.be.false;
  73. expect( operation.is( 'noOperation' ) ).to.be.false;
  74. expect( operation.is( 'detachOperation' ) ).to.be.false;
  75. expect( operation.is( 'rootAttributeOperation' ) ).to.be.false;
  76. expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false;
  77. } );
  78. } );
  79. it( 'should insert attribute to the set of nodes', () => {
  80. root._insertChild( 0, new Text( 'bar' ) );
  81. model.applyOperation(
  82. new AttributeOperation(
  83. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  84. 'isNew',
  85. undefined, // `undefined` should also be accepted as a value, it is same as `null`.
  86. true,
  87. doc.version
  88. )
  89. );
  90. expect( doc.version ).to.equal( 1 );
  91. expect( root.maxOffset ).to.equal( 3 );
  92. expect( root.getChild( 0 ).hasAttribute( 'isNew' ) ).to.be.true;
  93. expect( root.getChild( 0 ).data ).to.equal( 'ba' );
  94. expect( root.getChild( 1 ).hasAttribute( 'isNew' ) ).to.be.false;
  95. expect( root.getChild( 1 ).data ).to.equal( 'r' );
  96. } );
  97. it( 'should add attribute to the existing attributes', () => {
  98. root._insertChild( 0, new Text( 'x', { foo: true, bar: true } ) );
  99. model.applyOperation(
  100. new AttributeOperation(
  101. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  102. 'isNew',
  103. null,
  104. true,
  105. doc.version
  106. )
  107. );
  108. expect( doc.version ).to.equal( 1 );
  109. expect( root.maxOffset ).to.equal( 1 );
  110. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 3 );
  111. expect( root.getChild( 0 ).hasAttribute( 'isNew' ) ).to.be.true;
  112. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  113. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  114. } );
  115. it( 'should change attribute to the set of nodes', () => {
  116. root._insertChild( 0, new Text( 'bar', { isNew: false } ) );
  117. model.applyOperation(
  118. new AttributeOperation(
  119. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  120. 'isNew',
  121. false,
  122. true,
  123. doc.version
  124. )
  125. );
  126. expect( doc.version ).to.equal( 1 );
  127. expect( root.maxOffset ).to.equal( 3 );
  128. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  129. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.true;
  130. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  131. expect( root.getChild( 1 ).getAttribute( 'isNew' ) ).to.be.false;
  132. } );
  133. it( 'should change attribute in the middle of existing attributes', () => {
  134. root._insertChild( 0, new Text( 'x', { foo: true, x: 1, bar: true } ) );
  135. model.applyOperation(
  136. new AttributeOperation(
  137. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  138. 'x',
  139. 1,
  140. 2,
  141. doc.version
  142. )
  143. );
  144. expect( doc.version ).to.equal( 1 );
  145. expect( root.maxOffset ).to.equal( 1 );
  146. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 3 );
  147. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  148. expect( root.getChild( 0 ).getAttribute( 'x' ) ).to.equal( 2 );
  149. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.be.true;
  150. } );
  151. it( 'should work correctly if old and new value are same', () => {
  152. root._insertChild( 0, new Text( 'bar', { foo: 'bar' } ) );
  153. model.applyOperation(
  154. new AttributeOperation(
  155. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  156. 'foo',
  157. 'bar',
  158. 'bar',
  159. doc.version
  160. )
  161. );
  162. expect( doc.version ).to.equal( 1 );
  163. expect( root.childCount ).to.equal( 1 );
  164. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  165. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 'bar' );
  166. } );
  167. it( 'should remove attribute', () => {
  168. root._insertChild( 0, new Text( 'x', { foo: true, x: true, bar: true } ) );
  169. model.applyOperation(
  170. new AttributeOperation(
  171. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  172. 'x',
  173. true,
  174. null,
  175. doc.version
  176. )
  177. );
  178. expect( doc.version ).to.equal( 1 );
  179. expect( root.maxOffset ).to.equal( 1 );
  180. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 2 );
  181. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  182. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  183. } );
  184. describe( '_validate()', () => {
  185. it( 'should not throw for non-primitive attribute values', () => {
  186. root._insertChild( 0, new Text( 'x', { foo: [ 'bar', 'xyz' ] } ) );
  187. expect( () => {
  188. const operation = new AttributeOperation(
  189. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  190. 'foo',
  191. [ 'bar', 'xyz' ],
  192. true,
  193. doc.version
  194. );
  195. operation._validate();
  196. } ).to.not.throw( Error );
  197. } );
  198. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  199. root._insertChild( 0, new Text( 'x' ) );
  200. expectToThrowCKEditorError( () => {
  201. const operation = new AttributeOperation(
  202. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  203. 'foo',
  204. true,
  205. null,
  206. doc.version
  207. );
  208. operation._validate();
  209. }, /attribute-operation-wrong-old-value/, model );
  210. } );
  211. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  212. root._insertChild( 0, new Text( 'x', { x: 1 } ) );
  213. expectToThrowCKEditorError( () => {
  214. const operation = new AttributeOperation(
  215. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  216. 'x',
  217. null,
  218. 2,
  219. doc.version
  220. );
  221. operation._validate();
  222. }, /attribute-operation-attribute-exists/, model );
  223. } );
  224. it( 'should not throw when attribute value is the same', () => {
  225. root._insertChild( 0, new Text( 'x', { foo: true } ) );
  226. expect( () => {
  227. const operation = new AttributeOperation(
  228. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  229. 'foo',
  230. true,
  231. true,
  232. doc.version
  233. );
  234. operation._validate();
  235. } ).to.not.throw();
  236. } );
  237. it( 'should throw for a non-flat range', () => {
  238. root._insertChild( 0, [
  239. new Element( 'paragraph', null, new Text( 'Foo' ) ),
  240. new Element( 'paragraph', null, new Text( 'Bar' ) )
  241. ] );
  242. expectToThrowCKEditorError( () => {
  243. const operation = new AttributeOperation(
  244. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 1, 1 ] ) ),
  245. 'x',
  246. null,
  247. 2,
  248. doc.version
  249. );
  250. operation._validate();
  251. }, /attribute-operation-range-not-flat/, model );
  252. } );
  253. } );
  254. it( 'should create an AttributeOperation as a reverse', () => {
  255. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  256. const operation = new AttributeOperation( range, 'x', 'old', 'new', doc.version );
  257. const reverse = operation.getReversed();
  258. expect( reverse ).to.be.an.instanceof( AttributeOperation );
  259. expect( reverse.baseVersion ).to.equal( 1 );
  260. expect( reverse.range.isEqual( range ) ).to.be.true;
  261. expect( reverse.key ).to.equal( 'x' );
  262. expect( reverse.oldValue ).to.equal( 'new' );
  263. expect( reverse.newValue ).to.equal( 'old' );
  264. } );
  265. it( 'should undo adding attribute by applying reverse operation', () => {
  266. root._insertChild( 0, new Text( 'bar' ) );
  267. const operation = new AttributeOperation(
  268. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  269. 'isNew',
  270. null,
  271. true,
  272. doc.version
  273. );
  274. const reverse = operation.getReversed();
  275. model.applyOperation( operation );
  276. model.applyOperation( reverse );
  277. expect( doc.version ).to.equal( 2 );
  278. expect( root.maxOffset ).to.equal( 3 );
  279. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 0 );
  280. } );
  281. it( 'should undo changing attribute by applying reverse operation', () => {
  282. root._insertChild( 0, new Text( 'bar', { isNew: false } ) );
  283. const operation = new AttributeOperation(
  284. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  285. 'isNew',
  286. false,
  287. true,
  288. doc.version
  289. );
  290. const reverse = operation.getReversed();
  291. model.applyOperation( operation );
  292. model.applyOperation( reverse );
  293. expect( doc.version ).to.equal( 2 );
  294. expect( root.maxOffset ).to.equal( 3 );
  295. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  296. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.false;
  297. } );
  298. it( 'should undo remove attribute by applying reverse operation', () => {
  299. root._insertChild( 0, new Text( 'bar', { foo: true } ) );
  300. const operation = new AttributeOperation(
  301. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  302. 'foo',
  303. true,
  304. null,
  305. doc.version
  306. );
  307. const reverse = operation.getReversed();
  308. model.applyOperation( operation );
  309. model.applyOperation( reverse );
  310. expect( doc.version ).to.equal( 2 );
  311. expect( root.maxOffset ).to.equal( 3 );
  312. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  313. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  314. } );
  315. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  316. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  317. const baseVersion = doc.version;
  318. const op = new AttributeOperation( range, 'foo', 'old', 'new', baseVersion );
  319. const clone = op.clone();
  320. // New instance rather than a pointer to the old instance.
  321. expect( clone ).not.to.be.equal( op );
  322. expect( clone ).to.be.instanceof( AttributeOperation );
  323. expect( clone.range.isEqual( range ) ).to.be.true;
  324. expect( clone.key ).to.equal( 'foo' );
  325. expect( clone.oldValue ).to.equal( 'old' );
  326. expect( clone.newValue ).to.equal( 'new' );
  327. expect( clone.baseVersion ).to.equal( baseVersion );
  328. } );
  329. it( 'should merge characters in node list', () => {
  330. const attrA = { foo: 'a' };
  331. const attrB = { foo: 'b' };
  332. root._insertChild( 0, new Text( 'abc', attrA ) );
  333. root._insertChild( 1, new Text( 'xyz', attrB ) );
  334. model.applyOperation(
  335. new AttributeOperation(
  336. new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ),
  337. 'foo',
  338. 'a',
  339. 'b',
  340. doc.version
  341. )
  342. );
  343. expect( root.getChild( 0 ).data ).to.equal( 'a' );
  344. expect( root.getChild( 1 ).data ).to.equal( 'bcxyz' );
  345. } );
  346. describe( 'toJSON', () => {
  347. it( 'should create proper serialized object', () => {
  348. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  349. const op = new AttributeOperation(
  350. range,
  351. 'key',
  352. null,
  353. 'newValue',
  354. doc.version
  355. );
  356. const serialized = op.toJSON();
  357. expect( serialized.__className ).to.equal( 'AttributeOperation' );
  358. expect( serialized ).to.deep.equal( {
  359. __className: 'AttributeOperation',
  360. baseVersion: 0,
  361. key: 'key',
  362. newValue: 'newValue',
  363. oldValue: null,
  364. range: range.toJSON()
  365. } );
  366. } );
  367. } );
  368. describe( 'fromJSON', () => {
  369. it( 'should create proper AttributeOperation from json object', () => {
  370. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  371. const op = new AttributeOperation(
  372. range,
  373. 'key',
  374. null,
  375. 'newValue',
  376. doc.version
  377. );
  378. const serialized = op.toJSON();
  379. const deserialized = AttributeOperation.fromJSON( serialized, doc );
  380. expect( deserialized ).to.deep.equal( op );
  381. } );
  382. } );
  383. } );