attributedelta.js 15 KB

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