8
0

attributedelta.js 15 KB

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