8
0

attributedelta.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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( { singleCharacters: 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( { singleCharacters: true } ) )
  120. .map( item => item.getAttribute( 'a' ) || '-' )
  121. .join( '' );
  122. }
  123. describe( 'setAttr', () => {
  124. it( 'should set the attribute on the range', () => {
  125. batch.setAttr( 'a', 3, getRange( 3, 6 ) );
  126. expect( getOperationsCount() ).to.equal( 1 );
  127. expect( getChangesAttrsCount() ).to.equal( 3 );
  128. expect( getCompressedAttrs() ).to.equal( '111333111222---1112------' );
  129. } );
  130. it( 'should split the operations if parts of the range have different attributes', () => {
  131. batch.setAttr( 'a', 3, getRange( 4, 14 ) );
  132. expect( getOperationsCount() ).to.equal( 4 );
  133. expect( getChangesAttrsCount() ).to.equal( 10 );
  134. expect( getCompressedAttrs() ).to.equal( '111-3333333333-1112------' );
  135. } );
  136. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  137. batch.setAttr( 'a', 2, getRange( 4, 14 ) );
  138. expect( getOperationsCount() ).to.equal( 3 );
  139. expect( getChangesAttrsCount() ).to.equal( 7 );
  140. expect( getCompressedAttrs() ).to.equal( '111-2222222222-1112------' );
  141. } );
  142. it( 'should strip the range if the beginning have the attribute', () => {
  143. batch.setAttr( 'a', 1, getRange( 1, 5 ) );
  144. expect( getOperationsCount() ).to.equal( 1 );
  145. expect( getChangesAttrsCount() ).to.equal( 2 );
  146. expect( getCompressedAttrs() ).to.equal( '11111-111222---1112------' );
  147. } );
  148. it( 'should strip the range if the ending have the attribute', () => {
  149. batch.setAttr( 'a', 1, getRange( 13, 17 ) );
  150. expect( getOperationsCount() ).to.equal( 1 );
  151. expect( getChangesAttrsCount() ).to.equal( 2 );
  152. expect( getCompressedAttrs() ).to.equal( '111---111222-111112------' );
  153. } );
  154. it( 'should do nothing if the range has attribute', () => {
  155. batch.setAttr( 'a', 1, getRange( 0, 3 ) );
  156. expect( getOperationsCount() ).to.equal( 0 );
  157. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  158. } );
  159. it( 'should not check range\'s start position node when creating operations', () => {
  160. let range = new Range(
  161. new Position( root, [ 18, 1 ] ),
  162. new Position( root, [ 19 ] )
  163. );
  164. batch.setAttr( 'a', 1, range );
  165. expect( getOperationsCount() ).to.equal( 1 );
  166. expect( getChangesAttrsCount() ).to.equal( 2 );
  167. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
  168. } );
  169. it( 'should not change elements attribute if range contains closing tag', () => {
  170. let range = new Range(
  171. new Position( root, [ 18, 1 ] ),
  172. new Position( root, [ 21 ] )
  173. );
  174. batch.setAttr( 'a', 1, range );
  175. expect( getOperationsCount() ).to.equal( 1 );
  176. expect( getChangesAttrsCount() ).to.equal( 4 );
  177. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
  178. } );
  179. it( 'should not create an operation if the range contains only closing tag', () => {
  180. let range = new Range(
  181. new Position( root, [ 18, 3 ] ),
  182. new Position( root, [ 19 ] )
  183. );
  184. batch.setAttr( 'a', 3, range );
  185. expect( getOperationsCount() ).to.equal( 0 );
  186. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  187. } );
  188. it( 'should not create an operation if is collapsed', () => {
  189. batch.setAttr( 'a', 1, getRange( 3, 3 ) );
  190. expect( getOperationsCount() ).to.equal( 0 );
  191. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  192. } );
  193. it( 'should create a proper operations for the mixed range', () => {
  194. batch.setAttr( 'a', 1, getRange( 0, 20 ) );
  195. expect( getOperationsCount() ).to.equal( 5 );
  196. expect( getChangesAttrsCount() ).to.equal( 14 );
  197. expect( getCompressedAttrs() ).to.equal( '11111111111111111111111--' );
  198. } );
  199. it( 'should be chainable', () => {
  200. const chain = batch.setAttr( 'a', 3, getRange( 3, 6 ) );
  201. expect( chain ).to.equal( batch );
  202. } );
  203. } );
  204. describe( 'removeAttr', () => {
  205. it( 'should remove the attribute on the range', () => {
  206. batch.removeAttr( 'a', getRange( 0, 2 ) );
  207. expect( getOperationsCount() ).to.equal( 1 );
  208. expect( getChangesAttrsCount() ).to.equal( 2 );
  209. expect( getCompressedAttrs() ).to.equal( '--1---111222---1112------' );
  210. } );
  211. it( 'should split the operations if parts of the range have different attributes', () => {
  212. batch.removeAttr( 'a', getRange( 7, 11 ) );
  213. expect( getOperationsCount() ).to.equal( 2 );
  214. expect( getChangesAttrsCount() ).to.equal( 4 );
  215. expect( getCompressedAttrs() ).to.equal( '111---1----2---1112------' );
  216. } );
  217. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  218. batch.removeAttr( 'a', getRange( 1, 7 ) );
  219. expect( getOperationsCount() ).to.equal( 2 );
  220. expect( getChangesAttrsCount() ).to.equal( 3 );
  221. expect( getCompressedAttrs() ).to.equal( '1------11222---1112------' );
  222. } );
  223. it( 'should strip the range if the beginning have no attribute', () => {
  224. batch.removeAttr( 'a', getRange( 4, 12 ) );
  225. expect( getOperationsCount() ).to.equal( 2 );
  226. expect( getChangesAttrsCount() ).to.equal( 6 );
  227. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  228. } );
  229. it( 'should strip the range if the ending have no attribute', () => {
  230. batch.removeAttr( 'a', getRange( 7, 15 ) );
  231. expect( getOperationsCount() ).to.equal( 2 );
  232. expect( getChangesAttrsCount() ).to.equal( 5 );
  233. expect( getCompressedAttrs() ).to.equal( '111---1--------1112------' );
  234. } );
  235. it( 'should do nothing if the range has no attribute', () => {
  236. batch.removeAttr( 'a', getRange( 4, 5 ) );
  237. expect( getOperationsCount() ).to.equal( 0 );
  238. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  239. } );
  240. it( 'should not check range\'s start position node when creating operations', () => {
  241. let range = new Range(
  242. new Position( root, [ 18, 3 ] ),
  243. new Position( root, [ 19 ] )
  244. );
  245. batch.removeAttr( 'a', range );
  246. expect( getOperationsCount() ).to.equal( 0 );
  247. expect( getChangesAttrsCount() ).to.equal( 0 );
  248. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  249. } );
  250. it( 'should not apply operation twice in the range contains opening and closing tags', () => {
  251. batch.removeAttr( 'a', getRange( 18, 22 ) );
  252. expect( getOperationsCount() ).to.equal( 1 );
  253. expect( getChangesAttrsCount() ).to.equal( 1 );
  254. expect( getCompressedAttrs() ).to.equal( '111---111222---111-------' );
  255. } );
  256. it( 'should not create an operation if range is collapsed', () => {
  257. batch.removeAttr( 'a', getRange( 3, 3 ) );
  258. expect( getOperationsCount() ).to.equal( 0 );
  259. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  260. } );
  261. it( 'should create a proper operations for the mixed range', () => {
  262. batch.removeAttr( 'a', getRange( 3, 15 ) );
  263. expect( getOperationsCount() ).to.equal( 2 );
  264. expect( getChangesAttrsCount() ).to.equal( 6 );
  265. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  266. } );
  267. it( 'should be chainable', () => {
  268. const chain = batch.removeAttr( 'a', getRange( 0, 2 ) );
  269. expect( chain ).to.equal( batch );
  270. } );
  271. } );
  272. } );
  273. } );