8
0

attributeoperation.js 14 KB

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