attributeoperation.js 12 KB

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