8
0

changeoperation.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document */
  6. 'use strict';
  7. var modules = bender.amd.require(
  8. 'document/document',
  9. 'document/changeoperation',
  10. 'document/position',
  11. 'document/range',
  12. 'document/character',
  13. 'document/attribute',
  14. 'ckeditorerror' );
  15. describe( 'ChangeOperation', function() {
  16. it( 'should insert attribute to the set of nodes', function() {
  17. var Document = modules[ 'document/document' ];
  18. var ChangeOperation = modules[ 'document/changeoperation' ];
  19. var Position = modules[ 'document/position' ];
  20. var Range = modules[ 'document/range' ];
  21. var Character = modules[ 'document/character' ];
  22. var Attribute = modules[ 'document/attribute' ];
  23. var doc = new Document();
  24. var newAttr = new Attribute( 'isNew', true );
  25. doc.root.children.push( new Character( doc.root, 'b' ) );
  26. doc.root.children.push( new Character( doc.root, 'a' ) );
  27. doc.root.children.push( new Character( doc.root, 'r' ) );
  28. doc.applyOperation( new ChangeOperation(
  29. new Range( new Position( [ 0 ], doc.root ), new Position( [ 2 ], doc.root ) ),
  30. null,
  31. newAttr,
  32. doc.version ) );
  33. expect( doc.version ).to.be.equal( 1 );
  34. expect( doc.root.children.length ).to.be.equal( 3 );
  35. expect( doc.root.children.get( 0 ).hasAttr( newAttr ) ).to.be.true;
  36. expect( doc.root.children.get( 1 ).hasAttr( newAttr ) ).to.be.true;
  37. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 0 );
  38. } );
  39. it( 'should insert attribute to multiple ranges', function() {
  40. var Document = modules[ 'document/document' ];
  41. var ChangeOperation = modules[ 'document/changeoperation' ];
  42. var Position = modules[ 'document/position' ];
  43. var Range = modules[ 'document/range' ];
  44. var Character = modules[ 'document/character' ];
  45. var Attribute = modules[ 'document/attribute' ];
  46. var doc = new Document();
  47. var newAttr = new Attribute( 'isNew', true );
  48. doc.root.children.push( new Character( doc.root, 'b' ) );
  49. doc.root.children.push( new Character( doc.root, 'a' ) );
  50. doc.root.children.push( new Character( doc.root, 'r' ) );
  51. doc.applyOperation( new ChangeOperation(
  52. [
  53. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  54. new Range( new Position( [ 2 ], doc.root ), new Position( [ 3 ], doc.root ) )
  55. ],
  56. null,
  57. newAttr,
  58. doc.version ) );
  59. expect( doc.version ).to.be.equal( 1 );
  60. expect( doc.root.children.length ).to.be.equal( 3 );
  61. expect( doc.root.children.get( 0 ).hasAttr( newAttr ) ).to.be.true;
  62. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 0 );
  63. expect( doc.root.children.get( 2 ).hasAttr( newAttr ) ).to.be.true;
  64. } );
  65. it( 'should add attribute to the existing attributes', function() {
  66. var Document = modules[ 'document/document' ];
  67. var ChangeOperation = modules[ 'document/changeoperation' ];
  68. var Position = modules[ 'document/position' ];
  69. var Range = modules[ 'document/range' ];
  70. var Character = modules[ 'document/character' ];
  71. var Attribute = modules[ 'document/attribute' ];
  72. var doc = new Document();
  73. var newAttr = new Attribute( 'isNew', true );
  74. var fooAttr = new Attribute( 'foo', true );
  75. var barAttr = new Attribute( 'bar', true );
  76. doc.root.children.push( new Character( doc.root, 'x', [ fooAttr, barAttr ] ) );
  77. doc.applyOperation( new ChangeOperation(
  78. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  79. null,
  80. newAttr,
  81. doc.version ) );
  82. expect( doc.version ).to.be.equal( 1 );
  83. expect( doc.root.children.length ).to.be.equal( 1 );
  84. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 3 );
  85. expect( doc.root.children.get( 0 ).hasAttr( newAttr ) ).to.be.true;
  86. expect( doc.root.children.get( 0 ).hasAttr( fooAttr ) ).to.be.true;
  87. expect( doc.root.children.get( 0 ).hasAttr( barAttr ) ).to.be.true;
  88. } );
  89. it( 'should change attributes on multiple ranges', function() {
  90. var Document = modules[ 'document/document' ];
  91. var ChangeOperation = modules[ 'document/changeoperation' ];
  92. var Position = modules[ 'document/position' ];
  93. var Range = modules[ 'document/range' ];
  94. var Character = modules[ 'document/character' ];
  95. var Attribute = modules[ 'document/attribute' ];
  96. var doc = new Document();
  97. var oldAttr = new Attribute( 'isNew', false );
  98. var newAttr = new Attribute( 'isNew', true );
  99. doc.root.children.push( new Character( doc.root, 'b', [ oldAttr ] ) );
  100. doc.root.children.push( new Character( doc.root, 'a', [ oldAttr ] ) );
  101. doc.root.children.push( new Character( doc.root, 'r', [ oldAttr ] ) );
  102. doc.applyOperation( new ChangeOperation(
  103. [
  104. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  105. new Range( new Position( [ 2 ], doc.root ), new Position( [ 3 ], doc.root ) )
  106. ],
  107. oldAttr,
  108. newAttr,
  109. doc.version ) );
  110. expect( doc.version ).to.be.equal( 1 );
  111. expect( doc.root.children.length ).to.be.equal( 3 );
  112. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 1 );
  113. expect( doc.root.children.get( 0 ).hasAttr( newAttr ) ).to.be.true;
  114. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 1 );
  115. expect( doc.root.children.get( 1 ).hasAttr( oldAttr ) ).to.be.true;
  116. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 1 );
  117. expect( doc.root.children.get( 2 ).hasAttr( newAttr ) ).to.be.true;
  118. } );
  119. it( 'should change attribute to the set of nodes', function() {
  120. var Document = modules[ 'document/document' ];
  121. var ChangeOperation = modules[ 'document/changeoperation' ];
  122. var Position = modules[ 'document/position' ];
  123. var Range = modules[ 'document/range' ];
  124. var Character = modules[ 'document/character' ];
  125. var Attribute = modules[ 'document/attribute' ];
  126. var doc = new Document();
  127. var oldAttr = new Attribute( 'isNew', false );
  128. var newAttr = new Attribute( 'isNew', true );
  129. doc.root.children.push( new Character( doc.root, 'b', [ oldAttr ] ) );
  130. doc.root.children.push( new Character( doc.root, 'a', [ oldAttr ] ) );
  131. doc.root.children.push( new Character( doc.root, 'r', [ oldAttr ] ) );
  132. doc.applyOperation( new ChangeOperation(
  133. new Range( new Position( [ 0 ], doc.root ), new Position( [ 2 ], doc.root ) ),
  134. oldAttr,
  135. newAttr,
  136. doc.version ) );
  137. expect( doc.version ).to.be.equal( 1 );
  138. expect( doc.root.children.length ).to.be.equal( 3 );
  139. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 1 );
  140. expect( doc.root.children.get( 0 ).hasAttr( newAttr ) ).to.be.true;
  141. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 1 );
  142. expect( doc.root.children.get( 1 ).hasAttr( newAttr ) ).to.be.true;
  143. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 1 );
  144. expect( doc.root.children.get( 2 ).hasAttr( oldAttr ) ).to.be.true;
  145. } );
  146. it( 'should change attribute in the middle of existing attributes', function() {
  147. var Document = modules[ 'document/document' ];
  148. var ChangeOperation = modules[ 'document/changeoperation' ];
  149. var Position = modules[ 'document/position' ];
  150. var Range = modules[ 'document/range' ];
  151. var Character = modules[ 'document/character' ];
  152. var Attribute = modules[ 'document/attribute' ];
  153. var doc = new Document();
  154. var fooAttr = new Attribute( 'foo', true );
  155. var x1Attr = new Attribute( 'x', 1 );
  156. var x2Attr = new Attribute( 'x', 2 );
  157. var barAttr = new Attribute( 'bar', true );
  158. doc.root.children.push( new Character( doc.root, 'x', [ fooAttr, x1Attr, barAttr ] ) );
  159. doc.applyOperation( new ChangeOperation(
  160. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  161. x1Attr,
  162. x2Attr,
  163. doc.version ) );
  164. expect( doc.version ).to.be.equal( 1 );
  165. expect( doc.root.children.length ).to.be.equal( 1 );
  166. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 3 );
  167. expect( doc.root.children.get( 0 ).hasAttr( fooAttr ) ).to.be.true;
  168. expect( doc.root.children.get( 0 ).hasAttr( x2Attr ) ).to.be.true;
  169. expect( doc.root.children.get( 0 ).hasAttr( barAttr ) ).to.be.true;
  170. } );
  171. it( 'should remove attribute', function() {
  172. var Document = modules[ 'document/document' ];
  173. var ChangeOperation = modules[ 'document/changeoperation' ];
  174. var Position = modules[ 'document/position' ];
  175. var Range = modules[ 'document/range' ];
  176. var Character = modules[ 'document/character' ];
  177. var Attribute = modules[ 'document/attribute' ];
  178. var doc = new Document();
  179. var fooAttr = new Attribute( 'foo', true );
  180. var xAttr = new Attribute( 'x', true );
  181. var barAttr = new Attribute( 'bar', true );
  182. doc.root.children.push( new Character( doc.root, 'x', [ fooAttr, xAttr, barAttr ] ) );
  183. doc.applyOperation( new ChangeOperation(
  184. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  185. xAttr,
  186. null,
  187. doc.version ) );
  188. expect( doc.version ).to.be.equal( 1 );
  189. expect( doc.root.children.length ).to.be.equal( 1 );
  190. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 2 );
  191. expect( doc.root.children.get( 0 ).hasAttr( fooAttr ) ).to.be.true;
  192. expect( doc.root.children.get( 0 ).hasAttr( barAttr ) ).to.be.true;
  193. } );
  194. it( 'should remove attributes on multiple ranges', function() {
  195. var Document = modules[ 'document/document' ];
  196. var ChangeOperation = modules[ 'document/changeoperation' ];
  197. var Position = modules[ 'document/position' ];
  198. var Range = modules[ 'document/range' ];
  199. var Character = modules[ 'document/character' ];
  200. var Attribute = modules[ 'document/attribute' ];
  201. var doc = new Document();
  202. var fooAttr = new Attribute( 'foo', true );
  203. doc.root.children.push( new Character( doc.root, 'b', [ fooAttr ] ) );
  204. doc.root.children.push( new Character( doc.root, 'a', [ fooAttr ] ) );
  205. doc.root.children.push( new Character( doc.root, 'r', [ fooAttr ] ) );
  206. doc.applyOperation( new ChangeOperation(
  207. [
  208. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  209. new Range( new Position( [ 2 ], doc.root ), new Position( [ 3 ], doc.root ) )
  210. ],
  211. fooAttr,
  212. null,
  213. doc.version ) );
  214. expect( doc.version ).to.be.equal( 1 );
  215. expect( doc.root.children.length ).to.be.equal( 3 );
  216. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 0 );
  217. expect( doc.root.children.get( 1 ).hasAttr( fooAttr ) ).to.be.true;
  218. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 0 );
  219. } );
  220. it( 'should create a change operation as a reverse', function() {
  221. var Document = modules[ 'document/document' ];
  222. var ChangeOperation = modules[ 'document/changeoperation' ];
  223. var Position = modules[ 'document/position' ];
  224. var Range = modules[ 'document/range' ];
  225. var Attribute = modules[ 'document/attribute' ];
  226. var doc = new Document();
  227. var oldAttr = new Attribute( 'x', 'old' );
  228. var newAttr = new Attribute( 'x', 'new' );
  229. var ranges = [ new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ) ];
  230. var oppertaion = new ChangeOperation( ranges, oldAttr, newAttr, doc.version );
  231. var reverse = oppertaion.reverseOperation();
  232. expect( reverse ).to.be.an.instanceof( ChangeOperation );
  233. expect( reverse.baseVersion ).to.be.equals( 1 );
  234. expect( reverse.ranges ).to.be.equals( ranges );
  235. expect( reverse.oldAttr ).to.be.equals( newAttr );
  236. expect( reverse.newAttr ).to.be.equals( oldAttr );
  237. } );
  238. it( 'should undo insert attribute by applying reverse operation', function() {
  239. var Document = modules[ 'document/document' ];
  240. var ChangeOperation = modules[ 'document/changeoperation' ];
  241. var Position = modules[ 'document/position' ];
  242. var Range = modules[ 'document/range' ];
  243. var Character = modules[ 'document/character' ];
  244. var Attribute = modules[ 'document/attribute' ];
  245. var doc = new Document();
  246. var newAttr = new Attribute( 'isNew', true );
  247. doc.root.children.push( new Character( doc.root, 'b' ) );
  248. doc.root.children.push( new Character( doc.root, 'a' ) );
  249. doc.root.children.push( new Character( doc.root, 'r' ) );
  250. var oppertaion = new ChangeOperation(
  251. new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),
  252. null,
  253. newAttr,
  254. doc.version );
  255. var reverse = oppertaion.reverseOperation();
  256. doc.applyOperation( oppertaion );
  257. doc.applyOperation( reverse );
  258. expect( doc.version ).to.be.equal( 2 );
  259. expect( doc.root.children.length ).to.be.equal( 3 );
  260. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 0 );
  261. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 0 );
  262. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 0 );
  263. } );
  264. it( 'should undo change attribute by applying reverse operation', function() {
  265. var Document = modules[ 'document/document' ];
  266. var ChangeOperation = modules[ 'document/changeoperation' ];
  267. var Position = modules[ 'document/position' ];
  268. var Range = modules[ 'document/range' ];
  269. var Character = modules[ 'document/character' ];
  270. var Attribute = modules[ 'document/attribute' ];
  271. var doc = new Document();
  272. var oldAttr = new Attribute( 'isNew', false );
  273. var newAttr = new Attribute( 'isNew', true );
  274. doc.root.children.push( new Character( doc.root, 'b', [ oldAttr ] ) );
  275. doc.root.children.push( new Character( doc.root, 'a', [ oldAttr ] ) );
  276. doc.root.children.push( new Character( doc.root, 'r', [ oldAttr ] ) );
  277. var oppertaion = new ChangeOperation(
  278. new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),
  279. oldAttr,
  280. newAttr,
  281. doc.version );
  282. var reverse = oppertaion.reverseOperation();
  283. doc.applyOperation( oppertaion );
  284. doc.applyOperation( reverse );
  285. expect( doc.version ).to.be.equal( 2 );
  286. expect( doc.root.children.length ).to.be.equal( 3 );
  287. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 1 );
  288. expect( doc.root.children.get( 0 ).hasAttr( oldAttr ) ).to.be.true;
  289. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 1 );
  290. expect( doc.root.children.get( 1 ).hasAttr( oldAttr ) ).to.be.true;
  291. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 1 );
  292. expect( doc.root.children.get( 2 ).hasAttr( oldAttr ) ).to.be.true;
  293. } );
  294. it( 'should undo remove attribute by applying reverse operation', function() {
  295. var Document = modules[ 'document/document' ];
  296. var ChangeOperation = modules[ 'document/changeoperation' ];
  297. var Position = modules[ 'document/position' ];
  298. var Range = modules[ 'document/range' ];
  299. var Character = modules[ 'document/character' ];
  300. var Attribute = modules[ 'document/attribute' ];
  301. var doc = new Document();
  302. var fooAttr = new Attribute( 'foo', false );
  303. doc.root.children.push( new Character( doc.root, 'b', [ fooAttr ] ) );
  304. doc.root.children.push( new Character( doc.root, 'a', [ fooAttr ] ) );
  305. doc.root.children.push( new Character( doc.root, 'r', [ fooAttr ] ) );
  306. var oppertaion = new ChangeOperation(
  307. new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),
  308. fooAttr,
  309. null,
  310. doc.version );
  311. var reverse = oppertaion.reverseOperation();
  312. doc.applyOperation( oppertaion );
  313. doc.applyOperation( reverse );
  314. expect( doc.version ).to.be.equal( 2 );
  315. expect( doc.root.children.length ).to.be.equal( 3 );
  316. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 1 );
  317. expect( doc.root.children.get( 0 ).hasAttr( fooAttr ) ).to.be.true;
  318. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 1 );
  319. expect( doc.root.children.get( 1 ).hasAttr( fooAttr ) ).to.be.true;
  320. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 1 );
  321. expect( doc.root.children.get( 2 ).hasAttr( fooAttr ) ).to.be.true;
  322. } );
  323. it( 'should throw an error when one try to remove and the attribute does not exists', function() {
  324. var Document = modules[ 'document/document' ];
  325. var ChangeOperation = modules[ 'document/changeoperation' ];
  326. var Position = modules[ 'document/position' ];
  327. var Range = modules[ 'document/range' ];
  328. var Character = modules[ 'document/character' ];
  329. var Attribute = modules[ 'document/attribute' ];
  330. var CKEditorError = modules.ckeditorerror;
  331. var doc = new Document();
  332. var fooAttr = new Attribute( 'foo', true );
  333. doc.root.children.push( new Character( doc.root, 'x' ) );
  334. expect( function() {
  335. doc.applyOperation( new ChangeOperation(
  336. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  337. fooAttr,
  338. null,
  339. doc.version ) );
  340. } ).to.throw( CKEditorError, /operation-change-no-attr-to-remove/ );
  341. } );
  342. it( 'should throw an error when one try to insert and the attribute already exists', function() {
  343. var Document = modules[ 'document/document' ];
  344. var ChangeOperation = modules[ 'document/changeoperation' ];
  345. var Position = modules[ 'document/position' ];
  346. var Range = modules[ 'document/range' ];
  347. var Character = modules[ 'document/character' ];
  348. var Attribute = modules[ 'document/attribute' ];
  349. var CKEditorError = modules.ckeditorerror;
  350. var doc = new Document();
  351. var x1Attr = new Attribute( 'x', 1 );
  352. var x2Attr = new Attribute( 'x', 2 );
  353. doc.root.children.push( new Character( doc.root, 'x', [ x1Attr ] ) );
  354. expect( function() {
  355. doc.applyOperation( new ChangeOperation(
  356. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  357. null,
  358. x2Attr,
  359. doc.version ) );
  360. } ).to.throw( CKEditorError, /operation-change-attr-exists/ );
  361. } );
  362. it( 'should throw an error when one try to change and the new and old attributes have different keys', function() {
  363. var Document = modules[ 'document/document' ];
  364. var ChangeOperation = modules[ 'document/changeoperation' ];
  365. var Position = modules[ 'document/position' ];
  366. var Range = modules[ 'document/range' ];
  367. var Character = modules[ 'document/character' ];
  368. var Attribute = modules[ 'document/attribute' ];
  369. var CKEditorError = modules.ckeditorerror;
  370. var doc = new Document();
  371. var fooAttr = new Attribute( 'foo', true );
  372. var barAttr = new Attribute( 'bar', true );
  373. doc.root.children.push( new Character( doc.root, 'x' ) );
  374. expect( function() {
  375. doc.applyOperation( new ChangeOperation(
  376. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  377. fooAttr,
  378. barAttr,
  379. doc.version ) );
  380. } ).to.throw( CKEditorError, /operation-change-different-keys/ );
  381. } );
  382. it( 'should throw an error when one try to change and the old attribute does not exists', function() {
  383. var Document = modules[ 'document/document' ];
  384. var ChangeOperation = modules[ 'document/changeoperation' ];
  385. var Position = modules[ 'document/position' ];
  386. var Range = modules[ 'document/range' ];
  387. var Character = modules[ 'document/character' ];
  388. var Attribute = modules[ 'document/attribute' ];
  389. var CKEditorError = modules.ckeditorerror;
  390. var doc = new Document();
  391. var x1Attr = new Attribute( 'x', 1 );
  392. var x2Attr = new Attribute( 'x', 2 );
  393. doc.root.children.push( new Character( doc.root, 'x' ) );
  394. expect( function() {
  395. doc.applyOperation( new ChangeOperation(
  396. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  397. x1Attr,
  398. x2Attr,
  399. doc.version ) );
  400. } ).to.throw( CKEditorError, /operation-change-no-attr-to-change/ );
  401. } );
  402. } );