attributeoperation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import Element from '../../../src/model/element';
  7. import Text from '../../../src/model/text';
  8. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  9. import Position from '../../../src/model/position';
  10. import Range from '../../../src/model/range';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. import count from '@ckeditor/ckeditor5-utils/src/count';
  13. import { jsonParseStringify, wrapInDelta } from '../../../tests/model/_utils/utils';
  14. describe( 'AttributeOperation', () => {
  15. let doc, root;
  16. beforeEach( () => {
  17. doc = new 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.insertChildren( 0, new Text( 'bar' ) );
  54. doc.applyOperation( wrapInDelta(
  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.insertChildren( 0, new Text( 'x', { foo: true, bar: true } ) );
  72. doc.applyOperation( wrapInDelta(
  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.insertChildren( 0, new Text( 'bar', { isNew: false } ) );
  90. doc.applyOperation( wrapInDelta(
  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.insertChildren( 0, new Text( 'x', { foo: true, x: 1, bar: true } ) );
  108. doc.applyOperation( wrapInDelta(
  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 remove attribute', () => {
  125. root.insertChildren( 0, new Text( 'x', { foo: true, x: true, bar: true } ) );
  126. doc.applyOperation( wrapInDelta(
  127. new AttributeOperation(
  128. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  129. 'x',
  130. true,
  131. null,
  132. doc.version
  133. )
  134. ) );
  135. expect( doc.version ).to.equal( 1 );
  136. expect( root.maxOffset ).to.equal( 1 );
  137. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 2 );
  138. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  139. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  140. } );
  141. it( 'should not throw for non-primitive attribute values', () => {
  142. root.insertChildren( 0, new Text( 'x', { foo: [ 'bar', 'xyz' ] } ) );
  143. expect( () => {
  144. doc.applyOperation( wrapInDelta(
  145. new AttributeOperation(
  146. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  147. 'foo',
  148. [ 'bar', 'xyz' ],
  149. true,
  150. doc.version
  151. )
  152. ) );
  153. } ).to.not.throw( Error );
  154. } );
  155. it( 'should create an AttributeOperation as a reverse', () => {
  156. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  157. const operation = new AttributeOperation( range, 'x', 'old', 'new', doc.version );
  158. const reverse = operation.getReversed();
  159. expect( reverse ).to.be.an.instanceof( AttributeOperation );
  160. expect( reverse.baseVersion ).to.equal( 1 );
  161. expect( reverse.range.isEqual( range ) ).to.be.true;
  162. expect( reverse.key ).to.equal( 'x' );
  163. expect( reverse.oldValue ).to.equal( 'new' );
  164. expect( reverse.newValue ).to.equal( 'old' );
  165. } );
  166. it( 'should undo adding attribute by applying reverse operation', () => {
  167. root.insertChildren( 0, new Text( 'bar' ) );
  168. const operation = new AttributeOperation(
  169. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  170. 'isNew',
  171. null,
  172. true,
  173. doc.version
  174. );
  175. const reverse = operation.getReversed();
  176. doc.applyOperation( wrapInDelta( operation ) );
  177. doc.applyOperation( wrapInDelta( reverse ) );
  178. expect( doc.version ).to.equal( 2 );
  179. expect( root.maxOffset ).to.equal( 3 );
  180. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 0 );
  181. } );
  182. it( 'should not set attribute of element if change range starts in the middle of that element', () => {
  183. const eleA = new Element( 'a', [], new Text( 'abc' ) );
  184. const eleB = new Element( 'b', [], new Text( 'xyz' ) );
  185. root.insertChildren( 0, [ eleA, eleB ] );
  186. doc.applyOperation( wrapInDelta(
  187. new AttributeOperation(
  188. new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ),
  189. 'foo',
  190. null,
  191. true,
  192. doc.version
  193. )
  194. ) );
  195. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
  196. } );
  197. it( 'should not remove attribute of element if change range starts in the middle of that element', () => {
  198. const fooAttr = { foo: true };
  199. const eleA = new Element( 'a', fooAttr, new Text( 'abc' ) );
  200. const eleB = new Element( 'b', fooAttr, new Text( 'xyz' ) );
  201. root.insertChildren( 0, [ eleA, eleB ] );
  202. doc.applyOperation( wrapInDelta(
  203. new AttributeOperation(
  204. new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 0 ] ) ),
  205. 'foo',
  206. true,
  207. null,
  208. doc.version
  209. )
  210. ) );
  211. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  212. } );
  213. it( 'should undo changing attribute by applying reverse operation', () => {
  214. root.insertChildren( 0, new Text( 'bar', { isNew: false } ) );
  215. const operation = new AttributeOperation(
  216. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  217. 'isNew',
  218. false,
  219. true,
  220. doc.version
  221. );
  222. const reverse = operation.getReversed();
  223. doc.applyOperation( wrapInDelta( operation ) );
  224. doc.applyOperation( wrapInDelta( reverse ) );
  225. expect( doc.version ).to.equal( 2 );
  226. expect( root.maxOffset ).to.equal( 3 );
  227. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  228. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.false;
  229. } );
  230. it( 'should undo remove attribute by applying reverse operation', () => {
  231. root.insertChildren( 0, new Text( 'bar', { foo: true } ) );
  232. const operation = new AttributeOperation(
  233. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  234. 'foo',
  235. true,
  236. null,
  237. doc.version
  238. );
  239. const reverse = operation.getReversed();
  240. doc.applyOperation( wrapInDelta( operation ) );
  241. doc.applyOperation( wrapInDelta( reverse ) );
  242. expect( doc.version ).to.equal( 2 );
  243. expect( root.maxOffset ).to.equal( 3 );
  244. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  245. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  246. } );
  247. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  248. root.insertChildren( 0, new Text( 'x' ) );
  249. expect( () => {
  250. doc.applyOperation( wrapInDelta(
  251. new AttributeOperation(
  252. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  253. 'foo',
  254. true,
  255. null,
  256. doc.version
  257. )
  258. ) );
  259. } ).to.throw( CKEditorError, /attribute-operation-wrong-old-value/ );
  260. } );
  261. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  262. root.insertChildren( 0, new Text( 'x', { x: 1 } ) );
  263. expect( () => {
  264. doc.applyOperation( wrapInDelta(
  265. new AttributeOperation(
  266. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  267. 'x',
  268. null,
  269. 2,
  270. doc.version
  271. )
  272. ) );
  273. } ).to.throw( CKEditorError, /attribute-operation-attribute-exists/ );
  274. } );
  275. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  276. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  277. const baseVersion = doc.version;
  278. const op = new AttributeOperation( range, 'foo', 'old', 'new', baseVersion );
  279. const clone = op.clone();
  280. // New instance rather than a pointer to the old instance.
  281. expect( clone ).not.to.be.equal( op );
  282. expect( clone ).to.be.instanceof( AttributeOperation );
  283. expect( clone.range.isEqual( range ) ).to.be.true;
  284. expect( clone.key ).to.equal( 'foo' );
  285. expect( clone.oldValue ).to.equal( 'old' );
  286. expect( clone.newValue ).to.equal( 'new' );
  287. expect( clone.baseVersion ).to.equal( baseVersion );
  288. } );
  289. it( 'should merge characters in node list', () => {
  290. const attrA = { foo: 'a' };
  291. const attrB = { foo: 'b' };
  292. root.insertChildren( 0, new Text( 'abc', attrA ) );
  293. root.insertChildren( 1, new Text( 'xyz', attrB ) );
  294. doc.applyOperation( wrapInDelta(
  295. new AttributeOperation(
  296. new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ),
  297. 'foo',
  298. 'a',
  299. 'b',
  300. doc.version
  301. ) )
  302. );
  303. expect( root.getChild( 0 ).data ).to.equal( 'a' );
  304. expect( root.getChild( 1 ).data ).to.equal( 'bcxyz' );
  305. } );
  306. describe( 'toJSON', () => {
  307. it( 'should create proper serialized object', () => {
  308. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  309. const op = new AttributeOperation(
  310. range,
  311. 'key',
  312. null,
  313. 'newValue',
  314. doc.version
  315. );
  316. const serialized = jsonParseStringify( op );
  317. expect( serialized.__className ).to.equal( 'engine.model.operation.AttributeOperation' );
  318. expect( serialized ).to.deep.equal( {
  319. __className: 'engine.model.operation.AttributeOperation',
  320. baseVersion: 0,
  321. key: 'key',
  322. newValue: 'newValue',
  323. oldValue: null,
  324. range: jsonParseStringify( range )
  325. } );
  326. } );
  327. } );
  328. describe( 'fromJSON', () => {
  329. it( 'should create proper AttributeOperation from json object', () => {
  330. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  331. const op = new AttributeOperation(
  332. range,
  333. 'key',
  334. null,
  335. 'newValue',
  336. doc.version
  337. );
  338. const serialized = jsonParseStringify( op );
  339. const deserialized = AttributeOperation.fromJSON( serialized, doc );
  340. expect( deserialized ).to.deep.equal( op );
  341. } );
  342. } );
  343. } );