8
0

attributedelta.js 18 KB

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