attributeoperation.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, operation */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import Element from '/ckeditor5/core/treemodel/element.js';
  9. import AttributeOperation from '/ckeditor5/core/treemodel/operation/attributeoperation.js';
  10. import Position from '/ckeditor5/core/treemodel/position.js';
  11. import Range from '/ckeditor5/core/treemodel/range.js';
  12. import Text from '/ckeditor5/core/treemodel/text.js';
  13. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  14. describe( 'AttributeOperation', () => {
  15. let doc, root;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot( 'root' );
  19. } );
  20. it( 'should have proper type', () => {
  21. const op = new AttributeOperation(
  22. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  23. 'isNew',
  24. null,
  25. true,
  26. doc.version
  27. );
  28. expect( op.type ).to.equal( 'attribute' );
  29. } );
  30. it( 'should insert attribute to the set of nodes', () => {
  31. root.insertChildren( 0, 'bar' );
  32. doc.applyOperation(
  33. new AttributeOperation(
  34. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  35. 'isNew',
  36. null,
  37. true,
  38. doc.version
  39. )
  40. );
  41. expect( doc.version ).to.equal( 1 );
  42. expect( root.getChildCount() ).to.equal( 3 );
  43. expect( root.getChild( 0 ).hasAttribute( 'isNew' ) ).to.be.true;
  44. expect( root.getChild( 1 ).hasAttribute( 'isNew' ) ).to.be.true;
  45. expect( root.getChild( 2 )._attrs.size ).to.equal( 0 );
  46. } );
  47. it( 'should add attribute to the existing attributes', () => {
  48. root.insertChildren( 0, new Text( 'x', { foo: true, bar: true } ) );
  49. doc.applyOperation(
  50. new AttributeOperation(
  51. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  52. 'isNew',
  53. null,
  54. true,
  55. doc.version
  56. )
  57. );
  58. expect( doc.version ).to.equal( 1 );
  59. expect( root.getChildCount() ).to.equal( 1 );
  60. expect( root.getChild( 0 )._attrs.size ).to.equal( 3 );
  61. expect( root.getChild( 0 ).hasAttribute( 'isNew' ) ).to.be.true;
  62. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  63. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  64. } );
  65. it( 'should change attribute to the set of nodes', () => {
  66. root.insertChildren( 0, new Text( 'bar', { isNew: false } ) );
  67. doc.applyOperation(
  68. new AttributeOperation(
  69. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  70. 'isNew',
  71. false,
  72. true,
  73. doc.version
  74. )
  75. );
  76. expect( doc.version ).to.equal( 1 );
  77. expect( root.getChildCount() ).to.equal( 3 );
  78. expect( root.getChild( 0 )._attrs.size ).to.equal( 1 );
  79. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.true;
  80. expect( root.getChild( 1 )._attrs.size ).to.equal( 1 );
  81. expect( root.getChild( 1 ).getAttribute( 'isNew' ) ).to.be.true;
  82. expect( root.getChild( 2 )._attrs.size ).to.equal( 1 );
  83. expect( root.getChild( 2 ).getAttribute( 'isNew' ) ).to.be.false;
  84. } );
  85. it( 'should change attribute in the middle of existing attributes', () => {
  86. root.insertChildren( 0, new Text( 'x', { foo: true, x: 1, bar: true } ) );
  87. doc.applyOperation(
  88. new AttributeOperation(
  89. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  90. 'x',
  91. 1,
  92. 2,
  93. doc.version
  94. )
  95. );
  96. expect( doc.version ).to.equal( 1 );
  97. expect( root.getChildCount() ).to.equal( 1 );
  98. expect( root.getChild( 0 )._attrs.size ).to.equal( 3 );
  99. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  100. expect( root.getChild( 0 ).getAttribute( 'x' ) ).to.equal( 2 );
  101. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.be.true;
  102. } );
  103. it( 'should remove attribute', () => {
  104. root.insertChildren( 0, new Text( 'x', { foo: true, x: true, bar: true } ) );
  105. doc.applyOperation(
  106. new AttributeOperation(
  107. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  108. 'x',
  109. true,
  110. null,
  111. doc.version
  112. )
  113. );
  114. expect( doc.version ).to.equal( 1 );
  115. expect( root.getChildCount() ).to.equal( 1 );
  116. expect( root.getChild( 0 )._attrs.size ).to.equal( 2 );
  117. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  118. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  119. } );
  120. it( 'should create an AttributeOperation as a reverse', () => {
  121. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  122. let operation = new AttributeOperation( range, 'x', 'old', 'new', doc.version );
  123. let reverse = operation.getReversed();
  124. expect( reverse ).to.be.an.instanceof( AttributeOperation );
  125. expect( reverse.baseVersion ).to.equal( 1 );
  126. expect( reverse.range.isEqual( range ) ).to.be.true;
  127. expect( reverse.key ).to.equal( 'x' );
  128. expect( reverse.oldValue ).to.equal( 'new' );
  129. expect( reverse.newValue ).to.equal( 'old' );
  130. } );
  131. it( 'should undo adding attribute by applying reverse operation', () => {
  132. root.insertChildren( 0, 'bar' );
  133. let operation = new AttributeOperation(
  134. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  135. 'isNew',
  136. null,
  137. true,
  138. doc.version
  139. );
  140. let reverse = operation.getReversed();
  141. doc.applyOperation( operation );
  142. doc.applyOperation( reverse );
  143. expect( doc.version ).to.equal( 2 );
  144. expect( root.getChildCount() ).to.equal( 3 );
  145. expect( root.getChild( 0 )._attrs.size ).to.equal( 0 );
  146. expect( root.getChild( 1 )._attrs.size ).to.equal( 0 );
  147. expect( root.getChild( 2 )._attrs.size ).to.equal( 0 );
  148. } );
  149. it( 'should not set attribute of element if change range starts in the middle of that element', () => {
  150. let eleA = new Element( 'a', [], 'abc' );
  151. let eleB = new Element( 'b', [], 'xyz' );
  152. root.insertChildren( 0, [ eleA, eleB ] );
  153. doc.applyOperation(
  154. new AttributeOperation(
  155. new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ),
  156. 'foo',
  157. null,
  158. true,
  159. doc.version
  160. )
  161. );
  162. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
  163. } );
  164. it( 'should not remove attribute of element if change range starts in the middle of that element', () => {
  165. let fooAttr = { foo: true };
  166. let eleA = new Element( 'a', fooAttr, 'abc' );
  167. let eleB = new Element( 'b', fooAttr, 'xyz' );
  168. root.insertChildren( 0, [ eleA, eleB ] );
  169. doc.applyOperation(
  170. new AttributeOperation(
  171. new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 0 ] ) ),
  172. 'foo',
  173. true,
  174. null,
  175. doc.version
  176. )
  177. );
  178. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  179. } );
  180. it( 'should undo changing attribute by applying reverse operation', () => {
  181. root.insertChildren( 0, new Text( 'bar', { isNew: false } ) );
  182. let operation = new AttributeOperation(
  183. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  184. 'isNew',
  185. false,
  186. true,
  187. doc.version
  188. );
  189. let reverse = operation.getReversed();
  190. doc.applyOperation( operation );
  191. doc.applyOperation( reverse );
  192. expect( doc.version ).to.equal( 2 );
  193. expect( root.getChildCount() ).to.equal( 3 );
  194. expect( root.getChild( 0 )._attrs.size ).to.equal( 1 );
  195. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.false;
  196. expect( root.getChild( 1 )._attrs.size ).to.equal( 1 );
  197. expect( root.getChild( 1 ).getAttribute( 'isNew' ) ).to.be.false;
  198. expect( root.getChild( 2 )._attrs.size ).to.equal( 1 );
  199. expect( root.getChild( 2 ).getAttribute( 'isNew' ) ).to.be.false;
  200. } );
  201. it( 'should undo remove attribute by applying reverse operation', () => {
  202. root.insertChildren( 0, new Text( 'bar', { foo: true } ) );
  203. let operation = new AttributeOperation(
  204. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  205. 'foo',
  206. true,
  207. null,
  208. doc.version
  209. );
  210. let reverse = operation.getReversed();
  211. doc.applyOperation( operation );
  212. doc.applyOperation( reverse );
  213. expect( doc.version ).to.equal( 2 );
  214. expect( root.getChildCount() ).to.equal( 3 );
  215. expect( root.getChild( 0 )._attrs.size ).to.equal( 1 );
  216. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  217. expect( root.getChild( 1 )._attrs.size ).to.equal( 1 );
  218. expect( root.getChild( 1 ).getAttribute( 'foo' ) ).to.be.true;
  219. expect( root.getChild( 2 )._attrs.size ).to.equal( 1 );
  220. expect( root.getChild( 2 ).getAttribute( 'foo' ) ).to.be.true;
  221. } );
  222. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  223. root.insertChildren( 0, 'x' );
  224. expect( () => {
  225. doc.applyOperation(
  226. new AttributeOperation(
  227. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  228. 'foo',
  229. true,
  230. null,
  231. doc.version
  232. )
  233. );
  234. } ).to.throw( CKEditorError, /operation-attribute-no-attr-to-remove/ );
  235. } );
  236. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  237. root.insertChildren( 0, new Text( 'x', { x: 1 } ) );
  238. expect( () => {
  239. doc.applyOperation(
  240. new AttributeOperation(
  241. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  242. 'x',
  243. null,
  244. 2,
  245. doc.version
  246. )
  247. );
  248. } ).to.throw( CKEditorError, /operation-attribute-attr-exists/ );
  249. } );
  250. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  251. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  252. let baseVersion = doc.version;
  253. let op = new AttributeOperation( range, 'foo', 'old', 'new', baseVersion );
  254. let clone = op.clone();
  255. // New instance rather than a pointer to the old instance.
  256. expect( clone ).not.to.be.equal( op );
  257. expect( clone ).to.be.instanceof( AttributeOperation );
  258. expect( clone.range.isEqual( range ) ).to.be.true;
  259. expect( clone.key ).to.equal( 'foo' );
  260. expect( clone.oldValue ).to.equal( 'old' );
  261. expect( clone.newValue ).to.equal( 'new' );
  262. expect( clone.baseVersion ).to.equal( baseVersion );
  263. } );
  264. it( 'should merge characters in node list', () => {
  265. let attrA = { foo: 'a' };
  266. let attrB = { foo: 'b' };
  267. root.insertChildren( 0, new Text( 'abc', attrA ) );
  268. root.insertChildren( 3, new Text( 'xyz', attrB ) );
  269. doc.applyOperation(
  270. new AttributeOperation(
  271. new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ),
  272. 'foo',
  273. 'a',
  274. 'b',
  275. doc.version
  276. )
  277. );
  278. expect( root._children._nodes[ 0 ].text ).to.equal( 'a' );
  279. expect( root._children._nodes[ 1 ].text ).to.equal( 'bcxyz' );
  280. } );
  281. } );