attributeoperation.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  13. import count from 'ckeditor5/utils/count.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. undefined, // `undefined` should also be accepted as a value, it is same as `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, new Text( 'bar' ) );
  55. doc.applyOperation( wrapInDelta(
  56. new AttributeOperation(
  57. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  58. 'isNew',
  59. undefined, // `undefined` should also be accepted as a value, it is same as `null`.
  60. true,
  61. doc.version
  62. )
  63. ) );
  64. expect( doc.version ).to.equal( 1 );
  65. expect( root.maxOffset ).to.equal( 3 );
  66. expect( root.getChild( 0 ).hasAttribute( 'isNew' ) ).to.be.true;
  67. expect( root.getChild( 0 ).data ).to.equal( 'ba' );
  68. expect( root.getChild( 1 ).hasAttribute( 'isNew' ) ).to.be.false;
  69. expect( root.getChild( 1 ).data ).to.equal( 'r' );
  70. } );
  71. it( 'should add attribute to the existing attributes', () => {
  72. root.insertChildren( 0, new Text( 'x', { foo: true, bar: true } ) );
  73. doc.applyOperation( wrapInDelta(
  74. new AttributeOperation(
  75. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  76. 'isNew',
  77. null,
  78. true,
  79. doc.version
  80. )
  81. ) );
  82. expect( doc.version ).to.equal( 1 );
  83. expect( root.maxOffset ).to.equal( 1 );
  84. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 3 );
  85. expect( root.getChild( 0 ).hasAttribute( 'isNew' ) ).to.be.true;
  86. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  87. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  88. } );
  89. it( 'should change attribute to the set of nodes', () => {
  90. root.insertChildren( 0, new Text( 'bar', { isNew: false } ) );
  91. doc.applyOperation( wrapInDelta(
  92. new AttributeOperation(
  93. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  94. 'isNew',
  95. false,
  96. true,
  97. doc.version
  98. )
  99. ) );
  100. expect( doc.version ).to.equal( 1 );
  101. expect( root.maxOffset ).to.equal( 3 );
  102. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  103. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.true;
  104. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  105. expect( root.getChild( 1 ).getAttribute( 'isNew' ) ).to.be.false;
  106. } );
  107. it( 'should change attribute in the middle of existing attributes', () => {
  108. root.insertChildren( 0, new Text( 'x', { foo: true, x: 1, bar: true } ) );
  109. doc.applyOperation( wrapInDelta(
  110. new AttributeOperation(
  111. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  112. 'x',
  113. 1,
  114. 2,
  115. doc.version
  116. )
  117. ) );
  118. expect( doc.version ).to.equal( 1 );
  119. expect( root.maxOffset ).to.equal( 1 );
  120. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 3 );
  121. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  122. expect( root.getChild( 0 ).getAttribute( 'x' ) ).to.equal( 2 );
  123. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.be.true;
  124. } );
  125. it( 'should remove attribute', () => {
  126. root.insertChildren( 0, new Text( 'x', { foo: true, x: true, bar: true } ) );
  127. doc.applyOperation( wrapInDelta(
  128. new AttributeOperation(
  129. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  130. 'x',
  131. true,
  132. null,
  133. doc.version
  134. )
  135. ) );
  136. expect( doc.version ).to.equal( 1 );
  137. expect( root.maxOffset ).to.equal( 1 );
  138. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 2 );
  139. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  140. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  141. } );
  142. it( 'should not throw for non-primitive attribute values', () => {
  143. root.insertChildren( 0, new Text( 'x', { foo: [ 'bar', 'xyz' ] } ) );
  144. expect( () => {
  145. doc.applyOperation( wrapInDelta(
  146. new AttributeOperation(
  147. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  148. 'foo',
  149. [ 'bar', 'xyz' ],
  150. true,
  151. doc.version
  152. )
  153. ) );
  154. } ).to.not.throw( Error );
  155. } );
  156. it( 'should create an AttributeOperation as a reverse', () => {
  157. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  158. let operation = new AttributeOperation( range, 'x', 'old', 'new', doc.version );
  159. let reverse = operation.getReversed();
  160. expect( reverse ).to.be.an.instanceof( AttributeOperation );
  161. expect( reverse.baseVersion ).to.equal( 1 );
  162. expect( reverse.range.isEqual( range ) ).to.be.true;
  163. expect( reverse.key ).to.equal( 'x' );
  164. expect( reverse.oldValue ).to.equal( 'new' );
  165. expect( reverse.newValue ).to.equal( 'old' );
  166. } );
  167. it( 'should undo adding attribute by applying reverse operation', () => {
  168. root.insertChildren( 0, new Text( 'bar' ) );
  169. let operation = new AttributeOperation(
  170. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  171. 'isNew',
  172. null,
  173. true,
  174. doc.version
  175. );
  176. let reverse = operation.getReversed();
  177. doc.applyOperation( wrapInDelta( operation ) );
  178. doc.applyOperation( wrapInDelta( reverse ) );
  179. expect( doc.version ).to.equal( 2 );
  180. expect( root.maxOffset ).to.equal( 3 );
  181. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 0 );
  182. } );
  183. it( 'should not set attribute of element if change range starts in the middle of that element', () => {
  184. let eleA = new Element( 'a', [], new Text( 'abc' ) );
  185. let eleB = new Element( 'b', [], new Text( 'xyz' ) );
  186. root.insertChildren( 0, [ eleA, eleB ] );
  187. doc.applyOperation( wrapInDelta(
  188. new AttributeOperation(
  189. new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ),
  190. 'foo',
  191. null,
  192. true,
  193. doc.version
  194. )
  195. ) );
  196. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
  197. } );
  198. it( 'should not remove attribute of element if change range starts in the middle of that element', () => {
  199. let fooAttr = { foo: true };
  200. let eleA = new Element( 'a', fooAttr, new Text( 'abc' ) );
  201. let eleB = new Element( 'b', fooAttr, new Text( 'xyz' ) );
  202. root.insertChildren( 0, [ eleA, eleB ] );
  203. doc.applyOperation( wrapInDelta(
  204. new AttributeOperation(
  205. new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 0 ] ) ),
  206. 'foo',
  207. true,
  208. null,
  209. doc.version
  210. )
  211. ) );
  212. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  213. } );
  214. it( 'should undo changing attribute by applying reverse operation', () => {
  215. root.insertChildren( 0, new Text( 'bar', { isNew: false } ) );
  216. let operation = new AttributeOperation(
  217. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  218. 'isNew',
  219. false,
  220. true,
  221. doc.version
  222. );
  223. let reverse = operation.getReversed();
  224. doc.applyOperation( wrapInDelta( operation ) );
  225. doc.applyOperation( wrapInDelta( reverse ) );
  226. expect( doc.version ).to.equal( 2 );
  227. expect( root.maxOffset ).to.equal( 3 );
  228. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  229. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.false;
  230. } );
  231. it( 'should undo remove attribute by applying reverse operation', () => {
  232. root.insertChildren( 0, new Text( 'bar', { foo: true } ) );
  233. let operation = new AttributeOperation(
  234. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  235. 'foo',
  236. true,
  237. null,
  238. doc.version
  239. );
  240. let reverse = operation.getReversed();
  241. doc.applyOperation( wrapInDelta( operation ) );
  242. doc.applyOperation( wrapInDelta( reverse ) );
  243. expect( doc.version ).to.equal( 2 );
  244. expect( root.maxOffset ).to.equal( 3 );
  245. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  246. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  247. } );
  248. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  249. root.insertChildren( 0, new Text( 'x' ) );
  250. expect( () => {
  251. doc.applyOperation( wrapInDelta(
  252. new AttributeOperation(
  253. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  254. 'foo',
  255. true,
  256. null,
  257. doc.version
  258. )
  259. ) );
  260. } ).to.throw( CKEditorError, /attribute-operation-wrong-old-value/ );
  261. } );
  262. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  263. root.insertChildren( 0, new Text( 'x', { x: 1 } ) );
  264. expect( () => {
  265. doc.applyOperation( wrapInDelta(
  266. new AttributeOperation(
  267. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  268. 'x',
  269. null,
  270. 2,
  271. doc.version
  272. )
  273. ) );
  274. } ).to.throw( CKEditorError, /attribute-operation-attribute-exists/ );
  275. } );
  276. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  277. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  278. let baseVersion = doc.version;
  279. let op = new AttributeOperation( range, 'foo', 'old', 'new', baseVersion );
  280. let clone = op.clone();
  281. // New instance rather than a pointer to the old instance.
  282. expect( clone ).not.to.be.equal( op );
  283. expect( clone ).to.be.instanceof( AttributeOperation );
  284. expect( clone.range.isEqual( range ) ).to.be.true;
  285. expect( clone.key ).to.equal( 'foo' );
  286. expect( clone.oldValue ).to.equal( 'old' );
  287. expect( clone.newValue ).to.equal( 'new' );
  288. expect( clone.baseVersion ).to.equal( baseVersion );
  289. } );
  290. it( 'should merge characters in node list', () => {
  291. let attrA = { foo: 'a' };
  292. let attrB = { foo: 'b' };
  293. root.insertChildren( 0, new Text( 'abc', attrA ) );
  294. root.insertChildren( 1, new Text( 'xyz', attrB ) );
  295. doc.applyOperation( wrapInDelta(
  296. new AttributeOperation(
  297. new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ),
  298. 'foo',
  299. 'a',
  300. 'b',
  301. doc.version
  302. ) )
  303. );
  304. expect( root.getChild( 0 ).data ).to.equal( 'a' );
  305. expect( root.getChild( 1 ).data ).to.equal( 'bcxyz' );
  306. } );
  307. it( 'should return undefined upon execution if new value is same as old value', () => {
  308. root.insertChildren( 0, new Text( 'bar', { foo: true } ) );
  309. let operation = new AttributeOperation(
  310. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  311. 'foo',
  312. true,
  313. true,
  314. doc.version
  315. );
  316. const result = operation._execute();
  317. expect( result ).to.be.undefined;
  318. } );
  319. describe( 'toJSON', () => {
  320. it( 'should create proper serialized object', () => {
  321. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  322. const op = new AttributeOperation(
  323. range,
  324. 'key',
  325. null,
  326. 'newValue',
  327. doc.version
  328. );
  329. const serialized = jsonParseStringify( op );
  330. expect( serialized.__className ).to.equal( 'engine.model.operation.AttributeOperation' );
  331. expect( serialized ).to.deep.equal( {
  332. __className: 'engine.model.operation.AttributeOperation',
  333. baseVersion: 0,
  334. key: 'key',
  335. newValue: 'newValue',
  336. oldValue: null,
  337. range: jsonParseStringify( range )
  338. } );
  339. } );
  340. } );
  341. describe( 'fromJSON', () => {
  342. it( 'should create proper AttributeOperation from json object', () => {
  343. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  344. const op = new AttributeOperation(
  345. range,
  346. 'key',
  347. null,
  348. 'newValue',
  349. doc.version
  350. );
  351. const serialized = jsonParseStringify( op );
  352. const deserialized = AttributeOperation.fromJSON( serialized, doc );
  353. expect( deserialized ).to.deep.equal( op );
  354. } );
  355. } );
  356. } );