attributedelta.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 coreTestUtils from '/tests/core/_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. const getIteratorCount = coreTestUtils.getIteratorCount;
  14. describe( 'Batch', () => {
  15. let doc, root, batch;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot( 'root' );
  19. batch = doc.batch();
  20. } );
  21. function getOperationsCount() {
  22. let count = 0;
  23. for ( let delta of batch.deltas ) {
  24. count += getIteratorCount( delta.operations );
  25. }
  26. return count;
  27. }
  28. describe( 'change attribute on node', () => {
  29. let node, text, char;
  30. beforeEach( () => {
  31. node = new Element( 'p', { a: 1 } );
  32. text = new Text( 'c', { a: 1 } );
  33. root.insertChildren( 0, [ node, text ] );
  34. char = root.getChild( 1 );
  35. } );
  36. describe( 'setAttr', () => {
  37. it( 'should create the attribute on element', () => {
  38. batch.setAttr( 'b', 2, node );
  39. expect( getOperationsCount() ).to.equal( 1 );
  40. expect( node.getAttribute( 'b' ) ).to.equal( 2 );
  41. } );
  42. it( 'should change the attribute of element', () => {
  43. batch.setAttr( 'a', 2, node );
  44. expect( getOperationsCount() ).to.equal( 1 );
  45. expect( node.getAttribute( 'a' ) ).to.equal( 2 );
  46. } );
  47. it( 'should create the attribute on text node', () => {
  48. batch.setAttr( 'b', 2, char );
  49. expect( getOperationsCount() ).to.equal( 1 );
  50. expect( root.getChild( 1 ).getAttribute( 'b' ) ).to.equal( 2 );
  51. } );
  52. it( 'should change the attribute of text node', () => {
  53. batch.setAttr( 'a', 2, char );
  54. expect( getOperationsCount() ).to.equal( 1 );
  55. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.equal( 2 );
  56. } );
  57. it( 'should do nothing if the attribute value is the same', () => {
  58. batch.setAttr( 'a', 1, node );
  59. expect( getOperationsCount() ).to.equal( 0 );
  60. expect( node.getAttribute( 'a' ) ).to.equal( 1 );
  61. } );
  62. it( 'should be chainable', () => {
  63. const chain = batch.setAttr( 'b', 2, node );
  64. expect( chain ).to.equal( batch );
  65. } );
  66. } );
  67. describe( 'removeAttr', () => {
  68. it( 'should remove the attribute from element', () => {
  69. batch.removeAttr( 'a', node );
  70. expect( getOperationsCount() ).to.equal( 1 );
  71. expect( node.getAttribute( 'a' ) ).to.be.undefined;
  72. } );
  73. it( 'should remove the attribute from character', () => {
  74. batch.removeAttr( 'a', char );
  75. expect( getOperationsCount() ).to.equal( 1 );
  76. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.be.undefined;
  77. } );
  78. it( 'should do nothing if the attribute is not set', () => {
  79. batch.removeAttr( 'b', node );
  80. expect( getOperationsCount() ).to.equal( 0 );
  81. } );
  82. it( 'should be chainable', () => {
  83. const chain = batch.removeAttr( 'a', node );
  84. expect( chain ).to.equal( batch );
  85. } );
  86. } );
  87. } );
  88. describe( 'change attribute on range', () => {
  89. beforeEach( () => {
  90. root.insertChildren( 0, [
  91. new Text( 'xxx', { a: 1 } ),
  92. 'xxx',
  93. new Text( 'xxx', { a: 1 } ),
  94. new Text( 'xxx', { a: 2 } ),
  95. 'xxx',
  96. new Text( 'xxx', { a: 1 } ),
  97. new Element( 'e', { a: 2 }, 'xxx' ),
  98. 'xxx'
  99. ] );
  100. } );
  101. function getRange( startIndex, endIndex ) {
  102. return new Range(
  103. Position.createFromParentAndOffset( root, startIndex ),
  104. Position.createFromParentAndOffset( root, endIndex )
  105. );
  106. }
  107. function getChangesAttrsCount() {
  108. let count = 0;
  109. for ( let delta of batch.deltas ) {
  110. for ( let operation of delta.operations ) {
  111. count += getIteratorCount( operation.range.getItems( true ) );
  112. }
  113. }
  114. return count;
  115. }
  116. function getCompressedAttrs() {
  117. // default: 111---111222---1112------
  118. const range = Range.createFromElement( root );
  119. return Array.from( range.getItems( true ) ).map( item => item.getAttribute( 'a' ) || '-' ).join( '' );
  120. }
  121. describe( 'setAttr', () => {
  122. it( 'should set the attribute on the range', () => {
  123. batch.setAttr( 'a', 3, getRange( 3, 6 ) );
  124. expect( getOperationsCount() ).to.equal( 1 );
  125. expect( getChangesAttrsCount() ).to.equal( 3 );
  126. expect( getCompressedAttrs() ).to.equal( '111333111222---1112------' );
  127. } );
  128. it( 'should split the operations if parts of the range have different attributes', () => {
  129. batch.setAttr( 'a', 3, getRange( 4, 14 ) );
  130. expect( getOperationsCount() ).to.equal( 4 );
  131. expect( getChangesAttrsCount() ).to.equal( 10 );
  132. expect( getCompressedAttrs() ).to.equal( '111-3333333333-1112------' );
  133. } );
  134. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  135. batch.setAttr( 'a', 2, getRange( 4, 14 ) );
  136. expect( getOperationsCount() ).to.equal( 3 );
  137. expect( getChangesAttrsCount() ).to.equal( 7 );
  138. expect( getCompressedAttrs() ).to.equal( '111-2222222222-1112------' );
  139. } );
  140. it( 'should strip the range if the beginning have the attribute', () => {
  141. batch.setAttr( 'a', 1, getRange( 1, 5 ) );
  142. expect( getOperationsCount() ).to.equal( 1 );
  143. expect( getChangesAttrsCount() ).to.equal( 2 );
  144. expect( getCompressedAttrs() ).to.equal( '11111-111222---1112------' );
  145. } );
  146. it( 'should strip the range if the ending have the attribute', () => {
  147. batch.setAttr( 'a', 1, getRange( 13, 17 ) );
  148. expect( getOperationsCount() ).to.equal( 1 );
  149. expect( getChangesAttrsCount() ).to.equal( 2 );
  150. expect( getCompressedAttrs() ).to.equal( '111---111222-111112------' );
  151. } );
  152. it( 'should do nothing if the range has attribute', () => {
  153. batch.setAttr( 'a', 1, getRange( 0, 3 ) );
  154. expect( getOperationsCount() ).to.equal( 0 );
  155. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  156. } );
  157. it( 'should not check range\'s start position node when creating operations', () => {
  158. let range = new Range(
  159. new Position( root, [ 18, 1 ] ),
  160. new Position( root, [ 19 ] )
  161. );
  162. batch.setAttr( 'a', 1, range );
  163. expect( getOperationsCount() ).to.equal( 1 );
  164. expect( getChangesAttrsCount() ).to.equal( 2 );
  165. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
  166. } );
  167. it( 'should not change elements attribute if range contains closing tag', () => {
  168. let range = new Range(
  169. new Position( root, [ 18, 1 ] ),
  170. new Position( root, [ 21 ] )
  171. );
  172. batch.setAttr( 'a', 1, range );
  173. expect( getOperationsCount() ).to.equal( 1 );
  174. expect( getChangesAttrsCount() ).to.equal( 4 );
  175. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
  176. } );
  177. it( 'should not create an operation if the range contains only closing tag', () => {
  178. let range = new Range(
  179. new Position( root, [ 18, 3 ] ),
  180. new Position( root, [ 19 ] )
  181. );
  182. batch.setAttr( 'a', 3, range );
  183. expect( getOperationsCount() ).to.equal( 0 );
  184. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  185. } );
  186. it( 'should not create an operation if is collapsed', () => {
  187. batch.setAttr( 'a', 1, getRange( 3, 3 ) );
  188. expect( getOperationsCount() ).to.equal( 0 );
  189. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  190. } );
  191. it( 'should create a proper operations for the mixed range', () => {
  192. batch.setAttr( 'a', 1, getRange( 0, 20 ) );
  193. expect( getOperationsCount() ).to.equal( 5 );
  194. expect( getChangesAttrsCount() ).to.equal( 14 );
  195. expect( getCompressedAttrs() ).to.equal( '11111111111111111111111--' );
  196. } );
  197. it( 'should be chainable', () => {
  198. const chain = batch.setAttr( 'a', 3, getRange( 3, 6 ) );
  199. expect( chain ).to.equal( batch );
  200. } );
  201. } );
  202. describe( 'removeAttr', () => {
  203. it( 'should remove the attribute on the range', () => {
  204. batch.removeAttr( 'a', getRange( 0, 2 ) );
  205. expect( getOperationsCount() ).to.equal( 1 );
  206. expect( getChangesAttrsCount() ).to.equal( 2 );
  207. expect( getCompressedAttrs() ).to.equal( '--1---111222---1112------' );
  208. } );
  209. it( 'should split the operations if parts of the range have different attributes', () => {
  210. batch.removeAttr( 'a', getRange( 7, 11 ) );
  211. expect( getOperationsCount() ).to.equal( 2 );
  212. expect( getChangesAttrsCount() ).to.equal( 4 );
  213. expect( getCompressedAttrs() ).to.equal( '111---1----2---1112------' );
  214. } );
  215. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  216. batch.removeAttr( 'a', getRange( 1, 7 ) );
  217. expect( getOperationsCount() ).to.equal( 2 );
  218. expect( getChangesAttrsCount() ).to.equal( 3 );
  219. expect( getCompressedAttrs() ).to.equal( '1------11222---1112------' );
  220. } );
  221. it( 'should strip the range if the beginning have no attribute', () => {
  222. batch.removeAttr( 'a', getRange( 4, 12 ) );
  223. expect( getOperationsCount() ).to.equal( 2 );
  224. expect( getChangesAttrsCount() ).to.equal( 6 );
  225. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  226. } );
  227. it( 'should strip the range if the ending have no attribute', () => {
  228. batch.removeAttr( 'a', getRange( 7, 15 ) );
  229. expect( getOperationsCount() ).to.equal( 2 );
  230. expect( getChangesAttrsCount() ).to.equal( 5 );
  231. expect( getCompressedAttrs() ).to.equal( '111---1--------1112------' );
  232. } );
  233. it( 'should do nothing if the range has no attribute', () => {
  234. batch.removeAttr( 'a', getRange( 4, 5 ) );
  235. expect( getOperationsCount() ).to.equal( 0 );
  236. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  237. } );
  238. it( 'should not check range\'s start position node when creating operations', () => {
  239. let range = new Range(
  240. new Position( root, [ 18, 3 ] ),
  241. new Position( root, [ 19 ] )
  242. );
  243. batch.removeAttr( 'a', range );
  244. expect( getOperationsCount() ).to.equal( 0 );
  245. expect( getChangesAttrsCount() ).to.equal( 0 );
  246. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  247. } );
  248. it( 'should not apply operation twice in the range contains opening and closing tags', () => {
  249. batch.removeAttr( 'a', getRange( 18, 22 ) );
  250. expect( getOperationsCount() ).to.equal( 1 );
  251. expect( getChangesAttrsCount() ).to.equal( 1 );
  252. expect( getCompressedAttrs() ).to.equal( '111---111222---111-------' );
  253. } );
  254. it( 'should not create an operation if range is collapsed', () => {
  255. batch.removeAttr( 'a', getRange( 3, 3 ) );
  256. expect( getOperationsCount() ).to.equal( 0 );
  257. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  258. } );
  259. it( 'should create a proper operations for the mixed range', () => {
  260. batch.removeAttr( 'a', getRange( 3, 15 ) );
  261. expect( getOperationsCount() ).to.equal( 2 );
  262. expect( getChangesAttrsCount() ).to.equal( 6 );
  263. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  264. } );
  265. it( 'should be chainable', () => {
  266. const chain = batch.removeAttr( 'a', getRange( 0, 2 ) );
  267. expect( chain ).to.equal( batch );
  268. } );
  269. } );
  270. } );
  271. } );