attributeoperation.js 13 KB

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