attributeoperation.js 12 KB

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