8
0

attributeoperation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 Text from '/ckeditor5/engine/model/text.js';
  10. import AttributeOperation from '/ckeditor5/engine/model/operation/attributeoperation.js';
  11. import Position from '/ckeditor5/engine/model/position.js';
  12. import Range from '/ckeditor5/engine/model/range.js';
  13. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  14. import count from '/ckeditor5/utils/count.js';
  15. import { jsonParseStringify, wrapInDelta } from '/tests/engine/model/_utils/utils.js';
  16. describe( 'AttributeOperation', () => {
  17. let doc, root;
  18. beforeEach( () => {
  19. doc = new Document();
  20. root = doc.createRoot();
  21. } );
  22. describe( 'type', () => {
  23. it( 'should be addAttribute for adding attribute', () => {
  24. const op = new AttributeOperation(
  25. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  26. 'key',
  27. null,
  28. 'newValue',
  29. doc.version
  30. );
  31. expect( op.type ).to.equal( 'addAttribute' );
  32. } );
  33. it( 'should be removeAttribute for removing attribute', () => {
  34. const op = new AttributeOperation(
  35. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  36. 'key',
  37. 'oldValue',
  38. null,
  39. doc.version
  40. );
  41. expect( op.type ).to.equal( 'removeAttribute' );
  42. } );
  43. it( 'should be changeAttribute for removing attribute', () => {
  44. const op = new AttributeOperation(
  45. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  46. 'key',
  47. 'oldValue',
  48. 'newValue',
  49. doc.version
  50. );
  51. expect( op.type ).to.equal( 'changeAttribute' );
  52. } );
  53. } );
  54. it( 'should insert attribute to the set of nodes', () => {
  55. root.insertChildren( 0, new Text( 'bar' ) );
  56. doc.applyOperation( wrapInDelta(
  57. new AttributeOperation(
  58. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  59. 'isNew',
  60. null,
  61. true,
  62. doc.version
  63. )
  64. ) );
  65. expect( doc.version ).to.equal( 1 );
  66. expect( root.getMaxOffset() ).to.equal( 3 );
  67. expect( root.getChild( 0 ).hasAttribute( 'isNew' ) ).to.be.true;
  68. expect( root.getChild( 0 ).data ).to.equal( 'ba' );
  69. expect( root.getChild( 1 ).hasAttribute( 'isNew' ) ).to.be.false;
  70. expect( root.getChild( 1 ).data ).to.equal( 'r' );
  71. } );
  72. it( 'should add attribute to the existing attributes', () => {
  73. root.insertChildren( 0, new Text( 'x', { foo: true, bar: true } ) );
  74. doc.applyOperation( wrapInDelta(
  75. new AttributeOperation(
  76. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  77. 'isNew',
  78. null,
  79. true,
  80. doc.version
  81. )
  82. ) );
  83. expect( doc.version ).to.equal( 1 );
  84. expect( root.getMaxOffset() ).to.equal( 1 );
  85. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 3 );
  86. expect( root.getChild( 0 ).hasAttribute( 'isNew' ) ).to.be.true;
  87. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  88. expect( root.getChild( 0 ).hasAttribute( 'bar' ) ).to.be.true;
  89. } );
  90. it( 'should change attribute to the set of nodes', () => {
  91. root.insertChildren( 0, new Text( 'bar', { isNew: false } ) );
  92. doc.applyOperation( wrapInDelta(
  93. new AttributeOperation(
  94. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  95. 'isNew',
  96. false,
  97. true,
  98. doc.version
  99. )
  100. ) );
  101. expect( doc.version ).to.equal( 1 );
  102. expect( root.getMaxOffset() ).to.equal( 3 );
  103. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  104. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.true;
  105. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  106. expect( root.getChild( 1 ).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.getMaxOffset() ).to.equal( 1 );
  121. expect( count( root.getChild( 0 ).getAttributes() ) ).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.getMaxOffset() ).to.equal( 1 );
  139. expect( count( root.getChild( 0 ).getAttributes() ) ).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, new Text( '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.getMaxOffset() ).to.equal( 3 );
  168. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 0 );
  169. } );
  170. it( 'should not set attribute of element if change range starts in the middle of that element', () => {
  171. let eleA = new Element( 'a', [], new Text( 'abc' ) );
  172. let eleB = new Element( 'b', [], new Text( 'xyz' ) );
  173. root.insertChildren( 0, [ eleA, eleB ] );
  174. doc.applyOperation( wrapInDelta(
  175. new AttributeOperation(
  176. new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ),
  177. 'foo',
  178. null,
  179. true,
  180. doc.version
  181. )
  182. ) );
  183. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
  184. } );
  185. it( 'should not remove attribute of element if change range starts in the middle of that element', () => {
  186. let fooAttr = { foo: true };
  187. let eleA = new Element( 'a', fooAttr, new Text( 'abc' ) );
  188. let eleB = new Element( 'b', fooAttr, new Text( 'xyz' ) );
  189. root.insertChildren( 0, [ eleA, eleB ] );
  190. doc.applyOperation( wrapInDelta(
  191. new AttributeOperation(
  192. new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 0 ] ) ),
  193. 'foo',
  194. true,
  195. null,
  196. doc.version
  197. )
  198. ) );
  199. expect( root.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  200. } );
  201. it( 'should undo changing attribute by applying reverse operation', () => {
  202. root.insertChildren( 0, new Text( 'bar', { isNew: false } ) );
  203. let operation = new AttributeOperation(
  204. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  205. 'isNew',
  206. false,
  207. true,
  208. doc.version
  209. );
  210. let reverse = operation.getReversed();
  211. doc.applyOperation( wrapInDelta( operation ) );
  212. doc.applyOperation( wrapInDelta( reverse ) );
  213. expect( doc.version ).to.equal( 2 );
  214. expect( root.getMaxOffset() ).to.equal( 3 );
  215. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  216. expect( root.getChild( 0 ).getAttribute( 'isNew' ) ).to.be.false;
  217. } );
  218. it( 'should undo remove attribute by applying reverse operation', () => {
  219. root.insertChildren( 0, new Text( 'bar', { foo: true } ) );
  220. let operation = new AttributeOperation(
  221. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  222. 'foo',
  223. true,
  224. null,
  225. doc.version
  226. );
  227. let reverse = operation.getReversed();
  228. doc.applyOperation( wrapInDelta( operation ) );
  229. doc.applyOperation( wrapInDelta( reverse ) );
  230. expect( doc.version ).to.equal( 2 );
  231. expect( root.getMaxOffset() ).to.equal( 3 );
  232. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  233. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.be.true;
  234. } );
  235. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  236. root.insertChildren( 0, new Text( 'x' ) );
  237. expect( () => {
  238. doc.applyOperation( wrapInDelta(
  239. new AttributeOperation(
  240. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  241. 'foo',
  242. true,
  243. null,
  244. doc.version
  245. )
  246. ) );
  247. } ).to.throw( CKEditorError, /operation-attribute-wrong-old-value/ );
  248. } );
  249. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  250. root.insertChildren( 0, new Text( 'x', { x: 1 } ) );
  251. expect( () => {
  252. doc.applyOperation( wrapInDelta(
  253. new AttributeOperation(
  254. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  255. 'x',
  256. null,
  257. 2,
  258. doc.version
  259. )
  260. ) );
  261. } ).to.throw( CKEditorError, /operation-attribute-attr-exists/ );
  262. } );
  263. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  264. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  265. let baseVersion = doc.version;
  266. let op = new AttributeOperation( range, 'foo', 'old', 'new', baseVersion );
  267. let clone = op.clone();
  268. // New instance rather than a pointer to the old instance.
  269. expect( clone ).not.to.be.equal( op );
  270. expect( clone ).to.be.instanceof( AttributeOperation );
  271. expect( clone.range.isEqual( range ) ).to.be.true;
  272. expect( clone.key ).to.equal( 'foo' );
  273. expect( clone.oldValue ).to.equal( 'old' );
  274. expect( clone.newValue ).to.equal( 'new' );
  275. expect( clone.baseVersion ).to.equal( baseVersion );
  276. } );
  277. it( 'should merge characters in node list', () => {
  278. let attrA = { foo: 'a' };
  279. let attrB = { foo: 'b' };
  280. root.insertChildren( 0, new Text( 'abc', attrA ) );
  281. root.insertChildren( 1, new Text( 'xyz', attrB ) );
  282. doc.applyOperation( wrapInDelta(
  283. new AttributeOperation(
  284. new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ),
  285. 'foo',
  286. 'a',
  287. 'b',
  288. doc.version
  289. ) )
  290. );
  291. expect( root.getChild( 0 ).data ).to.equal( 'a' );
  292. expect( root.getChild( 1 ).data ).to.equal( 'bcxyz' );
  293. } );
  294. describe( 'toJSON', () => {
  295. it( 'should create proper serialized object', () => {
  296. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  297. const op = new AttributeOperation(
  298. range,
  299. 'key',
  300. null,
  301. 'newValue',
  302. doc.version
  303. );
  304. const serialized = jsonParseStringify( op );
  305. expect( serialized.__className ).to.equal( 'engine.model.operation.AttributeOperation' );
  306. expect( serialized ).to.deep.equal( {
  307. __className: 'engine.model.operation.AttributeOperation',
  308. baseVersion: 0,
  309. key: 'key',
  310. newValue: 'newValue',
  311. oldValue: null,
  312. range: jsonParseStringify( range )
  313. } );
  314. } );
  315. } );
  316. describe( 'fromJSON', () => {
  317. it( 'should create proper AttributeOperation from json object', () => {
  318. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) );
  319. const op = new AttributeOperation(
  320. range,
  321. 'key',
  322. null,
  323. 'newValue',
  324. doc.version
  325. );
  326. const serialized = jsonParseStringify( op );
  327. const deserialized = AttributeOperation.fromJSON( serialized, doc );
  328. expect( deserialized ).to.deep.equal( op );
  329. } );
  330. } );
  331. } );