8
0

attributeoperation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  6. import Text from '../../../src/model/text';
  7. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  8. import Position from '../../../src/model/position';
  9. import Range from '../../../src/model/range';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import count from '@ckeditor/ckeditor5-utils/src/count';
  12. import { jsonParseStringify } from '../../../tests/model/_utils/utils';
  13. describe( 'AttributeOperation', () => {
  14. let model, doc, root;
  15. beforeEach( () => {
  16. model = new Model();
  17. doc = model.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. undefined, // `undefined` should also be accepted as a value, it is same as `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._insertChild( 0, new Text( 'bar' ) );
  54. model.applyOperation(
  55. new AttributeOperation(
  56. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  57. 'isNew',
  58. undefined, // `undefined` should also be accepted as a value, it is same as `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._insertChild( 0, new Text( 'x', { foo: true, bar: true } ) );
  72. model.applyOperation(
  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._insertChild( 0, new Text( 'bar', { isNew: false } ) );
  90. model.applyOperation(
  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._insertChild( 0, new Text( 'x', { foo: true, x: 1, bar: true } ) );
  108. model.applyOperation(
  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 work correctly if old and new value are same', () => {
  125. root._insertChild( 0, new Text( 'bar', { foo: 'bar' } ) );
  126. model.applyOperation(
  127. new AttributeOperation(
  128. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  129. 'foo',
  130. 'bar',
  131. 'bar',
  132. doc.version
  133. )
  134. );
  135. expect( doc.version ).to.equal( 1 );
  136. expect( root.childCount ).to.equal( 1 );
  137. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  138. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 'bar' );
  139. } );
  140. it( 'should remove attribute', () => {
  141. root._insertChild( 0, new Text( 'x', { foo: true, x: true, bar: true } ) );
  142. model.applyOperation(
  143. new AttributeOperation(
  144. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  145. 'x',
  146. true,
  147. null,
  148. doc.version
  149. )
  150. );
  151. expect( doc.version ).to.equal( 1 );
  152. expect( root.maxOffset ).to.equal( 1 );
  153. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 2 );
  154. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  155. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  156. } );
  157. describe( '_validate()', () => {
  158. it( 'should not throw for non-primitive attribute values', () => {
  159. root._insertChild( 0, new Text( 'x', { foo: [ 'bar', 'xyz' ] } ) );
  160. expect( () => {
  161. const operation = new AttributeOperation(
  162. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  163. 'foo',
  164. [ 'bar', 'xyz' ],
  165. true,
  166. doc.version
  167. );
  168. operation._validate();
  169. } ).to.not.throw( Error );
  170. } );
  171. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  172. root._insertChild( 0, new Text( 'x' ) );
  173. expect( () => {
  174. const operation = new AttributeOperation(
  175. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  176. 'foo',
  177. true,
  178. null,
  179. doc.version
  180. );
  181. operation._validate();
  182. } ).to.throw( CKEditorError, /attribute-operation-wrong-old-value/ );
  183. } );
  184. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  185. root._insertChild( 0, new Text( 'x', { x: 1 } ) );
  186. expect( () => {
  187. const operation = new AttributeOperation(
  188. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  189. 'x',
  190. null,
  191. 2,
  192. doc.version
  193. );
  194. operation._validate();
  195. } ).to.throw( CKEditorError, /attribute-operation-attribute-exists/ );
  196. } );
  197. it( 'should not throw when attribute value is the same', () => {
  198. root._insertChild( 0, new Text( 'x', { foo: true } ) );
  199. expect( () => {
  200. const operation = new AttributeOperation(
  201. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  202. 'foo',
  203. true,
  204. true,
  205. doc.version
  206. );
  207. operation._validate();
  208. } ).to.not.throw();
  209. } );
  210. } );
  211. it( 'should create an AttributeOperation as a reverse', () => {
  212. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  213. const operation = new AttributeOperation( range, 'x', 'old', 'new', doc.version );
  214. const reverse = operation.getReversed();
  215. expect( reverse ).to.be.an.instanceof( AttributeOperation );
  216. expect( reverse.baseVersion ).to.equal( 1 );
  217. expect( reverse.range.isEqual( range ) ).to.be.true;
  218. expect( reverse.key ).to.equal( 'x' );
  219. expect( reverse.oldValue ).to.equal( 'new' );
  220. expect( reverse.newValue ).to.equal( 'old' );
  221. } );
  222. it( 'should undo adding attribute by applying reverse operation', () => {
  223. root._insertChild( 0, new Text( 'bar' ) );
  224. const operation = new AttributeOperation(
  225. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  226. 'isNew',
  227. null,
  228. true,
  229. doc.version
  230. );
  231. const reverse = operation.getReversed();
  232. model.applyOperation( operation );
  233. model.applyOperation( reverse );
  234. expect( doc.version ).to.equal( 2 );
  235. expect( root.maxOffset ).to.equal( 3 );
  236. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 0 );
  237. } );
  238. it( 'should undo changing attribute by applying reverse operation', () => {
  239. root._insertChild( 0, new Text( 'bar', { isNew: false } ) );
  240. const operation = new AttributeOperation(
  241. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  242. 'isNew',
  243. false,
  244. true,
  245. doc.version
  246. );
  247. const reverse = operation.getReversed();
  248. model.applyOperation( operation );
  249. model.applyOperation( reverse );
  250. expect( doc.version ).to.equal( 2 );
  251. expect( root.maxOffset ).to.equal( 3 );
  252. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  253. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.false;
  254. } );
  255. it( 'should undo remove attribute by applying reverse operation', () => {
  256. root._insertChild( 0, new Text( 'bar', { foo: true } ) );
  257. const operation = new AttributeOperation(
  258. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  259. 'foo',
  260. true,
  261. null,
  262. doc.version
  263. );
  264. const reverse = operation.getReversed();
  265. model.applyOperation( operation );
  266. model.applyOperation( reverse );
  267. expect( doc.version ).to.equal( 2 );
  268. expect( root.maxOffset ).to.equal( 3 );
  269. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  270. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  271. } );
  272. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  273. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  274. const baseVersion = doc.version;
  275. const op = new AttributeOperation( range, 'foo', 'old', 'new', baseVersion );
  276. const clone = op.clone();
  277. // New instance rather than a pointer to the old instance.
  278. expect( clone ).not.to.be.equal( op );
  279. expect( clone ).to.be.instanceof( AttributeOperation );
  280. expect( clone.range.isEqual( range ) ).to.be.true;
  281. expect( clone.key ).to.equal( 'foo' );
  282. expect( clone.oldValue ).to.equal( 'old' );
  283. expect( clone.newValue ).to.equal( 'new' );
  284. expect( clone.baseVersion ).to.equal( baseVersion );
  285. } );
  286. it( 'should merge characters in node list', () => {
  287. const attrA = { foo: 'a' };
  288. const attrB = { foo: 'b' };
  289. root._insertChild( 0, new Text( 'abc', attrA ) );
  290. root._insertChild( 1, new Text( 'xyz', attrB ) );
  291. model.applyOperation(
  292. new AttributeOperation(
  293. new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ),
  294. 'foo',
  295. 'a',
  296. 'b',
  297. doc.version
  298. )
  299. );
  300. expect( root.getChild( 0 ).data ).to.equal( 'a' );
  301. expect( root.getChild( 1 ).data ).to.equal( 'bcxyz' );
  302. } );
  303. describe( 'toJSON', () => {
  304. it( 'should create proper serialized object', () => {
  305. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  306. const op = new AttributeOperation(
  307. range,
  308. 'key',
  309. null,
  310. 'newValue',
  311. doc.version
  312. );
  313. const serialized = jsonParseStringify( op );
  314. expect( serialized.__className ).to.equal( 'engine.model.operation.AttributeOperation' );
  315. expect( serialized ).to.deep.equal( {
  316. __className: 'engine.model.operation.AttributeOperation',
  317. baseVersion: 0,
  318. key: 'key',
  319. newValue: 'newValue',
  320. oldValue: null,
  321. range: jsonParseStringify( range )
  322. } );
  323. } );
  324. } );
  325. describe( 'fromJSON', () => {
  326. it( 'should create proper AttributeOperation from json object', () => {
  327. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  328. const op = new AttributeOperation(
  329. range,
  330. 'key',
  331. null,
  332. 'newValue',
  333. doc.version
  334. );
  335. const serialized = jsonParseStringify( op );
  336. const deserialized = AttributeOperation.fromJSON( serialized, doc );
  337. expect( deserialized ).to.deep.equal( op );
  338. } );
  339. } );
  340. } );