attributedelta.js 18 KB

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