attributedelta.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import count from '@ckeditor/ckeditor5-utils/src/count';
  6. import Document from '../../../src/model/document';
  7. import Text from '../../../src/model/text';
  8. import Range from '../../../src/model/range';
  9. import Position from '../../../src/model/position';
  10. import Element from '../../../src/model/element';
  11. import { default as AttributeDelta, RootAttributeDelta } from '../../../src/model/delta/attributedelta';
  12. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  13. import NoOperation from '../../../src/model/operation/nooperation';
  14. import { jsonParseStringify } from '../../../tests/model/_utils/utils';
  15. describe( 'Batch', () => {
  16. let batch, doc, root;
  17. const correctDeltaMatcher = sinon.match( operation => {
  18. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  19. } );
  20. beforeEach( () => {
  21. doc = new Document();
  22. root = doc.createRoot();
  23. batch = doc.batch();
  24. } );
  25. function getOperationsCount() {
  26. let totalNumber = 0;
  27. for ( const delta of batch.deltas ) {
  28. totalNumber += count( delta.operations );
  29. }
  30. return totalNumber;
  31. }
  32. describe( 'change attribute on node', () => {
  33. let node, text;
  34. beforeEach( () => {
  35. node = new Element( 'p', { a: 1 } );
  36. text = new Text( 'c', { a: 1 } );
  37. root.insertChildren( 0, [ node, text ] );
  38. } );
  39. describe( 'setAttribute', () => {
  40. it( 'should create the attribute on element', () => {
  41. batch.setAttribute( node, 'b', 2 );
  42. expect( getOperationsCount() ).to.equal( 1 );
  43. expect( node.getAttribute( 'b' ) ).to.equal( 2 );
  44. } );
  45. it( 'should change the attribute of element', () => {
  46. batch.setAttribute( node, 'a', 2 );
  47. expect( getOperationsCount() ).to.equal( 1 );
  48. expect( node.getAttribute( 'a' ) ).to.equal( 2 );
  49. } );
  50. it( 'should create the attribute on text node', () => {
  51. batch.setAttribute( text, 'b', 2 );
  52. expect( getOperationsCount() ).to.equal( 1 );
  53. expect( root.getChild( 1 ).getAttribute( 'b' ) ).to.equal( 2 );
  54. } );
  55. it( 'should change the attribute of text node', () => {
  56. batch.setAttribute( text, 'a', 2 );
  57. expect( getOperationsCount() ).to.equal( 1 );
  58. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.equal( 2 );
  59. } );
  60. it( 'should do nothing if the attribute value is the same', () => {
  61. batch.setAttribute( node, 'a', 1 );
  62. expect( getOperationsCount() ).to.equal( 0 );
  63. expect( node.getAttribute( 'a' ) ).to.equal( 1 );
  64. } );
  65. it( 'should be chainable', () => {
  66. const chain = batch.setAttribute( node, 'b', 2 );
  67. expect( chain ).to.equal( batch );
  68. } );
  69. it( 'should add delta to batch and operation to delta before applying operation', () => {
  70. sinon.spy( doc, 'applyOperation' );
  71. batch.setAttribute( node, 'b', 2 );
  72. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  73. } );
  74. } );
  75. describe( 'removeAttribute', () => {
  76. it( 'should remove the attribute from element', () => {
  77. batch.removeAttribute( node, 'a' );
  78. expect( getOperationsCount() ).to.equal( 1 );
  79. expect( node.getAttribute( 'a' ) ).to.be.undefined;
  80. } );
  81. it( 'should remove the attribute from character', () => {
  82. batch.removeAttribute( text, 'a' );
  83. expect( getOperationsCount() ).to.equal( 1 );
  84. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.be.undefined;
  85. } );
  86. it( 'should do nothing if the attribute is not set', () => {
  87. batch.removeAttribute( node, 'b' );
  88. expect( getOperationsCount() ).to.equal( 0 );
  89. } );
  90. it( 'should be chainable', () => {
  91. const chain = batch.removeAttribute( node, 'a' );
  92. expect( chain ).to.equal( batch );
  93. } );
  94. it( 'should add delta to batch and operation to delta before applying operation', () => {
  95. sinon.spy( doc, 'applyOperation' );
  96. batch.removeAttribute( node, 'a' );
  97. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  98. } );
  99. } );
  100. } );
  101. describe( 'change attribute on range', () => {
  102. beforeEach( () => {
  103. root.insertChildren( 0, [
  104. new Text( 'xxx', { a: 1 } ),
  105. new Text( 'xxx' ),
  106. new Text( 'xxx', { a: 1 } ),
  107. new Text( 'xxx', { a: 2 } ),
  108. new Text( 'xxx' ),
  109. new Text( 'xxx', { a: 1 } ),
  110. new Element( 'e', { a: 2 }, new Text( 'xxx' ) ),
  111. new Text( 'xxx' )
  112. ] );
  113. } );
  114. function getRange( startIndex, endIndex ) {
  115. return new Range(
  116. Position.createFromParentAndOffset( root, startIndex ),
  117. Position.createFromParentAndOffset( root, endIndex )
  118. );
  119. }
  120. function getChangesAttrsCount() {
  121. let totalNumber = 0;
  122. for ( const delta of batch.deltas ) {
  123. for ( const operation of delta.operations ) {
  124. totalNumber += count( operation.range.getItems( { singleCharacters: true } ) );
  125. }
  126. }
  127. return totalNumber;
  128. }
  129. function getCompressedAttrs() {
  130. // default: 111---111222---1112------
  131. const range = Range.createIn( root );
  132. return Array.from( range.getItems( { singleCharacters: true } ) )
  133. .map( item => item.getAttribute( 'a' ) || '-' )
  134. .join( '' );
  135. }
  136. describe( 'setAttribute', () => {
  137. it( 'should set the attribute on the range', () => {
  138. batch.setAttribute( getRange( 3, 6 ), 'a', 3 );
  139. expect( getOperationsCount() ).to.equal( 1 );
  140. expect( getChangesAttrsCount() ).to.equal( 3 );
  141. expect( getCompressedAttrs() ).to.equal( '111333111222---1112------' );
  142. } );
  143. it( 'should split the operations if parts of the range have different attributes', () => {
  144. batch.setAttribute( getRange( 4, 14 ), 'a', 3 );
  145. expect( getOperationsCount() ).to.equal( 4 );
  146. expect( getChangesAttrsCount() ).to.equal( 10 );
  147. expect( getCompressedAttrs() ).to.equal( '111-3333333333-1112------' );
  148. } );
  149. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  150. batch.setAttribute( getRange( 4, 14 ), 'a', 2 );
  151. expect( getOperationsCount() ).to.equal( 3 );
  152. expect( getChangesAttrsCount() ).to.equal( 7 );
  153. expect( getCompressedAttrs() ).to.equal( '111-2222222222-1112------' );
  154. } );
  155. it( 'should strip the range if the beginning have the attribute', () => {
  156. batch.setAttribute( getRange( 1, 5 ), 'a', 1 );
  157. expect( getOperationsCount() ).to.equal( 1 );
  158. expect( getChangesAttrsCount() ).to.equal( 2 );
  159. expect( getCompressedAttrs() ).to.equal( '11111-111222---1112------' );
  160. } );
  161. it( 'should strip the range if the ending have the attribute', () => {
  162. batch.setAttribute( getRange( 13, 17 ), 'a', 1 );
  163. expect( getOperationsCount() ).to.equal( 1 );
  164. expect( getChangesAttrsCount() ).to.equal( 2 );
  165. expect( getCompressedAttrs() ).to.equal( '111---111222-111112------' );
  166. } );
  167. it( 'should do nothing if the range has attribute', () => {
  168. batch.setAttribute( getRange( 0, 3 ), 'a', 1 );
  169. expect( getOperationsCount() ).to.equal( 0 );
  170. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  171. } );
  172. it( 'should not check range\'s start position node when creating operations', () => {
  173. const range = new Range(
  174. new Position( root, [ 18, 1 ] ),
  175. new Position( root, [ 19 ] )
  176. );
  177. batch.setAttribute( range, 'a', 1 );
  178. expect( getOperationsCount() ).to.equal( 1 );
  179. expect( getChangesAttrsCount() ).to.equal( 2 );
  180. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
  181. } );
  182. it( 'should not change elements attribute if range contains closing tag', () => {
  183. const range = new Range(
  184. new Position( root, [ 18, 1 ] ),
  185. new Position( root, [ 21 ] )
  186. );
  187. batch.setAttribute( range, 'a', 1 );
  188. expect( getOperationsCount() ).to.equal( 1 );
  189. expect( getChangesAttrsCount() ).to.equal( 4 );
  190. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
  191. } );
  192. it( 'should not create an operation if the range contains only closing tag', () => {
  193. const range = new Range(
  194. new Position( root, [ 18, 3 ] ),
  195. new Position( root, [ 19 ] )
  196. );
  197. batch.setAttribute( range, 'a', 3 );
  198. expect( getOperationsCount() ).to.equal( 0 );
  199. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  200. } );
  201. it( 'should not create an operation if is collapsed', () => {
  202. batch.setAttribute( getRange( 3, 3 ), 'a', 1 );
  203. expect( getOperationsCount() ).to.equal( 0 );
  204. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  205. } );
  206. it( 'should create a proper operations for the mixed range', () => {
  207. batch.setAttribute( getRange( 0, 20 ), 'a', 1 );
  208. expect( getOperationsCount() ).to.equal( 5 );
  209. expect( getChangesAttrsCount() ).to.equal( 14 );
  210. expect( getCompressedAttrs() ).to.equal( '11111111111111111111111--' );
  211. } );
  212. it( 'should be chainable', () => {
  213. const chain = batch.setAttribute( getRange( 3, 6 ), 'a', 3 );
  214. expect( chain ).to.equal( batch );
  215. } );
  216. it( 'should add delta to batch and operation to delta before applying operation', () => {
  217. sinon.spy( doc, 'applyOperation' );
  218. batch.setAttribute( getRange( 3, 6 ), 'a', 3 );
  219. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  220. } );
  221. } );
  222. describe( 'removeAttribute', () => {
  223. it( 'should remove the attribute on the range', () => {
  224. batch.removeAttribute( getRange( 0, 2 ), 'a' );
  225. expect( getOperationsCount() ).to.equal( 1 );
  226. expect( getChangesAttrsCount() ).to.equal( 2 );
  227. expect( getCompressedAttrs() ).to.equal( '--1---111222---1112------' );
  228. } );
  229. it( 'should split the operations if parts of the range have different attributes', () => {
  230. batch.removeAttribute( getRange( 7, 11 ), 'a' );
  231. expect( getOperationsCount() ).to.equal( 2 );
  232. expect( getChangesAttrsCount() ).to.equal( 4 );
  233. expect( getCompressedAttrs() ).to.equal( '111---1----2---1112------' );
  234. } );
  235. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  236. batch.removeAttribute( getRange( 1, 7 ), 'a' );
  237. expect( getOperationsCount() ).to.equal( 2 );
  238. expect( getChangesAttrsCount() ).to.equal( 3 );
  239. expect( getCompressedAttrs() ).to.equal( '1------11222---1112------' );
  240. } );
  241. it( 'should strip the range if the beginning have no attribute', () => {
  242. batch.removeAttribute( getRange( 4, 12 ), 'a' );
  243. expect( getOperationsCount() ).to.equal( 2 );
  244. expect( getChangesAttrsCount() ).to.equal( 6 );
  245. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  246. } );
  247. it( 'should strip the range if the ending have no attribute', () => {
  248. batch.removeAttribute( getRange( 7, 15 ), 'a' );
  249. expect( getOperationsCount() ).to.equal( 2 );
  250. expect( getChangesAttrsCount() ).to.equal( 5 );
  251. expect( getCompressedAttrs() ).to.equal( '111---1--------1112------' );
  252. } );
  253. it( 'should do nothing if the range has no attribute', () => {
  254. batch.removeAttribute( getRange( 4, 5 ), 'a' );
  255. expect( getOperationsCount() ).to.equal( 0 );
  256. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  257. } );
  258. it( 'should not check range\'s start position node when creating operations', () => {
  259. const range = new Range(
  260. new Position( root, [ 18, 3 ] ),
  261. new Position( root, [ 19 ] )
  262. );
  263. batch.removeAttribute( range, 'a' );
  264. expect( getOperationsCount() ).to.equal( 0 );
  265. expect( getChangesAttrsCount() ).to.equal( 0 );
  266. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  267. } );
  268. it( 'should not apply operation twice in the range contains opening and closing tags', () => {
  269. batch.removeAttribute( getRange( 18, 22 ), 'a' );
  270. expect( getOperationsCount() ).to.equal( 1 );
  271. expect( getChangesAttrsCount() ).to.equal( 1 );
  272. expect( getCompressedAttrs() ).to.equal( '111---111222---111-------' );
  273. } );
  274. it( 'should not create an operation if range is collapsed', () => {
  275. batch.removeAttribute( getRange( 3, 3 ), 'a' );
  276. expect( getOperationsCount() ).to.equal( 0 );
  277. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  278. } );
  279. it( 'should create a proper operations for the mixed range', () => {
  280. batch.removeAttribute( getRange( 3, 15 ), 'a' );
  281. expect( getOperationsCount() ).to.equal( 2 );
  282. expect( getChangesAttrsCount() ).to.equal( 6 );
  283. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  284. } );
  285. it( 'should be chainable', () => {
  286. const chain = batch.removeAttribute( getRange( 0, 2 ), 'a' );
  287. expect( chain ).to.equal( batch );
  288. } );
  289. it( 'should add delta to batch and operation to delta before applying operation', () => {
  290. sinon.spy( doc, 'applyOperation' );
  291. batch.removeAttribute( getRange( 0, 2 ), 'a' );
  292. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  293. } );
  294. } );
  295. } );
  296. describe( 'change attribute on root element', () => {
  297. describe( 'setAttribute', () => {
  298. it( 'should create the attribute on root', () => {
  299. batch.setAttribute( root, 'b', 2 );
  300. expect( getOperationsCount() ).to.equal( 1 );
  301. expect( root.getAttribute( 'b' ) ).to.equal( 2 );
  302. } );
  303. it( 'should change the attribute of root', () => {
  304. batch.setAttribute( root, 'a', 2 );
  305. expect( getOperationsCount() ).to.equal( 1 );
  306. expect( root.getAttribute( 'a' ) ).to.equal( 2 );
  307. } );
  308. it( 'should do nothing if the attribute value is the same', () => {
  309. batch.setAttribute( root, 'a', 1 );
  310. expect( getOperationsCount() ).to.equal( 1 );
  311. batch.setAttribute( root, 'a', 1 );
  312. expect( getOperationsCount() ).to.equal( 1 );
  313. expect( root.getAttribute( 'a' ) ).to.equal( 1 );
  314. } );
  315. } );
  316. describe( 'removeAttribute', () => {
  317. it( 'should remove the attribute from root', () => {
  318. batch.setAttribute( root, 'a', 1 );
  319. batch.removeAttribute( root, 'a' );
  320. expect( getOperationsCount() ).to.equal( 2 );
  321. expect( root.getAttribute( 'a' ) ).to.be.undefined;
  322. } );
  323. it( 'should do nothing if the attribute is not set', () => {
  324. batch.removeAttribute( root, 'b' );
  325. expect( getOperationsCount() ).to.equal( 0 );
  326. } );
  327. } );
  328. } );
  329. it( 'should not add empty delta to the batch', () => {
  330. const nodeA = new Element( 'p', { a: 1 } );
  331. const nodeB = new Element( 'p', { b: 2 } );
  332. root.insertChildren( 0, [ nodeA, nodeB ] );
  333. batch.setAttribute( nodeA, 'a', 1 );
  334. expect( batch.deltas.length ).to.equal( 0 );
  335. batch.removeAttribute( Range.createIn( root ), 'x' );
  336. expect( batch.deltas.length ).to.equal( 0 );
  337. } );
  338. } );
  339. describe( 'AttributeDelta', () => {
  340. let doc, root, delta;
  341. beforeEach( () => {
  342. doc = new Document();
  343. root = doc.createRoot();
  344. delta = new AttributeDelta();
  345. } );
  346. describe( 'type', () => {
  347. it( 'should be equal to attribute', () => {
  348. expect( delta.type ).to.equal( 'attribute' );
  349. } );
  350. } );
  351. describe( 'key', () => {
  352. it( 'should be null if there are no operations in delta', () => {
  353. expect( delta.key ).to.be.null;
  354. } );
  355. it( 'should be equal to attribute operations key that are in delta', () => {
  356. const range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  357. delta.addOperation( new AttributeOperation( range, 'key', 'old', 'new', 0 ) );
  358. expect( delta.key ).to.equal( 'key' );
  359. } );
  360. } );
  361. describe( 'value', () => {
  362. it( 'should be null if there are no operations in delta', () => {
  363. expect( delta.value ).to.be.null;
  364. } );
  365. it( 'should be equal to the value set by the delta operations', () => {
  366. const range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  367. delta.addOperation( new AttributeOperation( range, 'key', 'old', 'new', 0 ) );
  368. expect( delta.value ).to.equal( 'new' );
  369. } );
  370. } );
  371. describe( 'range', () => {
  372. it( 'should be null if there are no operations in delta', () => {
  373. expect( delta.range ).to.be.null;
  374. } );
  375. it( 'start and end should be equal to first and last changed position', () => {
  376. const rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  377. const rangeB = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  378. const rangeC = new Range( new Position( root, [ 5 ] ), new Position( root, [ 6 ] ) );
  379. delta.addOperation( new AttributeOperation( rangeA, 'key', 'oldA', 'new', 0 ) );
  380. delta.addOperation( new AttributeOperation( rangeB, 'key', 'oldB', 'new', 1 ) );
  381. delta.addOperation( new AttributeOperation( rangeC, 'key', 'oldC', 'new', 2 ) );
  382. expect( delta.range.start.path ).to.deep.equal( [ 1 ] );
  383. expect( delta.range.end.path ).to.deep.equal( [ 6 ] );
  384. } );
  385. it( 'should return correct values when some operations are NoOperations', () => {
  386. const rangeA = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  387. const rangeB = new Range( new Position( root, [ 5 ] ), new Position( root, [ 6 ] ) );
  388. delta.addOperation( new AttributeOperation( rangeA, 'key', 'oldA', 'new', 0 ) );
  389. delta.addOperation( new NoOperation( 1 ) );
  390. delta.addOperation( new AttributeOperation( rangeB, 'key', 'oldC', 'new', 2 ) );
  391. expect( delta.range.start.path ).to.deep.equal( [ 2 ] );
  392. expect( delta.range.end.path ).to.deep.equal( [ 6 ] );
  393. } );
  394. } );
  395. describe( 'getReversed', () => {
  396. it( 'should return empty AttributeDelta if there are no operations in delta', () => {
  397. const reversed = delta.getReversed();
  398. expect( reversed ).to.be.instanceof( AttributeDelta );
  399. expect( reversed.operations.length ).to.equal( 0 );
  400. } );
  401. it( 'should return correct AttributeDelta', () => {
  402. const rangeA = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  403. const rangeB = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  404. delta.addOperation( new AttributeOperation( rangeA, 'key', 'oldA', 'new', 0 ) );
  405. delta.addOperation( new AttributeOperation( rangeB, 'key', 'oldB', 'new', 1 ) );
  406. const reversed = delta.getReversed();
  407. expect( reversed ).to.be.instanceof( AttributeDelta );
  408. expect( reversed.operations.length ).to.equal( 2 );
  409. // Remember about reversed operations order.
  410. expect( reversed.operations[ 0 ] ).to.be.instanceof( AttributeOperation );
  411. expect( reversed.operations[ 0 ].range.isEqual( rangeB ) ).to.be.true;
  412. expect( reversed.operations[ 0 ].key ).to.equal( 'key' );
  413. expect( reversed.operations[ 0 ].oldValue ).to.equal( 'new' );
  414. expect( reversed.operations[ 0 ].newValue ).to.equal( 'oldB' );
  415. expect( reversed.operations[ 1 ] ).to.be.instanceof( AttributeOperation );
  416. expect( reversed.operations[ 1 ].range.isEqual( rangeA ) ).to.be.true;
  417. expect( reversed.operations[ 1 ].key ).to.equal( 'key' );
  418. expect( reversed.operations[ 1 ].oldValue ).to.equal( 'new' );
  419. expect( reversed.operations[ 1 ].newValue ).to.equal( 'oldA' );
  420. } );
  421. } );
  422. it( 'should provide proper className', () => {
  423. expect( AttributeDelta.className ).to.equal( 'engine.model.delta.AttributeDelta' );
  424. } );
  425. it( 'should not have _range property when converted to JSON', () => {
  426. const json = jsonParseStringify( delta );
  427. expect( json ).not.to.have.property( '_range' );
  428. } );
  429. } );
  430. describe( 'RootAttributeDelta', () => {
  431. it( 'should provide proper className', () => {
  432. expect( RootAttributeDelta.className ).to.equal( 'engine.model.delta.RootAttributeDelta' );
  433. } );
  434. } );