attributeoperation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. 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. 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 create an AttributeOperation as a reverse', () => {
  143. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  144. let operation = new AttributeOperation( range, 'x', 'old', 'new', doc.version );
  145. let reverse = operation.getReversed();
  146. expect( reverse ).to.be.an.instanceof( AttributeOperation );
  147. expect( reverse.baseVersion ).to.equal( 1 );
  148. expect( reverse.range.isEqual( range ) ).to.be.true;
  149. expect( reverse.key ).to.equal( 'x' );
  150. expect( reverse.oldValue ).to.equal( 'new' );
  151. expect( reverse.newValue ).to.equal( 'old' );
  152. } );
  153. it( 'should undo adding attribute by applying reverse operation', () => {
  154. root.insertChildren( 0, new Text( 'bar' ) );
  155. let operation = new AttributeOperation(
  156. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  157. 'isNew',
  158. null,
  159. true,
  160. doc.version
  161. );
  162. let reverse = operation.getReversed();
  163. doc.applyOperation( wrapInDelta( operation ) );
  164. doc.applyOperation( wrapInDelta( reverse ) );
  165. expect( doc.version ).to.equal( 2 );
  166. expect( root.maxOffset ).to.equal( 3 );
  167. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 0 );
  168. } );
  169. it( 'should not set attribute of element if change range starts in the middle of that element', () => {
  170. let eleA = new Element( 'a', [], new Text( 'abc' ) );
  171. let eleB = new Element( 'b', [], new Text( 'xyz' ) );
  172. root.insertChildren( 0, [ eleA, eleB ] );
  173. doc.applyOperation( wrapInDelta(
  174. new AttributeOperation(
  175. new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ),
  176. 'foo',
  177. null,
  178. true,
  179. doc.version
  180. )
  181. ) );
  182. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
  183. } );
  184. it( 'should not remove attribute of element if change range starts in the middle of that element', () => {
  185. let fooAttr = { foo: true };
  186. let eleA = new Element( 'a', fooAttr, new Text( 'abc' ) );
  187. let eleB = new Element( 'b', fooAttr, new Text( 'xyz' ) );
  188. root.insertChildren( 0, [ eleA, eleB ] );
  189. doc.applyOperation( wrapInDelta(
  190. new AttributeOperation(
  191. new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 0 ] ) ),
  192. 'foo',
  193. true,
  194. null,
  195. doc.version
  196. )
  197. ) );
  198. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  199. } );
  200. it( 'should undo changing attribute by applying reverse operation', () => {
  201. root.insertChildren( 0, new Text( 'bar', { isNew: false } ) );
  202. let operation = new AttributeOperation(
  203. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  204. 'isNew',
  205. false,
  206. true,
  207. doc.version
  208. );
  209. let reverse = operation.getReversed();
  210. doc.applyOperation( wrapInDelta( operation ) );
  211. doc.applyOperation( wrapInDelta( reverse ) );
  212. expect( doc.version ).to.equal( 2 );
  213. expect( root.maxOffset ).to.equal( 3 );
  214. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  215. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.false;
  216. } );
  217. it( 'should undo remove attribute by applying reverse operation', () => {
  218. root.insertChildren( 0, new Text( 'bar', { foo: true } ) );
  219. let operation = new AttributeOperation(
  220. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  221. 'foo',
  222. true,
  223. null,
  224. doc.version
  225. );
  226. let reverse = operation.getReversed();
  227. doc.applyOperation( wrapInDelta( operation ) );
  228. doc.applyOperation( wrapInDelta( reverse ) );
  229. expect( doc.version ).to.equal( 2 );
  230. expect( root.maxOffset ).to.equal( 3 );
  231. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  232. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  233. } );
  234. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  235. root.insertChildren( 0, new Text( 'x' ) );
  236. expect( () => {
  237. doc.applyOperation( wrapInDelta(
  238. new AttributeOperation(
  239. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  240. 'foo',
  241. true,
  242. null,
  243. doc.version
  244. )
  245. ) );
  246. } ).to.throw( CKEditorError, /attribute-operation-wrong-old-value/ );
  247. } );
  248. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  249. root.insertChildren( 0, new Text( 'x', { x: 1 } ) );
  250. expect( () => {
  251. doc.applyOperation( wrapInDelta(
  252. new AttributeOperation(
  253. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  254. 'x',
  255. null,
  256. 2,
  257. doc.version
  258. )
  259. ) );
  260. } ).to.throw( CKEditorError, /attribute-operation-attribute-exists/ );
  261. } );
  262. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  263. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  264. let baseVersion = doc.version;
  265. let op = new AttributeOperation( range, 'foo', 'old', 'new', baseVersion );
  266. let clone = op.clone();
  267. // New instance rather than a pointer to the old instance.
  268. expect( clone ).not.to.be.equal( op );
  269. expect( clone ).to.be.instanceof( AttributeOperation );
  270. expect( clone.range.isEqual( range ) ).to.be.true;
  271. expect( clone.key ).to.equal( 'foo' );
  272. expect( clone.oldValue ).to.equal( 'old' );
  273. expect( clone.newValue ).to.equal( 'new' );
  274. expect( clone.baseVersion ).to.equal( baseVersion );
  275. } );
  276. it( 'should merge characters in node list', () => {
  277. let attrA = { foo: 'a' };
  278. let attrB = { foo: 'b' };
  279. root.insertChildren( 0, new Text( 'abc', attrA ) );
  280. root.insertChildren( 1, new Text( 'xyz', attrB ) );
  281. doc.applyOperation( wrapInDelta(
  282. new AttributeOperation(
  283. new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ),
  284. 'foo',
  285. 'a',
  286. 'b',
  287. doc.version
  288. ) )
  289. );
  290. expect( root.getChild( 0 ).data ).to.equal( 'a' );
  291. expect( root.getChild( 1 ).data ).to.equal( 'bcxyz' );
  292. } );
  293. describe( 'toJSON', () => {
  294. it( 'should create proper serialized object', () => {
  295. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  296. const op = new AttributeOperation(
  297. range,
  298. 'key',
  299. null,
  300. 'newValue',
  301. doc.version
  302. );
  303. const serialized = jsonParseStringify( op );
  304. expect( serialized.__className ).to.equal( 'engine.model.operation.AttributeOperation' );
  305. expect( serialized ).to.deep.equal( {
  306. __className: 'engine.model.operation.AttributeOperation',
  307. baseVersion: 0,
  308. key: 'key',
  309. newValue: 'newValue',
  310. oldValue: null,
  311. range: jsonParseStringify( range )
  312. } );
  313. } );
  314. } );
  315. describe( 'fromJSON', () => {
  316. it( 'should create proper AttributeOperation from json object', () => {
  317. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  318. const op = new AttributeOperation(
  319. range,
  320. 'key',
  321. null,
  322. 'newValue',
  323. doc.version
  324. );
  325. const serialized = jsonParseStringify( op );
  326. const deserialized = AttributeOperation.fromJSON( serialized, doc );
  327. expect( deserialized ).to.deep.equal( op );
  328. } );
  329. } );
  330. } );