attributedelta.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, delta */
  6. 'use strict';
  7. import utils from '/ckeditor5/utils/utils.js';
  8. import Document from '/ckeditor5/core/treemodel/document.js';
  9. import Text from '/ckeditor5/core/treemodel/text.js';
  10. import Range from '/ckeditor5/core/treemodel/range.js';
  11. import Position from '/ckeditor5/core/treemodel/position.js';
  12. import Element from '/ckeditor5/core/treemodel/element.js';
  13. import AttributeDelta from '/ckeditor5/core/treemodel/delta/attributedelta.js';
  14. import AttributeOperation from '/ckeditor5/core/treemodel/operation/attributeoperation.js';
  15. let doc, root;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot( 'root' );
  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 count = 0;
  30. for ( let delta of batch.deltas ) {
  31. count += utils.count( delta.operations );
  32. }
  33. return count;
  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( 'setAttr', () => {
  44. it( 'should create the attribute on element', () => {
  45. batch.setAttr( 'b', 2, node );
  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.setAttr( 'a', 2, node );
  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.setAttr( 'b', 2, char );
  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.setAttr( 'a', 2, char );
  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.setAttr( 'a', 1, node );
  66. expect( getOperationsCount() ).to.equal( 0 );
  67. expect( node.getAttribute( 'a' ) ).to.equal( 1 );
  68. } );
  69. it( 'should be chainable', () => {
  70. const chain = batch.setAttr( 'b', 2, node );
  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.setAttr( 'b', 2, node );
  76. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  77. } );
  78. } );
  79. describe( 'removeAttr', () => {
  80. it( 'should remove the attribute from element', () => {
  81. batch.removeAttr( 'a', node );
  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.removeAttr( 'a', char );
  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.removeAttr( 'b', node );
  92. expect( getOperationsCount() ).to.equal( 0 );
  93. } );
  94. it( 'should be chainable', () => {
  95. const chain = batch.removeAttr( 'a', node );
  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.removeAttr( 'a', node );
  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 count = 0;
  126. for ( let delta of batch.deltas ) {
  127. for ( let operation of delta.operations ) {
  128. count += utils.count( operation.range.getItems( { singleCharacters: true } ) );
  129. }
  130. }
  131. return count;
  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( 'setAttr', () => {
  141. it( 'should set the attribute on the range', () => {
  142. batch.setAttr( 'a', 3, getRange( 3, 6 ) );
  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.setAttr( 'a', 3, getRange( 4, 14 ) );
  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.setAttr( 'a', 2, getRange( 4, 14 ) );
  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.setAttr( 'a', 1, getRange( 1, 5 ) );
  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.setAttr( 'a', 1, getRange( 13, 17 ) );
  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.setAttr( 'a', 1, getRange( 0, 3 ) );
  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.setAttr( 'a', 1, range );
  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.setAttr( 'a', 1, range );
  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.setAttr( 'a', 3, range );
  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.setAttr( 'a', 1, getRange( 3, 3 ) );
  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.setAttr( 'a', 1, getRange( 0, 20 ) );
  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.setAttr( 'a', 3, getRange( 3, 6 ) );
  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.setAttr( 'a', 3, getRange( 3, 6 ) );
  223. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  224. } );
  225. } );
  226. describe( 'removeAttr', () => {
  227. it( 'should remove the attribute on the range', () => {
  228. batch.removeAttr( 'a', getRange( 0, 2 ) );
  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.removeAttr( 'a', getRange( 7, 11 ) );
  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.removeAttr( 'a', getRange( 1, 7 ) );
  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.removeAttr( 'a', getRange( 4, 12 ) );
  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.removeAttr( 'a', getRange( 7, 15 ) );
  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.removeAttr( 'a', getRange( 4, 5 ) );
  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.removeAttr( 'a', range );
  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.removeAttr( 'a', getRange( 18, 22 ) );
  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.removeAttr( 'a', getRange( 3, 3 ) );
  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.removeAttr( 'a', getRange( 3, 15 ) );
  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.removeAttr( 'a', getRange( 0, 2 ) );
  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.removeAttr( 'a', getRange( 0, 2 ) );
  296. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  297. } );
  298. } );
  299. } );
  300. } );
  301. describe( 'AttributeDelta', () => {
  302. let delta;
  303. beforeEach( () => {
  304. delta = new AttributeDelta();
  305. } );
  306. describe( 'key', () => {
  307. it( 'should be null if there are no operations in delta', () => {
  308. expect( delta.key ).to.be.null;
  309. } );
  310. it( 'should be equal to attribute operations key that are in delta', () => {
  311. let range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  312. delta.addOperation( new AttributeOperation( range, 'key', 'old', 'new', 0 ) );
  313. expect( delta.key ).to.equal( 'key' );
  314. } );
  315. } );
  316. describe( 'value', () => {
  317. it( 'should be null if there are no operations in delta', () => {
  318. expect( delta.value ).to.be.null;
  319. } );
  320. it( 'should be equal to the value set by the delta operations', () => {
  321. let range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  322. delta.addOperation( new AttributeOperation( range, 'key', 'old', 'new', 0 ) );
  323. expect( delta.value ).to.equal( 'new' );
  324. } );
  325. } );
  326. describe( 'range', () => {
  327. it( 'should be null if there are no operations in delta', () => {
  328. expect( delta.range ).to.be.null;
  329. } );
  330. it( 'should be equal to the range on which delta operates', () => {
  331. // Delta operates on range [ 1 ] to [ 6 ] but omits [ 4 ] - [ 5 ] for "a reason".
  332. // Still the range should be from [ 1 ] to [ 6 ]. Delta may not apply anything on [ 4 ] - [ 5 ]
  333. // because it already has proper attribute.
  334. let rangeA = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  335. let rangeB = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  336. let rangeC = new Range( new Position( root, [ 5 ] ), new Position( root, [ 6 ] ) );
  337. delta.addOperation( new AttributeOperation( rangeA, 'key', 'oldA', 'new', 0 ) );
  338. delta.addOperation( new AttributeOperation( rangeB, 'key', 'oldB', 'new', 1 ) );
  339. delta.addOperation( new AttributeOperation( rangeC, 'key', 'oldC', 'new', 2 ) );
  340. expect( delta.range.start.path ).to.deep.equal( [ 1 ] );
  341. expect( delta.range.end.path ).to.deep.equal( [ 6 ] );
  342. } );
  343. } );
  344. describe( 'getReversed', () => {
  345. it( 'should return empty AttributeDelta if there are no operations in delta', () => {
  346. let reversed = delta.getReversed();
  347. expect( reversed ).to.be.instanceof( AttributeDelta );
  348. expect( reversed.operations.length ).to.equal( 0 );
  349. } );
  350. it( 'should return correct AttributeDelta', () => {
  351. let rangeA = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) );
  352. let rangeB = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  353. delta.addOperation( new AttributeOperation( rangeA, 'key', 'oldA', 'new', 0 ) );
  354. delta.addOperation( new AttributeOperation( rangeB, 'key', 'oldB', 'new', 1 ) );
  355. let reversed = delta.getReversed();
  356. expect( reversed ).to.be.instanceof( AttributeDelta );
  357. expect( reversed.operations.length ).to.equal( 2 );
  358. // Remember about reversed operations order.
  359. expect( reversed.operations[ 0 ] ).to.be.instanceof( AttributeOperation );
  360. expect( reversed.operations[ 0 ].range.isEqual( rangeB ) ).to.be.true;
  361. expect( reversed.operations[ 0 ].key ).to.equal( 'key' );
  362. expect( reversed.operations[ 0 ].oldValue ).to.equal( 'new' );
  363. expect( reversed.operations[ 0 ].newValue ).to.equal( 'oldB' );
  364. expect( reversed.operations[ 1 ] ).to.be.instanceof( AttributeOperation );
  365. expect( reversed.operations[ 1 ].range.isEqual( rangeA ) ).to.be.true;
  366. expect( reversed.operations[ 1 ].key ).to.equal( 'key' );
  367. expect( reversed.operations[ 1 ].oldValue ).to.equal( 'new' );
  368. expect( reversed.operations[ 1 ].newValue ).to.equal( 'oldA' );
  369. } );
  370. } );
  371. } );