attributedelta.js 18 KB

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