8
0

attributedelta.js 18 KB

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