attributeoperation.js 13 KB

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