8
0

attributeoperation.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 remove attribute', () => {
  150. root.insertChildren( 0, new Text( 'x', { foo: true, x: true, bar: true } ) );
  151. model.applyOperation( wrapInDelta(
  152. new AttributeOperation(
  153. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  154. 'x',
  155. true,
  156. null,
  157. doc.version
  158. )
  159. ) );
  160. expect( doc.version ).to.equal( 1 );
  161. expect( root.maxOffset ).to.equal( 1 );
  162. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 2 );
  163. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  164. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  165. } );
  166. it( 'should not throw for non-primitive attribute values', () => {
  167. root.insertChildren( 0, new Text( 'x', { foo: [ 'bar', 'xyz' ] } ) );
  168. expect( () => {
  169. model.applyOperation( wrapInDelta(
  170. new AttributeOperation(
  171. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  172. 'foo',
  173. [ 'bar', 'xyz' ],
  174. true,
  175. doc.version
  176. )
  177. ) );
  178. } ).to.not.throw( Error );
  179. } );
  180. it( 'should create an AttributeOperation as a reverse', () => {
  181. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  182. const operation = new AttributeOperation( range, 'x', 'old', 'new', doc.version );
  183. const reverse = operation.getReversed();
  184. expect( reverse ).to.be.an.instanceof( AttributeOperation );
  185. expect( reverse.baseVersion ).to.equal( 1 );
  186. expect( reverse.range.isEqual( range ) ).to.be.true;
  187. expect( reverse.key ).to.equal( 'x' );
  188. expect( reverse.oldValue ).to.equal( 'new' );
  189. expect( reverse.newValue ).to.equal( 'old' );
  190. } );
  191. it( 'should undo adding attribute by applying reverse operation', () => {
  192. root.insertChildren( 0, new Text( 'bar' ) );
  193. const operation = new AttributeOperation(
  194. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  195. 'isNew',
  196. null,
  197. true,
  198. doc.version
  199. );
  200. const reverse = operation.getReversed();
  201. model.applyOperation( wrapInDelta( operation ) );
  202. model.applyOperation( wrapInDelta( reverse ) );
  203. expect( doc.version ).to.equal( 2 );
  204. expect( root.maxOffset ).to.equal( 3 );
  205. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 0 );
  206. } );
  207. it( 'should not set attribute of element if change range starts in the middle of that element', () => {
  208. const eleA = new Element( 'a', [], new Text( 'abc' ) );
  209. const eleB = new Element( 'b', [], new Text( 'xyz' ) );
  210. root.insertChildren( 0, [ eleA, eleB ] );
  211. model.applyOperation( wrapInDelta(
  212. new AttributeOperation(
  213. new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ),
  214. 'foo',
  215. null,
  216. true,
  217. doc.version
  218. )
  219. ) );
  220. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
  221. } );
  222. it( 'should not remove attribute of element if change range starts in the middle of that element', () => {
  223. const fooAttr = { foo: true };
  224. const eleA = new Element( 'a', fooAttr, new Text( 'abc' ) );
  225. const eleB = new Element( 'b', fooAttr, new Text( 'xyz' ) );
  226. root.insertChildren( 0, [ eleA, eleB ] );
  227. model.applyOperation( wrapInDelta(
  228. new AttributeOperation(
  229. new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 0 ] ) ),
  230. 'foo',
  231. true,
  232. null,
  233. doc.version
  234. )
  235. ) );
  236. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  237. } );
  238. it( 'should undo changing attribute by applying reverse operation', () => {
  239. root.insertChildren( 0, new Text( 'bar', { isNew: false } ) );
  240. const operation = new AttributeOperation(
  241. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  242. 'isNew',
  243. false,
  244. true,
  245. doc.version
  246. );
  247. const reverse = operation.getReversed();
  248. model.applyOperation( wrapInDelta( operation ) );
  249. model.applyOperation( wrapInDelta( reverse ) );
  250. expect( doc.version ).to.equal( 2 );
  251. expect( root.maxOffset ).to.equal( 3 );
  252. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  253. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.false;
  254. } );
  255. it( 'should undo remove attribute by applying reverse operation', () => {
  256. root.insertChildren( 0, new Text( 'bar', { foo: true } ) );
  257. const operation = new AttributeOperation(
  258. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  259. 'foo',
  260. true,
  261. null,
  262. doc.version
  263. );
  264. const reverse = operation.getReversed();
  265. model.applyOperation( wrapInDelta( operation ) );
  266. model.applyOperation( wrapInDelta( reverse ) );
  267. expect( doc.version ).to.equal( 2 );
  268. expect( root.maxOffset ).to.equal( 3 );
  269. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  270. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  271. } );
  272. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  273. root.insertChildren( 0, new Text( 'x' ) );
  274. expect( () => {
  275. model.applyOperation( wrapInDelta(
  276. new AttributeOperation(
  277. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  278. 'foo',
  279. true,
  280. null,
  281. doc.version
  282. )
  283. ) );
  284. } ).to.throw( CKEditorError, /attribute-operation-wrong-old-value/ );
  285. } );
  286. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  287. root.insertChildren( 0, new Text( 'x', { x: 1 } ) );
  288. expect( () => {
  289. model.applyOperation( wrapInDelta(
  290. new AttributeOperation(
  291. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  292. 'x',
  293. null,
  294. 2,
  295. doc.version
  296. )
  297. ) );
  298. } ).to.throw( CKEditorError, /attribute-operation-attribute-exists/ );
  299. } );
  300. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  301. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  302. const baseVersion = doc.version;
  303. const op = new AttributeOperation( range, 'foo', 'old', 'new', baseVersion );
  304. const clone = op.clone();
  305. // New instance rather than a pointer to the old instance.
  306. expect( clone ).not.to.be.equal( op );
  307. expect( clone ).to.be.instanceof( AttributeOperation );
  308. expect( clone.range.isEqual( range ) ).to.be.true;
  309. expect( clone.key ).to.equal( 'foo' );
  310. expect( clone.oldValue ).to.equal( 'old' );
  311. expect( clone.newValue ).to.equal( 'new' );
  312. expect( clone.baseVersion ).to.equal( baseVersion );
  313. } );
  314. it( 'should merge characters in node list', () => {
  315. const attrA = { foo: 'a' };
  316. const attrB = { foo: 'b' };
  317. root.insertChildren( 0, new Text( 'abc', attrA ) );
  318. root.insertChildren( 1, new Text( 'xyz', attrB ) );
  319. model.applyOperation( wrapInDelta(
  320. new AttributeOperation(
  321. new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ),
  322. 'foo',
  323. 'a',
  324. 'b',
  325. doc.version
  326. ) )
  327. );
  328. expect( root.getChild( 0 ).data ).to.equal( 'a' );
  329. expect( root.getChild( 1 ).data ).to.equal( 'bcxyz' );
  330. } );
  331. it( 'should do nothing when attribute value is the same', () => {
  332. root.insertChildren( 0, new Text( 'x', { foo: true } ) );
  333. expect( () => {
  334. model.applyOperation( wrapInDelta(
  335. new AttributeOperation(
  336. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  337. 'foo',
  338. true,
  339. true,
  340. doc.version
  341. )
  342. ) );
  343. } ).to.not.throw();
  344. } );
  345. describe( 'toJSON', () => {
  346. it( 'should create proper serialized object', () => {
  347. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  348. const op = new AttributeOperation(
  349. range,
  350. 'key',
  351. null,
  352. 'newValue',
  353. doc.version
  354. );
  355. const serialized = jsonParseStringify( op );
  356. expect( serialized.__className ).to.equal( 'engine.model.operation.AttributeOperation' );
  357. expect( serialized ).to.deep.equal( {
  358. __className: 'engine.model.operation.AttributeOperation',
  359. baseVersion: 0,
  360. key: 'key',
  361. newValue: 'newValue',
  362. oldValue: null,
  363. range: jsonParseStringify( range )
  364. } );
  365. } );
  366. } );
  367. describe( 'fromJSON', () => {
  368. it( 'should create proper AttributeOperation from json object', () => {
  369. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  370. const op = new AttributeOperation(
  371. range,
  372. 'key',
  373. null,
  374. 'newValue',
  375. doc.version
  376. );
  377. const serialized = jsonParseStringify( op );
  378. const deserialized = AttributeOperation.fromJSON( serialized, doc );
  379. expect( deserialized ).to.deep.equal( op );
  380. } );
  381. } );
  382. } );