attributedelta.js 18 KB

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