attributeoperation.js 12 KB

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